-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathesbuild-common.mjs
More file actions
281 lines (277 loc) · 12.7 KB
/
Copy pathesbuild-common.mjs
File metadata and controls
281 lines (277 loc) · 12.7 KB
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
/*
* SonarQube JavaScript Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import esbuild from 'esbuild';
import textReplace from 'esbuild-plugin-text-replace';
import { copy } from 'esbuild-plugin-copy';
import { readFileSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { dirname } from 'node:path';
const stylelintPkgJson = readFileSync('node_modules/stylelint/package.json', 'utf8');
const testingLibraryPkgJson = JSON.parse(
readFileSync('node_modules/eslint-plugin-testing-library/package.json', 'utf8'),
);
const TESTING_LIBRARY_REQUIRE_EXPR =
'createRequire(import.meta.url)(resolve(dirname(fileURLToPath(import.meta.url)), "../package.json"))';
const testingLibraryDistSource = readFileSync(
'node_modules/eslint-plugin-testing-library/dist/index.mjs',
'utf8',
);
if (!testingLibraryDistSource.includes(TESTING_LIBRARY_REQUIRE_EXPR)) {
throw new Error(
'eslint-plugin-testing-library bundle patch target not found; the createRequire replacement ' +
'in esbuild-common.mjs is stale and the crash it fixes may have regressed. Re-check ' +
'node_modules/eslint-plugin-testing-library/dist/index.mjs after the dependency bump.',
);
}
const require = createRequire(import.meta.url);
const babelParserFromCore = JSON.stringify(
require.resolve('@babel/parser', {
paths: [require.resolve('@babel/core/package.json')],
}),
);
/**
* Build a SonarJS server bundle
* @param {Object} options
* @param {string} options.entryPoint - Entry point file (e.g., './server.mjs')
* @param {string} options.outfile - Output file (e.g., './bin/server.cjs')
* @param {Array} options.additionalAssets - Additional assets to copy (optional)
* @param {string} options.metafilePath - Path for esbuild dependency metadata (optional)
*/
export async function buildBundle({ entryPoint, outfile, additionalAssets = [], metafilePath }) {
const entryPointName = entryPoint.replace('./', '').replaceAll('.', String.raw`\.`);
const entryPointRegex = new RegExp(`${entryPointName}$`);
const result = await esbuild.build({
entryPoints: [entryPoint],
outfile,
format: 'cjs',
bundle: true,
loader: {
'.json': 'copy',
},
external: ['eslint/lib/util/glob-util', 'jiti'],
metafile: metafilePath !== undefined,
platform: 'node',
minify: true,
plugins: [
{
name: 'inline-package-imports',
setup(build) {
build.onLoad({ filter: /[\\/]package-imports\.json$/ }, ({ path }) => ({
contents: readFileSync(path, 'utf8'),
loader: 'json',
}));
},
},
textReplace({
include: entryPointRegex,
pattern: [['new URL(import.meta.url)', '__filename']],
}),
textReplace({
include: /lib[/\\]jsts[/\\]src[/\\]parsers[/\\]ast\.js$/,
pattern: [['path.dirname(fileURLToPath(import.meta.url))', '__dirname']],
}),
// Simplify the loader function in babel. At the end it's just importing Babel parser
// This matches the result of the TS compilation of the following lines
// https://github.com/babel/babel/blob/v7.25.1/eslint/babel-eslint-parser/src/parse.cts#L8-L12
textReplace({
include: /node_modules[/\\]@babel[/\\]eslint-parser[/\\]lib[/\\]parse\.cjs$/,
pattern: [
[
/const babelParser = require.*}\)\)/gms,
`const babelParser = require(${babelParserFromCore})`,
],
],
}),
// Babel 7 pulled in by eslint-plugin-react-hooks has optional .cts config support that
// references @babel/preset-typescript. The bridge bundle never loads Babel config files,
// so we can drop that branch instead of forcing an otherwise unused preset into the
// packaged runtime.
textReplace({
include: /@babel[/\\]core[/\\]lib[/\\]config[/\\]files[/\\]module-types\.js$/,
pattern: [
[
/const packageJson = require\("@babel\/preset-typescript\/package\.json"\);\s*if \(_semver\(\)\.lt\(packageJson\.version, "7\.21\.4"\)\) \{\s*console\.error\("`\.cts` configuration file failed to load, please try to update `@babel\/preset-typescript`\."\);\s*\}/g,
'',
],
[
/function getTSPreset\(filepath\) \{\s*try \{\s*return require\("@babel\/preset-typescript"\);\s*\} catch \(error\) \{\s*if \(error\.code !== "MODULE_NOT_FOUND"\) throw error;\s*let message = "You appear to be using a \.cts file as Babel configuration, but the `@babel\/preset-typescript` package was not found: please install it!";\s*if \(process\.versions\.pnp\) \{\s*message \+= `[\s\S]*?`;\s*\}\s*throw new _configError\.default\(message, filepath\);\s*\}\s*\}/g,
'function getTSPreset(filepath) { throw new _configError.default("You appear to be using a .cts file as Babel configuration, but the SonarJS bridge bundle does not support loading Babel .cts config files.", filepath); }',
],
],
}),
// eslint-plugin-vue legacy configs use require.resolve() to chain parent configs and set the
// parser. SonarJS only uses pluginVue.rules; the configs are never applied. Replacing
// require.resolve(...) with its string argument avoids a MODULE_NOT_FOUND crash at bridge
// startup caused by the bundled modules having no real on-disk paths.
textReplace({
include: /node_modules[/\\]eslint-plugin-vue[/\\]dist[/\\]configs[/\\]/,
pattern: [[/require\.resolve\(([^)]+)\)/g, '$1']],
}),
// Remove dynamic import of espree on ESLint Rule tester. In any case, it's never used in the bundle
textReplace({
include: /node_modules[/\\]eslint[/\\]lib[/\\]rule-tester[/\\]rule-tester\.js$/,
pattern: [
// https://github.com/eslint/eslint/blob/v8.57.0/lib/rule-tester/rule-tester.js#L56
['const espreePath = require.resolve("espree");', ''],
// https://github.com/eslint/eslint/blob/v8.57.0/lib/rule-tester/rule-tester.js#L781
['config.parser = espreePath;', ''],
],
}),
// Remove dynamic import of unused stylelint extensions
textReplace({
include: /node_modules[/\\]postcss-html[/\\]lib[/\\]syntax[/\\]build-syntax-resolver\.js$/,
pattern: [
['sugarss: () => require("sugarss"),', ''],
['"postcss-styl": () => require("postcss-styl"),', ''],
],
}),
// Remove createRequire from rolldown, used by tsdown, used by @stylistic
textReplace({
include: /node_modules[/\\]@stylistic[/\\]eslint-plugin[/\\]dist[/\\]vendor\.js$/,
pattern: [
[
'__importStar$4(__require("@eslint-community/eslint-utils"))',
'require("@eslint-community/eslint-utils")',
],
[
'__importStar$3(__require("@eslint-community/eslint-utils"))',
'require("@eslint-community/eslint-utils")',
],
[
'__importStar$2(__require("@eslint-community/eslint-utils"))',
'require("@eslint-community/eslint-utils")',
],
[
'__importStar$1(__require("@eslint-community/eslint-utils"))',
'require("@eslint-community/eslint-utils")',
],
[
'__importStar(__require("@eslint-community/eslint-utils"))',
'require("@eslint-community/eslint-utils")',
],
['__require("@typescript-eslint/types")', 'require("@typescript-eslint/types")'],
],
}),
// Remove createRequires
textReplace({
include: /node_modules[/\\]@stylistic[/\\]eslint-plugin[/\\]dist[/\\]rolldown-runtime\.js$/,
pattern: [['createRequire(import.meta.url);', 'createRequire(__filename);']],
}),
// Babel 8 config-file helpers still use createRequire(import.meta.url); in the bundled CJS
// output this becomes createRequire(undefined), which breaks standalone bridge startup.
textReplace({
include: /node_modules[/\\]@babel[/\\]core[/\\]lib[/\\]config[/\\]files[/\\]index\.js$/,
pattern: [[/createRequire\(import\.meta\.url\)/g, 'createRequire(__filename)']],
}),
textReplace({
include: /node_modules[/\\]@babel[/\\]eslint-parser[/\\]lib[/\\]index\.js$/,
pattern: [
[
/const require\$\d+ = createRequire\((?:import\.meta\.url|__filename)\);\s*const babelParser = require\$\d+\(require\$\d+\.resolve\("@babel\/parser", \{\s*paths: \[require\$\d+\.resolve\("@babel\/core\/package\.json"\)\]\s*\}\)\);/g,
`const babelParser = require(${babelParserFromCore});`,
],
],
}),
// eslint-plugin-testing-library reads its own name/version off package.json via
// createRequire(import.meta.url)(...); in the bundled CJS output import.meta.url is
// undefined, which breaks standalone bridge startup the same way the Babel case above does.
textReplace({
include: /node_modules[/\\]eslint-plugin-testing-library[/\\]dist[/\\]index\.mjs$/,
pattern: [
[
TESTING_LIBRARY_REQUIRE_EXPR,
JSON.stringify({
name: testingLibraryPkgJson.name,
version: testingLibraryPkgJson.version,
}),
],
],
}),
textReplace({
include: /node_modules[/\\]css-tree[/\\]lib[/\\]data-patch\.js$/,
pattern: [['const require = createRequire(import.meta.url);', '']],
}),
textReplace({
include: /node_modules[/\\]css-tree[/\\]lib[/\\]data\.js$/,
pattern: [['const require = createRequire(import.meta.url);', '']],
}),
textReplace({
include: /node_modules[/\\]css-tree[/\\]lib[/\\]version\.js$/,
pattern: [['const require = createRequire(import.meta.url);', '']],
}),
textReplace({
include: /node_modules[/\\]stylelint[/\\]lib[/\\]utils[/\\]mathMLTags\.mjs$/,
pattern: [['const require = createRequire(import.meta.url);', '']],
}),
textReplace({
include:
/node_modules[\/\\]stylelint[\/\\]lib[\/\\]rules[\/\\]declaration-property-value-no-unknown[\/\\]index\.mjs$/,
pattern: [['const require = createRequire(import.meta.url);', '']],
}),
// Dynamic import in module used by eslint-import-plugin. It always resolves to node resolver
textReplace({
include: /node_modules[/\\]eslint-module-utils[/\\]resolve\.js$/,
pattern: [
[
// https://github.com/import-js/eslint-plugin-import/blob/v2.11.0/utils/resolve.js#L157
'tryRequire(`eslint-import-resolver-${name}`, sourceFile)',
'require("eslint-import-resolver-node")',
],
],
}),
// The comparison by constructor name made by stylelint is not valid in the bundle because
// the Document object is named differently. We need to compare constructor object directly
textReplace({
include: /node_modules[/\\]stylelint[/\\]lib[/\\]lintPostcssResult\.mjs$/,
pattern: [
[
"postcssDoc && postcssDoc.constructor.name === 'Document' ? postcssDoc.nodes : [postcssDoc]",
"postcssDoc && (postcssDoc instanceof require('postcss').Document) ? postcssDoc.nodes : [postcssDoc]",
],
['const require = createRequire(import.meta.url);', ''],
],
}),
// do not let stylelint fs read its package.json
textReplace({
include: /node_modules[/\\]stylelint[/\\]lib[/\\]utils[/\\]FileCache.mjs$/,
pattern: [
[
"JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));",
stylelintPkgJson,
],
],
}),
copy({
resolveFrom: 'cwd',
assets: [
// We need to copy all typescript declaration files for type-checking, as typescript will
// look for those in the filesystem
{
from: ['./node_modules/typescript/lib/*.d.ts'],
to: ['./bin'],
},
...additionalAssets,
],
}),
],
});
if (metafilePath !== undefined) {
await mkdir(dirname(metafilePath), { recursive: true });
await writeFile(metafilePath, JSON.stringify(result.metafile));
}
}