-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathbuild.ts
167 lines (156 loc) · 5.33 KB
/
build.ts
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
/* eslint-disable no-process-exit, no-console */
import * as fs from 'node:fs/promises';
import { createRequire } from 'node:module';
import * as path from 'node:path';
import * as esbuild from 'esbuild';
function requireResolved(targetPath: string): string {
return createRequire(__filename).resolve(targetPath);
}
function normalizePath(filePath: string): string {
return filePath.replace(/\\/g, '/');
}
function requireMock(targetPath: string): Promise<string> {
return fs.readFile(requireResolved(targetPath), 'utf8');
}
function makeFilter(filePath: string[] | string): { filter: RegExp } {
const paths = Array.isArray(filePath) ? filePath : [filePath];
const norm = paths.map(item =>
normalizePath(item).replace(/\//g, '[\\\\/]').replace(/\./g, '\\.'),
);
return { filter: new RegExp('(' + norm.join('|') + ')$') };
}
function createResolve(
targetPath: string,
join: string,
): esbuild.OnResolveResult {
const resolvedPackage = requireResolved(targetPath + '/package.json');
return {
path: path.join(resolvedPackage, '../src/', join),
};
}
async function buildPackage(name: string, file: string): Promise<void> {
const eslintRoot = requireResolved('eslint/package.json');
const linterPath = path.join(eslintRoot, '../lib/linter/linter.js');
const rulesPath = path.join(eslintRoot, '../lib/rules/index.js');
await esbuild.build({
entryPoints: {
[name]: requireResolved(file),
},
format: 'cjs',
platform: 'browser',
bundle: true,
external: [],
minify: true,
treeShaking: true,
write: true,
target: 'es2020',
sourcemap: 'linked',
outdir: './dist/',
supported: {},
banner: {
// https://github.com/evanw/esbuild/issues/819
js: `define(['exports', 'vs/language/typescript/tsWorker'], function (exports) {`,
},
footer: {
// https://github.com/evanw/esbuild/issues/819
js: `});`,
},
define: {
'process.env.NODE_ENV': '"production"',
'process.env.NODE_DEBUG': 'false',
'process.env.IGNORE_TEST_WIN32': 'true',
'process.env.DEBUG': 'false',
'process.emitWarning': 'console.warn',
'process.platform': '"browser"',
'process.env.TIMING': 'undefined',
'define.amd': 'false',
global: 'window',
},
alias: {
util: requireResolved('./src/mock/util.js'),
assert: requireResolved('./src/mock/assert.js'),
path: requireResolved('./src/mock/path.js'),
typescript: requireResolved('./src/mock/typescript.js'),
'lru-cache': requireResolved('./src/mock/lru-cache.js'),
},
plugins: [
{
name: 'replace-plugin',
setup(build): void {
build.onLoad(
makeFilter([
'/eslint-utils/rule-tester/RuleTester.ts',
'/ts-eslint/ESLint.ts',
'/ts-eslint/RuleTester.ts',
'/ts-eslint/CLIEngine.ts',
]),
async args => {
console.log('onLoad:replace', args.path);
const contents = await requireMock('./src/mock/empty.js');
return { contents, loader: 'js' };
},
);
build.onLoad(
makeFilter('/eslint/lib/unsupported-api.js'),
async args => {
console.log('onLoad:eslint:unsupported-api', args.path);
let contents = await requireMock('./src/mock/eslint-rules.js');
// this is needed to bypass system module resolver
contents = contents.replace(
'vt:eslint/rules',
normalizePath(rulesPath),
);
return { contents, loader: 'js' };
},
);
build.onLoad(makeFilter('/eslint/lib/api.js'), async args => {
console.log('onLoad:eslint', args.path);
let text = await requireMock('./src/mock/eslint.js');
// this is needed to bypass system module resolver
text = text.replace('vt:eslint/linter', normalizePath(linterPath));
return { contents: text, loader: 'js' };
});
build.onResolve(
makeFilter([
'@typescript-eslint/typescript-estree',
'@typescript-eslint/typescript-estree/use-at-your-own-risk',
]),
() =>
createResolve(
'@typescript-eslint/typescript-estree',
'use-at-your-own-risk.ts',
),
);
const anyAlias = /^(@typescript-eslint\/[a-z-]+)\/([a-z-]+)$/;
build.onResolve({ filter: anyAlias }, args => {
const parts = args.path.match(anyAlias);
if (parts) {
return createResolve(parts[1], `${parts[2]}/index.ts`);
}
return null;
});
build.onResolve(makeFilter('@typescript-eslint/[a-z-]+'), args =>
createResolve(args.path, 'index.ts'),
);
build.onEnd(e => {
for (const error of e.errors) {
console.error(error);
}
for (const warning of e.warnings) {
console.warn(warning);
}
});
},
},
],
});
}
console.time('building eslint for web');
buildPackage('index', './src/index.js')
.then(() => {
console.timeEnd('building eslint for web');
})
.catch((e: unknown) => {
console.error(String(e));
process.exit(1);
});