@bleedingdev/modern-js-server-utils 3.9.0-ultramodern.17 → 3.9.0-ultramodern.19

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