@bleedingdev/modern-js-server-utils 3.9.0-ultramodern.2 → 3.9.0-ultramodern.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common/index.js +15 -3
- package/dist/cjs/compilers/rslib.js +305 -0
- package/dist/cjs/compilers/tsgo.js +192 -0
- package/dist/esm/common/index.mjs +15 -3
- package/dist/esm/compilers/rslib.mjs +257 -0
- package/dist/esm/compilers/tsgo.mjs +135 -0
- package/dist/esm-node/common/index.mjs +15 -3
- package/dist/esm-node/compilers/rslib.mjs +258 -0
- package/dist/esm-node/compilers/tsgo.mjs +140 -0
- package/dist/types/common/index.d.ts +2 -3
- package/dist/types/compilers/rslib.d.ts +10 -0
- package/dist/types/compilers/tsgo.d.ts +49 -0
- package/package.json +6 -7
- package/dist/cjs/compilers/typescript/importRewriter.js +0 -175
- package/dist/cjs/compilers/typescript/index.js +0 -314
- package/dist/cjs/compilers/typescript/tsconfigPathsPlugin.js +0 -194
- package/dist/esm/compilers/typescript/importRewriter.mjs +0 -134
- package/dist/esm/compilers/typescript/index.mjs +0 -257
- package/dist/esm/compilers/typescript/tsconfigPathsPlugin.mjs +0 -143
- package/dist/esm-node/compilers/typescript/importRewriter.mjs +0 -135
- package/dist/esm-node/compilers/typescript/index.mjs +0 -262
- package/dist/esm-node/compilers/typescript/tsconfigPathsPlugin.mjs +0 -145
- package/dist/types/compilers/typescript/importRewriter.d.ts +0 -8
- package/dist/types/compilers/typescript/index.d.ts +0 -35
- package/dist/types/compilers/typescript/tsconfigPathsPlugin.d.ts +0 -3
package/dist/cjs/common/index.js
CHANGED
|
@@ -31,6 +31,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
31
31
|
FILE_EXTENSIONS: ()=>FILE_EXTENSIONS,
|
|
32
32
|
compile: ()=>compile
|
|
33
33
|
});
|
|
34
|
+
const utils_namespaceObject = require("@modern-js/utils");
|
|
34
35
|
const external_path_namespaceObject = require("path");
|
|
35
36
|
const FILE_EXTENSIONS = [
|
|
36
37
|
'.js',
|
|
@@ -45,11 +46,22 @@ const validateAbsolutePaths = (filenames, messageFunc)=>{
|
|
|
45
46
|
filenames.forEach((filename)=>validateAbsolutePath(filename, messageFunc(filename)));
|
|
46
47
|
};
|
|
47
48
|
const compile = async (appDirectory, modernConfig, compileOptions)=>{
|
|
48
|
-
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
49
|
+
const { sourceDirs, distDir, excludeFiles = [], tsconfigPath } = compileOptions;
|
|
49
50
|
validateAbsolutePaths(sourceDirs, (dir)=>`source dir ${dir} is not an absolute path.`);
|
|
50
51
|
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
validateAbsolutePaths(excludeFiles, (file)=>`excluded file ${file} is not an absolute path.`);
|
|
53
|
+
if (!tsconfigPath) return;
|
|
54
|
+
const { compileServerSources } = await import("../compilers/rslib.js");
|
|
55
|
+
try {
|
|
56
|
+
await compileServerSources(appDirectory, modernConfig, {
|
|
57
|
+
...compileOptions,
|
|
58
|
+
tsconfigPath
|
|
59
|
+
});
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (compileOptions.throwErrorInsteadOfExit) throw error;
|
|
62
|
+
utils_namespaceObject.logger.error(error);
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
53
65
|
};
|
|
54
66
|
exports.FILE_EXTENSIONS = __webpack_exports__.FILE_EXTENSIONS;
|
|
55
67
|
exports.compile = __webpack_exports__.compile;
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
+
__webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
});
|
|
9
|
+
return getter;
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
14
|
+
var define = (defs, kind)=>{
|
|
15
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
[kind]: defs[key]
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
define(getters, "get");
|
|
21
|
+
define(values, "value");
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
(()=>{
|
|
25
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
26
|
+
})();
|
|
27
|
+
(()=>{
|
|
28
|
+
__webpack_require__.r = (exports1)=>{
|
|
29
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
30
|
+
value: 'Module'
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
33
|
+
value: true
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
var __webpack_exports__ = {};
|
|
38
|
+
__webpack_require__.r(__webpack_exports__);
|
|
39
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
+
compileServerSources: ()=>compileServerSources
|
|
41
|
+
});
|
|
42
|
+
const utils_namespaceObject = require("@modern-js/utils");
|
|
43
|
+
const external_path_namespaceObject = require("path");
|
|
44
|
+
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
45
|
+
const external_tsgo_js_namespaceObject = require("./tsgo.js");
|
|
46
|
+
const SCRIPT_RE = /\.(?:[cm]?[jt]s|[jt]sx)$/u;
|
|
47
|
+
const DECLARATION_RE = /\.d\.[cm]?ts$/u;
|
|
48
|
+
const TS_RE = /\.[cm]?tsx?$/u;
|
|
49
|
+
const toPosix = (file)=>file.split(external_path_default().sep).join(external_path_default().posix.sep);
|
|
50
|
+
const escapeGlob = (file)=>file.replace(/[()[\]{}*?!+@]/gu, '\\$&');
|
|
51
|
+
const walk = async (dir)=>{
|
|
52
|
+
if (!await utils_namespaceObject.fs.pathExists(dir)) return [];
|
|
53
|
+
const entries = await utils_namespaceObject.fs.readdir(dir, {
|
|
54
|
+
withFileTypes: true
|
|
55
|
+
});
|
|
56
|
+
const files = await Promise.all(entries.map((entry)=>{
|
|
57
|
+
const file = external_path_default().join(dir, entry.name);
|
|
58
|
+
if (entry.isDirectory()) return 'node_modules' === entry.name ? [] : walk(file);
|
|
59
|
+
return [
|
|
60
|
+
file
|
|
61
|
+
];
|
|
62
|
+
}));
|
|
63
|
+
return files.flat();
|
|
64
|
+
};
|
|
65
|
+
const outputExtension = (file)=>/\.m[jt]s$/u.test(file) ? '.mjs' : /\.c[jt]s$/u.test(file) ? '.cjs' : '.js';
|
|
66
|
+
const collectSources = async (sourceDirs, excludeFiles)=>{
|
|
67
|
+
const excluded = new Set(excludeFiles.map((file)=>external_path_default().normalize(file)));
|
|
68
|
+
return (await Promise.all(sourceDirs.map(walk))).flat().filter((file)=>SCRIPT_RE.test(file) && !DECLARATION_RE.test(file) && !excluded.has(external_path_default().normalize(file)));
|
|
69
|
+
};
|
|
70
|
+
const groupEntries = (appDirectory, files)=>{
|
|
71
|
+
const outputs = new Map();
|
|
72
|
+
const entries = {
|
|
73
|
+
'.js': [],
|
|
74
|
+
'.mjs': [],
|
|
75
|
+
'.cjs': []
|
|
76
|
+
};
|
|
77
|
+
for (const file of files){
|
|
78
|
+
const extension = outputExtension(file);
|
|
79
|
+
const output = `${file.replace(SCRIPT_RE, '')}${extension}`;
|
|
80
|
+
const existing = outputs.get(output);
|
|
81
|
+
if (existing) throw new Error(`"${existing}" and "${file}" both compile to "${toPosix(external_path_default().relative(appDirectory, output))}". Rename one of them.`);
|
|
82
|
+
outputs.set(output, file);
|
|
83
|
+
entries[extension].push(escapeGlob(toPosix(external_path_default().relative(appDirectory, file))));
|
|
84
|
+
}
|
|
85
|
+
return entries;
|
|
86
|
+
};
|
|
87
|
+
const resolveAlias = (appDirectory, alias)=>Object.fromEntries(Object.entries((0, utils_namespaceObject.mergeAlias)(alias)).map(([key, target])=>[
|
|
88
|
+
key,
|
|
89
|
+
[].concat(target).map((value)=>value.startsWith('.') ? external_path_default().resolve(appDirectory, value) : value)
|
|
90
|
+
]));
|
|
91
|
+
const copyAssets = async (sourceDir, appDirectory, distDir)=>{
|
|
92
|
+
if (!await utils_namespaceObject.fs.pathExists(sourceDir)) return;
|
|
93
|
+
await utils_namespaceObject.fs.copy(sourceDir, external_path_default().join(distDir, external_path_default().relative(appDirectory, sourceDir)), {
|
|
94
|
+
filter: (src)=>!SCRIPT_RE.test(src) && !src.endsWith('tsconfig.json')
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
const matchAlias = (alias, request)=>{
|
|
98
|
+
for (const [key, targets] of Object.entries(alias)){
|
|
99
|
+
const exact = key.endsWith('$');
|
|
100
|
+
const name = exact ? key.slice(0, -1) : key;
|
|
101
|
+
if (request === name || !exact && request.startsWith(`${name}/`)) return {
|
|
102
|
+
targets,
|
|
103
|
+
rest: request.slice(name.length)
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const isBareSpecifier = (specifier)=>!/^[./]/u.test(specifier) && !external_path_default().isAbsolute(specifier);
|
|
108
|
+
const relativeSpecifier = (from, to)=>{
|
|
109
|
+
const relative = toPosix(external_path_default().relative(from, to));
|
|
110
|
+
return relative.startsWith('.') ? relative : `./${relative}`;
|
|
111
|
+
};
|
|
112
|
+
const importEmittedFile = ({ appDirectory, distDir, alias, sources, reached, assets })=>async ({ request, context, contextInfo, getResolve })=>{
|
|
113
|
+
if (!request || !context || !contextInfo?.issuer || !getResolve) return;
|
|
114
|
+
const resolve = getResolve();
|
|
115
|
+
const resolved = await resolve(context, request).catch(()=>'');
|
|
116
|
+
if (!resolved) return /^[./]/u.test(request) ? void 0 : request;
|
|
117
|
+
const fromApp = external_path_default().relative(appDirectory, resolved);
|
|
118
|
+
if (fromApp.startsWith('..') || external_path_default().isAbsolute(fromApp) || resolved.includes(`${external_path_default().sep}node_modules${external_path_default().sep}`)) {
|
|
119
|
+
const aliased = matchAlias(alias, request);
|
|
120
|
+
if (!aliased) return request;
|
|
121
|
+
for (const target of aliased.targets.filter(isBareSpecifier)){
|
|
122
|
+
const specifier = `${target}${aliased.rest}`;
|
|
123
|
+
if (await resolve(context, specifier).catch(()=>'') === resolved) return specifier;
|
|
124
|
+
}
|
|
125
|
+
const outputDir = external_path_default().join(distDir, external_path_default().relative(appDirectory, external_path_default().dirname(contextInfo.issuer)));
|
|
126
|
+
return relativeSpecifier(outputDir, resolved);
|
|
127
|
+
}
|
|
128
|
+
const script = SCRIPT_RE.test(resolved) && !DECLARATION_RE.test(resolved);
|
|
129
|
+
if (script && !sources.has(resolved)) reached.add(resolved);
|
|
130
|
+
if (!script) assets.add(resolved);
|
|
131
|
+
const emitted = script ? `${resolved.replace(SCRIPT_RE, '')}${outputExtension(resolved)}` : resolved;
|
|
132
|
+
return relativeSpecifier(external_path_default().dirname(contextInfo.issuer), emitted);
|
|
133
|
+
};
|
|
134
|
+
const compileServerSources = async (appDirectory, config, options)=>{
|
|
135
|
+
const { sourceDirs, distDir, tsconfigPath, moduleType, excludeFiles = [] } = options;
|
|
136
|
+
const sources = new Set(await collectSources(sourceDirs, excludeFiles));
|
|
137
|
+
const copyAllAssets = async ()=>{
|
|
138
|
+
for (const sourceDir of sourceDirs)await copyAssets(sourceDir, appDirectory, distDir);
|
|
139
|
+
};
|
|
140
|
+
const tsgoBinPath = (0, external_tsgo_js_namespaceObject.getTsgoBinPath)(appDirectory);
|
|
141
|
+
const { config: tsconfig, resolvedConfigPath } = await (0, external_tsgo_js_namespaceObject.createResolvedTsgoConfig)(appDirectory, tsconfigPath, sourceDirs, tsgoBinPath, excludeFiles);
|
|
142
|
+
const compilerOptions = tsconfig.compilerOptions ?? {};
|
|
143
|
+
const tsconfigDir = external_path_default().dirname(tsconfigPath);
|
|
144
|
+
const roots = new Set((tsconfig.files ?? []).map((file)=>external_path_default().resolve(tsconfigDir, file)));
|
|
145
|
+
for (const file of sources){
|
|
146
|
+
const checked = true === compilerOptions.allowJs || TS_RE.test(file);
|
|
147
|
+
if (checked && !roots.has(external_path_default().resolve(file))) sources.delete(file);
|
|
148
|
+
}
|
|
149
|
+
const failOnTypeError = false !== compilerOptions.noEmitOnError;
|
|
150
|
+
const declaration = true === compilerOptions.declaration;
|
|
151
|
+
const formats = {
|
|
152
|
+
'.js': 'module' === moduleType ? 'esm' : 'cjs',
|
|
153
|
+
'.mjs': 'esm',
|
|
154
|
+
'.cjs': 'cjs'
|
|
155
|
+
};
|
|
156
|
+
const createLibs = (entries)=>{
|
|
157
|
+
const libs = Object.keys(entries).filter((extension)=>entries[extension].length > 0).map((extension)=>({
|
|
158
|
+
format: formats[extension],
|
|
159
|
+
bundle: false,
|
|
160
|
+
autoExtension: false,
|
|
161
|
+
outBase: appDirectory,
|
|
162
|
+
source: {
|
|
163
|
+
entry: {
|
|
164
|
+
index: entries[extension]
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
output: {
|
|
168
|
+
filename: {
|
|
169
|
+
js: `[name]${extension}`
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}));
|
|
173
|
+
if (declaration) {
|
|
174
|
+
libs[0].redirect = {
|
|
175
|
+
dts: {
|
|
176
|
+
extension: 'module' === moduleType
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
libs[0].dts = {
|
|
180
|
+
bundle: false,
|
|
181
|
+
distPath: distDir,
|
|
182
|
+
tsgo: true,
|
|
183
|
+
typescriptPath: (0, external_tsgo_js_namespaceObject.getTsgoModulePath)(appDirectory),
|
|
184
|
+
abortOnError: failOnTypeError
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
return libs;
|
|
188
|
+
};
|
|
189
|
+
const alias = resolveAlias(appDirectory, config.alias);
|
|
190
|
+
const assets = new Set();
|
|
191
|
+
const build = async ()=>{
|
|
192
|
+
if (0 === sources.size) return;
|
|
193
|
+
const { createRslib } = await import("@rslib/core");
|
|
194
|
+
for(;;){
|
|
195
|
+
const reached = new Set();
|
|
196
|
+
const rslib = await createRslib({
|
|
197
|
+
cwd: appDirectory,
|
|
198
|
+
config: {
|
|
199
|
+
lib: createLibs(groupEntries(appDirectory, sources)),
|
|
200
|
+
source: {
|
|
201
|
+
tsconfigPath: resolvedConfigPath,
|
|
202
|
+
...compilerOptions.experimentalDecorators ? {
|
|
203
|
+
decorators: {
|
|
204
|
+
version: 'legacy'
|
|
205
|
+
}
|
|
206
|
+
} : {}
|
|
207
|
+
},
|
|
208
|
+
resolve: {
|
|
209
|
+
alias
|
|
210
|
+
},
|
|
211
|
+
output: {
|
|
212
|
+
target: 'node',
|
|
213
|
+
distPath: {
|
|
214
|
+
root: distDir
|
|
215
|
+
},
|
|
216
|
+
cleanDistPath: false,
|
|
217
|
+
sourceMap: {
|
|
218
|
+
js: 'source-map'
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
performance: {
|
|
222
|
+
buildCache: false
|
|
223
|
+
},
|
|
224
|
+
tools: {
|
|
225
|
+
rspack: (rspackConfig)=>{
|
|
226
|
+
rspackConfig.externals = [
|
|
227
|
+
importEmittedFile({
|
|
228
|
+
appDirectory,
|
|
229
|
+
distDir,
|
|
230
|
+
alias,
|
|
231
|
+
sources,
|
|
232
|
+
reached,
|
|
233
|
+
assets
|
|
234
|
+
}),
|
|
235
|
+
...[
|
|
236
|
+
rspackConfig.externals ?? []
|
|
237
|
+
].flat()
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
await rslib.build();
|
|
244
|
+
if (0 === reached.size) return;
|
|
245
|
+
for (const file of reached)sources.add(file);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const typeCheck = async ()=>{
|
|
249
|
+
if (0 === roots.size) return;
|
|
250
|
+
const result = await (0, external_tsgo_js_namespaceObject.runTsgo)(tsgoBinPath, [
|
|
251
|
+
'-p',
|
|
252
|
+
resolvedConfigPath,
|
|
253
|
+
'--noEmit'
|
|
254
|
+
], {
|
|
255
|
+
cwd: appDirectory,
|
|
256
|
+
reject: false
|
|
257
|
+
});
|
|
258
|
+
if (0 === result.code) return;
|
|
259
|
+
const diagnostics = result.stdout.trim() || result.stderr.trim();
|
|
260
|
+
if (!failOnTypeError) return void utils_namespaceObject.logger.warn(`TS-Go type check found errors:\n${diagnostics}`);
|
|
261
|
+
throw new Error([
|
|
262
|
+
`TS-Go type check failed with exit code ${result.code}.`,
|
|
263
|
+
diagnostics
|
|
264
|
+
].filter(Boolean).join('\n'));
|
|
265
|
+
};
|
|
266
|
+
const checkReachedSources = async ()=>{
|
|
267
|
+
const reachedRoots = [
|
|
268
|
+
...sources
|
|
269
|
+
].filter((file)=>!roots.has(external_path_default().resolve(file)) && (true === compilerOptions.allowJs || TS_RE.test(file)));
|
|
270
|
+
if (0 === reachedRoots.length) return typeCheck();
|
|
271
|
+
for (const file of reachedRoots)roots.add(external_path_default().resolve(file));
|
|
272
|
+
tsconfig.files = [
|
|
273
|
+
...tsconfig.files ?? [],
|
|
274
|
+
...reachedRoots.map((file)=>toPosix(external_path_default().relative(tsconfigDir, file)))
|
|
275
|
+
];
|
|
276
|
+
await utils_namespaceObject.fs.writeFile(resolvedConfigPath, JSON.stringify(tsconfig, null, 2));
|
|
277
|
+
return typeCheck();
|
|
278
|
+
};
|
|
279
|
+
let results;
|
|
280
|
+
try {
|
|
281
|
+
const built = await Promise.allSettled([
|
|
282
|
+
build()
|
|
283
|
+
]);
|
|
284
|
+
results = [
|
|
285
|
+
...await Promise.allSettled([
|
|
286
|
+
checkReachedSources()
|
|
287
|
+
]),
|
|
288
|
+
...built
|
|
289
|
+
];
|
|
290
|
+
} finally{
|
|
291
|
+
await utils_namespaceObject.fs.remove(resolvedConfigPath);
|
|
292
|
+
}
|
|
293
|
+
const failure = results.find((result)=>'rejected' === result.status);
|
|
294
|
+
if (failure) throw failure.reason;
|
|
295
|
+
await copyAllAssets();
|
|
296
|
+
for (const asset of assets)await utils_namespaceObject.fs.copy(asset, external_path_default().join(distDir, external_path_default().relative(appDirectory, asset)));
|
|
297
|
+
utils_namespaceObject.logger.info('Server sources compiled');
|
|
298
|
+
};
|
|
299
|
+
exports.compileServerSources = __webpack_exports__.compileServerSources;
|
|
300
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
301
|
+
"compileServerSources"
|
|
302
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
303
|
+
Object.defineProperty(exports, '__esModule', {
|
|
304
|
+
value: true
|
|
305
|
+
});
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
+
__webpack_require__.d(getter, {
|
|
7
|
+
a: getter
|
|
8
|
+
});
|
|
9
|
+
return getter;
|
|
10
|
+
};
|
|
11
|
+
})();
|
|
12
|
+
(()=>{
|
|
13
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
14
|
+
var define = (defs, kind)=>{
|
|
15
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
[kind]: defs[key]
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
define(getters, "get");
|
|
21
|
+
define(values, "value");
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
(()=>{
|
|
25
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
26
|
+
})();
|
|
27
|
+
(()=>{
|
|
28
|
+
__webpack_require__.r = (exports1)=>{
|
|
29
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
30
|
+
value: 'Module'
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
33
|
+
value: true
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
var __webpack_exports__ = {};
|
|
38
|
+
__webpack_require__.r(__webpack_exports__);
|
|
39
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
+
createResolvedTsgoConfig: ()=>createResolvedTsgoConfig,
|
|
41
|
+
getTsgoBinPath: ()=>getTsgoBinPath,
|
|
42
|
+
getTsgoModulePath: ()=>getTsgoModulePath,
|
|
43
|
+
runTsgo: ()=>runTsgo
|
|
44
|
+
});
|
|
45
|
+
const utils_namespaceObject = require("@modern-js/utils");
|
|
46
|
+
const external_child_process_namespaceObject = require("child_process");
|
|
47
|
+
const external_path_namespaceObject = require("path");
|
|
48
|
+
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
49
|
+
let resolvedConfigCount = 0;
|
|
50
|
+
const createResolvedTsgoConfig = async (appDirectory, tsconfigPath, sourceDirs, tsgoBinPath, excludeFiles = [])=>{
|
|
51
|
+
const tsconfigDir = external_path_default().dirname(tsconfigPath);
|
|
52
|
+
const output = await runTsgo(tsgoBinPath, [
|
|
53
|
+
'--showConfig',
|
|
54
|
+
'-p',
|
|
55
|
+
tsconfigPath
|
|
56
|
+
], {
|
|
57
|
+
cwd: tsconfigDir
|
|
58
|
+
});
|
|
59
|
+
const config = JSON.parse(output.stdout);
|
|
60
|
+
config.compilerOptions ??= {};
|
|
61
|
+
const options = config.compilerOptions;
|
|
62
|
+
options.rootDir = appDirectory;
|
|
63
|
+
options.composite = false;
|
|
64
|
+
options.declarationMap = false;
|
|
65
|
+
options.incremental = false;
|
|
66
|
+
options.noEmit = true !== options.declaration;
|
|
67
|
+
options.jsx ??= 'react-jsx';
|
|
68
|
+
delete options.emitDeclarationOnly;
|
|
69
|
+
delete options.outDir;
|
|
70
|
+
delete options.tsBuildInfoFile;
|
|
71
|
+
config.files = filterSourceFiles(tsconfigDir, sourceDirs, config.files, excludeFiles);
|
|
72
|
+
delete config.include;
|
|
73
|
+
delete config.references;
|
|
74
|
+
if (void 0 !== options.baseUrl) {
|
|
75
|
+
const baseUrl = external_path_default().resolve(tsconfigDir, options.baseUrl);
|
|
76
|
+
if (options.paths) options.paths = Object.fromEntries(Object.entries(options.paths).map(([key, targets])=>[
|
|
77
|
+
key,
|
|
78
|
+
targets.map((target)=>{
|
|
79
|
+
const relative = external_path_default().relative(tsconfigDir, external_path_default().resolve(baseUrl, target)).split(external_path_default().sep).join(external_path_default().posix.sep);
|
|
80
|
+
return relative.startsWith('.') ? relative : `./${relative}`;
|
|
81
|
+
})
|
|
82
|
+
]));
|
|
83
|
+
delete options.baseUrl;
|
|
84
|
+
}
|
|
85
|
+
if ([
|
|
86
|
+
'node',
|
|
87
|
+
'node10'
|
|
88
|
+
].includes(String(options.moduleResolution).toLowerCase())) delete options.moduleResolution;
|
|
89
|
+
const resolvedConfigPath = external_path_default().join(tsconfigDir, `.tsgo.${process.pid}.${resolvedConfigCount++}.resolved.json`);
|
|
90
|
+
await utils_namespaceObject.fs.writeFile(resolvedConfigPath, JSON.stringify(config, null, 2));
|
|
91
|
+
return {
|
|
92
|
+
config,
|
|
93
|
+
resolvedConfigPath
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
const toPosix = (file)=>file.split(external_path_default().sep).join(external_path_default().posix.sep);
|
|
97
|
+
const filterSourceFiles = (tsconfigDir, sourceDirs, files = [], excludeFiles = [])=>{
|
|
98
|
+
const sourcePosixPaths = sourceDirs.map(toPosix);
|
|
99
|
+
const excludedPaths = new Set(excludeFiles.map((file)=>toPosix(external_path_default().normalize(file))));
|
|
100
|
+
return files.filter((fileName)=>{
|
|
101
|
+
const absoluteFileName = toPosix(external_path_default().resolve(tsconfigDir, fileName));
|
|
102
|
+
if (excludedPaths.has(absoluteFileName)) return false;
|
|
103
|
+
return fileName.endsWith('.d.ts') || sourcePosixPaths.some((sourceDir)=>absoluteFileName.includes(sourceDir));
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
const runTsgo = (tsgoBinPath, args, options)=>new Promise((resolve, reject)=>{
|
|
107
|
+
const child = (0, external_child_process_namespaceObject.spawn)(process.execPath, [
|
|
108
|
+
tsgoBinPath,
|
|
109
|
+
...args
|
|
110
|
+
], {
|
|
111
|
+
cwd: options.cwd,
|
|
112
|
+
stdio: [
|
|
113
|
+
'ignore',
|
|
114
|
+
'pipe',
|
|
115
|
+
'pipe'
|
|
116
|
+
]
|
|
117
|
+
});
|
|
118
|
+
let stdout = '';
|
|
119
|
+
let stderr = '';
|
|
120
|
+
child.stdout.on('data', (data)=>{
|
|
121
|
+
stdout += data;
|
|
122
|
+
});
|
|
123
|
+
child.stderr.on('data', (data)=>{
|
|
124
|
+
stderr += data;
|
|
125
|
+
});
|
|
126
|
+
child.on('error', reject);
|
|
127
|
+
child.on('close', (code)=>{
|
|
128
|
+
const result = {
|
|
129
|
+
stdout,
|
|
130
|
+
stderr,
|
|
131
|
+
code: code ?? 1
|
|
132
|
+
};
|
|
133
|
+
if (false !== options.reject && 0 !== result.code) return void reject(new Error(stderr || stdout || `tsgo exited with ${result.code}`));
|
|
134
|
+
resolve(result);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
const getTsgoBinEntry = (pkg)=>{
|
|
138
|
+
if ('string' == typeof pkg.bin) return pkg.bin;
|
|
139
|
+
return pkg.bin?.tsc ?? pkg.bin?.tsgo;
|
|
140
|
+
};
|
|
141
|
+
const TSGO_PACKAGES = [
|
|
142
|
+
"@typescript/native",
|
|
143
|
+
"@typescript/native-preview"
|
|
144
|
+
];
|
|
145
|
+
const resolveTsgoPackage = (resolvePaths)=>{
|
|
146
|
+
for (const name of TSGO_PACKAGES)try {
|
|
147
|
+
const pkgPath = require.resolve(`${name}/package.json`, {
|
|
148
|
+
paths: resolvePaths
|
|
149
|
+
});
|
|
150
|
+
return {
|
|
151
|
+
name,
|
|
152
|
+
pkgPath
|
|
153
|
+
};
|
|
154
|
+
} catch {}
|
|
155
|
+
throw new Error('tsgo could not be found! Please install "@typescript/native" (or legacy "@typescript/native-preview") in your project to type-check BFF/server code.');
|
|
156
|
+
};
|
|
157
|
+
const getTsgoBinPath = (appDirectory, resolvePaths = [
|
|
158
|
+
appDirectory,
|
|
159
|
+
__dirname
|
|
160
|
+
])=>{
|
|
161
|
+
const { pkgPath } = resolveTsgoPackage(resolvePaths);
|
|
162
|
+
const pkgDir = external_path_default().dirname(pkgPath);
|
|
163
|
+
const pkg = require(pkgPath);
|
|
164
|
+
const declaredBinEntry = getTsgoBinEntry(pkg);
|
|
165
|
+
const candidates = [
|
|
166
|
+
declaredBinEntry ? external_path_default().resolve(pkgDir, declaredBinEntry) : void 0,
|
|
167
|
+
external_path_default().join(pkgDir, 'bin/tsgo.js')
|
|
168
|
+
].filter((candidate)=>Boolean(candidate));
|
|
169
|
+
return candidates.find((candidate)=>utils_namespaceObject.fs.existsSync(candidate)) ?? candidates[0];
|
|
170
|
+
};
|
|
171
|
+
const getTsgoModulePath = (appDirectory, resolvePaths = [
|
|
172
|
+
appDirectory,
|
|
173
|
+
__dirname
|
|
174
|
+
])=>{
|
|
175
|
+
const { name } = resolveTsgoPackage(resolvePaths);
|
|
176
|
+
return require.resolve(name, {
|
|
177
|
+
paths: resolvePaths
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
exports.createResolvedTsgoConfig = __webpack_exports__.createResolvedTsgoConfig;
|
|
181
|
+
exports.getTsgoBinPath = __webpack_exports__.getTsgoBinPath;
|
|
182
|
+
exports.getTsgoModulePath = __webpack_exports__.getTsgoModulePath;
|
|
183
|
+
exports.runTsgo = __webpack_exports__.runTsgo;
|
|
184
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
185
|
+
"createResolvedTsgoConfig",
|
|
186
|
+
"getTsgoBinPath",
|
|
187
|
+
"getTsgoModulePath",
|
|
188
|
+
"runTsgo"
|
|
189
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
190
|
+
Object.defineProperty(exports, '__esModule', {
|
|
191
|
+
value: true
|
|
192
|
+
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { logger } from "@modern-js/utils";
|
|
1
2
|
import * as __rspack_external_path from "path";
|
|
2
3
|
const FILE_EXTENSIONS = [
|
|
3
4
|
'.js',
|
|
@@ -12,10 +13,21 @@ const validateAbsolutePaths = (filenames, messageFunc)=>{
|
|
|
12
13
|
filenames.forEach((filename)=>validateAbsolutePath(filename, messageFunc(filename)));
|
|
13
14
|
};
|
|
14
15
|
const compile = async (appDirectory, modernConfig, compileOptions)=>{
|
|
15
|
-
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
16
|
+
const { sourceDirs, distDir, excludeFiles = [], tsconfigPath } = compileOptions;
|
|
16
17
|
validateAbsolutePaths(sourceDirs, (dir)=>`source dir ${dir} is not an absolute path.`);
|
|
17
18
|
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
validateAbsolutePaths(excludeFiles, (file)=>`excluded file ${file} is not an absolute path.`);
|
|
20
|
+
if (!tsconfigPath) return;
|
|
21
|
+
const { compileServerSources } = await import("../compilers/rslib.mjs");
|
|
22
|
+
try {
|
|
23
|
+
await compileServerSources(appDirectory, modernConfig, {
|
|
24
|
+
...compileOptions,
|
|
25
|
+
tsconfigPath
|
|
26
|
+
});
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (compileOptions.throwErrorInsteadOfExit) throw error;
|
|
29
|
+
logger.error(error);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
20
32
|
};
|
|
21
33
|
export { FILE_EXTENSIONS, compile };
|