2023-12-17 19:34:12 +00:00
|
|
|
import path from "node:path";
|
|
|
|
|
|
|
|
|
|
import browserslistToEsbuild from "browserslist-to-esbuild";
|
2022-01-09 01:15:04 +08:00
|
|
|
import esbuild from "esbuild";
|
2023-08-28 04:05:28 +02:00
|
|
|
import { nodeModulesPolyfillPlugin as esbuildPluginNodeModulePolyfills } from "esbuild-plugins-node-modules-polyfill";
|
2023-12-17 19:34:12 +00:00
|
|
|
import createEsmUtils from "esm-utils";
|
|
|
|
|
|
|
|
|
|
import { DIST_DIR, PROJECT_ROOT } from "../utils/index.js";
|
|
|
|
|
import esbuildPluginAddDefaultExport from "./esbuild-plugins/add-default-export.js";
|
2023-03-28 16:18:53 +08:00
|
|
|
import esbuildPluginEvaluate from "./esbuild-plugins/evaluate.js";
|
|
|
|
|
import esbuildPluginLicense from "./esbuild-plugins/license.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import esbuildPluginPrimitiveDefine from "./esbuild-plugins/primitive-define.js";
|
|
|
|
|
import esbuildPluginReplaceModule from "./esbuild-plugins/replace-module.js";
|
|
|
|
|
import esbuildPluginShimCommonjsObjects from "./esbuild-plugins/shim-commonjs-objects.js";
|
2023-03-28 16:18:53 +08:00
|
|
|
import esbuildPluginStripNodeProtocol from "./esbuild-plugins/strip-node-protocol.js";
|
|
|
|
|
import esbuildPluginThrowWarnings from "./esbuild-plugins/throw-warnings.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import esbuildPluginUmd from "./esbuild-plugins/umd.js";
|
|
|
|
|
import esbuildPluginVisualizer from "./esbuild-plugins/visualizer.js";
|
2022-09-18 17:50:14 +08:00
|
|
|
import transform from "./transform/index.js";
|
2023-03-01 15:34:30 +08:00
|
|
|
import { getPackageFile } from "./utils.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: "",
|
|
|
|
|
},
|
2023-08-15 09:20:40 +08:00
|
|
|
// Transform `.at`, `Object.hasOwn`, and `String#replaceAll`
|
2022-09-18 17:50:14 +08:00
|
|
|
{
|
|
|
|
|
module: "*",
|
|
|
|
|
process: transform,
|
|
|
|
|
},
|
2024-06-13 09:13:41 +08:00
|
|
|
// Transform `require("typescript")` to `import`
|
|
|
|
|
{
|
|
|
|
|
module: "*",
|
|
|
|
|
process(text, file) {
|
|
|
|
|
text = text.replaceAll(
|
|
|
|
|
/(?:const|let|var) (\w+) = (?:__importStar\(require\("typescript"\)\)|require\("typescript"\));/g,
|
|
|
|
|
'import * as $1 from "typescript";',
|
|
|
|
|
);
|
|
|
|
|
if (/require\(["']typescript["']\)/.test(text)) {
|
|
|
|
|
throw new Error(`Unexpected \`require("typescript")\` in ${file}.`);
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
},
|
|
|
|
|
},
|
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
|
2024-06-05 11:32:07 +08:00
|
|
|
{
|
|
|
|
|
module: require.resolve("tslib"),
|
|
|
|
|
path: require.resolve("tslib").replace(/tslib\.js$/, "tslib.es6.js"),
|
|
|
|
|
},
|
2022-05-06 18:52:40 +08:00
|
|
|
// https://github.com/evanw/esbuild/issues/2103
|
|
|
|
|
{
|
2023-03-01 15:34:30 +08:00
|
|
|
module: getPackageFile("outdent/lib-module/index.js"),
|
2022-05-06 18:52:40 +08:00
|
|
|
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;",
|
2024-06-04 09:39:19 +08:00
|
|
|
String.raw`const line = "\n"`,
|
2022-10-04 18:09:24 +08:00
|
|
|
)
|
|
|
|
|
.replace(
|
2022-10-16 15:31:36 +08:00
|
|
|
"const line = (0, _detectNewline().default)(comments) ?? _os().EOL;",
|
2024-06-04 09:39:19 +08:00
|
|
|
String.raw`const line = "\n"`,
|
2022-10-04 18:09:24 +08:00
|
|
|
)
|
2023-04-23 09:41:05 +08:00
|
|
|
.replace(/\nfunction _os\(\).*?\n}/s, "")
|
|
|
|
|
.replace(/\nfunction _detectNewline\(\).*?\n}/s, ""),
|
2022-10-04 18:09:24 +08:00
|
|
|
},
|
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"] = "";
|
2023-07-29 15:32:17 +09:00
|
|
|
// @typescript-eslint/typescript-estree
|
|
|
|
|
define["process.env.TYPESCRIPT_ESLINT_EXPERIMENTAL_TSSERVER"] = "";
|
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 = () => {};",
|
2023-03-24 08:44:25 +08:00
|
|
|
},
|
|
|
|
|
// This module requires file access, should not include in universal bundle
|
|
|
|
|
{
|
|
|
|
|
module: path.join(PROJECT_ROOT, "src/utils/get-interpreter.js"),
|
|
|
|
|
text: "export default undefined;",
|
2022-03-24 21:22:41 +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
|
|
|
}),
|
2023-04-25 15:37:56 +08:00
|
|
|
buildOptions.addDefaultExport && esbuildPluginAddDefaultExport(),
|
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
|
2023-06-18 11:05:12 +08:00
|
|
|
tsconfigRaw: JSON.stringify({}),
|
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,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
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-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;
|