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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

262 lines
7.6 KiB
JavaScript
Raw Normal View History

import fs from "node:fs/promises";
2021-05-15 20:09:52 +08:00
import path from "node:path";
import createEsmUtils from "esm-utils";
2021-05-27 08:54:42 +08:00
import builtinModules from "builtin-modules";
import * as babel from "@babel/core";
import esbuild from "esbuild";
import { NodeModulesPolyfillPlugin as esbuildPluginNodeModulePolyfills } from "@esbuild-plugins/node-modules-polyfill";
import esbuildPluginTextReplace from "esbuild-plugin-text-replace";
import browserslistToEsbuild from "browserslist-to-esbuild";
2021-09-11 09:55:46 +08:00
import { PROJECT_ROOT, DIST_DIR } from "../utils/index.mjs";
import esbuildPluginEvaluate from "./esbuild-plugins/evaluate.mjs";
import esbuildPluginReplaceModule from "./esbuild-plugins/replace-module.mjs";
import esbuildPluginLicense from "./esbuild-plugins/license.mjs";
import esbuildPluginUmd from "./esbuild-plugins/umd.mjs";
2021-05-27 08:54:42 +08:00
import bundles from "./config.mjs";
2018-05-29 14:59:57 -03:00
2021-11-29 12:52:43 +08:00
const { __dirname, json } = createEsmUtils(import.meta);
const packageJson = json.loadSync("../../package.json");
2020-05-11 09:05:06 +08:00
const umdTarget = browserslistToEsbuild(packageJson.browserslist);
2018-05-29 14:59:57 -03:00
function getBabelConfig(bundle) {
const config = {
babelrc: false,
2021-05-12 08:11:43 +08:00
assumptions: {
setSpreadProperties: true,
},
sourceType: "unambiguous",
2018-06-21 10:29:42 +08:00
plugins: bundle.babelPlugins || [],
compact: false,
exclude: [/\/core-js\//],
2018-05-29 14:59:57 -03:00
};
const targets = { node: "10" };
2018-05-29 14:59:57 -03:00
if (bundle.target === "universal") {
targets.browsers = packageJson.browserslist;
2018-05-29 14:59:57 -03:00
}
config.presets = [
2020-01-10 09:21:44 +08:00
[
2021-05-12 08:11:43 +08:00
"@babel/preset-env",
2020-01-10 09:21:44 +08:00
{
targets,
exclude: [
"es.array.unscopables.flat",
"es.array.unscopables.flat-map",
"es.array.sort",
"es.promise",
"es.promise.finally",
"es.string.replace",
"es.symbol.description",
"es.typed-array.*",
"web.*",
],
2020-01-10 09:21:44 +08:00
modules: false,
useBuiltIns: "usage",
corejs: {
version: 3,
},
debug: false,
2020-01-10 09:21:44 +08:00
},
],
2018-05-29 14:59:57 -03:00
];
2021-05-12 08:11:43 +08:00
config.plugins.push([
"@babel/plugin-proposal-object-rest-spread",
{ useBuiltIns: true },
]);
2018-05-29 14:59:57 -03:00
return config;
}
2022-01-26 20:33:15 +08:00
async function* getEsbuildOptions(bundle, buildOptions) {
2018-05-29 14:59:57 -03:00
const replaceStrings = {
// `tslib` exports global variables
"createExporter(root": "createExporter({}",
2018-05-29 14:59:57 -03:00
};
2022-01-26 19:08:55 +08:00
const define = {
"process.env.PRETTIER_TARGET": JSON.stringify(bundle.target),
"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"
2022-01-26 19:08:55 +08:00
replaceStrings["process.env.PRETTIER_DEBUG"] = "globalThis.PRETTIER_DEBUG";
define.process = JSON.stringify({ env: {}, argv: [] });
// Replace `__dirname` and `__filename` with a fake value
// So `parser-typescript.js` won't contain a path of working directory
// See #8268
2022-01-26 19:08:55 +08:00
define.__filename = JSON.stringify(
"/prettier-security-filename-placeholder.js"
);
2022-01-26 19:08:55 +08:00
define.__dirname = JSON.stringify("/prettier-security-dirname-placeholder");
2018-05-29 14:59:57 -03:00
}
2020-05-11 09:05:06 +08:00
2021-05-27 08:54:42 +08:00
const replaceModule = {};
// Replace other bundled files
if (bundle.target === "node") {
// Replace package.json with dynamic `require("./package.json")`
replaceModule[path.join(PROJECT_ROOT, "package.json")] = {
path: "./package.json",
external: true,
};
// Dynamic require bundled files
2021-05-27 08:54:42 +08:00
for (const item of bundles) {
if (item.input !== bundle.input) {
replaceModule[path.join(PROJECT_ROOT, item.input)] = `./${item.output}`;
}
}
} else {
// Universal bundle only use version info from package.json
// Replace package.json with `{version: "{VERSION}"}`
replaceModule[path.join(PROJECT_ROOT, "package.json")] = {
contents: `module.exports = ${JSON.stringify({
version: packageJson.version,
})};`,
};
// Replace parser getters with `undefined`
for (const file of [
"src/language-css/parsers.js",
"src/language-graphql/parsers.js",
"src/language-handlebars/parsers.js",
"src/language-html/parsers.js",
"src/language-js/parse/parsers.js",
"src/language-markdown/parsers.js",
"src/language-yaml/parsers.js",
]) {
replaceModule[path.join(PROJECT_ROOT, file)] = { contents: "" };
}
2022-01-26 08:56:44 +08:00
// Prevent `esbuildPluginNodeModulePolyfills` include shim for this module
replaceModule.assert = {
contents: await fs.readFile(path.join(__dirname, "./shims/assert.cjs")),
};
}
2022-01-26 20:33:15 +08:00
let shouldMinify = buildOptions.minify;
2021-11-27 18:47:02 +08:00
if (typeof shouldMinify !== "boolean") {
shouldMinify = bundle.minify !== false && bundle.target === "universal";
}
const esbuildOptions = {
entryPoints: [path.join(PROJECT_ROOT, bundle.input)],
2022-01-26 19:08:55 +08:00
define,
bundle: true,
metafile: true,
plugins: [
esbuildPluginEvaluate(),
esbuildPluginReplaceModule({ ...replaceModule, ...bundle.replaceModule }),
2022-01-26 08:56:44 +08:00
bundle.target === "universal" && esbuildPluginNodeModulePolyfills(),
esbuildPluginTextReplace({
include: /\.[cm]?js$/,
// TODO[@fisker]: Use RegExp when possible
pattern: Object.entries({ ...replaceStrings, ...bundle.replace }),
}),
2022-01-26 20:33:15 +08:00
buildOptions.onLicenseFound &&
esbuildPluginLicense({
cwd: PROJECT_ROOT,
thirdParty: {
includePrivate: true,
2022-01-26 20:33:15 +08:00
output: buildOptions.onLicenseFound,
2018-06-13 17:23:33 -03:00
},
}),
].filter(Boolean),
minify: shouldMinify,
legalComments: "none",
external: [...(bundle.external || [])],
// Disable esbuild auto discover `tsconfig.json` file
tsconfig: path.join(__dirname, "empty-tsconfig.json"),
mainFields: ["main"],
target: ["node10"],
logLevel: "error",
2018-05-29 14:59:57 -03:00
};
2019-09-16 17:13:32 +08:00
if (bundle.target === "universal") {
esbuildOptions.target.push(...umdTarget);
2021-11-27 18:47:02 +08:00
yield {
...esbuildOptions,
2022-01-27 09:37:55 +08:00
outfile: bundle.output,
plugins: [
esbuildPluginUmd({ name: bundle.name }),
...esbuildOptions.plugins,
],
format: "umd",
};
2022-01-27 09:37:55 +08:00
yield {
...esbuildOptions,
outfile: `esm/${bundle.output.replace(".js", ".mjs")}`,
format: "esm",
};
2021-11-27 18:47:02 +08:00
} else {
esbuildOptions.external.push(
...builtinModules,
...bundles
.filter((item) => item.input !== bundle.input)
.map((item) => `./${item.output}`)
);
2019-09-16 17:13:32 +08:00
yield {
...esbuildOptions,
2022-01-27 09:37:55 +08:00
outfile: bundle.output,
format: "cjs",
};
}
2018-05-29 14:59:57 -03:00
}
2022-01-26 20:33:15 +08:00
async function runBuild(bundle, esbuildOptions, buildOptions) {
if (!buildOptions.babel) {
await esbuild.build(esbuildOptions);
return;
}
const { format, plugins, outfile } = esbuildOptions;
await esbuild.build({
...esbuildOptions,
plugins: plugins.filter(({ name }) => name !== "umd"),
format: format === "umd" ? "cjs" : format,
minify: false,
});
const text = await fs.readFile(outfile);
const { code } = await babel.transformAsync(text, {
filename: outfile,
...getBabelConfig(bundle),
});
await fs.writeFile(outfile, code);
await esbuild.build({
...esbuildOptions,
plugins: plugins.filter(({ name }) => name === "umd"),
entryPoints: [outfile],
allowOverwrite: true,
2018-05-29 14:59:57 -03:00
});
}
2022-01-27 09:37:55 +08:00
async function* createBundle(bundle, buildOptions) {
2022-01-26 20:33:15 +08:00
for await (const esbuildOptions of getEsbuildOptions(bundle, buildOptions)) {
2022-01-27 09:37:55 +08:00
const { outfile: file } = esbuildOptions;
2018-06-18 15:16:40 -03:00
2022-01-27 09:37:55 +08:00
if (
(buildOptions.files && !buildOptions.files.has(file)) ||
(buildOptions.playground && esbuildOptions.format !== "umd")
) {
yield { name: file, skipped: true };
continue;
}
esbuildOptions.outfile = path.join(DIST_DIR, buildOptions.saveAs || file);
yield { name: file, started: true };
await runBuild(bundle, esbuildOptions, buildOptions);
yield { name: file, file: esbuildOptions.outfile };
}
2021-05-15 20:09:52 +08:00
}
export default createBundle;