-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bundle.ts
150 lines (130 loc) · 4.38 KB
/
bundle.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import path from "path";
import * as rollup from "rollup";
import nodeBuiltins from "builtin-modules";
import nodeResolve from "@rollup/plugin-node-resolve";
import cjs from "@rollup/plugin-commonjs";
import sourcemaps from "rollup-plugin-sourcemaps";
import nodePolyfills from "rollup-plugin-polyfill-node";
import json from "@rollup/plugin-json";
import multiEntry from "@rollup/plugin-multi-entry";
import { leafCommand, makeCommandInfo } from "../../framework/command";
import { resolveProject, resolveRoot } from "../../util/resolveProject";
import { createPrinter } from "../../util/printer";
import { makeOnWarnForTesting, sourcemapsExtra } from "../../config/rollup.base.config";
const log = createPrinter("bundle");
export const commandInfo = makeCommandInfo(
"bundle",
"bundle a package using the default settings",
{
production: {
kind: "boolean",
default: true,
description: "build a CommonJS production bundle",
},
"browser-test": {
kind: "boolean",
default: true,
description: "build a bundle for browser testing",
},
"polyfill-node": {
kind: "boolean",
default: true,
description: "include a polyfill for Node.js builtin modules",
},
}
);
export default leafCommand(commandInfo, async (options) => {
const info = await resolveProject(process.cwd());
if (!info.packageJson.module) {
log.error(info.name, "does not specify a `module` field.");
return false;
}
const basePath = path
.relative(process.cwd(), path.dirname(path.parse(info.packageJson.module).dir))
.split(path.sep)
.join("/");
if (options.production) {
const baseConfig: rollup.RollupOptions = {
// Use the package's module field if it has one
input: info.packageJson.module,
external: [
...nodeBuiltins,
...Object.keys(info.packageJson.dependencies),
...Object.keys(info.packageJson.devDependencies),
],
preserveSymlinks: false,
plugins: [nodeResolve(), sourcemaps()],
};
try {
const bundle = await rollup.rollup(baseConfig);
const cjsOutput = info.packageJson.main ?? info.packageJson.exports?.["."]?.require;
if (!cjsOutput) {
throw new Error("Expecting valid main entry");
}
await bundle.write({
file: cjsOutput,
format: "cjs",
sourcemap: true,
exports: "named",
});
} catch (error: any) {
log.error(error);
return false;
}
log.success("Created production CommonJS bundle.");
}
if (options["browser-test"]) {
const pnpmStore = path
.relative(
process.cwd(),
path.join(await resolveRoot(), "common", "temp", "node_modules", ".pnpm")
)
.split(path.sep)
.join("/");
log.debug("Computed PNPM store relative path:", pnpmStore);
// Get a glob for a package name in the PNPM store
const globFromStore = (name: string): string =>
[pnpmStore, name.split("/").join("+"), "@*", "**/*.js"].join("/");
const browserTestConfig = {
input: {
include: [[basePath, "test", "**", "*.spec.js"].join("/")],
exclude: [[basePath, "test", "**", "node", "**"].join("/")],
},
preserveSymlinks: false,
plugins: [
multiEntry({ exports: false }),
nodeResolve({
mainFields: ["module", "browser"],
preferBuiltins: false,
}),
...(options["polyfill-node"] ? [nodePolyfills({ sourceMap: true })] : []),
cjs({
dynamicRequireTargets: [globFromStore("chai")],
}),
json(),
sourcemapsExtra(),
],
onwarn: makeOnWarnForTesting(),
// Disable tree-shaking of test code. In rollup-plugin-node-resolve@5.0.0,
// rollup started respecting the "sideEffects" field in package.json. Since
// our package.json sets "sideEffects=false", this also applies to test
// code, which causes all tests to be removed by tree-shaking.
treeshake: false,
};
try {
const browserBundle = await rollup.rollup(browserTestConfig as any);
await browserBundle.write({
file: `dist-test/index.browser.js`,
format: "umd",
sourcemap: true,
});
} catch (error: any) {
log.error(error);
return false;
}
log.success("Created browser testing bundle.");
}
return true;
});