-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsnowpack-tagged-scss.js
45 lines (39 loc) · 1.18 KB
/
snowpack-tagged-scss.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 scssPlugin = require('@snowpack/plugin-sass');
function stripFileExtension(filename) {
return filename.split('.').slice(0, -1).join('.');
}
module.exports = function sassPlugin(
snowpackConfig,
{ native, compilerOptions = {} } = {},
) {
const basePlugin = scssPlugin(snowpackConfig, { native, compilerOptions });
/** @type {import("snowpack").SnowpackPlugin } */
const plugin = {
...basePlugin,
name: 'snowpack-tagged-scss',
resolve: {
input: ['.scss', '.sass'],
output: ['.js', '.css'],
},
async load({ filePath, isDev }) {
const index = filePath.indexOf('src');
const afterSrc = [...filePath.slice(index)].reduce(
(accum, char) =>
char === '\\' || char === '/' ? [...accum, '..'] : accum,
[],
);
const stdout = await basePlugin.load({ filePath, isDev });
if (stripFileExtension(filePath).endsWith('global')) {
return { '.css': stdout };
}
return {
'.js': `import {css} from '${afterSrc.join(
'/',
)}/_snowpack/pkg/lit-element.js';
const style = css\`${stdout}\`;
export default style`,
};
},
};
return plugin;
};