forked from InboxSDK/InboxSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.ts
428 lines (396 loc) · 12.1 KB
/
gulpfile.babel.ts
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import fs from 'node:fs';
import path from 'node:path';
import gulp from 'gulp';
import destAtomic from 'gulp-dest-atomic';
import webpack from 'webpack';
import stdio from 'stdio';
import extReloader from './live/extReloader';
import fg from 'fast-glob';
import exec from './src/build/exec';
const sdkFilename = 'inboxsdk';
const args = stdio.getopt({
// --watch is a node flag that doesn't seem to work after Node 18.12 with gulp.
// using -w seems to work though.
watch: { key: 'w', description: 'Automatic rebuild' },
reloader: { key: 'r', description: 'Automatic extension reloader' },
hot: { key: 'h', description: 'hot module replacement' },
remote: {
description: 'Remote-loading bundle with integrated pageWorld script',
},
integratedPageWorld: {
description:
'Non-remote-loading bundle with integrated pageWorld script, for testing in projects that expect the remote-loading build',
},
minify: { key: 'm', description: 'Minify build' },
production: { key: 'p', description: 'Production build' },
copyToStreak: {
key: 'c',
description: 'Copy dev build to Streak dev build folder',
},
});
// Don't let production be built without minification.
// Could just make the production flag imply the minify flag, but that seems
// like it would harm discoverability.
if (args.remote && args.production && !args.minify) {
throw new Error('--remote --production requires --minify');
}
// --watch causes Browserify to use full paths in module references. We don't
// want those visible in production.
if (args.production && (args.watch || args.integratedPageWorld)) {
throw new Error(
'--production can not be used with --watch, or --integratedPageWorld',
);
}
process.env.NODE_ENV = args.production ? 'production' : 'development';
process.env.IMPLEMENTATION_URL = args.production
? 'https://www.inboxsdk.com/build/platform-implementation.js'
: 'http://localhost:4567/platform-implementation.js';
async function setupExamples() {
// Copy inboxsdk.js (and .map) to all subdirs under examples/
let dirs: string[] = [];
if (args.copyToStreak) {
dirs.push('../MailFoo/extensions/devBuilds/chrome/');
}
let srcs: string[] = [];
if (!args.remote && !args.integratedPageWorld) {
srcs = [
'./packages/core/inboxsdk.js',
'./packages/core/pageWorld.js',
'./packages/core/background.js',
];
dirs = dirs.concat(await fg(['examples/*'], { onlyDirectories: true }));
} else if (args.remote) {
dirs.push('./dist');
srcs = [
'./packages/core/inboxsdk.js*',
'./packages/core/platform-implementation.js*',
'./packages/core/pageWorld.js*',
];
} else if (args.integratedPageWorld) {
dirs.push('./dist');
srcs = ['./packages/core/inboxsdk.js', './packages/core/pageWorld.js'];
}
let stream = gulp.src(srcs);
for (const dir of dirs) {
stream = stream.pipe(destAtomic(dir));
}
await new Promise((resolve, reject) => {
stream.on('error', (err: any) => {
reject(err);
});
stream.on('finish', () => {
resolve(undefined);
});
});
if (args.reloader) {
await extReloader();
}
}
gulp.task('noop', () => {
// This task exists so we can run `yarn gulp noop` to check that this
// script loads without any side effects.
});
async function getVersion(): Promise<string> {
const results = await Promise.all([
exec('git rev-list HEAD --max-count=1'),
exec('git status --porcelain'),
]);
const commit = results[0].toString().trim().slice(0, 16);
const isModified = /^\s*M/m.test(results[1].toString());
const packageJson = JSON.parse(
await fs.promises.readFile(
path.join(__dirname, 'packages/core/package.json'),
'utf8',
),
);
let version = `${packageJson.version}-${Date.now()}-${commit}`;
if (isModified) {
version += '-MODIFIED';
}
if (args.watch) {
version += '-WATCH';
}
return version;
}
const enum OutputLibraryType {
/** Remote and npm output format for compatibility's sake */
UMD = 'umd',
/** Future output format */
ESM = 'module',
}
interface WebpackTaskOptions {
entry: webpack.Configuration['entry'];
devtool?: webpack.Configuration['devtool'];
disableMinification?: boolean;
afterBuild?: () => Promise<void>;
plugins?: webpack.Configuration['plugins'];
}
async function webpackTask({
entry,
disableMinification,
...options
}: WebpackTaskOptions): Promise<void> {
const willMinify = (args.minify && !disableMinification) ?? false;
const VERSION = await getVersion();
const bundler = webpack({
devtool: options.devtool ?? 'source-map',
entry,
mode: args.production ? 'production' : 'development',
module: {
rules: [
{
resourceQuery: /raw/,
type: 'asset/source',
},
{
exclude: /(node_modules|dist|packages\/core)/,
test: /\.m?[jt]sx?$/,
use: {
loader: 'babel-loader',
options: {
plugins: [
[
'transform-inline-environment-variables',
{
include: ['NODE_ENV', 'IMPLEMENTATION_URL', 'VERSION'],
},
],
],
},
},
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
options: {
insert: (htmlElement: HTMLStyleElement) => {
htmlElement.setAttribute(
'data-inboxsdk-version',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- this is injected by webpack
///@ts-ignore
SDK_VERSION,
);
document.head.append(htmlElement);
},
},
},
{
loader: 'css-loader',
options: {
// importLoaders: 1,
modules: {
exportLocalsConvention: 'dashesOnly',
localIdentName: args.production
? 'inboxsdk__[hash:base64]'
: 'inboxsdk__[name]__[local][hash:base64]',
mode: (resourcePath: string) =>
resourcePath.endsWith('.module.css') ? 'local' : 'global',
namedExport: true,
},
},
},
{ loader: 'postcss-loader' },
],
},
],
},
optimization: {
minimize: willMinify,
},
output: {
path: path.join(__dirname, 'packages/core'),
uniqueName: 'inboxsdk_' + VERSION,
},
plugins: [
new webpack.DefinePlugin({
SDK_VERSION: JSON.stringify(VERSION),
/**
* This flag would allow us to build an npm build with MV2 support.
* We keep this off so npm builds don't trigger false-positives
* in Chrome Web Store reviews checking for dynamically loaded code in MV3 extensions.
* This flag has no effect for remote/non-npm builds.
*/
NPM_MV2_SUPPORT: JSON.stringify(false),
}),
...(willMinify || args.production
? [
new webpack.BannerPlugin({
banner: fs.readFileSync('./src/inboxsdk-js/header.ts', 'utf8'),
raw: true,
entryOnly: true,
}),
]
: []),
// Work around for Buffer is undefined:
// https://github.com/webpack/changelog-v5/issues/10
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
// transitively from react-draggable-list -> react-motion -> performance-now uses 'process/browser'
process: 'process/browser',
}),
...(options.plugins ?? []),
],
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
fallback: {
// 'querystring' used in the SDK directly
querystring: require.resolve('querystring-es3/'),
},
},
stats: {
errorDetails: true,
},
} satisfies webpack.Configuration);
if (args.watch) {
bundler.watch({}, (err, stats) => {
if (err) {
console.error(err);
return;
}
console.log(stats?.toString({ colors: true }));
options.afterBuild?.();
});
} else {
await new Promise<void>((resolve, reject) => {
bundler.run((err, stats) => {
if (err) {
console.error(err);
reject(err);
return;
}
console.log(stats?.toString({ colors: true }));
resolve();
});
});
return options.afterBuild?.();
}
}
const pageWorld: webpack.EntryObject = {
pageWorld: './src/injected-js/main',
};
gulp.task('clean', async () => {
const folders = ['./dist', './packages/core/src', './packages/core/test'];
await Promise.all([
...folders.map((folder) =>
fs.promises.rm(folder, { force: true, recursive: true }),
),
fs.promises.rm('./packages/core/inboxsdk.min.js', { force: true }),
]);
const outputFiles = [
'./packages/core/inboxsdk.js',
'./packages/core/platform-implementation.js',
'./packages/core/pageWorld.js',
];
outputFiles.flatMap((filename) => [
fs.promises.rm(filename, { force: true }),
fs.promises.rm(filename + '.LICENSE.txt', { force: true }),
fg(filename + '*.map', { onlyFiles: true }).then((mapFiles) =>
Promise.all(
mapFiles.map((mapFile) => fs.promises.rm(mapFile, { force: true })),
),
),
]);
});
/**
* Copy handwritten type definitions (inboxsdk.d.ts) and those generated from source.
*/
gulp.task('types', async () => {
const files = await fg(['./src/**/*.d.ts'], {
onlyFiles: true,
ignore: ['packages/core'],
});
for (const f of files) {
const newPath = path.join('./packages/core', f);
await fs.promises.mkdir(path.dirname(newPath), { recursive: true });
await fs.promises.copyFile(f, newPath);
}
await exec('yarn typedefs');
});
let config: WebpackTaskOptions;
const remoteCompatConfig = {
afterBuild: setupExamples,
} as const;
const sourceMapPlugin = new webpack.SourceMapDevToolPlugin({
fileContext: '../',
// Adds hash to the end of the sourcemap URL so that
// remote code source maps are not cached erroneously by Sentry
// if and when https://github.com/getsentry/sentry/issues/54819 is patched up.
filename: '[file].[contenthash].map',
publicPath: 'https://www.inboxsdk.com/build/',
});
if (args.remote) {
config = {
...remoteCompatConfig,
devtool: false,
entry: {
[sdkFilename]: {
library: {
export: 'default',
name: 'InboxSDK',
type: OutputLibraryType.UMD,
},
import: './src/inboxsdk-js/inboxsdk-REMOTE',
},
'platform-implementation':
'./src/platform-implementation-js/main-INTEGRATED-PAGEWORLD',
},
plugins: [sourceMapPlugin],
};
} else if (args.integratedPageWorld) {
// non-remote bundle built for compatibility with remote bundle
config = {
...remoteCompatConfig,
devtool: 'inline-source-map',
entry: {
[sdkFilename]: {
library: {
export: 'default',
name: 'InboxSDK',
type: OutputLibraryType.UMD,
},
import: './src/inboxsdk-js/inboxsdk-NONREMOTE-INTEGRATED-PAGEWORLD',
},
},
};
} else {
// standard npm non-remote bundle
config = {
devtool: args.production ? false : 'inline-source-map',
entry: {
...pageWorld,
[sdkFilename]: {
library: {
export: 'default',
name: 'InboxSDK',
type: OutputLibraryType.UMD,
},
import: './src/inboxsdk-js/inboxsdk-NONREMOTE',
},
},
disableMinification: true,
afterBuild: async () => {
setupExamples();
},
};
}
gulp.task('sdk', async () => {
// Only the npm build contains pageWorld as of writing.
if (config.entry instanceof Object && !('pageWorld' in config.entry)) {
await webpackTask({
entry: pageWorld,
devtool: args.remote ? false : 'inline-source-map',
plugins: [sourceMapPlugin],
});
}
return webpackTask(config);
});
gulp.task('default', gulp.parallel('sdk', 'types'));
gulp.task(
'server',
gulp.series('sdk', async function serverRun() {
const app = await import('./live/app');
app.run();
}),
);