-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
52 lines (45 loc) · 1.44 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
46
47
48
49
50
51
52
const { processString } = require('uglifycss');
const { dirname, join } = require('path');
const execa = require('execa');
const { readFileSync } = require('fs');
const illegalChars = new Map();
illegalChars.set('\\', '\\\\');
illegalChars.set('`', '\\`');
illegalChars.set('$', '\\$');
function stringToTemplateLiteral(s) {
if (!s) {
return '``';
}
let res = '';
for (let i = 0; i < s.length; i++) {
const c = s.charAt(i);
res += illegalChars.get(c) || c;
}
return `\`${res}\``;
}
module.exports = function () {
return {
name: 'snowpack-lit-scss-plugin',
resolve: {
input: ['.scss', '.sass'],
output: ['.js']
},
async load({ filePath, isDev }) {
const input = readFileSync(filePath, 'utf-8');
const options = { input, preferLocal: true };
const args = ['--stdin', '--load-path', dirname(filePath), '--load-path', join(process.cwd(), 'node_modules')];
let { stdout, stderr } = await execa('sass', args, options);
if (stderr) {
console.log(stderr);
}
if (!isDev) {
stdout = processString(stdout, {});
}
if (/\.lit\.scss$/.exec(filePath)) {
return `import { css } from 'lit-element'; export default css${stringToTemplateLiteral(stdout)};`
} else {
return `const style = document.createElement('style'); style.innerHTML = ${stringToTemplateLiteral(stdout)}; document.head.appendChild(style);`;
}
}
};
};