-
Notifications
You must be signed in to change notification settings - Fork 69
/
build.before.js
84 lines (73 loc) · 2.47 KB
/
build.before.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
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
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const sass = require('sass');
const sassExtract = require('sass-extract');
const createSassExtractJsPlugin = require('./sass-extract-js/plugin');
const sassExtractJsPlugin = createSassExtractJsPlugin({ camelCase: false, hex: true });
const packageInfo = require('./package.json');
/** 自定义构建脚本 - 前置 */
module.exports = ({ context, log, modifyUserConfig, onHook }) => {
const { command, rootDir } = context;
// 编译 主题包scss变量 到 [theme].style.js
const themePath = path.resolve(rootDir, 'src/themes');
fs.readdirSync(themePath).forEach((theme) => {
if (/\w+.scss$/.test(theme) && theme !== 'base.scss' && theme !== 'index.scss') {
const rendered = sassExtract.renderSync({
file: themePath + '/' + theme,
}, {
// sass-extract 3.0 新增,支持 dart-sass
implementation: sass,
plugins: [sassExtractJsPlugin],
});
fs.writeFileSync(themePath + '/' + theme.replace(/\.scss$/, '.style.ts'), `export default ${JSON.stringify(rendered.vars)};`);
log.info(`build theme: ${theme}`);
}
});
if (command === 'build') {
const tempCssFile = [];
const scssFile = glob.sync(
`${rootDir}/src/**/index.scss`
);
// 额外增加 cdn.scss
scssFile.push(`${rootDir}/src/cdn.scss`);
scssFile.push(`${rootDir}/src/Wicon/cdn.scss`);
scssFile.forEach((item, i) => {
if (item.indexOf('themes/index.scss') > -1) {
return;
}
const rendered = sass.renderSync({
file: item
});
const cssFileName = item.replace(/\.scss$/, '.css');
fs.writeFileSync(cssFileName, rendered.css);
tempCssFile.push(cssFileName);
log.info(`scss to css: ${item}`);
});
// 构建后清除 css 文件
onHook('after.build.compile', () => {
tempCssFile.forEach((file) => {
fs.unlinkSync(file);
log.info(`clean css: ${file}`);
});
log.info('构建完成');
});
}
const myBabelPlugins = [
["babel-plugin-transform-define", {
__VERSION__: packageInfo.version,
__THEME__: 'index',
}],
];
if (command === 'build') {
myBabelPlugins.push(
["babel-plugin-transform-rename-import", {
replacements: [
{ original: '^(.+?)\\.scss$', replacement: '$1.css' },
],
}]
);
}
// 自定义babel插件
modifyUserConfig('babelPlugins', myBabelPlugins);
};