forked from GoogleChrome/workbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-window-packages.js
More file actions
117 lines (101 loc) · 3.63 KB
/
build-window-packages.js
File metadata and controls
117 lines (101 loc) · 3.63 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
/*
Copyright 2019 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
const {parallel, series} = require('gulp');
const {rollup} = require('rollup');
const fse = require('fs-extra');
const ol = require('common-tags').oneLine;
const upath = require('upath');
const {transpilePackageOrSkip} = require('./transpile-typescript').functions;
const constants = require('./utils/constants');
const logHelper = require('../infra/utils/log-helper');
const packageRunner = require('./utils/package-runner');
const pkgPathToName = require('./utils/pkg-path-to-name');
const rollupHelper = require('./utils/rollup-helper');
const versionModule = require('./utils/version-module');
async function buildWindowBundle(packagePath, buildType) {
const packageName = pkgPathToName(packagePath);
const packageIndex = upath.join(packagePath, 'index.mjs');
if (!(await fse.exists(packageIndex))) {
throw new Error(`Could not find ${packageIndex}`);
}
const outputDirectory = upath.join(packagePath,
constants.PACKAGE_BUILD_DIRNAME);
const esmFilename = `${packageName}.${buildType.slice(0, 4)}.mjs`;
const esmLegacyFilename = `${packageName}.${buildType.slice(0, 4)}.es5.mjs`;
const umdFilename = `${packageName}.${buildType.slice(0, 4)}.umd.js`;
const onwarn = (warning) => {
// This can occur when using rollup-plugin-replace.
if (buildType === constants.BUILD_TYPES.prod &&
warning.code === 'UNUSED_EXTERNAL_IMPORT') {
logHelper.warn(`[${warning.code}] ${warning.message}`);
return;
}
// The final builds should have no warnings.
if (warning.code && warning.message) {
throw new Error(ol`Unhandled Rollup Warning:
[${warning.code}] ${warning.message}`);
} else {
throw new Error(`Unhandled Rollup Warning: ${warning}`);
}
};
const mjsBundle = await rollup({
input: packageIndex,
plugins: rollupHelper.getDefaultPlugins(buildType, 'esm', false),
onwarn,
});
const es5Bundle = await rollup({
input: packageIndex,
plugins: rollupHelper.getDefaultPlugins(buildType, 'esm', true),
onwarn,
});
const umdBundle = await rollup({
input: packageIndex,
plugins: rollupHelper.getDefaultPlugins(buildType, 'umd', true),
onwarn,
});
// Generate both a native module and a UMD module (for compat).
await Promise.all([
mjsBundle.write({
file: upath.join(outputDirectory, esmFilename),
sourcemap: true,
format: 'esm',
}),
es5Bundle.write({
file: upath.join(outputDirectory, esmLegacyFilename),
sourcemap: true,
format: 'esm',
}),
umdBundle.write({
file: upath.join(outputDirectory, umdFilename),
sourcemap: true,
format: 'umd',
name: 'workbox',
}),
]);
}
// This reads a little cleaner with a function to generate the sub-sequences.
function windowBundleSequence() {
const transpilations = packageRunner(
'build_window_packages_transpile_typescript', 'window',
transpilePackageOrSkip);
const builds = Object.keys(constants.BUILD_TYPES).map((type) => packageRunner(
'build_window_packages_bundle', 'window', buildWindowBundle,
constants.BUILD_TYPES[type]));
return series(
parallel(transpilations),
// This needs to be a series, not in parallel, so that there isn't a
// race condition with the terser nameCache.
series(builds),
);
}
module.exports = {
build_window_packages: series(
parallel(packageRunner('build_window_packages_version_module', 'window',
versionModule)),
windowBundleSequence(),
),
};