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/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import es from 'event-stream';
import gulp from 'gulp';
import filter from 'gulp-filter';
import path from 'path';
import fs from 'fs';
import pump from 'pump';
import VinylFile from 'vinyl';
import * as bundle from './bundle.ts';
import esbuild from 'esbuild';
import sourcemaps from 'gulp-sourcemaps';
import fancyLog from 'fancy-log';
import ansiColors from 'ansi-colors';
import { getTargetStringFromTsConfig } from './tsconfigUtils.ts';
import svgmin from 'gulp-svgmin';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
declare module 'gulp-sourcemaps' {
interface WriteOptions {
addComment?: boolean;
includeContent?: boolean;
sourceRoot?: string | WriteMapper;
sourceMappingURL?: ((f: any) => string);
sourceMappingURLPrefix?: string | WriteMapper;
clone?: boolean | CloneOptions;
}
}
const REPO_ROOT_PATH = path.join(import.meta.dirname, '../..');
export interface IBundleESMTaskOpts {
/**
* The folder to read files from.
*/
src: string;
/**
* The entry points to bundle.
*/
entryPoints: Array<bundle.IEntryPoint | string>;
/**
* Other resources to consider (svg, etc.)
*/
resources?: string[];
/**
* File contents interceptor for a given path.
*/
fileContentMapper?: (path: string) => ((contents: string) => Promise<string> | string) | undefined;
/**
* Allows to skip the removal of TS boilerplate. Use this when
* the entry point is small and the overhead of removing the
* boilerplate makes the file larger in the end.
*/
skipTSBoilerplateRemoval?: (entryPointName: string) => boolean;
}
const DEFAULT_FILE_HEADER = [
'/*!--------------------------------------------------------',
' * Copyright (C) Microsoft Corporation. All rights reserved.',
' *--------------------------------------------------------*/'
].join('\n');
function bundleESMTask(opts: IBundleESMTaskOpts): NodeJS.ReadWriteStream {
const resourcesStream = es.through(); // this stream will contain the resources
const bundlesStream = es.through(); // this stream will contain the bundled files
const target = getBuildTarget();
const entryPoints = opts.entryPoints.map(entryPoint => {
if (typeof entryPoint === 'string') {
return { name: path.parse(entryPoint).name };
}
return entryPoint;
});
const bundleAsync = async () => {
const files: VinylFile[] = [];
const tasks: Promise<any>[] = [];
for (const entryPoint of entryPoints) {
fancyLog(`Bundled entry point: ${ansiColors.yellow(entryPoint.name)}...`);
// support for 'dest' via esbuild#in/out
const dest = entryPoint.dest?.replace(/\.[^/.]+$/, '') ?? entryPoint.name;
// banner contents
const banner = {
js: DEFAULT_FILE_HEADER,
css: DEFAULT_FILE_HEADER
};
// TS Boilerplate
if (!opts.skipTSBoilerplateRemoval?.(entryPoint.name)) {
const tslibPath = path.join(require.resolve('tslib'), '../tslib.es6.js');
banner.js += await fs.promises.readFile(tslibPath, 'utf-8');
}
const contentsMapper: esbuild.Plugin = {
name: 'contents-mapper',
setup(build) {
build.onLoad({ filter: /\.js$/ }, async ({ path }) => {
const contents = await fs.promises.readFile(path, 'utf-8');
// TS Boilerplate
let newContents: string;
if (!opts.skipTSBoilerplateRemoval?.(entryPoint.name)) {
newContents = bundle.removeAllTSBoilerplate(contents);
} else {
newContents = contents;
}
// File Content Mapper
const mapper = opts.fileContentMapper?.(path.replace(/\\/g, '/'));
if (mapper) {
newContents = await mapper(newContents);
}
return { contents: newContents };
});
}
};
const externalOverride: esbuild.Plugin = {
name: 'external-override',
setup(build) {
// We inline selected modules that are we depend on on startup without
// a conditional `await import(...)` by hooking into the resolution.
build.onResolve({ filter: /^minimist$/ }, () => {
return { path: path.join(REPO_ROOT_PATH, 'node_modules', 'minimist', 'index.js'), external: false };
});
},
};
const task = esbuild.build({
bundle: true,
packages: 'external', // "external all the things", see https://esbuild.github.io/api/#packages
platform: 'neutral', // makes esm
format: 'esm',
sourcemap: 'external',
plugins: [contentsMapper, externalOverride],
target: [target],
loader: {
'.ttf': 'file',
'.svg': 'file',
'.png': 'file',
'.sh': 'file',
},
assetNames: 'media/[name]', // moves media assets into a sub-folder "media"
banner,
entryPoints: [
{
in: path.join(REPO_ROOT_PATH, opts.src, `${entryPoint.name}.js`),
out: dest,
}
],
outdir: path.join(REPO_ROOT_PATH, opts.src),
write: false, // enables res.outputFiles
metafile: true, // enables res.metafile
// minify: NOT enabled because we have a separate minify task that takes care of the TSLib banner as well
}).then(res => {
for (const file of res.outputFiles) {
let sourceMapFile: esbuild.OutputFile | undefined = undefined;
if (file.path.endsWith('.js')) {
sourceMapFile = res.outputFiles.find(f => f.path === `${file.path}.map`);
}
const fileProps = {
contents: Buffer.from(file.contents),
sourceMap: sourceMapFile ? JSON.parse(sourceMapFile.text) : undefined, // support gulp-sourcemaps
path: file.path,
base: path.join(REPO_ROOT_PATH, opts.src)
};
files.push(new VinylFile(fileProps));
}
});
tasks.push(task);
}
await Promise.all(tasks);
return { files };
};
bundleAsync().then((output) => {
// bundle output (JS, CSS, SVG...)
es.readArray(output.files).pipe(bundlesStream);
// forward all resources
gulp.src(opts.resources ?? [], { base: `${opts.src}`, allowEmpty: true }).pipe(resourcesStream);
});
const result = es.merge(
bundlesStream,
resourcesStream
);
return result
.pipe(sourcemaps.write('./', {
sourceRoot: undefined,
addComment: true,
includeContent: true
}));
}
export interface IBundleTaskOpts {
/**
* Destination folder for the bundled files.
*/
out: string;
/**
* Bundle ESM modules (using esbuild).
*/
esm: IBundleESMTaskOpts;
}
export function bundleTask(opts: IBundleTaskOpts): () => NodeJS.ReadWriteStream {
return function () {
return bundleESMTask(opts.esm).pipe(gulp.dest(opts.out));
};
}
export function minifyTask(src: string, sourceMapBaseUrl?: string): (cb: any) => void {
const sourceMappingURL = sourceMapBaseUrl ? ((f: any) => `${sourceMapBaseUrl}/${f.relative}.map`) : undefined;
const target = getBuildTarget();
return cb => {
const esbuildFilter = filter('**/*.{js,css}', { restore: true });
const svgFilter = filter('**/*.svg', { restore: true });
pump(
gulp.src([src + '/**', '!' + src + '/**/*.map']),
esbuildFilter,
sourcemaps.init({ loadMaps: true }),
es.map((f: any, cb) => {
esbuild.build({
entryPoints: [f.path],
minify: true,
sourcemap: 'external',
outdir: '.',
packages: 'external', // "external all the things", see https://esbuild.github.io/api/#packages
platform: 'neutral', // makes esm
target: [target],
write: false,
}).then(res => {
const jsOrCSSFile = res.outputFiles.find(f => /\.(js|css)$/.test(f.path))!;
const sourceMapFile = res.outputFiles.find(f => /\.(js|css)\.map$/.test(f.path))!;
const contents = Buffer.from(jsOrCSSFile.contents);
const unicodeMatch = contents.toString().match(/[^\x00-\xFF]+/g);
if (unicodeMatch) {
cb(new Error(`Found non-ascii character ${unicodeMatch[0]} in the minified output of ${f.path}. Non-ASCII characters in the output can cause performance problems when loading. Please review if you have introduced a regular expression that esbuild is not automatically converting and convert it to using unicode escape sequences.`));
} else {
f.contents = contents;
f.sourceMap = JSON.parse(sourceMapFile.text);
cb(undefined, f);
}
}, cb);
}),
esbuildFilter.restore,
svgFilter,
svgmin(),
svgFilter.restore,
sourcemaps.write('./', {
sourceMappingURL,
sourceRoot: undefined,
includeContent: true,
addComment: true
}),
gulp.dest(src + '-min'),
(err: any) => cb(err));
};
}
function getBuildTarget() {
const tsconfigPath = path.join(REPO_ROOT_PATH, 'src', 'tsconfig.base.json');
return getTargetStringFromTsConfig(tsconfigPath);
}