2021-05-15 20:09:52 +08:00
|
|
|
import path from "node:path";
|
2022-10-16 11:53:24 +08:00
|
|
|
import { createRequire } from "node:module";
|
2021-05-15 20:09:52 +08:00
|
|
|
import createEsmUtils from "esm-utils";
|
2022-01-09 01:15:04 +08:00
|
|
|
import esbuild from "esbuild";
|
|
|
|
|
import { NodeModulesPolyfillPlugin as esbuildPluginNodeModulePolyfills } from "@esbuild-plugins/node-modules-polyfill";
|
2022-01-26 17:55:45 +08:00
|
|
|
import browserslistToEsbuild from "browserslist-to-esbuild";
|
2021-09-11 09:55:46 +08:00
|
|
|
import { PROJECT_ROOT, DIST_DIR } from "../utils/index.mjs";
|
2022-01-09 01:15:04 +08:00
|
|
|
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";
|
2022-02-17 09:47:35 +08:00
|
|
|
import esbuildPluginInteropDefault from "./esbuild-plugins/interop-default.mjs";
|
2022-01-27 11:16:50 +08:00
|
|
|
import esbuildPluginVisualizer from "./esbuild-plugins/visualizer.mjs";
|
2022-03-02 13:44:00 +08:00
|
|
|
import esbuildPluginStripNodeProtocol from "./esbuild-plugins/strip-node-protocol.mjs";
|
2022-03-15 22:07:36 +08:00
|
|
|
import esbuildPluginThrowWarnings from "./esbuild-plugins/throw-warnings.mjs";
|
2022-08-11 14:08:08 +08:00
|
|
|
import esbuildPluginShimCommonjsObjects from "./esbuild-plugins/shim-commonjs-objects.mjs";
|
2022-10-27 09:29:22 +08:00
|
|
|
import esbuildPluginPrimitiveDefine from "./esbuild-plugins/primitive-define.mjs";
|
2022-09-18 17:50:14 +08:00
|
|
|
import transform from "./transform/index.js";
|
2018-05-29 14:59:57 -03:00
|
|
|
|
2022-03-25 16:37:34 +08:00
|
|
|
const { dirname, readJsonSync, require } = createEsmUtils(import.meta);
|
2022-02-11 18:02:04 +08:00
|
|
|
const packageJson = readJsonSync("../../package.json");
|
2020-05-11 09:05:06 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
const universalTarget = browserslistToEsbuild(packageJson.browserslist);
|
2022-10-06 15:20:19 +08:00
|
|
|
const getRelativePath = (from, to) => {
|
|
|
|
|
const relativePath = path.posix.relative(path.dirname(`/${from}`), `/${to}`);
|
|
|
|
|
if (!relativePath.startsWith(".")) {
|
|
|
|
|
return `./${relativePath}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return relativePath;
|
|
|
|
|
};
|
2020-10-22 13:32:50 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
|
|
|
|
|
// Save dependencies to file
|
|
|
|
|
file.dependencies = [];
|
2022-03-13 17:04:22 +08:00
|
|
|
|
2022-03-20 14:04:30 +08:00
|
|
|
const replaceModule = [
|
2022-02-17 09:47:35 +08:00
|
|
|
// Use `require` directly
|
2022-03-17 09:44:08 +08:00
|
|
|
{
|
2022-03-24 15:17:26 +08:00
|
|
|
module: "*",
|
2022-03-17 09:44:08 +08:00
|
|
|
find: "const require = createRequire(import.meta.url);",
|
|
|
|
|
replacement: "",
|
|
|
|
|
},
|
2022-03-17 09:54:48 +08:00
|
|
|
// Use `__dirname` directly
|
|
|
|
|
{
|
2022-03-24 15:17:26 +08:00
|
|
|
module: "*",
|
2022-03-17 09:54:48 +08:00
|
|
|
find: "const __dirname = path.dirname(fileURLToPath(import.meta.url));",
|
|
|
|
|
replacement: "",
|
|
|
|
|
},
|
2022-09-18 17:50:14 +08:00
|
|
|
// Transform `.at` and `Object.hasOwn`
|
|
|
|
|
{
|
|
|
|
|
module: "*",
|
|
|
|
|
process: transform,
|
|
|
|
|
},
|
2022-03-24 11:20:55 +08:00
|
|
|
// #12493, not sure what the problem is, but replace the cjs version with esm version seems fix it
|
2022-10-16 11:53:24 +08:00
|
|
|
...[
|
|
|
|
|
require.resolve("tslib"),
|
|
|
|
|
createRequire(require.resolve("vnopts")).resolve("tslib"),
|
|
|
|
|
createRequire(require.resolve("tsutils")).resolve("tslib"),
|
|
|
|
|
].map((file) => ({
|
|
|
|
|
module: file,
|
|
|
|
|
path: file.replace(/tslib\.js$/, "tslib.es6.js"),
|
|
|
|
|
})),
|
2022-05-06 18:52:40 +08:00
|
|
|
// https://github.com/evanw/esbuild/issues/2103
|
|
|
|
|
{
|
|
|
|
|
module: path.join(
|
|
|
|
|
path.dirname(require.resolve("outdent/package.json")),
|
|
|
|
|
"lib-module/index.js"
|
|
|
|
|
),
|
|
|
|
|
process(text) {
|
|
|
|
|
const index = text.indexOf('if (typeof module !== "undefined") {');
|
|
|
|
|
if (index === -1) {
|
|
|
|
|
throw new Error("Unexpected code");
|
|
|
|
|
}
|
|
|
|
|
return text.slice(0, index);
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-10-04 18:09:24 +08:00
|
|
|
/*
|
|
|
|
|
`jest-docblock` try to detect new line in code, and it will fallback to `os.EOL`,
|
|
|
|
|
We already replaced line end to `\n` before calling it
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
module: require.resolve("jest-docblock"),
|
|
|
|
|
process: (text) =>
|
|
|
|
|
text
|
|
|
|
|
.replace(
|
2022-10-16 15:31:36 +08:00
|
|
|
"const line = (0, _detectNewline().default)(docblock) ?? _os().EOL;",
|
2022-10-04 18:09:24 +08:00
|
|
|
'const line = "\\n"'
|
|
|
|
|
)
|
|
|
|
|
.replace(
|
2022-10-16 15:31:36 +08:00
|
|
|
"const line = (0, _detectNewline().default)(comments) ?? _os().EOL;",
|
2022-10-04 18:09:24 +08:00
|
|
|
'const line = "\\n"'
|
|
|
|
|
)
|
|
|
|
|
.replace(/\nfunction _os().*?\n}/s, "")
|
|
|
|
|
.replace(/\nfunction _detectNewline().*?\n}/s, ""),
|
|
|
|
|
},
|
2022-03-14 20:02:02 +08:00
|
|
|
];
|
2022-01-26 19:08:55 +08:00
|
|
|
|
|
|
|
|
const define = {
|
2022-10-27 09:29:22 +08:00
|
|
|
"process.env.PRETTIER_TARGET": file.platform,
|
|
|
|
|
"process.env.NODE_ENV": "production",
|
2022-01-26 19:08:55 +08:00
|
|
|
};
|
|
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
if (file.platform === "universal") {
|
2018-05-29 14:59:57 -03:00
|
|
|
// We can't reference `process` in UMD bundles and this is
|
|
|
|
|
// an undocumented "feature"
|
2022-03-20 14:04:30 +08:00
|
|
|
replaceModule.push({
|
|
|
|
|
module: "*",
|
2022-03-14 20:02:02 +08:00
|
|
|
find: "process.env.PRETTIER_DEBUG",
|
|
|
|
|
replacement: "globalThis.PRETTIER_DEBUG",
|
|
|
|
|
});
|
2022-01-26 19:08:55 +08:00
|
|
|
|
2022-10-27 09:29:22 +08:00
|
|
|
define.process = undefined;
|
|
|
|
|
// @babel/code-frame/lib/index.js
|
|
|
|
|
define["process.emitWarning"] = undefined;
|
|
|
|
|
// postcss/lib/postcss.js
|
|
|
|
|
define["process.env.LANG"] = "";
|
2022-01-26 19:08:55 +08:00
|
|
|
|
|
|
|
|
// Replace `__dirname` and `__filename` with a fake value
|
|
|
|
|
// So `parser-typescript.js` won't contain a path of working directory
|
2020-05-11 09:23:13 +08:00
|
|
|
// See #8268
|
2022-10-27 09:29:22 +08:00
|
|
|
define.__filename = "/prettier-security-filename-placeholder.js";
|
|
|
|
|
define.__dirname = "/prettier-security-dirname-placeholder";
|
2018-05-29 14:59:57 -03:00
|
|
|
}
|
2020-05-11 09:05:06 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
if (file.platform === "node") {
|
|
|
|
|
// External other bundled files
|
|
|
|
|
replaceModule.push(
|
|
|
|
|
...files
|
|
|
|
|
.filter(
|
|
|
|
|
(bundle) =>
|
|
|
|
|
bundle.input === "package.json" ||
|
|
|
|
|
(file.input !== bundle.input && bundle.output.format === "esm")
|
|
|
|
|
)
|
|
|
|
|
.map((bundle) => {
|
|
|
|
|
let output = bundle.output.file;
|
|
|
|
|
if (
|
|
|
|
|
file.output.file === "index.cjs" &&
|
2022-10-13 17:05:20 +08:00
|
|
|
bundle.output.file === "doc.mjs"
|
2022-09-25 11:50:59 +08:00
|
|
|
) {
|
|
|
|
|
output = "doc.js";
|
|
|
|
|
}
|
2022-08-11 14:08:08 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
return {
|
|
|
|
|
module: path.join(PROJECT_ROOT, bundle.input),
|
2022-10-06 15:20:19 +08:00
|
|
|
external: getRelativePath(file.output.file, output),
|
2022-09-25 11:50:59 +08:00
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-07-13 15:16:31 +08:00
|
|
|
} else {
|
2022-03-24 21:22:41 +08:00
|
|
|
replaceModule.push(
|
|
|
|
|
// When running build script with `--no-minify`, `esbuildPluginNodeModulePolyfills` shim `module` module incorrectly
|
|
|
|
|
{
|
|
|
|
|
module: "*",
|
|
|
|
|
find: 'import { createRequire } from "node:module";',
|
|
|
|
|
replacement: "",
|
|
|
|
|
},
|
2022-10-04 18:09:24 +08:00
|
|
|
// Prevent `esbuildPluginNodeModulePolyfills` shim `assert`, which will include a big `buffer` shim
|
|
|
|
|
// TODO[@fisker]: Find a better way
|
|
|
|
|
{
|
|
|
|
|
module: "*",
|
|
|
|
|
find: ' from "node:assert";',
|
|
|
|
|
replacement: ` from ${JSON.stringify(
|
|
|
|
|
path.join(dirname, "./shims/assert.js")
|
|
|
|
|
)};`,
|
|
|
|
|
},
|
2022-03-24 21:22:41 +08:00
|
|
|
// Prevent `esbuildPluginNodeModulePolyfills` include shim for this module
|
|
|
|
|
{
|
|
|
|
|
module: "assert",
|
2022-03-25 16:37:34 +08:00
|
|
|
path: path.join(dirname, "./shims/assert.js"),
|
2022-03-24 21:22:41 +08:00
|
|
|
},
|
|
|
|
|
// `esbuildPluginNodeModulePolyfills` didn't shim this module
|
|
|
|
|
{
|
|
|
|
|
module: "module",
|
|
|
|
|
text: "export const createRequire = () => {};",
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-07-12 14:33:52 +08:00
|
|
|
|
2021-07-15 09:53:34 +08:00
|
|
|
// Replace parser getters with `undefined`
|
2022-02-21 10:25:39 +08:00
|
|
|
for (const file of [
|
2022-02-21 14:22:51 +08:00
|
|
|
"src/language-css/parsers.js",
|
2022-02-21 10:25:39 +08:00
|
|
|
"src/language-graphql/parsers.js",
|
2022-02-21 16:44:56 +08:00
|
|
|
"src/language-html/parsers.js",
|
2022-02-21 14:46:56 +08:00
|
|
|
"src/language-handlebars/parsers.js",
|
2022-02-21 15:53:02 +08:00
|
|
|
"src/language-js/parse/parsers.js",
|
2022-02-21 10:25:39 +08:00
|
|
|
"src/language-markdown/parsers.js",
|
2022-02-21 13:49:08 +08:00
|
|
|
"src/language-yaml/parsers.js",
|
2022-02-28 11:12:37 +08:00
|
|
|
// This module requires file access, should not include in universal bundle
|
|
|
|
|
"src/utils/get-interpreter.js",
|
2022-02-21 10:25:39 +08:00
|
|
|
]) {
|
2022-03-24 15:02:29 +08:00
|
|
|
replaceModule.push({
|
|
|
|
|
module: path.join(PROJECT_ROOT, file),
|
|
|
|
|
text: "export default undefined;",
|
|
|
|
|
});
|
2022-02-20 23:57:53 +08:00
|
|
|
}
|
2021-07-13 15:16:31 +08:00
|
|
|
}
|
2021-07-12 14:33:52 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
const { buildOptions } = file;
|
|
|
|
|
const shouldMinify =
|
|
|
|
|
cliOptions.minify ?? buildOptions.minify ?? file.platform === "universal";
|
2022-03-14 16:30:12 +08:00
|
|
|
|
2022-01-09 01:15:04 +08:00
|
|
|
const esbuildOptions = {
|
2022-09-25 11:50:59 +08:00
|
|
|
entryPoints: [path.join(PROJECT_ROOT, file.input)],
|
2022-01-09 01:15:04 +08:00
|
|
|
bundle: true,
|
|
|
|
|
metafile: true,
|
|
|
|
|
plugins: [
|
2022-10-27 09:29:22 +08:00
|
|
|
esbuildPluginPrimitiveDefine(define),
|
2022-01-09 01:15:04 +08:00
|
|
|
esbuildPluginEvaluate(),
|
2022-03-02 13:44:00 +08:00
|
|
|
esbuildPluginStripNodeProtocol(),
|
2022-03-20 14:04:30 +08:00
|
|
|
esbuildPluginReplaceModule({
|
2022-09-25 11:50:59 +08:00
|
|
|
replacements: [...replaceModule, ...(buildOptions.replaceModule ?? [])],
|
2022-01-09 01:15:04 +08:00
|
|
|
}),
|
2022-09-25 11:50:59 +08:00
|
|
|
file.platform === "universal" && esbuildPluginNodeModulePolyfills(),
|
|
|
|
|
shouldCollectLicenses &&
|
2022-01-09 01:15:04 +08:00
|
|
|
esbuildPluginLicense({
|
|
|
|
|
cwd: PROJECT_ROOT,
|
|
|
|
|
thirdParty: {
|
|
|
|
|
includePrivate: true,
|
2022-09-25 11:50:59 +08:00
|
|
|
output: (dependencies) => file.dependencies.push(...dependencies),
|
2022-01-09 01:15:04 +08:00
|
|
|
},
|
|
|
|
|
}),
|
2022-09-25 11:50:59 +08:00
|
|
|
cliOptions.reports &&
|
|
|
|
|
esbuildPluginVisualizer({ formats: cliOptions.reports }),
|
2022-06-01 19:23:13 +08:00
|
|
|
esbuildPluginThrowWarnings({
|
2022-09-25 11:50:59 +08:00
|
|
|
allowDynamicRequire: file.platform === "node",
|
|
|
|
|
allowDynamicImport: file.platform === "node",
|
2022-06-01 19:23:13 +08:00
|
|
|
}),
|
2022-01-09 01:15:04 +08:00
|
|
|
].filter(Boolean),
|
|
|
|
|
minify: shouldMinify,
|
|
|
|
|
legalComments: "none",
|
2022-09-25 11:50:59 +08:00
|
|
|
external: ["pnpapi", ...(buildOptions.external ?? [])],
|
2022-01-09 01:15:04 +08:00
|
|
|
// Disable esbuild auto discover `tsconfig.json` file
|
2022-03-25 16:37:34 +08:00
|
|
|
tsconfig: path.join(dirname, "empty-tsconfig.json"),
|
2022-09-25 11:50:59 +08:00
|
|
|
target: [...(buildOptions.target ?? ["node14"])],
|
2022-01-14 15:58:13 +08:00
|
|
|
logLevel: "error",
|
2022-09-25 11:50:59 +08:00
|
|
|
format: file.output.format,
|
|
|
|
|
outfile: path.join(DIST_DIR, cliOptions.saveAs ?? file.output.file),
|
2022-10-20 08:26:35 +08:00
|
|
|
// https://esbuild.github.io/api/#main-fields
|
|
|
|
|
mainFields: file.platform === "node" ? ["module", "main"] : undefined,
|
2022-01-09 01:15:04 +08:00
|
|
|
};
|
2021-09-12 23:26:29 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
if (file.platform === "universal") {
|
|
|
|
|
if (!buildOptions.target) {
|
|
|
|
|
esbuildOptions.target.push(...universalTarget);
|
2022-03-13 22:47:02 +08:00
|
|
|
}
|
2022-01-09 01:15:04 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
if (file.output.format === "umd") {
|
|
|
|
|
esbuildOptions.plugins.push(
|
2022-02-17 09:47:35 +08:00
|
|
|
esbuildPluginUmd({
|
2022-09-25 11:50:59 +08:00
|
|
|
name: file.output.umdVariableName,
|
|
|
|
|
interopDefault: buildOptions.interopDefault ?? true,
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-01-09 01:15:04 +08:00
|
|
|
} else {
|
2022-02-28 18:38:33 +08:00
|
|
|
esbuildOptions.platform = "node";
|
2022-09-25 11:50:59 +08:00
|
|
|
esbuildOptions.external.push(...files.map((file) => file.output.file));
|
2020-10-26 09:44:09 +08:00
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
if (file.output.format !== "esm" && buildOptions.interopDefault) {
|
2022-02-17 09:47:35 +08:00
|
|
|
esbuildOptions.plugins.push(esbuildPluginInteropDefault());
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 14:08:08 +08:00
|
|
|
// https://github.com/evanw/esbuild/issues/1921
|
2022-09-25 11:50:59 +08:00
|
|
|
if (file.output.format === "esm") {
|
2022-08-11 14:08:08 +08:00
|
|
|
esbuildOptions.plugins.push(esbuildPluginShimCommonjsObjects());
|
|
|
|
|
}
|
2020-10-26 09:44:09 +08:00
|
|
|
}
|
2022-09-25 11:50:59 +08:00
|
|
|
|
|
|
|
|
return esbuildOptions;
|
2018-05-29 14:59:57 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
async function runEsbuild(options) {
|
|
|
|
|
const esbuildOptions = getEsbuildOptions(options);
|
2022-01-14 16:07:45 +08:00
|
|
|
await esbuild.build(esbuildOptions);
|
2018-05-29 14:59:57 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-25 11:50:59 +08:00
|
|
|
export default runEsbuild;
|