forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-standalone.js
More file actions
135 lines (123 loc) · 3.69 KB
/
build-standalone.js
File metadata and controls
135 lines (123 loc) · 3.69 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
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-require-imports */
const esbuild = require('esbuild');
const { spawnSync } = require('child_process');
const ignorePlugin = require('esbuild-plugin-ignore');
const fs = require('fs');
const path = require('path');
const UMD_GLOBAL_NAME = 'AliLowCodeCodeGenerator';
const enableAnalyze = process.env.ANALYZE === 'true';
const buildConfig = {
entryPoints: ['src/standalone.ts'],
outfile: 'dist/standalone.js',
metafile: enableAnalyze,
bundle: true,
target: ['chrome69'],
format: 'cjs',
sourcemap: true,
sourcesContent: true,
plugins: [
ignorePlugin([
{
resourceRegExp: /^fs$/,
contextRegExp: /./,
},
// @alilc/lowcode-types 中误依赖了 react,这里忽略下
{
resourceRegExp: /^react$/,
contextRegExp: /./,
},
{
resourceRegExp: /setter-config/,
contextRegExp: /lowcode-types|..[\\/]types/,
},
]),
],
define: {
process: JSON.stringify({
env: {
NODE_ENV: 'production',
STANDALONE: 'true',
},
}),
},
treeShaking: true,
};
// 执行脚本
(async () => {
try {
console.log('building...');
const result = await esbuild.build({
...buildConfig,
minify: false,
minifyWhitespace: false,
minifyIdentifiers: false,
minifySyntax: false,
legalComments: 'external',
});
if (result.errors.length > 0) {
throw result.errors;
}
if (result.warnings.length > 0) {
result.warnings.forEach((warnings) => {
console.warn(warnings);
});
}
if (enableAnalyze) {
const metaFile = buildConfig.outfile.replace(/\.js/, '.meta.json');
const statsFile = buildConfig.outfile.replace(/\.js/, '.stats.html');
fs.writeFileSync(metaFile, JSON.stringify(result.metafile || {}), { encoding: 'utf-8' });
spawnSync('npx', ['esbuild-visualizer', '--metadata', metaFile, '--filename', statsFile], {
shell: true,
stdio: 'inherit',
});
spawnSync('open', [statsFile], {
shell: true,
stdio: 'inherit',
});
return;
}
const outFileContent = transformCjsToUmdFile(buildConfig.outfile);
console.log('minifying...');
const minifiedOutFile = buildConfig.outfile.replace(/\.js$/, '.min.js');
const minifiedResult = esbuild.transformSync(outFileContent, {
minify: true,
sourcemap: true,
sourcesContent: true,
sourcefile: path.basename(buildConfig.outfile),
});
fs.writeFileSync(minifiedOutFile, minifiedResult.code, { encoding: 'utf-8' });
fs.writeFileSync(`${minifiedOutFile}.map`, minifiedResult.map, { encoding: 'utf-8' });
minifiedResult.warnings.forEach((warnings) => {
console.log(warnings);
});
console.log('done');
} catch (e) {
console.error(e);
process.exit(1);
}
})();
// esbuild 没有直接提供 UMD 格式,所以这里我们自行包装转换下
function transformCjsToUmdFile(file) {
const globalName = UMD_GLOBAL_NAME;
const fileContent = fs.readFileSync(file, { encoding: 'utf-8' });
const transformedFileContent = `(function (global, factory) {
if (typeof exports === 'object' && typeof module !== 'undefined'){
factory(module, exports);
} else if (typeof define === 'function' && define.amd) {
define(['module', 'exports'], factory);
} else {
global = global || self;
var m = { exports: {} };
factory(m, m.exports);
global.${globalName} = m.exports;
}
}(this, function (module, exports) {
'use strict';
${fileContent};
return module.exports;
}));
`;
fs.writeFileSync(file, transformedFileContent, { encoding: 'utf-8' });
return transformedFileContent;
}