-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (40 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const pluginSass = require("@snowpack/plugin-sass");
const path = require("path");
const picomatch = require("picomatch");
const cssResultModule = cssText => `\
import {css} from "lit-element";
export default css\`
${cssText.replace(/([$`\\])/g, "\\$1")}\`;
`;
const cssProxyModule = cssText => `\
document.head
.appendChild(document.createElement("style"))
.appendChild(document.createTextNode(\`
${cssText.replace(/([$`\\])/g, "\\$1")}\`));
`;
module.exports = function (snowpackConfig, pluginOptions) {
let delegate = pluginSass(snowpackConfig, pluginOptions);
let basedir = pluginOptions.basedir || process.cwd();
let include = pluginOptions.include ? picomatch(pluginOptions.include) : ()=>true;
let exclude = pluginOptions.exclude ? picomatch(pluginOptions.exclude) : ()=>false;
function accept(filePath) {
const relativePath = path.relative(basedir, filePath);
return include(relativePath) && !exclude(relativePath);
}
return {
name: "snowpack-plugin-css-result",
resolve: {input: [".scss", ".sass"], output: [".js"]},
onChange(args) {
delegate.markChanged = this.markChanged;
delegate.onChange(args);
},
async load({filePath, isDev}) {
const content = await delegate.load({filePath, isDev});
if (content) {
return accept(filePath) ? cssResultModule(content) : cssProxyModule(content);
} else {
return '';
}
}
};
};