Files
test-repo/scripts/build/bundler.js
T

279 lines
7.0 KiB
JavaScript
Raw Normal View History

2018-05-29 14:59:57 -03:00
"use strict";
2018-06-18 15:16:40 -03:00
const execa = require("execa");
2018-05-29 14:59:57 -03:00
const path = require("path");
const { rollup } = require("rollup");
const webpack = require("webpack");
const resolve = require("@rollup/plugin-node-resolve");
2020-05-11 09:05:06 +08:00
const rollupPluginAlias = require("@rollup/plugin-alias");
const commonjs = require("@rollup/plugin-commonjs");
2018-05-29 14:59:57 -03:00
const nodeGlobals = require("rollup-plugin-node-globals");
const json = require("@rollup/plugin-json");
const replace = require("@rollup/plugin-replace");
2019-06-08 03:58:05 +08:00
const { terser } = require("rollup-plugin-terser");
2018-05-29 14:59:57 -03:00
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");
2019-06-07 18:20:40 -03:00
const externals = require("./rollup-plugins/externals");
2018-05-29 14:59:57 -03:00
2020-05-11 09:05:06 +08:00
const PROJECT_ROOT = path.resolve(__dirname, "../..");
2018-05-29 14:59:57 -03:00
const EXTERNALS = [
"assert",
"buffer",
"constants",
"crypto",
"events",
"fs",
"module",
"os",
"path",
"stream",
"url",
"util",
"readline",
2020-01-22 21:36:07 +08:00
"tty",
2018-05-29 14:59:57 -03:00
];
2020-05-11 09:05:06 +08:00
const entries = [
// Force using the CJS file, instead of ESM; i.e. get the file
// from `"main"` instead of `"module"` (rollup default) of package.json
{
find: "outdent",
replacement: require.resolve("outdent"),
},
{
find: "lines-and-columns",
replacement: require.resolve("lines-and-columns"),
},
// `handlebars` causes webpack warning by using `require.extensions`
// `dist/handlebars.js` also complaint on `window` variable
// use cjs build instead
// https://github.com/prettier/prettier/issues/6656
{
find: "handlebars",
replacement: require.resolve("handlebars/dist/cjs/handlebars.js"),
},
{
find: "@angular/compiler/src",
replacement: path.resolve(
`${PROJECT_ROOT}/node_modules/@angular/compiler/esm2015/src`
),
},
];
2018-05-29 14:59:57 -03:00
function getBabelConfig(bundle) {
const config = {
babelrc: false,
2018-06-21 10:29:42 +08:00
plugins: bundle.babelPlugins || [],
2020-03-21 22:07:52 +02:00
compact: bundle.type === "plugin" ? false : "auto",
2018-05-29 14:59:57 -03:00
};
if (bundle.type === "core") {
config.plugins.push(
require.resolve("./babel-plugins/transform-custom-require")
);
}
const targets = { node: "10" };
2018-05-29 14:59:57 -03:00
if (bundle.target === "universal") {
2020-01-29 08:03:36 +08:00
targets.browsers = [
">0.5%",
"not ie 11",
"not safari 5.1",
2020-03-21 22:07:52 +02:00
"not op_mini all",
2020-01-29 08:03:36 +08:00
];
2018-05-29 14:59:57 -03:00
}
config.presets = [
2020-01-10 09:21:44 +08:00
[
require.resolve("@babel/preset-env"),
{
targets,
exclude: ["transform-async-to-generator"],
2020-03-21 22:07:52 +02:00
modules: false,
},
],
2018-05-29 14:59:57 -03:00
];
2020-01-14 14:00:46 +02:00
config.plugins.push([
require.resolve("@babel/plugin-proposal-object-rest-spread"),
2020-03-21 22:07:52 +02:00
{ loose: true, useBuiltIns: true },
2020-01-14 14:00:46 +02:00
]);
2018-05-29 14:59:57 -03:00
return config;
}
function getRollupConfig(bundle) {
const config = {
2019-06-08 03:58:05 +08:00
input: bundle.input,
2018-05-29 14:59:57 -03:00
onwarn(warning) {
if (
// We use `eval("require")` to enable dynamic requires in the
// custom parser API
warning.code === "EVAL" ||
2019-06-08 03:58:05 +08:00
// ignore `MIXED_EXPORTS` warn
warning.code === "MIXED_EXPORTS" ||
2018-05-29 14:59:57 -03:00
(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);
2020-03-21 22:07:52 +02:00
},
2018-05-29 14:59:57 -03:00
};
const replaceStrings = {
"process.env.PRETTIER_TARGET": JSON.stringify(bundle.target),
2020-03-21 22:07:52 +02:00
"process.env.NODE_ENV": JSON.stringify("production"),
2018-05-29 14:59:57 -03:00
};
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);
2020-05-11 09:05:06 +08:00
const alias = { ...bundle.alias };
alias.entries = [...entries, ...(alias.entries || [])];
2018-05-29 14:59:57 -03:00
config.plugins = [
2019-06-08 03:58:05 +08:00
replace({
values: replaceStrings,
2020-03-21 22:07:52 +02:00
delimiters: ["", ""],
2019-06-08 03:58:05 +08:00
}),
2018-05-29 14:59:57 -03:00
executable(),
evaluate(),
2018-05-29 14:59:57 -03:00
json(),
2020-05-11 09:05:06 +08:00
rollupPluginAlias(alias),
2018-05-29 14:59:57 -03:00
bundle.target === "universal" &&
nativeShims(path.resolve(__dirname, "shims")),
resolve({
extensions: [".js", ".json"],
2020-03-21 22:07:52 +02:00
preferBuiltins: bundle.target === "node",
2018-05-29 14:59:57 -03:00
}),
2020-01-31 08:13:37 +08:00
commonjs({
ignoreGlobal: bundle.target === "node",
2020-03-21 22:07:52 +02:00
...bundle.commonjs,
2020-01-31 08:13:37 +08:00
}),
2019-06-07 18:20:40 -03:00
externals(bundle.externals),
2018-05-29 14:59:57 -03:00
bundle.target === "universal" && nodeGlobals(),
babelConfig && babel(babelConfig),
2020-03-21 22:07:52 +02:00
bundle.type === "plugin" && terser(),
2018-05-29 14:59:57 -03:00
].filter(Boolean);
if (bundle.target === "node") {
2019-06-07 18:20:40 -03:00
config.external = EXTERNALS;
2018-05-29 14:59:57 -03:00
}
return config;
}
function getRollupOutputOptions(bundle) {
const options = {
2019-06-08 03:58:05 +08:00
file: `dist/${bundle.output}`,
strict: typeof bundle.strict === "undefined" ? true : bundle.strict,
2018-05-29 14:59:57 -03:00
};
2019-06-08 03:58:05 +08:00
2018-05-29 14:59:57 -03:00
if (bundle.target === "node") {
options.format = "cjs";
} else if (bundle.target === "universal") {
options.format = "umd";
2019-06-08 03:58:05 +08:00
options.name =
2018-05-29 14:59:57 -03:00
bundle.type === "plugin" ? `prettierPlugins.${bundle.name}` : bundle.name;
}
return options;
}
function getWebpackConfig(bundle) {
2018-06-13 17:23:33 -03:00
if (bundle.type !== "plugin" || bundle.target !== "universal") {
throw new Error("Must use rollup for this bundle");
2018-05-29 14:59:57 -03:00
}
const root = path.resolve(__dirname, "..", "..");
2019-09-16 17:13:32 +08:00
const config = {
2018-05-29 14:59:57 -03:00
entry: path.resolve(root, bundle.input),
2018-06-13 17:23:33 -03:00
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
2020-03-21 22:07:52 +02:00
options: getBabelConfig(bundle),
},
},
],
2018-06-13 17:23:33 -03:00
},
2018-05-29 14:59:57 -03:00
output: {
path: path.resolve(root, "dist"),
filename: bundle.output,
2018-06-13 17:23:33 -03:00
library: ["prettierPlugins", bundle.name],
2019-09-14 05:55:30 +08:00
libraryTarget: "umd",
// https://github.com/webpack/webpack/issues/6642
2020-03-21 22:07:52 +02:00
globalObject: 'new Function("return this")()',
},
2018-05-29 14:59:57 -03:00
};
2019-09-16 17:13:32 +08:00
if (bundle.terserOptions) {
const TerserPlugin = require("terser-webpack-plugin");
config.optimization = {
2020-03-21 22:07:52 +02:00
minimizer: [new TerserPlugin(bundle.terserOptions)],
2019-09-16 17:13:32 +08:00
};
}
return config;
2018-05-29 14:59:57 -03:00
}
function runWebpack(config) {
return new Promise((resolve, reject) => {
2020-03-21 22:07:52 +02:00
webpack(config, (err) => {
2018-05-29 14:59:57 -03:00
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
2018-06-18 15:16:40 -03:00
module.exports = async function createBundle(bundle, cache) {
2019-06-08 03:58:05 +08:00
const inputOptions = getRollupConfig(bundle);
const outputOptions = getRollupOutputOptions(bundle);
2018-06-18 15:16:40 -03:00
const useCache = await cache.checkBundle(
bundle.output,
2019-06-08 03:58:05 +08:00
inputOptions,
outputOptions
2018-06-18 15:16:40 -03:00
);
if (useCache) {
try {
await execa("cp", [
path.join(cache.cacheDir, "files", bundle.output),
2020-03-21 22:07:52 +02:00
"dist",
2018-06-18 15:16:40 -03:00
]);
return { cached: true };
} catch (err) {
// Proceed to build
}
}
2018-05-29 14:59:57 -03:00
if (bundle.bundler === "webpack") {
await runWebpack(getWebpackConfig(bundle));
} else {
2019-06-08 03:58:05 +08:00
const result = await rollup(inputOptions);
await result.write(outputOptions);
2018-05-29 14:59:57 -03:00
}
2018-06-18 15:16:40 -03:00
return { bundled: true };
2018-05-29 14:59:57 -03:00
};