-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsass.mjs
More file actions
128 lines (112 loc) · 3.27 KB
/
Copy pathsass.mjs
File metadata and controls
128 lines (112 loc) · 3.27 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import autoprefixer from 'autoprefixer';
import { globby } from 'globby';
import postcss from 'postcss';
import * as sass from 'sass-embedded';
import report from './report.mjs';
const toDist = path.join.bind(
null,
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../dist')
);
const stripComments = () => {
return {
postcssPlugin: 'postcss-strip-comments',
OnceExit(root) {
root.walkComments((node) => node.remove());
},
};
};
stripComments.postcss = true;
const _template = await readFile(
resolve(process.argv[1], '../styles.tmpl'),
'utf8'
);
const _postProcessor = postcss([autoprefixer, stripComments]);
export function fromTemplate(content) {
return _template.replace(/<%\s*content\s*%>/, content);
}
export async function compileSass(src, compiler) {
const compiled = await compiler.compileAsync(src, {
style: 'compressed',
loadPaths: ['node_modules', 'src'],
});
const out = _postProcessor.process(compiled.css).css;
return out.charCodeAt(0) === 0xfeff ? out.slice(1) : out;
}
export async function buildThemes(isProduction = false) {
const start = performance.now();
const [compiler, paths] = await Promise.all([
sass.initAsyncCompiler(),
globby('src/styles/themes/{light,dark}/*.scss'),
]);
try {
await Promise.all(
paths.map(async (sassFile) => {
const outputFile = isProduction
? toDist(
sassFile.replace(/\.scss$/, '.css').replace('src/styles/', '')
)
: sassFile.replace(/\.scss$/, '.css.ts');
if (isProduction) {
await mkdir(path.dirname(outputFile), { recursive: true });
writeFile(outputFile, await compileSass(sassFile, compiler), 'utf-8');
} else {
writeFile(
outputFile,
fromTemplate(await compileSass(sassFile, compiler)),
'utf-8'
);
}
})
);
} catch (err) {
await compiler.dispose();
report.error(err);
process.exit(1);
}
await compiler.dispose();
if (!isProduction) {
report.success(
`Themes generated in ${((performance.now() - start) / 1000).toFixed(2)}s`
);
}
}
export async function buildComponents(isProduction = false) {
const start = performance.now();
const [compiler, paths] = await Promise.all([
sass.initAsyncCompiler(),
globby([
'src/components/**/*.base.scss',
'src/components/**/*.common.scss',
'src/components/**/*.shared.scss',
'src/components/**/*.material.scss',
'src/components/**/*.bootstrap.scss',
'src/components/**/*.indigo.scss',
'src/components/**/*.fluent.scss',
]),
]);
try {
await Promise.all(
paths.map(async (path) =>
writeFile(
path.replace(/\.scss$/, '.css.ts'),
fromTemplate(await compileSass(path, compiler)),
'utf-8'
)
)
);
} catch (err) {
await compiler.dispose();
report.error(err);
process.exit(1);
}
await compiler.dispose();
if (!isProduction) {
report.success(
`Component styles generated in ${((performance.now() - start) / 1000).toFixed(2)}s`
);
}
}