forked from prettier/prettier
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bundler.js
260 lines (233 loc) · 6.32 KB
/
bundler.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
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
"use strict";
const execa = require("execa");
const path = require("path");
const { rollup } = require("rollup");
const webpack = require("webpack");
const resolve = require("rollup-plugin-node-resolve");
const alias = require("@rollup/plugin-alias");
const commonjs = require("rollup-plugin-commonjs");
const nodeGlobals = require("rollup-plugin-node-globals");
const json = require("rollup-plugin-json");
const replace = require("@rollup/plugin-replace");
const { terser } = require("rollup-plugin-terser");
const babel = require("rollup-plugin-babel");
const nativeShims = require("./rollup-plugins/native-shims");
const executable = require("./rollup-plugins/executable");
const evaluate = require("./rollup-plugins/evaluate");
const externals = require("./rollup-plugins/externals");
const EXTERNALS = [
"assert",
"buffer",
"constants",
"crypto",
"events",
"fs",
"module",
"os",
"path",
"stream",
"url",
"util",
"readline",
// See comment in jest.config.js
"graceful-fs"
];
function getBabelConfig(bundle) {
const config = {
babelrc: false,
plugins: bundle.babelPlugins || [],
exclude: [],
compact: bundle.type === "plugin" ? false : "auto"
};
const presetEnvConfig = {
targets: {
node: "4"
},
modules: false
};
if (bundle.type === "core") {
config.plugins.push(
require.resolve("./babel-plugins/transform-custom-require")
);
if (bundle.corejs) {
Object.assign(presetEnvConfig, {
useBuiltIns: "usage",
corejs: {
version: 3,
proposals: true
}
});
config.exclude.push(/\/core-js\//);
}
}
if (bundle.target === "universal") {
// From https://jamie.build/last-2-versions
presetEnvConfig.targets.browsers = [
">0.25%",
"not ie 11",
"not op_mini all"
];
}
config.presets = [[require.resolve("@babel/preset-env"), presetEnvConfig]];
return config;
}
function getRollupConfig(bundle) {
const config = {
input: bundle.input,
onwarn(warning) {
if (
// We use `eval("require")` to enable dynamic requires in the
// custom parser API
warning.code === "EVAL" ||
// ignore `MIXED_EXPORTS` warn
warning.code === "MIXED_EXPORTS" ||
(warning.code === "CIRCULAR_DEPENDENCY" &&
warning.importer.startsWith("node_modules"))
) {
return;
}
// web bundle can't have external requires
if (
warning.code === "UNRESOLVED_IMPORT" &&
bundle.target === "universal"
) {
throw new Error(
`Unresolved dependency in universal bundle: ${warning.source}`
);
}
console.warn(warning);
}
};
const replaceStrings = {
"process.env.PRETTIER_TARGET": JSON.stringify(bundle.target),
"process.env.NODE_ENV": JSON.stringify("production")
};
if (bundle.target === "universal") {
// We can't reference `process` in UMD bundles and this is
// an undocumented "feature"
replaceStrings["process.env.PRETTIER_DEBUG"] = "global.PRETTIER_DEBUG";
}
Object.assign(replaceStrings, bundle.replace);
const babelConfig = getBabelConfig(bundle);
config.plugins = [
replace({
values: replaceStrings,
delimiters: ["", ""]
}),
executable(),
evaluate(),
json(),
bundle.alias && alias(bundle.alias),
bundle.target === "universal" &&
nativeShims(path.resolve(__dirname, "shims")),
resolve({
extensions: [".js", ".json"],
preferBuiltins: bundle.target === "node"
}),
commonjs(
Object.assign(
bundle.target === "node" ? { ignoreGlobal: true } : {},
bundle.commonjs
)
),
externals(bundle.externals),
bundle.target === "universal" && nodeGlobals(),
babelConfig && babel(babelConfig),
bundle.type === "plugin" && terser()
].filter(Boolean);
if (bundle.target === "node") {
config.external = EXTERNALS;
}
return config;
}
function getRollupOutputOptions(bundle) {
const options = {
file: `dist/${bundle.output}`,
strict: typeof bundle.strict === "undefined" ? true : bundle.strict,
paths: [{ "graceful-fs": "fs" }]
};
if (bundle.target === "node") {
options.format = "cjs";
} else if (bundle.target === "universal") {
options.format = "umd";
options.name =
bundle.type === "plugin" ? `prettierPlugins.${bundle.name}` : bundle.name;
}
return options;
}
function getWebpackConfig(bundle) {
if (bundle.type !== "plugin" || bundle.target !== "universal") {
throw new Error("Must use rollup for this bundle");
}
const root = path.resolve(__dirname, "..", "..");
const config = {
entry: path.resolve(root, bundle.input),
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
options: getBabelConfig(bundle)
}
}
]
},
output: {
path: path.resolve(root, "dist"),
filename: bundle.output,
library: ["prettierPlugins", bundle.name],
libraryTarget: "umd",
// https://github.com/webpack/webpack/issues/6642
globalObject: 'new Function("return this")()'
}
};
if (bundle.terserOptions) {
const TerserPlugin = require("terser-webpack-plugin");
config.optimization = {
minimizer: [new TerserPlugin(bundle.terserOptions)]
};
}
return config;
}
function runWebpack(config) {
return new Promise((resolve, reject) => {
webpack(config, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
module.exports = async function createBundle(bundle, cache) {
if (bundle.corejs && bundle.type !== "core") {
throw new Error("Only core bundles can use corejs");
}
const inputOptions = getRollupConfig(bundle);
const outputOptions = getRollupOutputOptions(bundle);
const useCache = await cache.checkBundle(
bundle.output,
inputOptions,
outputOptions
);
if (useCache) {
try {
await execa("cp", [
path.join(cache.cacheDir, "files", bundle.output),
"dist"
]);
return { cached: true };
} catch (err) {
// Proceed to build
}
}
if (bundle.bundler === "webpack") {
await runWebpack(getWebpackConfig(bundle));
} else {
const result = await rollup(inputOptions);
await result.write(outputOptions);
}
return { bundled: true };
};