forked from opentiny/tiny-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
169 lines (156 loc) · 5.22 KB
/
compile.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import fs from 'node:fs';
import less from 'less';
import path from 'node:path';
import postcss from 'postcss';
import { rollup } from 'rollup';
import url from '@rollup/plugin-url';
import copy from 'rollup-plugin-copy';
import autoprefixer from 'autoprefixer';
import strip from '@rollup/plugin-strip';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
// 编译处理路径
const OUTPUT_DIR_PATH = './build';
// 编译less考虑的浏览器
const autoprefixerOption = {
overrideBrowserslist: [
'Firefox >= 40',
'chrome >= 50',
'ie > 11',
'edge >= 12'
]
}
// 拷贝的文件
const copyConfig = {
targets: [
{ src: './README.md', dest: OUTPUT_DIR_PATH },
{ src: './CompilePackage.json', dest: OUTPUT_DIR_PATH, rename: 'package.json' }
]
}
// JS输入编译配置
const inputOptions = {
// 编译需要排除的第三方依赖,未来增加新的依赖需要增加相应的依赖项
external: ['echarts', 'echarts/extension/bmap/bmap'],
input: './src/index.js',
plugins: [
// 解析第三方依赖模块
resolve(),
// commonjs转为esmodule
commonjs(),
// 解析非js文件导入,图片等
url(),
// babel编译es6降级,Unlike the regular babel plugin, getBabelOutputPlugin(...) will not automatically search for Babel configuration files.
getBabelOutputPlugin({
presets: [['@babel/preset-env', { loose: true, modules: false }]]
}),
strip({
// 删除debugger语句
debugger: true,
// 仅删除console.log
functions: ['console.log'],
}),
// 拷贝文件
copy(copyConfig)
]
}
// JS输出编译配置
const outputOptions = {
dir: OUTPUT_DIR_PATH,
// 输出为esm包
format: 'es',
// 保证编译结果保持原来的文件结构
preserveModules: true,
preserveModulesRoot: 'src',
}
// 清空build文件夹
function cleanBuild() {
console.log('build文件夹开始清除...');
// 递归删除文件
function deleteAllFile(path) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach((file) => {
const curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
deleteAllFile(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
if (fs.existsSync(OUTPUT_DIR_PATH)) {
deleteAllFile(OUTPUT_DIR_PATH)
console.log('build文件夹清除已完成。');
}
}
async function build() {
console.warn('JavaScript开始编译...');
let bundle
let buildFailed = false
try {
// 1.启动一次打包
bundle = await rollup(inputOptions);
// 2. 拿到 bundle 对象,根据每一份输出配置,调用 write 方法将产物写入磁盘
await bundle.write(outputOptions);
} catch (error) {
buildFailed = true;
console.error(error);
}
if (bundle) {
// 最后调用 bundle.close 方法结束打包
await bundle.close();
console.warn('JavaScript编译已完成。');
// 异步,下一步的编译静态文件必须是在打包完成后再去操作,不然读取不到相应的路径
return Promise.resolve()
}
process.exit(buildFailed ? 1 : 0);
}
function compileLess(inputFilePath, outputFilePath, root) {
const inputContent = fs.readFileSync(inputFilePath, 'utf8');
less.render(inputContent, {
paths: [root], // Specify search paths for @import directives
compress: true // Minify CSS output
}).then((output) => {
let processed = postcss([autoprefixer(autoprefixerOption)]).process(output.css, {});
fs.writeFileSync(outputFilePath, processed.css);
console.warn(`成功编译 ${inputFilePath} 到 ${outputFilePath}`);
}).catch((error) => {
console.error(`编译失败:${error}`);
});
}
// 递归文件夹,获得所有.less后缀文件
function traverseDirectory(dirPath, lessFiles) {
const dirEntries = fs.readdirSync(dirPath);
for (let entry of dirEntries) {
const filePath = path.join(dirPath, entry);
if (fs.statSync(filePath).isFile()) {
// 判断文件后缀名是否为.less
if (entry.endsWith('.less')) {
lessFiles.push(filePath);
}
} else {
traverseDirectory(filePath, lessFiles);
}
}
}
// 编译.less文件
function compileAssets() {
console.warn('开始编译.less静态资源...');
// 1. 找到所有后缀为.less的文件
const root = './src';
const lessFiles = [];
// 递归文件夹,获得所有.less后缀文件
traverseDirectory(root, lessFiles);
// 2. 编译所有.less文件
lessFiles.forEach(inputFilePath => {
let outputFilePath = inputFilePath.replace(/less/g, 'css').replace(/src/g, OUTPUT_DIR_PATH);
compileLess(inputFilePath, outputFilePath, root);
});
}
cleanBuild();
await build();
compileAssets();