@vendure/dashboard 3.3.6-master-202507110238 → 3.3.6

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.
Files changed (68) hide show
  1. package/dist/plugin/types.d.ts +40 -0
  2. package/dist/plugin/utils/ast-utils.d.ts +0 -5
  3. package/dist/plugin/utils/ast-utils.js +0 -67
  4. package/dist/plugin/utils/ast-utils.spec.js +1 -76
  5. package/dist/plugin/utils/compiler.d.ts +22 -0
  6. package/dist/plugin/utils/compiler.js +162 -0
  7. package/dist/plugin/utils/config-loader.d.ts +0 -120
  8. package/dist/plugin/utils/config-loader.js +1 -367
  9. package/dist/plugin/utils/logger.d.ts +3 -0
  10. package/dist/plugin/utils/logger.js +39 -0
  11. package/dist/plugin/utils/plugin-discovery.d.ts +27 -0
  12. package/dist/plugin/utils/plugin-discovery.js +343 -0
  13. package/dist/plugin/utils/tsconfig-utils.d.ts +9 -0
  14. package/dist/plugin/utils/tsconfig-utils.js +50 -0
  15. package/dist/plugin/vite-plugin-config-loader.d.ts +3 -3
  16. package/dist/plugin/vite-plugin-config-loader.js +13 -13
  17. package/dist/plugin/vite-plugin-dashboard-metadata.js +19 -2
  18. package/dist/plugin/vite-plugin-vendure-dashboard.d.ts +7 -7
  19. package/dist/plugin/vite-plugin-vendure-dashboard.js +2 -2
  20. package/package.json +134 -131
  21. package/src/app/routes/_authenticated/_orders/components/customer-address-selector.tsx +0 -1
  22. package/src/app/routes/_authenticated/_orders/components/payment-details.tsx +6 -2
  23. package/src/app/routes/_authenticated/_orders/components/state-transition-control.tsx +2 -2
  24. package/src/app/routes/_authenticated/_orders/orders_.draft.$id.tsx +0 -1
  25. package/src/app/routes/_authenticated/_products/components/option-value-input.tsx +1 -1
  26. package/src/app/routes/_authenticated/_zones/components/zone-countries-table.tsx +0 -7
  27. package/src/lib/components/layout/content-language-selector.tsx +1 -1
  28. package/src/lib/components/shared/asset/asset-preview.tsx +0 -6
  29. package/src/lib/components/shared/option-value-input.tsx +1 -1
  30. package/src/lib/components/shared/product-variant-selector.tsx +1 -1
  31. package/src/lib/components/ui/calendar.tsx +1 -1
  32. package/src/lib/framework/dashboard-widget/metrics-widget/index.tsx +1 -1
  33. package/src/lib/framework/dashboard-widget/orders-summary/index.tsx +0 -2
  34. package/src/lib/hooks/use-extended-list-query.ts +32 -30
  35. package/vite/tests/barrel-exports.spec.ts +13 -4
  36. package/{dist/plugin/tests/barrel-exports/my-plugin/src/my.plugin.js → vite/tests/fixtures-npm-plugin/fake_node_modules/test-plugin/index.js} +6 -6
  37. package/vite/tests/fixtures-npm-plugin/fake_node_modules/test-plugin/package.json +8 -0
  38. package/vite/tests/fixtures-npm-plugin/package.json +6 -0
  39. package/{dist/plugin/tests/barrel-exports/vendure-config.js → vite/tests/fixtures-npm-plugin/vendure-config.ts} +5 -6
  40. package/vite/tests/fixtures-path-alias/aliased-plugin/index.ts +1 -0
  41. package/vite/tests/fixtures-path-alias/aliased-plugin/src/aliased.plugin.ts +8 -0
  42. package/vite/tests/fixtures-path-alias/package.json +6 -0
  43. package/vite/tests/fixtures-path-alias/vendure-config.ts +19 -0
  44. package/vite/tests/npm-plugin.spec.ts +46 -0
  45. package/vite/tests/path-alias.spec.ts +33 -0
  46. package/vite/tests/tsconfig.json +11 -0
  47. package/vite/types.ts +44 -0
  48. package/vite/utils/ast-utils.spec.ts +1 -80
  49. package/vite/utils/ast-utils.ts +0 -86
  50. package/vite/utils/compiler.ts +244 -0
  51. package/vite/utils/config-loader.ts +0 -555
  52. package/vite/utils/logger.ts +43 -0
  53. package/vite/utils/plugin-discovery.ts +442 -0
  54. package/vite/utils/tsconfig-utils.ts +79 -0
  55. package/vite/vite-plugin-config-loader.ts +20 -17
  56. package/vite/vite-plugin-dashboard-metadata.ts +26 -7
  57. package/vite/vite-plugin-tailwind-source.ts +2 -2
  58. package/vite/vite-plugin-vendure-dashboard.ts +9 -9
  59. package/dist/plugin/tests/barrel-exports/my-plugin/index.d.ts +0 -1
  60. package/dist/plugin/tests/barrel-exports/my-plugin/index.js +0 -17
  61. package/dist/plugin/tests/barrel-exports/my-plugin/src/my.plugin.d.ts +0 -2
  62. package/dist/plugin/tests/barrel-exports/vendure-config.d.ts +0 -2
  63. package/dist/plugin/tests/barrel-exports.spec.js +0 -14
  64. /package/dist/plugin/{tests/barrel-exports.spec.d.ts → types.js} +0 -0
  65. /package/vite/tests/{barrel-exports → fixtures-barrel-exports}/my-plugin/index.ts +0 -0
  66. /package/vite/tests/{barrel-exports → fixtures-barrel-exports}/my-plugin/src/my.plugin.ts +0 -0
  67. /package/vite/tests/{barrel-exports → fixtures-barrel-exports}/package.json +0 -0
  68. /package/vite/tests/{barrel-exports → fixtures-barrel-exports}/vendure-config.ts +0 -0
