forked from prettier/prettier
/
config.mjs
321 lines (308 loc) · 10.2 KB
/
config.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import path from "node:path";
import { createRequire } from "node:module";
import createEsmUtils from "esm-utils";
import { PROJECT_ROOT } from "../utils/index.mjs";
const { require } = createEsmUtils(import.meta);
/**
* @typedef {Object} Bundle
* @property {string} input - input of the bundle
* @property {string?} output - path of the output file in the `dist/` folder
* @property {string?} name - name for the UMD bundle (for plugins, it'll be `prettierPlugins.${name}`)
* @property {'node' | 'universal'} target - should generate a CJS only for node or universal bundle
* @property {'core' | 'plugin'} type - it's a plugin bundle or core part of prettier
* @property {string[]} external - array of paths that should not be included in the final bundle
* @property {Object.<string, string | {code: string}>} replaceModule - module replacement path or code
* @property {{file: string, find: string, replacement: string}[]} replaceText - Text replacements
* @property {string[]} babelPlugins - babel plugins
* @property {boolean?} minify - minify
* @property {string[]?} esbuildTarget - ESBuild target
* @property {boolean?} skipBabel - Skip babel transform
* @typedef {Object} CommonJSConfig
* @property {string[]} ignore - paths of CJS modules to ignore
*/
/*
`diff` use deprecated folder mapping "./" in the "exports" field,
so we can't `require("diff/lib/diff/array.js")` directly.
To reduce the bundle size, replace the entry with smaller files.
We can switch to deep require once https://github.com/kpdecker/jsdiff/pull/351 get merged
*/
const replaceDiffPackageEntry = (file) => ({
[require.resolve("diff")]: path.join(
path.dirname(require.resolve("diff/package.json")),
file
),
});
/** @type {Bundle[]} */
const parsers = [
{
input: "src/language-js/parse/babel.js",
},
{
input: "src/language-js/parse/flow.js",
replaceText: [
// `flow-parser` use this for `globalThis`, can't work in strictMode
{
file: require.resolve("flow-parser"),
find: "(function(){return this}())",
replacement: "(globalThis)",
},
],
},
{
input: "src/language-js/parse/typescript.js",
replaceText: [
// `@typescript-eslint/typescript-estree` v4
{
file: "*",
find: 'require("globby")',
replacement: "{}",
},
{
file: "*",
find: "extra.projects = prepareAndTransformProjects(",
replacement: "extra.projects = [] || prepareAndTransformProjects(",
},
{
file: "*",
find: "process.versions.node",
replacement: JSON.stringify("999.999.999"),
},
{
file: "*",
find: "process.cwd()",
replacement: JSON.stringify("/prettier-security-dirname-placeholder"),
},
{
file: "*",
find: 'require("perf_hooks")',
replacement: "{}",
},
{
file: "*",
find: 'require("inspector")',
replacement: "{}",
},
{
file: "*",
find: "typescriptVersionIsAtLeast[version] = semverCheck(version);",
replacement: "typescriptVersionIsAtLeast[version] = true;",
},
// The next two replacement fixed webpack warning `Critical dependency: require function is used in a way in which dependencies cannot be statically extracted`
// #12338
{
file: require.resolve(
"@typescript-eslint/typescript-estree/dist/create-program/shared.js"
),
find: "moduleResolver = require(moduleResolverPath);",
replacement: "throw new Error('Dynamic require is not supported');",
},
{
file: require.resolve("typescript"),
process(text) {
return text.replace(
/(?<=\n)(?<indentString>\s+)function tryGetNodePerformanceHooks\(\) {.*?\n\k<indentString>}(?=\n)/s,
"function tryGetNodePerformanceHooks() {}"
);
},
},
...Object.entries({
// `typescript/lib/typescript.js` expose extra global objects
// `TypeScript`, `toolsVersion`, `globalThis`
'typeof process === "undefined" || process.browser': "false",
'typeof globalThis === "object"': "true",
"_fs.realpathSync.native":
"_fs.realpathSync && _fs.realpathSync.native",
// Remove useless `ts.sys`
"ts.sys = ": "ts.sys = undefined && ",
// Remove useless language service
"ts.realizeDiagnostics = ": "ts.realizeDiagnostics = undefined && ",
"ts.TypeScriptServicesFactory = ":
"ts.TypeScriptServicesFactory = undefined && ",
"var ShimBase = ": "var ShimBase = undefined && ",
"var TypeScriptServicesFactory = ":
"var TypeScriptServicesFactory = undefined && ",
"var LanguageServiceShimObject = ":
"var LanguageServiceShimObject = undefined && ",
"var CoreServicesShimHostAdapter = ":
"var CoreServicesShimHostAdapter = undefined && ",
"var LanguageServiceShimHostAdapter = ":
"var LanguageServiceShimHostAdapter = undefined && ",
"var ScriptSnapshotShimAdapter = ":
"var ScriptSnapshotShimAdapter = undefined && ",
"var ClassifierShimObject = ":
"var ClassifierShimObject = undefined && ",
"var CoreServicesShimObject = ":
"var CoreServicesShimObject = undefined && ",
"function simpleForwardCall(": "0 && function simpleForwardCall(",
"function forwardJSONCall(": "0 && function forwardJSONCall(",
"function forwardCall(": "0 && function forwardCall(",
"function realizeDiagnostics(": "0 && function realizeDiagnostics(",
"function realizeDiagnostic(": "0 && function realizeDiagnostic(",
"function convertClassifications(":
"0 && function convertClassifications(",
// Dynamic `require()`s
"ts.sys && ts.sys.require": "false",
"require(etwModulePath)": "undefined",
'require("source-map-support").install()': "",
"require(modulePath)": "undefined",
}).map(([find, replacement]) => ({
file: require.resolve("typescript"),
find,
replacement,
})),
],
replaceModule: {
[require.resolve("debug/src/browser.js")]:
require.resolve("./shims/debug.cjs"),
},
},
{
input: "src/language-js/parse/acorn-and-espree.js",
name: "prettierPlugins.espree",
// TODO: Rename this file to `parser-acorn-and-espree.js` or find a better way
output: "parser-espree.js",
replaceText: [
{
file: require.resolve("espree"),
find: "const Syntax = (function() {",
replacement: "const Syntax = undefined && (function() {",
},
{
file: require.resolve("espree"),
find: "var visitorKeys = require('eslint-visitor-keys');",
replacement: "var visitorKeys;",
},
],
},
{
input: "src/language-js/parse/meriyah.js",
},
{
input: "src/language-js/parse/angular.js",
replaceText: [
{
file: "*",
find: 'require("@angular/compiler/src/',
replacement: 'require("@angular/compiler/esm2015/src/',
},
],
},
{
input: "src/language-css/parser-postcss.js",
replaceText: [
{
// `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
file: require.resolve("postcss-values-parser/lib/parser.js"),
find: "node.constructor.name === 'Word'",
replacement: "node.type === 'word'",
},
],
},
{
input: "src/language-graphql/parser-graphql.js",
},
{
input: "src/language-markdown/parser-markdown.js",
replaceModule: {
[require.resolve("parse-entities/decode-entity.browser.js")]:
require.resolve("parse-entities/decode-entity.js"),
},
},
{
input: "src/language-handlebars/parser-glimmer.js",
},
{
input: "src/language-html/parser-html.js",
},
{
input: "src/language-yaml/parser-yaml.js",
replaceModule: {
// Use `tslib.es6.js`, so we can avoid `globalThis` shim
[require.resolve("tslib")]: require
.resolve("tslib")
.replace(/tslib\.js$/, "tslib.es6.js"),
},
},
].map((bundle) => {
const { name } = bundle.input.match(
/(?:parser-|parse\/)(?<name>.*?)\.js$/
).groups;
return {
type: "plugin",
target: "universal",
name: `prettierPlugins.${name}`,
output: `parser-${name}.js`,
...bundle,
};
});
/** @type {Bundle[]} */
const coreBundles = [
{
input: "src/index.js",
replaceText: [
{
file: require.resolve("@iarna/toml/lib/toml-parser.js"),
find: "const utilInspect = eval(\"require('util').inspect\")",
replacement: "const utilInspect = require('util').inspect",
},
],
replaceModule: replaceDiffPackageEntry("lib/diff/array.js"),
},
{
input: "src/document/index.js",
name: "doc",
output: "doc.js",
target: "universal",
format: "umd",
minify: false,
},
{
input: "src/standalone.js",
name: "prettier",
target: "universal",
replaceModule: {
[require.resolve("@babel/highlight")]: require.resolve(
"./shims/babel-highlight.cjs"
),
[createRequire(require.resolve("vnopts")).resolve("chalk")]:
require.resolve("./shims/chalk.cjs"),
...replaceDiffPackageEntry("lib/diff/array.js"),
},
replaceText: [
{
file: path.join(PROJECT_ROOT, "src/main/parser.js"),
find: "return requireParser(opts.parser);",
replacement: "",
},
],
},
{
input: "bin/prettier.js",
output: "bin-prettier.js",
esbuildTarget: ["node0.10"],
skipBabel: true,
},
{
input: "src/cli/index.js",
output: "cli.js",
external: ["benchmark"],
replaceModule: replaceDiffPackageEntry("lib/patch/create.js"),
},
{
input: "src/common/third-party.js",
replaceModule: {
// cosmiconfig@6 -> import-fresh can't find parentModule, since module is bundled
[require.resolve("parent-module")]: require.resolve(
"./shims/parent-module.cjs"
),
},
},
].map((bundle) => ({
type: "core",
target: "node",
output: path.basename(bundle.input),
...bundle,
}));
const configs = [...coreBundles, ...parsers];
export default configs;