-
Notifications
You must be signed in to change notification settings - Fork 802
/
index.js
211 lines (186 loc) · 6.97 KB
/
index.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
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
import esbuild from "esbuild";
import { existsSync, readdirSync, statSync, mkdirSync, writeFileSync, rmSync, readFileSync } from "fs";
import { join, relative, resolve } from "path";
import { exit } from "process";
import { globSync } from "glob";
export const defaultEntryPointGlobs = ['Modules/**/*Page.ts', 'Modules/**/*Page.tsx', 'Modules/**/ScriptInit.ts'];
export function checkIfTrigger() {
if (process.argv.slice(2).some(x => x == "--trigger")) {
if (existsSync('typings/serenity.corelib/_trigger.ts'))
rmSync('typings/serenity.corelib/_trigger.ts')
else {
if (!existsSync('typings/serenity.corelib/'))
mkdirSync('typings/serenity.corelib/');
writeFileSync('typings/serenity.corelib/_trigger.ts', '// for triggering build');
}
exit(0);
}
}
export const importAsGlobalsMapping = {
"@serenity-is/base": "Serenity",
"@serenity-is/corelib": "Serenity",
"@serenity-is/corelib/q": "Q",
"@serenity-is/corelib/slick": "Slick",
"@serenity-is/sleekgrid": "Slick",
"@serenity-is/extensions": "Serenity.Extensions",
"@serenity-is/pro.extensions": "Serenity"
}
export const esbuildOptions = (opt) => {
opt = Object.assign({}, opt);
var entryPointsRegEx;
if (opt.entryPointsRegEx !== undefined) {
entryPointsRegEx = opt.entryPointsRegEx;
delete opt.entryPointsRegEx;
}
var entryPointRoots = ['Modules'];
if (opt.entryPointRoots !== undefined) {
entryPointRoots = opt.entryPointRoots;
delete opt.entryPointRoots;
}
var entryPoints = opt.entryPoints;
if (entryPoints === void 0) {
let globs;
if (existsSync('sergen.json')) {
var json = readFileSync('sergen.json', 'utf8').trim();
var cfg = JSON.parse(json || {});
globs = cfg?.TSBuild?.EntryPoints;
if (globs === void 0 &&
cfg.Extends &&
existsSync(cfg.Extends)) {
json = readFileSync(cfg.Extends, 'utf8').trim();
cfg = JSON.parse(json || {});
globs = cfg?.TSBuild?.EntryPoints;
if (globs != null && globs[0] === '+') {
globs = [...defaultEntryPointGlobs, ...globs.slice(1)];
}
}
}
if (globs == null && !entryPointsRegEx) {
globs = defaultEntryPointGlobs;
}
if (globs != null) {
var include = globs.filter(x => !x.startsWith('!'));
var exclude = globs.filter(x => x.startsWith('!')).map(x => x.substring(1));
exclude.push(".git/**");
exclude.push("App_Data/**");
exclude.push("bin/**");
exclude.push("obj/**");
exclude.push("node_modules/**");
exclude.push("**/node_modules/**");
entryPoints = globSync(include, {
ignore: exclude,
nodir: true,
matchBase: true
});
}
else {
entryPoints = [];
entryPointRoots.forEach(root =>
scanDir(root)
.filter(p => p.match(entryPointsRegEx))
.forEach(p => entryPoints.push(root + '/' + p)));
}
}
var splitting = opt.splitting;
if (splitting === undefined)
splitting = !process.argv.slice(2).some(x => x == "--nosplit");
var plugins = opt.plugins;
if (plugins === undefined) {
plugins = [];
if ((opt.clean === undefined && splitting) || opt.clean)
plugins.push(cleanPlugin());
if (opt.importAsGlobals === undefined || opt.importAsGlobals)
plugins.push(importAsGlobalsPlugin(opt.importAsGlobals ?? importAsGlobalsMapping));
}
delete opt.clean;
delete opt.importAsGlobals;
return Object.assign({
absWorkingDir: resolve('./'),
bundle: true,
chunkNames: '_chunks/[name]-[hash]',
color: true,
entryPoints: entryPoints,
format: 'esm',
keepNames: true,
logLevel: 'info',
metafile: true,
minify: true,
outbase: "./",
outdir: 'wwwroot/esm',
plugins,
sourcemap: true,
splitting: splitting,
target: 'es2017',
watch: process.argv.slice(2).some(x => x == "--watch"),
}, opt);
}
export const build = async (opt) => {
opt = esbuildOptions(opt);
if (opt.watch) {
// this somehow resolves the issue that when debugging is stopped
// in Visual Studio, the node process stays alive
setInterval(() => {
process.stdout.write("");
}, 5000);
delete opt.watch;
const context = await esbuild.context(opt);
await context.watch();
}
else {
delete opt.watch;
await esbuild.build(opt);
}
};
function scanDir(dir, org) {
return readdirSync(dir).reduce((files, file) => {
const absolute = join(dir, file);
return [...files, ...(statSync(absolute).isDirectory()
? scanDir(absolute, org || dir)
: [relative(org || dir, absolute)])]
}, []);
}
// https://github.com/evanw/esbuild/issues/337
export function importAsGlobalsPlugin(mapping) {
const escRe = (s) => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
const filter = new RegExp(Object.keys(mapping).map((mod) =>
`^${escRe(mod)}$`).join("|"));
return {
name: "global-imports",
setup(build) {
build.onResolve({ filter }, (args) => {
if (!mapping[args.path])
throw new Error("Unknown global: " + args.path);
return { path: mapping[args.path], namespace: "external-global" };
});
build.onLoad({ filter: /.*/, namespace: "external-global" },
async (args) => {
return { contents: `module.exports = ${args.path};`, loader: "js" };
});
}
};
}
export function cleanPlugin() {
return {
name: 'clean',
setup(build) {
build.onEnd(result => {
try {
const { outputs } = result.metafile ?? {};
if (!outputs || !existsSync(build.initialOptions.outdir))
return;
const outputFiles = new Set(Object.keys(outputs));
scanDir(build.initialOptions.outdir).forEach(file => {
if (!file.endsWith('.js') && !file.endsWith('.js.map') && !file.endsWith('.css') && !file.endsWith('.css.map'))
return;
if (!outputFiles.has(join(build.initialOptions.outdir, file).replace(/\\/g, '/'))) {
console.log('esbuild clean: deleting extra file ' + file);
rmSync(join(build.initialOptions.outdir, file));
}
});
} catch (e) {
console.error(`esbuild clean: ${e}`);
}
});
}
}
}