@@ -1,555 +0,0 @@
1
- import { VendureConfig } from '@vendure/core';
2
- import fs from 'fs-extra';
3
- import path from 'path';
4
- import tsConfigPaths from 'tsconfig-paths';
5
- import * as ts from 'typescript';
6
- import { pathToFileURL } from 'url';
7
-
8
- import { findConfigExport, getPluginInfo } from './ast-utils.js';
9
-
10
- type Logger = {
11
- info: (message: string) => void;
12
- warn: (message: string) => void;
13
- debug: (message: string) => void;
14
- };
15
-
16
- export type PluginInfo = {
17
- name: string;
18
- pluginPath: string;
19
- dashboardEntryPath: string | undefined;
20
- };
21
-
22
- const defaultLogger: Logger = {
23
- info: (message: string) => {
24
- /* noop */
25
- },
26
- warn: (message: string) => {
27
- /* noop */
28
- },
29
- debug: (message: string) => {
30
- /* noop */
31
- },
32
- };
33
-
34
- /**
35
- * @description
36
- * The PathAdapter interface allows customization of how paths are handled
37
- * when compiling the Vendure config and its imports.
38
- *
39
- * This is particularly useful in complex project structures, such as monorepos,
40
- * where the Vendure config file may not be in the root directory,
41
- * or when you need to transform TypeScript path mappings.
42
- */
43
- export interface PathAdapter {
44
- /**
45
- * @description
46
- * A function to determine the path to the compiled Vendure config file. The default implementation
47
- * simple joins the output directory with the config file name:
48
- *
49
- * ```ts
50
- * return path.join(outputPath, configFileName)
51
- * ```
52
- *
53
- * However, in some cases with more complex project structures, you may need to
54
- * provide a custom implementation to ensure the compiled config file is
55
- * correctly located.
56
- *
57
- * @example
58
- * ```ts
59
- * getCompiledConfigPath: ({ inputRootDir, outputPath, configFileName }) => {
60
- * const projectName = inputRootDir.split('/libs/')[1].split('/')[0];
61
- * const pathAfterProject = inputRootDir.split(`/libs/${projectName}`)[1];
62
- * const compiledConfigFilePath = `${outputPath}/${projectName}${pathAfterProject}`;
63
- * return path.join(compiledConfigFilePath, configFileName);
64
- * },
65
- * ```
66
- */
67
- getCompiledConfigPath?: (params: {
68
- inputRootDir: string;
69
- outputPath: string;
70
- configFileName: string;
71
- }) => string;
72
- /**
73
- * If your project makes use of the TypeScript `paths` configuration, the compiler will
74
- * attempt to use these paths when compiling the Vendure config and its imports.
75
- *
76
- * In certain cases, you may need to transform these paths before they are used. For instance,
77
- * if your project is a monorepo and the paths are defined relative to the root of the monorepo,
78
- * you may need to adjust them to be relative to the output directory where the compiled files are located.
79
- *
80
- * @example
81
- * ```ts
82
- * transformTsConfigPathMappings: ({ phase, patterns }) => {
83
- * // "loading" phase is when the compiled Vendure code is being loaded by
84
- * // the plugin, in order to introspect the configuration of your app.
85
- * if (phase === 'loading') {
86
- * return patterns.map((p) =>
87
- * p.replace('libs/', '').replace(/.ts$/, '.js'),
88
- * );
89
- * }
90
- * return patterns;
91
- * },
92
- * ```
93
- * @param params
94
- */
95
- transformTsConfigPathMappings?: (params: {
96
- phase: 'compiling' | 'loading';
97
- alias: string;
98
- patterns: string[];
99
- }) => string[];
100
- }
101
-
102
- const defaultPathAdapter: Required<PathAdapter> = {
103
- getCompiledConfigPath: ({ outputPath, configFileName }) => path.join(outputPath, configFileName),
104
- transformTsConfigPathMappings: ({ patterns }) => patterns,
105
- };
106
-
107
- export interface ConfigLoaderOptions {
108
- vendureConfigPath: string;
109
- tempDir: string;
110
- pathAdapter?: PathAdapter;
111
- vendureConfigExport?: string;
112
- logger?: Logger;
113
- reportCompilationErrors?: boolean;
114
- }
115
-
116
- export interface LoadVendureConfigResult {
117
- vendureConfig: VendureConfig;
118
- exportedSymbolName: string;
119
- pluginInfo: PluginInfo[];
120
- }
121
-
122
- /**
123
- * @description
124
- * This function compiles the given Vendure config file and any imported relative files (i.e.
125
- * project files, not npm packages) into a temporary directory, and returns the compiled config.
126
- *
127
- * The reason we need to do this is that Vendure code makes use of TypeScript experimental decorators
128
- * (e.g. for NestJS decorators and TypeORM column decorators) which are not supported by esbuild.
129
- *
130
- * In Vite, when we load some TypeScript into the top-level Vite config file (in the end-user project), Vite
131
- * internally uses esbuild to temporarily compile that TypeScript code. Unfortunately, esbuild does not support
132
- * these experimental decorators, errors will be thrown as soon as e.g. a TypeORM column decorator is encountered.
133
- *
134
- * To work around this, we compile the Vendure config file and all its imports using the TypeScript compiler,
135
- * which fully supports these experimental decorators. The compiled files are then loaded by Vite, which is able
136
- * to handle the compiled JavaScript output.
137
- */
138
- export async function loadVendureConfig(options: ConfigLoaderOptions): Promise<LoadVendureConfigResult> {
139
- const { vendureConfigPath, vendureConfigExport, tempDir, pathAdapter } = options;
140
- const getCompiledConfigPath =
141
- pathAdapter?.getCompiledConfigPath ?? defaultPathAdapter.getCompiledConfigPath;
142
- const transformTsConfigPathMappings =
143
- pathAdapter?.transformTsConfigPathMappings ?? defaultPathAdapter.transformTsConfigPathMappings;
144
- const logger = options.logger || defaultLogger;
145
- const outputPath = tempDir;
146
- const configFileName = path.basename(vendureConfigPath);
147
- const inputRootDir = path.dirname(vendureConfigPath);
148
- await fs.remove(outputPath);
149
- const pluginInfo = await compileFile({
150
- inputRootDir,
151
- inputPath: vendureConfigPath,
152
- outputDir: outputPath,
153
- logger,
154
- transformTsConfigPathMappings,
155
- });
156
- const compiledConfigFilePath = pathToFileURL(
157
- getCompiledConfigPath({
158
- inputRootDir,
159
- outputPath,
160
- configFileName,
161
- }),
162
- ).href.replace(/.ts$/, '.js');
163
- // create package.json with type commonjs and save it to the output dir
164
- await fs.writeFile(path.join(outputPath, 'package.json'), JSON.stringify({ type: 'commonjs' }, null, 2));
165
-
166
- // We need to figure out the symbol exported by the config file by
167
- // analyzing the AST and finding an export with the type "VendureConfig"
168
- const sourceFile = ts.createSourceFile(
169
- vendureConfigPath,
170
- await fs.readFile(vendureConfigPath, 'utf-8'),
171
- ts.ScriptTarget.Latest,
172
- true,
173
- );
174
- const detectedExportedSymbolName = findConfigExport(sourceFile);
175
- const configExportedSymbolName = detectedExportedSymbolName || vendureConfigExport;
176
- if (!configExportedSymbolName) {
177
- throw new Error(
178
- `Could not find a variable exported as VendureConfig. Please specify the name of the exported variable using the "vendureConfigExport" option.`,
179
- );
180
- }
181
-
182
- // Register path aliases from tsconfig before importing
183
- const tsConfigInfo = await findTsConfigPaths(
184
- vendureConfigPath,
185
- logger,
186
- 'loading',
187
- transformTsConfigPathMappings,
188
- );
189
- if (tsConfigInfo) {
190
- tsConfigPaths.register({
191
- baseUrl: outputPath,
192
- paths: tsConfigInfo.paths,
193
- });
194
- }
195
-
196
- const config = await import(compiledConfigFilePath).then(m => m[configExportedSymbolName]);
197
- if (!config) {
198
- throw new Error(
199
- `Could not find a variable exported as VendureConfig with the name "${configExportedSymbolName}".`,
200
- );
201
- }
202
- return { vendureConfig: config, exportedSymbolName: configExportedSymbolName, pluginInfo };
203
- }
204
-
205
- /**
206
- * Finds and parses tsconfig files in the given directory and its parent directories.
207
- * Returns the paths configuration if found.
208
- */
209
- async function findTsConfigPaths(
210
- configPath: string,
211
- logger: Logger,
212
- phase: 'compiling' | 'loading',
213
- transformTsConfigPathMappings: Required<PathAdapter>['transformTsConfigPathMappings'],
214
- ): Promise<{ baseUrl: string; paths: Record<string, string[]> } | undefined> {
215
- const configDir = path.dirname(configPath);
216
- let currentDir = configDir;
217
-
218
- while (currentDir !== path.parse(currentDir).root) {
219
- try {
220
- const files = await fs.readdir(currentDir);
221
- const tsConfigFiles = files.filter(file => /^tsconfig(\..*)?\.json$/.test(file));
222
-
223
- for (const fileName of tsConfigFiles) {
224
- const tsConfigPath = path.join(currentDir, fileName);
225
- try {
226
- const tsConfigContent = await fs.readFile(tsConfigPath, 'utf-8');
227
- // Use JSON5 or similar parser if comments are expected in tsconfig.json
228
- // For simplicity, assuming standard JSON here. Handle parse errors.
229
- const tsConfig = JSON.parse(tsConfigContent);
230
- const compilerOptions = tsConfig.compilerOptions || {};
231
-
232
- if (compilerOptions.paths) {
233
- // Determine the effective baseUrl: explicitly set or the directory of tsconfig.json
234
- const tsConfigBaseUrl = path.resolve(currentDir, compilerOptions.baseUrl || '.');
235
- const paths: Record<string, string[]> = {};
236
-
237
- for (const [alias, patterns] of Object.entries(compilerOptions.paths)) {
238
- // Store paths as defined in tsconfig, they will be relative to baseUrl
239
- const normalizedPatterns = (patterns as string[]).map(pattern =>
240
- // Normalize slashes for consistency, keep relative
241
- pattern.replace(/\\/g, '/'),
242
- );
243
- paths[alias] = transformTsConfigPathMappings({
244
- phase,
245
- alias,
246
- patterns: normalizedPatterns,
247
- });
248
- }
249
- logger.debug(
250
- `Found tsconfig paths in ${tsConfigPath}: ${JSON.stringify(
251
- {
252
- baseUrl: tsConfigBaseUrl,
253
- paths,
254
- },
255
- null,
256
- 2,
257
- )}`,
258
- );
259
- return { baseUrl: tsConfigBaseUrl, paths };
260
- }
261
- } catch (e: any) {
262
- logger.warn(
263
- `Could not read or parse tsconfig file ${tsConfigPath}: ${e.message as string}`,
264
- );
265
- }
266
- }
267
- } catch (e: any) {
268
- // If we can't read the directory, just continue to the parent
269
- logger.warn(`Could not read directory ${currentDir}: ${e.message as string}`);
270
- }
271
- currentDir = path.dirname(currentDir);
272
- }
273
- logger.debug(`No tsconfig paths found traversing up from ${configDir}`);
274
- return undefined;
275
- }
276
-
277
- type CompileFileOptions = {
278
- inputRootDir: string;
279
- inputPath: string;
280
- outputDir: string;
281
- transformTsConfigPathMappings: Required<PathAdapter>['transformTsConfigPathMappings'];
282
- logger?: Logger;
283
- compiledFiles?: Set<string>;
284
- isRoot?: boolean;
285
- pluginInfo?: PluginInfo[];
286
- reportCompilationErrors?: boolean;
287
- };
288
-
289
- export async function compileFile({
290
- inputRootDir,
291
- inputPath,
292
- outputDir,
293
- transformTsConfigPathMappings,
294
- logger = defaultLogger,
295
- compiledFiles = new Set<string>(),
296
- isRoot = true,
297
- pluginInfo = [],
298
- reportCompilationErrors = false,
299
- }: CompileFileOptions): Promise<PluginInfo[]> {
300
- const absoluteInputPath = path.resolve(inputPath);
301
- if (compiledFiles.has(absoluteInputPath)) {
302
- return pluginInfo;
303
- }
304
- compiledFiles.add(absoluteInputPath);
305
-
306
- // Ensure output directory exists
307
- await fs.ensureDir(outputDir);
308
-
309
- // Read the source file
310
- const source = await fs.readFile(inputPath, 'utf-8');
311
-
312
- // Parse the source to find relative imports
313
- const sourceFile = ts.createSourceFile(absoluteInputPath, source, ts.ScriptTarget.Latest, true);
314
-
315
- const importPaths = new Set<string>();
316
- let tsConfigInfo: { baseUrl: string; paths: Record<string, string[]> } | undefined;
317
-
318
- if (isRoot) {
319
- tsConfigInfo = await findTsConfigPaths(
320
- absoluteInputPath,
321
- logger,
322
- 'compiling',
323
- transformTsConfigPathMappings,
324
- );
325
- if (tsConfigInfo) {
326
- logger?.debug(`Using TypeScript configuration: ${JSON.stringify(tsConfigInfo, null, 2)}`);
327
- }
328
- }
329
-
330
- async function collectImports(node: ts.Node) {
331
- if (
332
- (ts.isExportDeclaration(node) || ts.isImportDeclaration(node)) &&
333
- node.moduleSpecifier &&
334
- ts.isStringLiteral(node.moduleSpecifier)
335
- ) {
336
- const importPath = node.moduleSpecifier.text;
337
-
338
- // Handle relative imports
339
- if (importPath.startsWith('.')) {
340
- const resolvedPath = path.resolve(path.dirname(absoluteInputPath), importPath);
341
- // TODO: does this handle index files correctly?
342
- let resolvedTsPath = resolvedPath + '.ts';
343
- // Also check for .tsx if .ts doesn't exist
344
- if (!(await fs.pathExists(resolvedTsPath))) {
345
- const resolvedTsxPath = resolvedPath + '.tsx';
346
- if (await fs.pathExists(resolvedTsxPath)) {
347
- resolvedTsPath = resolvedTsxPath;
348
- } else {
349
- // If neither exists, maybe it's an index file?
350
- const resolvedIndexPath = path.join(resolvedPath, 'index.ts');
351
- if (await fs.pathExists(resolvedIndexPath)) {
352
- resolvedTsPath = resolvedIndexPath;
353
- } else {
354
- const resolvedIndexTsxPath = path.join(resolvedPath, 'index.tsx');
355
- if (await fs.pathExists(resolvedIndexTsxPath)) {
356
- resolvedTsPath = resolvedIndexTsxPath;
357
- } else {
358
- // If still not found, log a warning or let TS handle it later
359
- logger?.warn(
360
- `Could not resolve relative import "${importPath}" from "${absoluteInputPath}" to an existing .ts/.tsx file.`,
361
- );
362
- // Do not add to importPaths if we can't verify existence
363
- return;
364
- }
365
- }
366
- }
367
- }
368
- importPaths.add(resolvedTsPath);
369
- }
370
- // Handle path aliases if tsConfigInfo exists
371
- else if (tsConfigInfo) {
372
- // Attempt to resolve using path aliases
373
- let resolved = false;
374
- for (const [alias, patterns] of Object.entries(tsConfigInfo.paths)) {
375
- const aliasPrefix = alias.replace('*', '');
376
- const aliasSuffix = alias.endsWith('*') ? '*' : '';
377
-
378
- if (
379
- importPath.startsWith(aliasPrefix) &&
380
- (aliasSuffix === '*' || importPath === aliasPrefix)
381
- ) {
382
- const remainingImportPath = importPath.slice(aliasPrefix.length);
383
- for (const pattern of patterns) {
384
- const patternPrefix = pattern.replace('*', '');
385
- const patternSuffix = pattern.endsWith('*') ? '*' : '';
386
- // Ensure suffix match consistency (* vs exact)
387
- if (aliasSuffix !== patternSuffix) continue;
388
-
389
- const potentialPathBase = path.resolve(tsConfigInfo.baseUrl, patternPrefix);
390
- const resolvedPath = path.join(potentialPathBase, remainingImportPath);
391
-
392
- let resolvedTsPath = resolvedPath.endsWith('.ts')
393
- ? resolvedPath
394
- : resolvedPath + '.ts';
395
- // Similar existence checks as relative paths
396
- if (!(await fs.pathExists(resolvedTsPath))) {
397
- const resolvedTsxPath = resolvedPath + '.tsx';
398
- if (await fs.pathExists(resolvedTsxPath)) {
399
- resolvedTsPath = resolvedTsxPath;
400
- } else {
401
- const resolvedIndexPath = path.join(resolvedPath, 'index.ts');
402
- if (await fs.pathExists(resolvedIndexPath)) {
403
- resolvedTsPath = resolvedIndexPath;
404
- } else {
405
- const resolvedIndexTsxPath = path.join(resolvedPath, 'index.tsx');
406
- if (await fs.pathExists(resolvedIndexTsxPath)) {
407
- resolvedTsPath = resolvedIndexTsxPath;
408
- } else {
409
- // Path doesn't resolve to a file for this pattern
410
- continue;
411
- }
412
- }
413
- }
414
- }
415
- // Add the first successful resolution for this alias
416
- importPaths.add(resolvedTsPath);
417
- resolved = true;
418
- break; // Stop checking patterns for this alias
419
- }
420
- }
421
- if (resolved) break; // Stop checking other aliases if resolved
422
- }
423
- }
424
- // For all other imports (node_modules, etc), we should still add them to be processed
425
- // by the TypeScript compiler, even if we can't resolve them to a file
426
- else {
427
- // Add the import path as is - TypeScript will handle resolution
428
- // importPaths.add(importPath);
429
- }
430
- } else {
431
- const children = node.getChildren();
432
- for (const child of children) {
433
- // Only process nodes that could contain import statements
434
- if (
435
- ts.isSourceFile(child) ||
436
- ts.isModuleBlock(child) ||
437
- ts.isModuleDeclaration(child) ||
438
- ts.isImportDeclaration(child) ||
439
- ts.isExportDeclaration(child) ||
440
- child.kind === ts.SyntaxKind.SyntaxList
441
- ) {
442
- await collectImports(child);
443
- }
444
- }
445
- }
446
- }
447
-
448
- // Start collecting imports from the source file
449
- await collectImports(sourceFile);
450
-
451
- const extractedPluginInfo = getPluginInfo(sourceFile);
452
- if (extractedPluginInfo) {
453
- pluginInfo.push(extractedPluginInfo);
454
- }
455
-
456
- // Store the tsConfigInfo on the first call if found
457
- const rootTsConfigInfo = isRoot ? tsConfigInfo : undefined;
458
-
459
- // Recursively collect all files that need to be compiled
460
- for (const importPath of importPaths) {
461
- // Pass rootTsConfigInfo down, but set isRoot to false
462
- await compileFile({
463
- inputRootDir,
464
- inputPath: importPath,
465
- outputDir,
466
- logger,
467
- transformTsConfigPathMappings,
468
- compiledFiles,
469
- isRoot: false,
470
- pluginInfo,
471
- });
472
- }
473
-
474
- // If this is the root file (the one that started the compilation),
475
- // use the TypeScript compiler API to compile all files together
476
- if (isRoot) {
477
- logger.info(`Starting compilation for ${compiledFiles.size} files...`);
478
- const allFiles = Array.from(compiledFiles);
479
- const compilerOptions: ts.CompilerOptions = {
480
- // Base options
481
- target: ts.ScriptTarget.ES2020,
482
- module: ts.ModuleKind.CommonJS, // Output CommonJS for Node compatibility
483
- experimentalDecorators: true,
484
- emitDecoratorMetadata: true,
485
- esModuleInterop: true,
486
- skipLibCheck: true, // Faster compilation
487
- forceConsistentCasingInFileNames: true,
488
- moduleResolution: ts.ModuleResolutionKind.NodeJs, // Use Node.js module resolution
489
- incremental: false, // No need for incremental compilation
490
- noEmitOnError: false, // Continue emitting even with errors
491
- isolatedModules: true, // Treat files as separate modules
492
- strict: false, // Disable strict type checking for speed
493
- noUnusedLocals: false, // Skip unused locals check
494
- noUnusedParameters: false, // Skip unused parameters check
495
-
496
- // Output options
497
- outDir: outputDir, // Output directory for all compiled files
498
- sourceMap: false, // Generate source maps
499
- declaration: false, // Don't generate .d.ts files
500
-
501
- // Path resolution options - use info found from tsconfig
502
- baseUrl: rootTsConfigInfo ? rootTsConfigInfo.baseUrl : undefined, // Let TS handle resolution if no baseUrl
503
- paths: rootTsConfigInfo ? rootTsConfigInfo.paths : undefined,
504
- // rootDir: inputRootDir, // Often inferred correctly, can cause issues if set explicitly sometimes
505
- allowJs: true, // Allow JS files if needed, though we primarily collect TS
506
- resolveJsonModule: true, // Allow importing JSON
507
- };
508
-
509
- logger.debug(`compilerOptions: ${JSON.stringify(compilerOptions, null, 2)}`);
510
-
511
- // Create a Program to represent the compilation context
512
- const program = ts.createProgram(allFiles, compilerOptions);
513
- logger.info(`Emitting compiled files to ${outputDir}`);
514
- const emitResult = program.emit();
515
-
516
- if (reportCompilationErrors) {
517
- const hasEmitErrors = reportDiagnostics(program, emitResult, logger);
518
-
519
- if (hasEmitErrors) {
520
- throw new Error('TypeScript compilation failed with errors.');
521
- }
522
- }
523
-
524
- logger.info(`Successfully compiled ${allFiles.length} files to ${outputDir}`);
525
- }
526
- return pluginInfo;
527
- }
528
-
529
- function reportDiagnostics(program: ts.Program, emitResult: ts.EmitResult, logger: Logger) {
530
- const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
531
- let hasEmitErrors = emitResult.emitSkipped;
532
- allDiagnostics.forEach(diagnostic => {
533
- if (diagnostic.file && diagnostic.start) {
534
- const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
535
- const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
536
- const logFn = diagnostic.category === ts.DiagnosticCategory.Error ? logger.warn : logger.info;
537
- // eslint-disable-next-line no-console
538
- console.log(
539
- `TS${diagnostic.code} ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`,
540
- );
541
- if (diagnostic.category === ts.DiagnosticCategory.Error) {
542
- hasEmitErrors = true;
543
- }
544
- } else {
545
- const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
546
- const logFn = diagnostic.category === ts.DiagnosticCategory.Error ? logger.warn : logger.info;
547
- // eslint-disable-next-line no-console
548
- console.log(`TS${diagnostic.code}: ${message}`);
549
- if (diagnostic.category === ts.DiagnosticCategory.Error) {
550
- hasEmitErrors = true;
551
- }
552
- }
553
- });
554
- return hasEmitErrors;
555
- }
@@ -0,0 +1,43 @@
1
+ import { Logger } from '../types.js';
2
+
3
+ // ANSI color codes
4
+ const colors = {
5
+ grey: '\x1b[90m',
6
+ red: '\x1b[31m',
7
+ yellow: '\x1b[33m',
8
+ reset: '\x1b[0m',
9
+ } as const;
10
+
11
+ export const debugLogger: Logger = {
12
+ info: (message: string) => {
13
+ // eslint-disable-next-line no-console
14
+ console.log(`[INFO] ${message}`);
15
+ },
16
+ warn: (message: string) => {
17
+ // eslint-disable-next-line no-console
18
+ console.warn(`${colors.yellow}[WARN] ${message}${colors.reset}`);
19
+ },
20
+ debug: (message: string) => {
21
+ // eslint-disable-next-line no-console
22
+ console.debug(`${colors.grey}[DEBUG] ${message}${colors.reset}`);
23
+ },
24
+ error: (message: string) => {
25
+ // eslint-disable-next-line no-console
26
+ console.error(`${colors.red}[ERROR] ${message}${colors.reset}`);
27
+ },
28
+ };
29
+
30
+ export const noopLogger: Logger = {
31
+ info: () => {
32
+ /* noop */
33
+ },
34
+ warn: () => {
35
+ /* noop */
36
+ },
37
+ debug: () => {
38
+ /* noop */
39
+ },
40
+ error: () => {
41
+ /* noop */
42
+ },
43
+ };