Files
test-repo/scripts/build/packages/prettier.js
T

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

904 lines
28 KiB
JavaScript
Raw Normal View History

2026-05-28 01:45:35 +08:00
import { createRequire } from "node:module";
2021-05-15 20:09:52 +08:00
import path from "node:path";
2023-02-03 11:10:00 +08:00
import url from "node:url";
import { outdent } from "outdent";
2026-07-24 08:51:20 +08:00
import {
DIST_DIR,
PRODUCTION_MINIMAL_NODE_JS_VERSION,
PROJECT_ROOT,
} from "../../utilities/index.js";
2025-12-05 04:53:15 +08:00
import { createJavascriptModuleBuilder } from "../builders/javascript-module.js";
import esmifyTypescriptEslint from "../hacks/esmify-typescript-eslint.js";
import modifyTypescriptModule from "../hacks/modify-typescript-module.js";
import { getPackageFile } from "../utilities.js";
2025-05-27 09:35:31 +08:00
import {
2025-12-05 04:53:15 +08:00
createNodejsFileConfig,
createPackageMetaFilesConfig,
createTypesConfig,
createUniversalFileConfig,
} from "./config-helpers.js";
import { generatePackageJson } from "./prettier-package-json.js";
2022-01-09 01:15:04 +08:00
2026-05-28 01:45:35 +08:00
const require = createRequire(import.meta.url);
const resolveEsmModulePath = (specifier) =>
2026-05-28 01:45:35 +08:00
url.fileURLToPath(import.meta.resolve(specifier));
2018-05-24 15:30:45 -03:00
2022-09-25 11:50:59 +08:00
const extensions = {
esm: ".mjs",
umd: ".js",
cjs: ".cjs",
};
2025-12-05 04:53:15 +08:00
const packageConfig = {
packageName: "prettier",
sourceDirectory: PROJECT_ROOT,
distDirectory: path.join(DIST_DIR, "prettier"),
modules: [],
};
const mainModule = {
name: "Main",
files: [
createNodejsFileConfig({
input: "src/index.js",
replaceModule: [
// We don't need semver since we don't use `options.version`
2025-12-05 04:53:15 +08:00
{
module: require.resolve("editorconfig-without-wasm"),
process(text) {
text = text.replace("import * as semver from 'semver';", "");
text = text.replace("semver.gte(version, '0.10.0')", "true");
return text;
},
2025-12-05 04:53:15 +08:00
},
{
module: require.resolve("n-readlines"),
process(text) {
text = text.replace(
"const fs = require('fs')",
'import fs from "node:fs"',
);
text = text.replace("module.exports = ", "export default ");
return text;
},
2025-12-05 04:53:15 +08:00
},
// `parse-json` use another copy of `@babel/code-frame`
{
module: require.resolve("@babel/code-frame", {
paths: [require.resolve("parse-json")],
}),
path: require.resolve("@babel/code-frame"),
},
2026-05-19 04:14:49 +08:00
// `parse-json` uses old version of `@babel/code-frame`
// Remove this when `parse-json` supports @babel/code-frame v8
{
2026-05-28 01:45:35 +08:00
module: resolveEsmModulePath("parse-json"),
2026-05-19 04:14:49 +08:00
process(text) {
text = text.replace(
"return {line: Number(line), column: Number(column)}",
"return {line: Number(line), column: Number(column) - 1}",
);
text = text.replace(
"return indexToPosition(string, Number(index), {oneBased: true});",
"return indexToPosition(string, Number(index), {oneBasedLine: true});",
);
return text;
},
},
2026-02-01 08:57:18 +08:00
{
2026-05-28 01:45:35 +08:00
module: resolveEsmModulePath("@babel/code-frame"),
2026-02-01 08:57:18 +08:00
process(text) {
text = text.replace(
"from 'node:util'",
2026-05-28 01:45:35 +08:00
`from ${JSON.stringify(path.join(import.meta.dirname, "../shims/node-util.js"))}`,
2026-02-01 08:57:18 +08:00
);
return text;
},
},
2025-12-05 04:53:15 +08:00
],
addDefaultExport: true,
reuseDocModule: true,
format: "esm",
}),
createNodejsFileConfig("src/index.cjs"),
createUniversalFileConfig({
input: "src/standalone.js",
umdVariableName: "prettier",
replaceModule: [
// Smaller size
{
module: getPackageFile("picocolors/picocolors.browser.js"),
2026-05-28 01:45:35 +08:00
path: path.join(import.meta.dirname, "../shims/colors.js"),
2025-12-05 04:53:15 +08:00
},
],
}),
].flat(),
};
for (const file of mainModule.files) {
if (file.output === "standalone.mjs") {
file.playground = true;
}
}
const cliModule = {
name: "CLI",
files: [
{
input: "bin/prettier.cjs",
outputBaseName: "bin/prettier",
target: ["node0.10"],
replaceModule: [
{
module: path.join(PROJECT_ROOT, "bin/prettier.cjs"),
process(text) {
2026-07-24 08:51:20 +08:00
text = text.replace(
'require("../package.json").engines.node.replace(">=", "")',
JSON.stringify(PRODUCTION_MINIMAL_NODE_JS_VERSION),
);
2025-12-05 04:53:15 +08:00
text = text.replace(
"../src/cli/index.js",
"../internal/legacy-cli.mjs",
);
text = text.replace(
"../src/experimental-cli/index.js",
"../internal/experimental-cli.mjs",
);
return text;
},
},
],
},
{
input: "src/cli/index.js",
outputBaseName: "internal/legacy-cli",
external: ["tinybench"],
// TODO: Remove this when we drop support for Node.js v16
replaceModule: [
{
module: resolveEsmModulePath("@cacheable/memory"),
process: (text) =>
outdent`
const structuredClone =
globalThis.structuredClone ??
((value) => JSON.parse(JSON.stringify(value)));
${text}
`,
},
],
},
...[
{
input: "src/experimental-cli/index.js",
outputBaseName: "internal/experimental-cli",
replaceModule: [
{
module: getPackageFile("@prettier/cli/dist/prettier_serial.js"),
external: "./experimental-cli-worker.mjs",
},
{
module: getPackageFile("@prettier/cli/dist/prettier_parallel.js"),
find: 'new URL("./prettier_serial.js", import.meta.url)',
replacement:
'new URL("./experimental-cli-worker.mjs", import.meta.url)',
},
{
module: getPackageFile("json5/dist/index.mjs"),
find: "export default lib;",
replacement: "export default { parse };",
},
{
module: getPackageFile("js-yaml/dist/js-yaml.mjs"),
process(text) {
text = text.replaceAll(
/export \{ .* \};/g,
"export { JSON_SCHEMA, load };",
);
return text;
},
2025-12-05 04:53:15 +08:00
},
{
module: getPackageFile("smol-toml"),
path: getPackageFile("smol-toml/dist/parse.js"),
},
2025-12-05 04:53:15 +08:00
],
},
{
input: "src/experimental-cli/worker.js",
outputBaseName: "internal/experimental-cli-worker",
},
].map(({ input, outputBaseName, replaceModule = [] }) => ({
input,
outputBaseName,
replaceModule: [
...replaceModule,
{
module: getPackageFile("@prettier/cli/dist/constants.js"),
path: path.join(
PROJECT_ROOT,
"src/experimental-cli/constants.evaluate.js",
),
},
...["package.json", "index.mjs"].map((file) => ({
module: getPackageFile(`prettier/${file}`),
external: `../${file}`,
})),
],
})),
].flatMap((file) => createNodejsFileConfig(file)),
};
const docModule = {
name: "Doc",
files: [
createUniversalFileConfig({
input: "src/document/public.js",
outputBaseName: "doc",
umdVariableName: "doc",
minify: false,
}),
].flat(),
};
2022-09-25 11:50:59 +08:00
const pluginFiles = [
"src/plugins/estree.js",
2023-02-16 15:02:41 +08:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/babel.js",
2023-02-16 15:02:41 +08:00
replaceModule: [
{
// We don't use value of JSXText
module: require.resolve("@babel/parser"),
process: (text) =>
text.replaceAll(
/const entity\s?=\s?entities\[desc\];/g,
"const entity = undefined;",
2023-02-16 15:02:41 +08:00
),
},
],
},
2022-12-05 10:11:01 +08:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/flow.js",
2022-12-05 10:11:01 +08:00
replaceModule: [
{
2026-06-25 06:48:12 +08:00
module: getPackageFile("flow-parser/dist/FlowParser.js"),
process(text) {
2026-06-22 17:44:23 -07:00
text = outdent`
const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;
2026-06-22 17:44:23 -07:00
${text}
`;
return text;
},
},
{
2026-06-25 06:48:12 +08:00
module: getPackageFile("flow-parser/dist/FlowParserWASM.js"),
2026-06-22 17:44:23 -07:00
process(text) {
text = outdent`
const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;
2026-06-22 17:44:23 -07:00
${text}
`;
text = text.replaceAll("process.argv", "[]");
text = text.replaceAll('require("fs")', "undefined");
text = text.replaceAll('require("path")', "undefined");
text = text.replaceAll(
2026-06-22 17:44:23 -07:00
'require("crypto")',
`require(${JSON.stringify(path.join(import.meta.dirname, "../shims/crypto-random-fill-sync.cjs"))})`,
);
return text;
},
2022-12-05 10:11:01 +08:00
},
],
},
2018-05-24 15:30:45 -03:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/typescript.js",
replaceModule: [
2022-03-23 10:21:02 +08:00
{
module: require.resolve("typescript"),
process: modifyTypescriptModule,
2022-03-23 10:21:02 +08:00
},
2023-07-22 02:35:30 +08:00
{
2025-10-15 06:19:13 +08:00
module: getPackageFile("@typescript-eslint/typescript-estree"),
2023-07-22 02:35:30 +08:00
path: getPackageFile(
"@typescript-eslint/typescript-estree/dist/parser.js",
),
},
{
module: getPackageFile(
"@typescript-eslint/typescript-estree/dist/create-program/getScriptKind.js",
),
process: (text) =>
text
.replace(
'require("path")',
'{extname: file => "." + file.split(".").pop()}',
)
.replace(
'require("node:path")',
'{extname: file => "." + file.split(".").pop()}',
),
},
{
module: getPackageFile(
"@typescript-eslint/typescript-estree/dist/parseSettings/candidateTSConfigRootDirs.js",
),
process(text) {
return text.replace(
"process.cwd()",
JSON.stringify("/prettier-security-dirname-placeholder"),
);
},
},
{
module: getPackageFile(
"@typescript-eslint/typescript-estree/dist/parseSettings/createParseSettings.js",
),
process(text) {
return text
.replace(
"parseSettings.projects = ",
"parseSettings.projects = true ? new Map() : ",
)
.replace(
'require("node:path")',
'{extname: file => "." + file.split(".").pop()}',
)
.replace('require("@typescript-eslint/project-service")', "{}")
.replace(
"const tsconfigRootDir =",
"const tsconfigRootDir = undefined && ",
);
},
},
{
module: "*",
process: esmifyTypescriptEslint,
},
{
module: "*",
process(text, file) {
if (/require\(["'](?:typescript|ts-api-utils)["']\)/.test(text)) {
throw new Error(`Unexpected \`require("typescript")\` in ${file}.`);
}
return text;
},
},
...[
{
file: "@typescript-eslint/typescript-estree/dist/parseSettings/inferSingleRun.js",
text: "export const inferSingleRun = () => false;",
},
{
file: "@typescript-eslint/typescript-estree/dist/parseSettings/ExpiringCache.js",
text: outdent`
export const DEFAULT_TSCONFIG_CACHE_DURATION_SECONDS = undefined;
export const ExpiringCache = class {};
`,
},
{
file: "@typescript-eslint/typescript-estree/dist/parseSettings/getProjectConfigFiles.js",
text: "export const resolveProjectList = () => [];",
},
{
file: "@typescript-eslint/typescript-estree/dist/parseSettings/resolveProjectList.js",
text: "export const resolveProjectList = () => [];",
},
{
file: "@typescript-eslint/typescript-estree/dist/parseSettings/warnAboutTSVersion.js",
text: "export const handleUnsupportedTSVersion = () => {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/version-check.js",
text: "export const typescriptVersionIsAtLeast = new Proxy({}, {get: () => true})",
},
{
file: "@typescript-eslint/typescript-estree/dist/jsx/xhtml-entities.js",
text: "export const xhtmlEntities = {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/simple-traverse.js",
text: "export const simpleTraverse = () => {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/create-program/shared.js",
text: "export const ensureAbsolutePath = path => path;",
},
{
file: "@typescript-eslint/typescript-estree/dist/create-program/createIsolatedProgram.js",
text: "export const createIsolatedProgram = () => {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/create-program/useProvidedPrograms.js",
text: outdent`
export const useProvidedPrograms = () => {};
export const createProgramFromConfigFile = () => {};
`,
},
{
file: "@typescript-eslint/typescript-estree/dist/create-program/validateDefaultProjectForFilesGlob.js",
text: "export const validateDefaultProjectForFilesGlob = () => {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/create-program/getWatchProgramsForProjects.js",
text: "export const getWatchProgramsForProjects = () => {};",
},
"@typescript-eslint/typescript-estree/dist/create-program/describeFilePath.js",
{
file: "@typescript-eslint/typescript-estree/dist/create-program/createProjectProgram.js",
text: "export const createProjectProgram = () => {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/useProgramFromProjectService.js",
text: "export const useProgramFromProjectService = () => {};",
},
{
file: "@typescript-eslint/typescript-estree/dist/semantic-or-syntactic-errors.js",
text: "export const getFirstSemanticOrSyntacticError = () => {};",
},
].map((options) => {
options = typeof options === "string" ? { file: options } : options;
return {
module: getPackageFile(options.file),
text: options.text || "export {};",
};
}),
2022-10-23 11:19:57 +08:00
// Only needed if `range`/`loc` in parse options is `false`
{
module: getPackageFile("debug/src/browser.js"),
2026-05-28 01:45:35 +08:00
path: path.join(import.meta.dirname, "../shims/debug.js"),
},
2023-11-27 14:25:46 +08:00
{
module: require.resolve("ts-api-utils"),
process() {
throw new Error(
"Please replace the CJS version of 'ts-api-utils' with ESM version.",
);
},
},
{
module: getPackageFile(
"@typescript-eslint/types/dist/generated/ast-spec.js",
2025-10-15 06:19:13 +08:00
"@typescript-eslint/typescript-estree",
2023-11-27 14:25:46 +08:00
),
text: outdent`
const TYPE_STORE = new Proxy({}, {get: (_, type) => type});
export { TYPE_STORE as AST_TOKEN_TYPES, TYPE_STORE as AST_NODE_TYPES};
`,
2023-11-27 14:25:46 +08:00
},
2024-06-13 09:13:41 +08:00
// Use named import from `typescript`
{
module: getPackageFile("ts-api-utils/lib/index.js"),
2023-11-27 14:25:46 +08:00
process(text) {
const typescriptVariables = text
.matchAll(/import (?<variable>\w+) from ["']typescript["']/g)
.map((match) => match.groups.variable)
.toArray();
2024-06-13 09:56:03 +08:00
// Remove `'property' in typescript` check
text = text.replaceAll(
new RegExp(
String.raw`".*?" in (?:${typescriptVariables.join("|")})(?=\W)`,
"g",
2024-06-13 09:56:03 +08:00
),
"true",
);
2024-06-13 09:13:41 +08:00
text = text.replaceAll(
/(?<=import )(?=\w+ from ["']typescript["'])/g,
2024-06-13 09:13:41 +08:00
"* as ",
2023-11-27 14:25:46 +08:00
);
2024-11-25 22:47:30 +08:00
2023-11-27 14:25:46 +08:00
return text;
},
},
2022-03-14 20:02:02 +08:00
],
2018-05-24 15:30:45 -03:00
},
2020-10-13 09:14:02 +08:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/acorn.js",
replaceModule: [
2026-01-10 15:07:34 +08:00
{
module: getPackageFile("espree/lib/espree.js"),
process(text) {
2026-03-01 04:12:55 +08:00
// We don't use tokens
2026-01-10 15:07:34 +08:00
text = text.replace(
'import TokenTranslator from "./token-translator.js";',
"",
);
text = text.replaceAll("options.tokens === true", "false");
return text;
},
2022-03-14 20:02:02 +08:00
},
{
2023-02-16 15:02:41 +08:00
// We don't use value of JSXText
module: getPackageFile("acorn-jsx/xhtml.js"),
2023-02-16 15:02:41 +08:00
text: "module.exports = {};",
2022-03-14 20:02:02 +08:00
},
{
module: getPackageFile("acorn-jsx/index.js"),
find: 'require("acorn")',
replacement: "undefined",
},
2022-03-14 20:02:02 +08:00
],
2020-10-13 09:14:02 +08:00
},
2023-02-03 11:10:00 +08:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/meriyah.js",
2023-02-03 11:10:00 +08:00
replaceModule: [
2024-09-18 19:48:57 +08:00
{
module: resolveEsmModulePath("meriyah"),
process(text) {
// `structuredClone` is used to clone node
text = outdent`
const structuredClone =
globalThis.structuredClone ??
((node) => ({ ...node, range: [...node.range] }));
${text}
`;
// We don't use value of JSXText
text = text.replace(
"const decodeMap = ",
"const decodeMap = undefined &&",
);
text = text.replace(
"const entities = ",
"const entities = undefined &&",
);
text = text.replaceAll(
"parser.tokenValue = decodeHTMLStrict(",
"parser.tokenValue = (",
);
return text;
},
2023-02-03 11:10:00 +08:00
},
],
},
2018-11-04 23:03:07 +08:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/angular.js",
2022-10-11 11:26:30 +08:00
replaceModule: [
{
module: resolveEsmModulePath("@angular/compiler"),
process(text) {
text = text.replace(
"const phases = [",
"const phases = undefined && [",
);
text = text.replace("publishFacade(_global)", "");
text = text.replace(
"const serializerVisitor = new GetMsgSerializerVisitor()",
"",
);
text = text.replace(
"const compatibilityMode = CompatibilityMode.TemplateDefinitionBuilder",
"",
);
text = text.replace(
"const domSchema = new DomElementSchemaRegistry()",
"",
);
text = text.replace(
"const elementRegistry = new DomElementSchemaRegistry()",
"",
);
text = text.replace(
"const serializerVisitor$1 = new _SerializerVisitor()",
"",
);
text = text.replace(
"const NON_BINDABLE_VISITOR = new NonBindableVisitor()",
"",
);
text = text.replace(
"const NULL_EXPR = new LiteralExpr(null, null, null)",
"",
);
text = text.replace(
"const TYPED_NULL_EXPR = new LiteralExpr(null, INFERRED_TYPE, null)",
"",
);
text = text.replace("const _visitor = new _Visitor$2()", "");
text = text.replaceAll(
/const (.*?) = new BuiltinType\(BuiltinTypeName\..*?\);/g,
"const $1 = undefined;",
);
text = text.replaceAll(/var output_ast =.*?;\n/gs, "var output_ast");
text = text.replace(
"const serializer = new IcuSerializerVisitor();",
"",
);
text = text.replace(
"const _TAG_DEFINITION = new XmlTagDefinition();",
"",
);
text = text.replaceAll(
/function transformExpressionsInExpression\(.*?\n.*?\n\}\n/gs,
"function transformExpressionsInExpression(){}",
);
text = text.replaceAll(
/const deferTriggerToR3TriggerInstructionsMap = new Map\(\[\n.*?\n\]\);\n/gs,
"const deferTriggerToR3TriggerInstructionsMap = undefined;",
);
text = text.replaceAll(
/const BINARY_OPERATORS = new Map\(\[\n.*?\n\]\);\n/gs,
"const BINARY_OPERATORS = undefined;",
);
text = text.replaceAll(
/const PIPE_BINDINGS = \[\n.*?\n\];\n/gs,
"const PIPE_BINDINGS = undefined;",
);
text = text.replaceAll(
/const TEXT_INTERPOLATE_CONFIG = \{\n.*?\n\};\n/gs,
"const TEXT_INTERPOLATE_CONFIG = undefined;",
);
text = text.replaceAll(
/const CHAINABLE = new Set\(\[\n.*?\n\]\);\n/gs,
"const CHAINABLE = undefined;",
);
text = text.replaceAll(
/const PROPERTY_INTERPOLATE_CONFIG = \{\n.*?\n\};\n/gs,
"const PROPERTY_INTERPOLATE_CONFIG = undefined;",
);
text = text.replaceAll(
/const STYLE_PROP_INTERPOLATE_CONFIG = \{\n.*?\n\};\n/gs,
"const STYLE_PROP_INTERPOLATE_CONFIG = undefined;",
);
text = text.replaceAll(
/const ATTRIBUTE_INTERPOLATE_CONFIG = \{\n.*?\n\};\n/gs,
"const ATTRIBUTE_INTERPOLATE_CONFIG = undefined;",
);
text = text.replaceAll(
/const STYLE_MAP_INTERPOLATE_CONFIG = \{\n.*?\n\};\n/gs,
"const STYLE_MAP_INTERPOLATE_CONFIG = undefined;",
);
text = text.replaceAll(
/const CLASS_MAP_INTERPOLATE_CONFIG = \{\n.*?\n\};\n/gs,
"const CLASS_MAP_INTERPOLATE_CONFIG = undefined;",
);
text = text.replaceAll(
/const PURE_FUNCTION_CONFIG = \{\n.*?\n\};\n/gs,
"const PURE_FUNCTION_CONFIG = undefined;",
);
text = text.replaceAll(
/const NAMED_ENTITIES = \{\n.*?\n\};\n/gs,
"const NAMED_ENTITIES = {};",
);
return text;
},
2022-10-11 11:26:30 +08:00
},
],
2018-11-04 23:03:07 +08:00
},
2018-05-24 15:30:45 -03:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/postcss.js",
replaceModule: [
2022-03-20 21:56:09 +08:00
// The following two replacements prevent load `source-map` module
{
module: getPackageFile("postcss/lib/previous-map.js"),
2022-03-20 21:56:09 +08:00
text: "module.exports = class {};",
},
{
module: getPackageFile("postcss/lib/map-generator.js"),
2022-03-20 21:56:09 +08:00
text: "module.exports = class { generate() {} };",
},
{
module: getPackageFile("postcss/lib/input.js"),
process: (text) =>
text.replace("require('url')", "{}").replace("require('path')", "{}"),
},
// `postcss-values-parser` uses constructor.name, it will be changed by bundler
// https://github.com/shellscape/postcss-values-parser/blob/c00f858ab8c86ce9f06fdb702e8f26376f467248/lib/parser.js#L499
{
module: getPackageFile("postcss-values-parser/lib/parser.js"),
find: "node.constructor.name === 'Word'",
replacement: "node.type === 'word'",
},
2022-10-29 16:28:20 +08:00
// Prevent `node:util`, `node:utl`, and `node:path` shim
{
module: getPackageFile("postcss-values-parser/lib/tokenize.js"),
2022-10-29 16:28:20 +08:00
process: (text) =>
text
.replace("require('util')", "{}")
.replace(
"let message = util.format('Unclosed %s at line: %d, column: %d, token: %d', what, line, pos - offset, pos);",
"let message = `Unclosed ${what} at line: ${line}, column: ${pos - offset}, token: ${pos}`;",
)
.replace(
"let message = util.format('Syntax error at line: %d, column: %d, token: %d', line, pos - offset, pos);",
"let message = `Syntax error at line: ${line}, column: ${pos - offset}, token: ${pos}`;",
),
},
2022-03-14 20:02:02 +08:00
],
2018-05-24 15:30:45 -03:00
},
2026-06-06 11:21:39 +08:00
{
input: "src/plugins/graphql.js",
replaceModule: [
{
module: resolveEsmModulePath("graphql/language/ast"),
process(text) {
text = text.replace(
"new Set(Object.keys(QueryDocumentKeys))",
"/* @__PURE__ */ new Set(/* @__PURE__ */ Object.keys(QueryDocumentKeys))",
);
return text;
},
},
],
},
2018-05-24 15:30:45 -03:00
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/markdown.js",
replaceModule: [
{
module: getPackageFile("decode-named-character-reference/index.dom.js"),
path: getPackageFile("decode-named-character-reference"),
},
{
module: getPackageFile("parse-entities/decode-entity.browser.js"),
path: getPackageFile("parse-entities/decode-entity.js"),
},
],
2018-05-24 15:30:45 -03:00
},
{
2023-03-24 08:44:25 +08:00
input: "src/plugins/glimmer.js",
2022-08-10 19:32:44 +08:00
replaceModule: [
2023-11-03 19:37:52 +08:00
...["@glimmer/util", "@glimmer/wire-format", "@glimmer/syntax"].map(
(packageName) => ({
module: getPackageFile(`${packageName}/dist/prod/index.js`),
path: getPackageFile(`${packageName}/dist/dev/index.js`),
}),
),
2022-08-10 19:32:44 +08:00
{
2023-11-03 19:37:52 +08:00
module: getPackageFile("@glimmer/syntax/dist/dev/index.js"),
process(text) {
text = text.replace(
'import { DEBUG } from "@glimmer/env";',
"const DEBUG = false;",
);
2023-11-03 19:37:52 +08:00
// This passed to plugins, our plugin don't need access to the options
text = text.replace(/(?<=\sconst syntax = )\{.*?\n\}(?=;\n)/s, "{}");
2023-11-03 19:37:52 +08:00
text = text.replaceAll(
/\sclass \S+ extends[(\s]+node\(.*?\).*?\{(?:\n.*?\n)?\}\n/gs,
"\n",
2023-11-03 19:37:52 +08:00
);
text = text.replaceAll(
/\nvar api\S* = \s*(?:\/\*#__PURE__\*\/)?\s*Object\.freeze\(\{.*?\n\}\);/gs,
2023-11-03 19:37:52 +08:00
"",
);
text = text.replace(
"const ARGUMENT_RESOLUTION = ",
"const ARGUMENT_RESOLUTION = undefined &&",
);
text = text.replace(
"const HTML_RESOLUTION = ",
"const HTML_RESOLUTION = undefined &&",
);
text = text.replace(
"const LOCAL_DEBUG = ",
"const LOCAL_DEBUG = false &&",
);
2025-12-01 19:33:56 +08:00
text = text.replace(
/(?<=\n)export .*?;/,
2025-12-01 19:33:56 +08:00
"export { preprocess, getVoidTags, visitorKeys };",
);
2023-11-03 19:37:52 +08:00
return text;
},
2022-10-22 23:35:19 +08:00
},
{
module: getPackageFile("@handlebars/parser/dist/esm/index.js"),
path: getPackageFile("@handlebars/parser/dist/esm/parse.js"),
2022-10-22 23:35:19 +08:00
},
2022-08-10 19:32:44 +08:00
],
2018-05-24 15:30:45 -03:00
},
2023-03-24 08:44:25 +08:00
"src/plugins/html.js",
2026-06-01 10:18:29 +08:00
{
input: "src/plugins/yaml.js",
replaceModule: [
{
module: getPackageFile("yaml/browser/dist/schema/yaml-1.1/binary.js"),
process(text) {
// We don't care about content of `binary`
text = text.replace(
"onError('This environment does not support reading binary tags; either Buffer or atob is required');",
"",
);
return text;
},
},
// `Error#cause` for Node.js v14
{
module: getPackageFile("yaml-unist-parser/dist/yaml-syntax-error.mjs"),
process(text) {
text = text.replace(
"this.code = error.code;",
"this.cause ??= error; this.code = error.code;",
);
return text;
},
},
],
},
2025-12-05 04:53:15 +08:00
];
function createPluginModule(file) {
2022-09-25 11:50:59 +08:00
if (typeof file === "string") {
file = { input: file };
}
2025-12-05 04:53:15 +08:00
const { input, ...buildOptions } = file;
const { pluginName } = input.match(
/\/plugins\/(?<pluginName>.*?)\.js$/,
2025-12-05 04:53:15 +08:00
).groups;
2022-09-25 11:50:59 +08:00
2025-12-05 04:53:15 +08:00
const umdVariableName = `prettierPlugins.${pluginName}`;
2025-12-05 04:53:15 +08:00
const files = [
{
format: "esm",
file: `${pluginName}${extensions.esm}`,
},
{
format: "umd",
file: `${pluginName}${extensions.umd}`,
umdVariableName,
},
].map((output) => ({
input,
output: `plugins/${output.file}`,
build: createJavascriptModuleBuilder({
platform: "universal",
addDefaultExport: output.format === "esm",
...buildOptions,
format: output.format,
umdVariableName: output.umdVariableName,
}),
playground: output.format === "esm",
}));
return {
2025-12-05 04:53:15 +08:00
name: `Plugin[${pluginName}]`,
files: [
...files,
...createTypesConfig({
input,
2025-12-05 04:53:15 +08:00
outputBaseName: `plugins/${pluginName}`,
isPlugin: true,
}),
],
};
}
const pluginModules = pluginFiles.map((file) => createPluginModule(file));
packageConfig.modules.push(
mainModule,
cliModule,
docModule,
...pluginModules,
createPackageMetaFilesConfig({
"package.json": generatePackageJson,
packageDisplayName: "Prettier",
}),
2022-09-25 11:50:59 +08:00
);
2025-12-05 04:53:15 +08:00
export default packageConfig;