-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathrollup-utils.js
More file actions
388 lines (362 loc) · 9.73 KB
/
Copy pathrollup-utils.js
File metadata and controls
388 lines (362 loc) · 9.73 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// 生成打包配置
import cliProgress from "cli-progress";
import figlet from "figlet";
import ora from "ora";
import path from "path";
import { createRequire } from "module";
import { terser } from "rollup-plugin-terser";
import visualizer from "rollup-plugin-visualizer";
import serve from "rollup-plugin-serve";
import livereload from "rollup-plugin-livereload";
import delFile from "rollup-plugin-delete";
const require = createRequire(import.meta.url);
const packageInfo = require("./package.json");
let hasPrintedProjectBanner = false;
const color = {
cyan: value => `\x1b[36m${value}\x1b[0m`,
green: value => `\x1b[32m${value}\x1b[0m`,
red: value => `\x1b[1;31m${value}\x1b[0m`,
gray: value => `\x1b[90m${value}\x1b[0m`
};
const isEnabled = value => value === true || value === "true";
const formatDuration = startTime => {
const elapsed = Date.now() - startTime;
if (elapsed < 1000) {
return `${elapsed}ms`;
}
return `${(elapsed / 1000).toFixed(1)}s`;
};
const normalizeModulePath = id => {
if (!id || id.includes("\0")) {
return "internal module";
}
return path
.relative(process.cwd(), id.split("?")[0])
.split(path.sep)
.join("/");
};
const truncateText = (value, maxLength = 56) => {
if (value.length <= maxLength) {
return value;
}
return `...${value.slice(value.length - maxLength + 3)}`;
};
const printProjectBanner = buildName => {
if (hasPrintedProjectBanner || process.env.CI || process.env.BUILD_BANNER === "false") {
return;
}
hasPrintedProjectBanner = true;
const title = figlet.textSync("JS ScreenShot", {
font: "Big",
horizontalLayout: "full",
verticalLayout: "default"
}).trimEnd();
const banner = title
.split("\n")
.map(line => color.red(line))
.join("\n");
const meta = `${packageInfo.name} v${packageInfo.version} | ${buildName}`;
process.stderr.write(`\n${banner}\n${color.gray(meta)}\n\n`);
};
// 处理output对象中的format字段(传入的参数会与rollup所定义的参数不符,因此需要在这里进行转换)
const buildFormat = formatVal => {
let finalFormatVal = formatVal;
switch (formatVal) {
case "esm":
finalFormatVal = "es";
break;
case "common":
finalFormatVal = "cjs";
break;
default:
break;
}
return finalFormatVal;
};
/**
* 根据外部条件判断是否需要给对象添加属性
* @param obj 对象名
* @param condition 条件
* @param propName 属性名
* @param propValue 属性值
*/
const addProperty = (obj, condition, propName, propValue) => {
// 条件成立则添加
if (condition) {
obj[propName] = propValue;
}
};
const buildConfig = (packagingFormat = [], compressedState = "false") => {
const outputConfig = [];
for (let i = 0; i < packagingFormat.length; i++) {
const pkgFormat = packagingFormat[i];
// 根据packagingFormat字段来构建对应格式的包
const config = {
file: `dist/screenShotPlugin.${pkgFormat}.js`,
format: buildFormat(pkgFormat),
name: "screenShotPlugin"
};
// 是否需要对代码进行压缩
addProperty(config, compressedState === "true", "plugins", [
terser({
output: {
comments: false // 删除注释
}
})
]);
addProperty(config, pkgFormat === "common", "exports", "named");
outputConfig.push(config);
}
return outputConfig;
};
const buildCopyTargetsConfig = (useDevServer = "false") => {
const result = [
{
src: "src/assets/fonts/**",
dest: "dist/assets/fonts"
}
];
if (useDevServer === "true") {
result.push({
src: "public/**",
dest: "dist"
});
}
return result;
};
// 生成打包后的模块占用信息
const enablePKGStats = (status = "false") => {
if (status === "true") {
return visualizer({
filename: "dist/bundle-stats.html"
});
}
return null;
};
const buildProgressPlugin = ({
useDevServer = "false",
outputCount = 1,
compressedState = "false"
} = {}) => {
const isWatchMode = isEnabled(useDevServer);
const totalOutputs = Math.max(Number(outputCount) || 1, 1);
const buildName = isWatchMode
? "dev build"
: isEnabled(compressedState)
? "production build"
: "build";
let progressBar = null;
let spinner = null;
let startTime = 0;
let currentProgress = 0;
let transformedModules = 0;
let writtenOutputs = 0;
let failed = false;
const stopProgress = () => {
if (progressBar) {
progressBar.stop();
progressBar = null;
}
if (spinner) {
spinner.stop();
spinner = null;
}
};
const updateProgress = (value, stage) => {
if (failed) {
return;
}
currentProgress = Math.max(currentProgress, Math.min(Math.floor(value), 99));
const stageText = truncateText(stage);
if (spinner) {
spinner.text = `${buildName} - ${stageText}`;
return;
}
if (progressBar) {
progressBar.update(currentProgress, {
stage: stageText
});
}
};
const failProgress = () => {
if (failed) {
return;
}
failed = true;
const duration = formatDuration(startTime);
if (spinner) {
spinner.stopAndPersist({
symbol: color.red("[fail]"),
text: `${buildName} failed after ${duration}`
});
spinner = null;
return;
}
if (progressBar) {
progressBar.stop();
progressBar = null;
process.stderr.write(`${color.red("build failed")} ${color.gray(duration)}\n`);
}
};
const completeProgress = () => {
if (failed) {
return;
}
const duration = formatDuration(startTime);
if (spinner) {
spinner.stopAndPersist({
symbol: color.green("[ok]"),
text: `${buildName} ready in ${duration}`
});
spinner = null;
return;
}
if (progressBar) {
progressBar.update(100, {
stage: `done in ${duration}`
});
progressBar.stop();
progressBar = null;
process.stderr.write(`${color.green("build complete")} ${color.gray(duration)}\n`);
}
};
return {
name: "screen-shot-build-progress",
buildStart() {
startTime = Date.now();
currentProgress = 0;
transformedModules = 0;
writtenOutputs = 0;
failed = false;
stopProgress();
printProjectBanner(buildName);
if (isWatchMode) {
spinner = ora({
text: `${buildName} - starting`,
spinner: "line",
color: "cyan"
}).start();
return;
}
progressBar = new cliProgress.SingleBar({
format: `${color.cyan("rollup")} |{bar}| {percentage}% | {stage}`,
barsize: 34,
barCompleteChar: "=",
barIncompleteChar: "-",
hideCursor: true,
clearOnComplete: false,
stopOnComplete: false,
noTTYOutput: !process.env.CI,
notTTYSchedule: 1000,
autopadding: true
});
progressBar.start(100, 3, {
stage: "starting"
});
currentProgress = 3;
},
resolveId() {
updateProgress(8, "resolving imports");
return null;
},
load() {
updateProgress(12, "loading modules");
return null;
},
transform(code, id) {
transformedModules += 1;
updateProgress(
Math.min(76, 16 + transformedModules * 0.45),
`${transformedModules} modules - ${normalizeModulePath(id)}`
);
return null;
},
buildEnd(error) {
if (error) {
failProgress();
return;
}
updateProgress(78, `${transformedModules} modules ready`);
},
renderStart(outputOptions) {
updateProgress(82, `render ${outputOptions.format || "bundle"}`);
},
generateBundle(outputOptions, bundle) {
if (isWatchMode && spinner) {
spinner.stopAndPersist({
symbol: color.cyan("[build]"),
text: `${buildName} generated ${Object.keys(bundle).length} assets`
});
spinner = null;
return;
}
updateProgress(90, `generate ${Object.keys(bundle).length} assets`);
},
writeBundle(outputOptions) {
writtenOutputs += 1;
const outputName = outputOptions.file
? path.basename(outputOptions.file)
: outputOptions.dir || "dist";
if (isWatchMode && spinner) {
spinner.stopAndPersist({
symbol: color.cyan("[build]"),
text: `${buildName} wrote ${outputName}`
});
spinner = null;
return;
}
updateProgress(92 + Math.min((writtenOutputs / totalOutputs) * 6, 6), `write ${outputName}`);
},
renderError() {
failProgress();
},
closeBundle() {
if (isWatchMode && !spinner) {
process.stderr.write(`${color.green("[ok]")} ${buildName} ready in ${formatDuration(startTime)}\n`);
return;
}
completeProgress();
}
};
};
const enableDevServer = status => {
// 默认清空dist目录下的文件
let serverConfig = [delFile({ targets: "dist/*" })];
if (status === "true") {
// dev模式下不需要对dist目录进行清空
serverConfig = [
serve({
// 服务器启动的文件夹,访问此路径下的index.html文件
contentBase: "dist",
port: 8123
}),
// watch dist目录,当目录中的文件发生变化时,刷新页面
livereload("dist")
];
}
return serverConfig;
};
const buildTSConfig = (useDevServer = "false") => {
return {
tsconfig: "tsconfig.json",
tsconfigOverride: {
compilerOptions: {
// dev模式下不生成.d.ts文件
declaration: useDevServer !== "true",
// 指定目标环境为es5
target: "es5"
},
// 打包时排除tests目录
exclude: ["tests"]
},
clean: true
};
};
export {
buildConfig,
buildCopyTargetsConfig,
enablePKGStats,
enableDevServer,
buildTSConfig,
buildProgressPlugin
};