2018-05-29 20:59:57 +03:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-18 21:16:40 +03:00
|
|
|
const execa = require("execa");
|
2018-05-29 20:59:57 +03:00
|
|
|
const path = require("path");
|
|
|
|
const { rollup } = require("rollup");
|
|
|
|
const webpack = require("webpack");
|
|
|
|
const resolve = require("rollup-plugin-node-resolve");
|
2019-10-31 15:16:58 +03:00
|
|
|
const alias = require("@rollup/plugin-alias");
|
2018-05-29 20:59:57 +03:00
|
|
|
const commonjs = require("rollup-plugin-commonjs");
|
|
|
|
const nodeGlobals = require("rollup-plugin-node-globals");
|
|
|
|
const json = require("rollup-plugin-json");
|
2019-10-30 16:07:01 +03:00
|
|
|
const replace = require("@rollup/plugin-replace");
|
2019-06-07 22:58:05 +03:00
|
|
|
const { terser } = require("rollup-plugin-terser");
|
2018-05-29 20:59:57 +03:00
|
|
|
const babel = require("rollup-plugin-babel");
|
|
|
|
const nativeShims = require("./rollup-plugins/native-shims");
|
|
|
|
const executable = require("./rollup-plugins/executable");
|
2018-09-02 11:20:22 +03:00
|
|
|
const evaluate = require("./rollup-plugins/evaluate");
|
2019-06-08 00:20:40 +03:00
|
|
|
const externals = require("./rollup-plugins/externals");
|
2018-05-29 20:59:57 +03:00
|
|
|
|
|
|
|
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,
|
2018-06-21 05:29:42 +03:00
|
|
|
plugins: bundle.babelPlugins || [],
|
2018-05-29 20:59:57 +03:00
|
|
|
compact: bundle.type === "plugin" ? false : "auto"
|
|
|
|
};
|
|
|
|
if (bundle.type === "core") {
|
|
|
|
config.plugins.push(
|
|
|
|
require.resolve("./babel-plugins/transform-custom-require")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const targets = { node: 4 };
|
|
|
|
if (bundle.target === "universal") {
|
|
|
|
// From https://jamie.build/last-2-versions
|
|
|
|
targets.browsers = [">0.25%", "not ie 11", "not op_mini all"];
|
|
|
|
}
|
|
|
|
config.presets = [
|
|
|
|
[require.resolve("@babel/preset-env"), { targets, modules: false }]
|
|
|
|
];
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRollupConfig(bundle) {
|
|
|
|
const config = {
|
2019-06-07 22:58:05 +03:00
|
|
|
input: bundle.input,
|
2018-05-29 20: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-07 22:58:05 +03:00
|
|
|
// ignore `MIXED_EXPORTS` warn
|
|
|
|
warning.code === "MIXED_EXPORTS" ||
|
2018-05-29 20: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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const replaceStrings = {
|
2018-12-11 13:59:59 +03:00
|
|
|
"process.env.PRETTIER_TARGET": JSON.stringify(bundle.target),
|
2018-07-01 17:22:40 +03:00
|
|
|
"process.env.NODE_ENV": JSON.stringify("production")
|
2018-05-29 20: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);
|
|
|
|
|
|
|
|
config.plugins = [
|
2019-06-07 22:58:05 +03:00
|
|
|
replace({
|
|
|
|
values: replaceStrings,
|
|
|
|
delimiters: ["", ""]
|
|
|
|
}),
|
2018-05-29 20:59:57 +03:00
|
|
|
executable(),
|
2018-09-02 11:20:22 +03:00
|
|
|
evaluate(),
|
2018-05-29 20:59:57 +03:00
|
|
|
json(),
|
2018-06-21 05:29:42 +03:00
|
|
|
bundle.alias && alias(bundle.alias),
|
2018-05-29 20:59:57 +03:00
|
|
|
bundle.target === "universal" &&
|
|
|
|
nativeShims(path.resolve(__dirname, "shims")),
|
|
|
|
resolve({
|
|
|
|
extensions: [".js", ".json"],
|
|
|
|
preferBuiltins: bundle.target === "node"
|
|
|
|
}),
|
2019-05-15 18:57:04 +03:00
|
|
|
commonjs(
|
|
|
|
Object.assign(
|
|
|
|
bundle.target === "node" ? { ignoreGlobal: true } : {},
|
|
|
|
bundle.commonjs
|
|
|
|
)
|
|
|
|
),
|
2019-06-08 00:20:40 +03:00
|
|
|
externals(bundle.externals),
|
2018-05-29 20:59:57 +03:00
|
|
|
bundle.target === "universal" && nodeGlobals(),
|
|
|
|
babelConfig && babel(babelConfig),
|
2019-06-07 22:58:05 +03:00
|
|
|
bundle.type === "plugin" && terser()
|
2018-05-29 20:59:57 +03:00
|
|
|
].filter(Boolean);
|
|
|
|
|
|
|
|
if (bundle.target === "node") {
|
2019-06-08 00:20:40 +03:00
|
|
|
config.external = EXTERNALS;
|
2018-05-29 20:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRollupOutputOptions(bundle) {
|
|
|
|
const options = {
|
2019-06-07 22:58:05 +03:00
|
|
|
file: `dist/${bundle.output}`,
|
|
|
|
strict: typeof bundle.strict === "undefined" ? true : bundle.strict,
|
|
|
|
paths: [{ "graceful-fs": "fs" }]
|
2018-05-29 20:59:57 +03:00
|
|
|
};
|
2019-06-07 22:58:05 +03:00
|
|
|
|
2018-05-29 20:59:57 +03:00
|
|
|
if (bundle.target === "node") {
|
|
|
|
options.format = "cjs";
|
|
|
|
} else if (bundle.target === "universal") {
|
|
|
|
options.format = "umd";
|
2019-06-07 22:58:05 +03:00
|
|
|
options.name =
|
2018-05-29 20:59:57 +03:00
|
|
|
bundle.type === "plugin" ? `prettierPlugins.${bundle.name}` : bundle.name;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getWebpackConfig(bundle) {
|
2018-06-13 23:23:33 +03:00
|
|
|
if (bundle.type !== "plugin" || bundle.target !== "universal") {
|
|
|
|
throw new Error("Must use rollup for this bundle");
|
2018-05-29 20:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const root = path.resolve(__dirname, "..", "..");
|
2019-09-16 12:13:32 +03:00
|
|
|
const config = {
|
2018-05-29 20:59:57 +03:00
|
|
|
entry: path.resolve(root, bundle.input),
|
2018-06-13 23:23:33 +03:00
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.js$/,
|
|
|
|
use: {
|
|
|
|
loader: "babel-loader",
|
|
|
|
options: getBabelConfig(bundle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2018-05-29 20:59:57 +03:00
|
|
|
output: {
|
|
|
|
path: path.resolve(root, "dist"),
|
|
|
|
filename: bundle.output,
|
2018-06-13 23:23:33 +03:00
|
|
|
library: ["prettierPlugins", bundle.name],
|
2019-09-14 00:55:30 +03:00
|
|
|
libraryTarget: "umd",
|
|
|
|
// https://github.com/webpack/webpack/issues/6642
|
|
|
|
globalObject: 'new Function("return this")()'
|
|
|
|
}
|
2018-05-29 20:59:57 +03:00
|
|
|
};
|
2019-09-16 12:13:32 +03:00
|
|
|
|
|
|
|
if (bundle.terserOptions) {
|
|
|
|
const TerserPlugin = require("terser-webpack-plugin");
|
|
|
|
|
|
|
|
config.optimization = {
|
2019-10-30 15:33:52 +03:00
|
|
|
minimizer: [new TerserPlugin(bundle.terserOptions)]
|
2019-09-16 12:13:32 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
2018-05-29 20:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function runWebpack(config) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
webpack(config, err => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 21:16:40 +03:00
|
|
|
module.exports = async function createBundle(bundle, cache) {
|
2019-06-07 22:58:05 +03:00
|
|
|
const inputOptions = getRollupConfig(bundle);
|
|
|
|
const outputOptions = getRollupOutputOptions(bundle);
|
|
|
|
|
2018-06-18 21:16:40 +03:00
|
|
|
const useCache = await cache.checkBundle(
|
|
|
|
bundle.output,
|
2019-06-07 22:58:05 +03:00
|
|
|
inputOptions,
|
|
|
|
outputOptions
|
2018-06-18 21:16:40 +03:00
|
|
|
);
|
|
|
|
if (useCache) {
|
|
|
|
try {
|
|
|
|
await execa("cp", [
|
|
|
|
path.join(cache.cacheDir, "files", bundle.output),
|
|
|
|
"dist"
|
|
|
|
]);
|
|
|
|
return { cached: true };
|
|
|
|
} catch (err) {
|
|
|
|
// Proceed to build
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:59:57 +03:00
|
|
|
if (bundle.bundler === "webpack") {
|
|
|
|
await runWebpack(getWebpackConfig(bundle));
|
|
|
|
} else {
|
2019-06-07 22:58:05 +03:00
|
|
|
const result = await rollup(inputOptions);
|
|
|
|
await result.write(outputOptions);
|
2018-05-29 20:59:57 +03:00
|
|
|
}
|
2018-06-18 21:16:40 +03:00
|
|
|
|
|
|
|
return { bundled: true };
|
2018-05-29 20:59:57 +03:00
|
|
|
};
|