Repository navigation
Expand file tree
/
Copy pathesbuild.js
More file actions
150 lines (134 loc) · 4.51 KB
/
Copy pathesbuild.js
File metadata and controls
150 lines (134 loc) · 4.51 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
/* eslint-disable no-console */
/* global process */
'use strict';
const fs = require('fs');
const path = require('path');
const esbuild = require('esbuild');
function parseArgs(argv) {
const args = new Set(argv.slice(2));
return {
production: args.has('--production') || args.has('--prod'),
watch: args.has('--watch'),
};
}
function tsconfigPathsPlugin({ tsconfigPath }) {
const absTsconfigPath = path.resolve(tsconfigPath);
const raw = JSON.parse(fs.readFileSync(absTsconfigPath, 'utf8'));
const compilerOptions = raw.compilerOptions || {};
const baseUrl = compilerOptions.baseUrl ? path.resolve(path.dirname(absTsconfigPath), compilerOptions.baseUrl) : path.dirname(absTsconfigPath);
const paths = compilerOptions.paths || {};
/** @type {{prefix: string, targets: string[]}[]} */
const mappings = Object.entries(paths)
.map(([key, targets]) => {
const prefix = key.endsWith('/*') ? key.slice(0, -2) : key;
const targetList = Array.isArray(targets) ? targets : [targets];
const resolved = targetList.map((t) => {
const cleaned = typeof t === 'string' ? t : '';
const withoutStar = cleaned.replace('/*', '').replace('*', '');
return path.resolve(baseUrl, withoutStar);
});
return { prefix, targets: resolved };
})
.filter((m) => m.prefix.length > 0 && m.targets.length > 0);
return {
name: 'tsconfig-paths',
setup(build) {
build.onResolve({ filter: /^[^./].*/ }, (args) => {
for (const m of mappings) {
if (args.path === m.prefix || args.path.startsWith(`${m.prefix}/`)) {
const suffix = args.path === m.prefix ? '' : args.path.slice(m.prefix.length + 1);
for (const targetBase of m.targets) {
const candidate = suffix ? path.join(targetBase, suffix) : targetBase;
const resolved = resolveCandidate(candidate);
if (resolved) return { path: resolved };
}
}
}
return null;
});
},
};
}
function resolveCandidate(candidate) {
try {
if (fs.existsSync(candidate)) {
const stat = fs.statSync(candidate);
if (stat.isFile()) return candidate;
if (stat.isDirectory()) {
const idx = resolveCandidate(path.join(candidate, 'index'));
if (idx) return idx;
}
}
const exts = ['.ts', '.tsx', '.js', '.mjs', '.cjs'];
for (const ext of exts) {
const p = `${candidate}${ext}`;
if (fs.existsSync(p) && fs.statSync(p).isFile()) return p;
}
} catch {
// ignore and fall through
}
return null;
}
// emits markers that the VS Code esbuild background problem matcher keys on,
// so `watch` works as a preLaunchTask (launch waits for the first build to finish).
function esbuildProblemMatcherPlugin() {
return {
name: 'esbuild-problem-matcher',
setup(build) {
build.onStart(() => console.log('[watch] build started'));
build.onEnd((result) => {
result.errors.forEach(({ text, location }) => {
console.error(`✘ [ERROR] ${text}`);
if (location) console.error(` ${path.resolve(location.file)}:${location.line}:${location.column}:`);
});
console.log('[watch] build finished');
});
},
};
}
async function buildAll({ production, watch }) {
const tsconfig = path.resolve(__dirname, 'tsconfig.json');
const shared = {
entryPoints: [path.resolve(__dirname, 'src', 'extension.ts')],
bundle: true,
sourcemap: true,
minify: production,
tsconfig,
plugins: [tsconfigPathsPlugin({ tsconfigPath: tsconfig })],
loader: {
'.html': 'text',
'.css': 'text',
},
logLevel: 'info',
};
const nodeOptions = {
...shared,
platform: 'node',
format: 'cjs',
outfile: path.resolve(__dirname, 'dist', 'extension.js'),
external: ['vscode'],
plugins: [...shared.plugins, esbuildProblemMatcherPlugin()],
};
const webOptions = {
...shared,
platform: 'browser',
format: 'cjs',
outfile: path.resolve(__dirname, 'dist', 'web', 'extension.js'),
external: ['vscode'],
define: {
'process.env.BROWSER': 'true',
},
};
if (watch) {
const nodeCtx = await esbuild.context(nodeOptions);
const webCtx = await esbuild.context(webOptions);
await Promise.all([nodeCtx.watch(), webCtx.watch()]);
console.log('[esbuild] watching…');
return;
}
await Promise.all([esbuild.build(nodeOptions), esbuild.build(webOptions)]);
}
buildAll(parseArgs(process.argv)).catch((err) => {
console.error(err);
process.exitCode = 1;
});