@vendure/dashboard 3.2.2 → 3.2.4

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 (92) hide show
  1. package/dist/plugin/utils/ast-utils.d.ts +10 -0
  2. package/dist/plugin/utils/ast-utils.js +96 -0
  3. package/dist/plugin/utils/ast-utils.spec.d.ts +1 -0
  4. package/dist/plugin/utils/ast-utils.spec.js +120 -0
  5. package/dist/plugin/{config-loader.d.ts → utils/config-loader.d.ts} +22 -8
  6. package/dist/plugin/utils/config-loader.js +325 -0
  7. package/dist/plugin/{schema-generator.d.ts → utils/schema-generator.d.ts} +5 -0
  8. package/dist/plugin/{schema-generator.js → utils/schema-generator.js} +6 -0
  9. package/dist/plugin/{ui-config.js → utils/ui-config.js} +2 -2
  10. package/dist/plugin/vite-plugin-admin-api-schema.js +2 -2
  11. package/dist/plugin/vite-plugin-config-loader.d.ts +2 -3
  12. package/dist/plugin/vite-plugin-config-loader.js +18 -9
  13. package/dist/plugin/vite-plugin-dashboard-metadata.js +12 -14
  14. package/dist/plugin/vite-plugin-gql-tada.js +2 -2
  15. package/dist/plugin/vite-plugin-ui-config.js +3 -2
  16. package/package.json +8 -6
  17. package/src/app/app-providers.tsx +8 -8
  18. package/src/app/main.tsx +1 -1
  19. package/src/app/routes/_authenticated/_assets/assets.graphql.ts +26 -0
  20. package/src/app/routes/_authenticated/_assets/assets.tsx +2 -2
  21. package/src/app/routes/_authenticated/_assets/assets_.$id.tsx +156 -0
  22. package/src/app/routes/_authenticated/_orders/components/customer-address-selector.tsx +104 -0
  23. package/src/app/routes/_authenticated/_orders/components/edit-order-table.tsx +228 -0
  24. package/src/app/routes/_authenticated/_orders/components/money-gross-net.tsx +18 -0
  25. package/src/app/routes/_authenticated/_orders/components/order-address.tsx +2 -1
  26. package/src/app/routes/_authenticated/_orders/components/order-line-custom-fields-form.tsx +38 -0
  27. package/src/app/routes/_authenticated/_orders/components/order-table-totals.tsx +53 -0
  28. package/src/app/routes/_authenticated/_orders/components/order-table.tsx +8 -49
  29. package/src/app/routes/_authenticated/_orders/components/shipping-method-selector.tsx +65 -0
  30. package/src/app/routes/_authenticated/_orders/orders.graphql.ts +187 -1
  31. package/src/app/routes/_authenticated/_orders/orders.tsx +39 -18
  32. package/src/app/routes/_authenticated/_orders/orders_.$id.tsx +31 -9
  33. package/src/app/routes/_authenticated/_orders/orders_.draft.$id.tsx +418 -0
  34. package/src/app/routes/_authenticated/_products/products.tsx +1 -1
  35. package/src/app/routes/_authenticated.tsx +12 -1
  36. package/src/lib/components/data-table/add-filter-menu.tsx +61 -0
  37. package/src/lib/components/data-table/data-table-column-header.tsx +0 -13
  38. package/src/lib/components/data-table/data-table-filter-badge.tsx +75 -0
  39. package/src/lib/components/data-table/data-table-filter-dialog.tsx +27 -28
  40. package/src/lib/components/data-table/data-table-types.ts +1 -0
  41. package/src/lib/components/data-table/data-table-view-options.tsx +72 -23
  42. package/src/lib/components/data-table/data-table.tsx +23 -24
  43. package/src/lib/components/data-table/filters/data-table-boolean-filter.tsx +57 -0
  44. package/src/lib/components/data-table/filters/data-table-datetime-filter.tsx +93 -0
  45. package/src/lib/components/data-table/filters/data-table-id-filter.tsx +58 -0
  46. package/src/lib/components/data-table/filters/data-table-number-filter.tsx +119 -0
  47. package/src/lib/components/data-table/filters/data-table-string-filter.tsx +62 -0
  48. package/src/lib/components/data-table/human-readable-operator.tsx +65 -0
  49. package/src/lib/components/layout/nav-user.tsx +4 -4
  50. package/src/lib/components/shared/asset/asset-focal-point-editor.tsx +93 -0
  51. package/src/lib/components/shared/{asset-gallery.tsx → asset/asset-gallery.tsx} +51 -20
  52. package/src/lib/components/shared/{asset-picker-dialog.tsx → asset/asset-picker-dialog.tsx} +1 -1
  53. package/src/lib/components/shared/{asset-preview-dialog.tsx → asset/asset-preview-dialog.tsx} +1 -7
  54. package/src/lib/components/shared/asset/asset-preview-selector.tsx +34 -0
  55. package/src/lib/components/shared/asset/asset-preview.tsx +128 -0
  56. package/src/lib/components/shared/asset/asset-properties.tsx +46 -0
  57. package/src/lib/components/shared/{focal-point-control.tsx → asset/focal-point-control.tsx} +1 -1
  58. package/src/lib/components/shared/custom-fields-form.tsx +4 -3
  59. package/src/lib/components/shared/customer-selector.tsx +13 -14
  60. package/src/lib/components/shared/detail-page-button.tsx +2 -2
  61. package/src/lib/components/shared/entity-assets.tsx +3 -3
  62. package/src/lib/components/shared/navigation-confirmation.tsx +39 -0
  63. package/src/lib/components/shared/paginated-list-data-table.tsx +9 -1
  64. package/src/lib/components/shared/product-variant-selector.tsx +111 -0
  65. package/src/lib/components/shared/vendure-image.tsx +1 -1
  66. package/src/lib/components/ui/calendar.tsx +508 -63
  67. package/src/lib/framework/document-introspection/get-document-structure.spec.ts +113 -3
  68. package/src/lib/framework/document-introspection/get-document-structure.ts +70 -11
  69. package/src/lib/framework/form-engine/use-generated-form.tsx +8 -7
  70. package/src/lib/framework/layout-engine/page-layout.tsx +4 -0
  71. package/src/lib/framework/page/list-page.tsx +23 -4
  72. package/src/lib/framework/page/use-detail-page.ts +1 -0
  73. package/src/lib/graphql/fragments.tsx +8 -0
  74. package/src/lib/index.ts +5 -5
  75. package/src/lib/providers/auth.tsx +12 -9
  76. package/src/lib/providers/channel-provider.tsx +1 -0
  77. package/src/lib/providers/server-config.tsx +7 -1
  78. package/src/lib/providers/user-settings.tsx +24 -0
  79. package/vite/utils/ast-utils.spec.ts +128 -0
  80. package/vite/utils/ast-utils.ts +119 -0
  81. package/vite/utils/config-loader.ts +410 -0
  82. package/vite/{schema-generator.ts → utils/schema-generator.ts} +7 -1
  83. package/vite/{ui-config.ts → utils/ui-config.ts} +2 -2
  84. package/vite/vite-plugin-admin-api-schema.ts +2 -2
  85. package/vite/vite-plugin-config-loader.ts +25 -13
  86. package/vite/vite-plugin-dashboard-metadata.ts +19 -15
  87. package/vite/vite-plugin-gql-tada.ts +2 -2
  88. package/vite/vite-plugin-ui-config.ts +3 -2
  89. package/dist/plugin/config-loader.js +0 -141
  90. package/src/lib/components/shared/asset-preview.tsx +0 -345
  91. package/vite/config-loader.ts +0 -181
  92. /package/dist/plugin/{ui-config.d.ts → utils/ui-config.d.ts} +0 -0
@@ -0,0 +1,128 @@
1
+ import ts from 'typescript';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ import { getPluginInfo, findConfigExport } from './ast-utils.js';
5
+
6
+ describe('getPluginInfo', () => {
7
+ it('should return undefined when no plugin class is found', () => {
8
+ const sourceText = `
9
+ class NotAPlugin {
10
+ constructor() {}
11
+ }
12
+ `;
13
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
14
+ const result = getPluginInfo(sourceFile);
15
+ expect(result).toBeUndefined();
16
+ });
17
+
18
+ it('should return plugin info when a valid plugin class is found', () => {
19
+ const sourceText = `
20
+ @VendurePlugin({
21
+ imports: [],
22
+ providers: []
23
+ })
24
+ class TestPlugin {
25
+ constructor() {}
26
+ }
27
+ `;
28
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
29
+ const result = getPluginInfo(sourceFile);
30
+ expect(result).toEqual({
31
+ name: 'TestPlugin',
32
+ pluginPath: 'path/to',
33
+ dashboardEntryPath: undefined,
34
+ });
35
+ });
36
+
37
+ it('should handle multiple classes but only return the plugin one', () => {
38
+ const sourceText = `
39
+ class NotAPlugin {
40
+ constructor() {}
41
+ }
42
+
43
+ @VendurePlugin({
44
+ imports: [],
45
+ providers: []
46
+ })
47
+ class TestPlugin {
48
+ constructor() {}
49
+ }
50
+
51
+ class AnotherClass {
52
+ constructor() {}
53
+ }
54
+ `;
55
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
56
+ const result = getPluginInfo(sourceFile);
57
+ expect(result).toEqual({
58
+ name: 'TestPlugin',
59
+ pluginPath: 'path/to',
60
+ dashboardEntryPath: undefined,
61
+ });
62
+ });
63
+
64
+ it('should determine the dashboard entry path when it is provided', () => {
65
+ const sourceText = `
66
+ @VendurePlugin({
67
+ imports: [],
68
+ providers: [],
69
+ dashboard: './dashboard/index.tsx',
70
+ })
71
+ class TestPlugin {
72
+ constructor() {}
73
+ }
74
+ `;
75
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
76
+ const result = getPluginInfo(sourceFile);
77
+ expect(result).toEqual({
78
+ name: 'TestPlugin',
79
+ pluginPath: 'path/to',
80
+ dashboardEntryPath: './dashboard/index.tsx',
81
+ });
82
+ });
83
+ });
84
+
85
+ describe('findConfigExport', () => {
86
+ it('should return undefined when no VendureConfig export is found', () => {
87
+ const sourceText = `
88
+ export const notConfig = {
89
+ some: 'value'
90
+ };
91
+ `;
92
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
93
+ const result = findConfigExport(sourceFile);
94
+ expect(result).toBeUndefined();
95
+ });
96
+
97
+ it('should find exported variable with VendureConfig type', () => {
98
+ const sourceText = `
99
+ import { VendureConfig } from '@vendure/core';
100
+
101
+ export const config: VendureConfig = {
102
+ authOptions: {
103
+ tokenMethod: 'bearer'
104
+ }
105
+ };
106
+ `;
107
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
108
+ const result = findConfigExport(sourceFile);
109
+ expect(result).toBe('config');
110
+ });
111
+
112
+ it('should find exported variable with VendureConfig type among other exports', () => {
113
+ const sourceText = `
114
+ import { VendureConfig } from '@vendure/core';
115
+
116
+ export const otherExport = 'value';
117
+ export const config: VendureConfig = {
118
+ authOptions: {
119
+ tokenMethod: 'bearer'
120
+ }
121
+ };
122
+ export const anotherExport = 123;
123
+ `;
124
+ const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
125
+ const result = findConfigExport(sourceFile);
126
+ expect(result).toBe('config');
127
+ });
128
+ });
@@ -0,0 +1,119 @@
1
+ import path from 'path';
2
+ import ts from 'typescript';
3
+
4
+ import { PluginInfo } from './config-loader.js';
5
+
6
+ /**
7
+ * Get the plugin info from the source file.
8
+ */
9
+ export function getPluginInfo(sourceFile: ts.SourceFile): PluginInfo | undefined {
10
+ const classDeclaration = sourceFile.statements.find(statement => {
11
+ return (
12
+ statement.kind === ts.SyntaxKind.ClassDeclaration &&
13
+ statement.getText().includes('@VendurePlugin(')
14
+ );
15
+ });
16
+ if (classDeclaration) {
17
+ const identifier = classDeclaration.getChildren().find(child => {
18
+ return child.kind === ts.SyntaxKind.Identifier;
19
+ });
20
+ const dashboardEntryPath = classDeclaration
21
+ .getChildren()
22
+ .map(child => {
23
+ if (child.kind === ts.SyntaxKind.SyntaxList) {
24
+ const pluginDecorator = child.getChildren().find(_child => {
25
+ return _child.kind === ts.SyntaxKind.Decorator;
26
+ });
27
+ if (pluginDecorator) {
28
+ const callExpression = findFirstDescendantOfKind(
29
+ pluginDecorator,
30
+ ts.SyntaxKind.CallExpression,
31
+ );
32
+ if (callExpression) {
33
+ const objectLiteral = findFirstDescendantOfKind(
34
+ callExpression,
35
+ ts.SyntaxKind.ObjectLiteralExpression,
36
+ );
37
+ if (objectLiteral && ts.isObjectLiteralExpression(objectLiteral)) {
38
+ // Now find the specific 'dashboard' property
39
+ const dashboardProperty = objectLiteral.properties.find(
40
+ prop =>
41
+ ts.isPropertyAssignment(prop) && prop.name?.getText() === 'dashboard',
42
+ );
43
+
44
+ if (
45
+ dashboardProperty &&
46
+ ts.isPropertyAssignment(dashboardProperty) &&
47
+ ts.isStringLiteral(dashboardProperty.initializer)
48
+ ) {
49
+ const dashboardPath = dashboardProperty.initializer.text;
50
+ return dashboardPath;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ })
57
+ .filter(Boolean)?.[0];
58
+ if (identifier) {
59
+ return {
60
+ name: identifier.getText(),
61
+ pluginPath: path.dirname(sourceFile.fileName),
62
+ dashboardEntryPath,
63
+ };
64
+ }
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Given the AST of a TypeScript file, finds the name of the variable exported as VendureConfig.
70
+ */
71
+ export function findConfigExport(sourceFile: ts.SourceFile): string | undefined {
72
+ let exportedSymbolName: string | undefined;
73
+
74
+ function visit(node: ts.Node) {
75
+ if (
76
+ ts.isVariableStatement(node) &&
77
+ node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)
78
+ ) {
79
+ node.declarationList.declarations.forEach(declaration => {
80
+ if (ts.isVariableDeclaration(declaration)) {
81
+ const typeNode = declaration.type;
82
+ if (typeNode && ts.isTypeReferenceNode(typeNode)) {
83
+ const typeName = typeNode.typeName;
84
+ if (ts.isIdentifier(typeName) && typeName.text === 'VendureConfig') {
85
+ if (ts.isIdentifier(declaration.name)) {
86
+ exportedSymbolName = declaration.name.text;
87
+ }
88
+ }
89
+ }
90
+ }
91
+ });
92
+ }
93
+ ts.forEachChild(node, visit);
94
+ }
95
+
96
+ visit(sourceFile);
97
+ return exportedSymbolName;
98
+ }
99
+
100
+ function findFirstDescendantOfKind(node: ts.Node, kind: ts.SyntaxKind): ts.Node | undefined {
101
+ let foundNode: ts.Node | undefined;
102
+
103
+ function visit(_node: ts.Node) {
104
+ if (foundNode) {
105
+ // Stop searching if we already found it
106
+ return;
107
+ }
108
+ if (_node.kind === kind) {
109
+ foundNode = _node;
110
+ return;
111
+ }
112
+ // Recursively visit children
113
+ ts.forEachChild(_node, visit);
114
+ }
115
+
116
+ // Start the traversal from the initial node's children
117
+ ts.forEachChild(node, visit);
118
+ return foundNode;
119
+ }
@@ -0,0 +1,410 @@
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
+ export interface ConfigLoaderOptions {
35
+ vendureConfigPath: string;
36
+ tempDir: string;
37
+ vendureConfigExport?: string;
38
+ logger?: Logger;
39
+ }
40
+
41
+ export interface LoadVendureConfigResult {
42
+ vendureConfig: VendureConfig;
43
+ exportedSymbolName: string;
44
+ pluginInfo: PluginInfo[];
45
+ }
46
+
47
+ /**
48
+ * @description
49
+ * This function compiles the given Vendure config file and any imported relative files (i.e.
50
+ * project files, not npm packages) into a temporary directory, and returns the compiled config.
51
+ *
52
+ * The reason we need to do this is that Vendure code makes use of TypeScript experimental decorators
53
+ * (e.g. for NestJS decorators and TypeORM column decorators) which are not supported by esbuild.
54
+ *
55
+ * In Vite, when we load some TypeScript into the top-level Vite config file (in the end-user project), Vite
56
+ * internally uses esbuild to temporarily compile that TypeScript code. Unfortunately, esbuild does not support
57
+ * these experimental decorators, errors will be thrown as soon as e.g. a TypeORM column decorator is encountered.
58
+ *
59
+ * To work around this, we compile the Vendure config file and all its imports using the TypeScript compiler,
60
+ * which fully supports these experimental decorators. The compiled files are then loaded by Vite, which is able
61
+ * to handle the compiled JavaScript output.
62
+ */
63
+ export async function loadVendureConfig(options: ConfigLoaderOptions): Promise<LoadVendureConfigResult> {
64
+ const { vendureConfigPath, vendureConfigExport, tempDir } = options;
65
+ const logger = options.logger || defaultLogger;
66
+ const outputPath = tempDir;
67
+ const configFileName = path.basename(vendureConfigPath);
68
+ const inputRootDir = path.dirname(vendureConfigPath);
69
+ await fs.remove(outputPath);
70
+ const pluginInfo = await compileFile(inputRootDir, vendureConfigPath, outputPath, logger);
71
+ const compiledConfigFilePath = pathToFileURL(path.join(outputPath, configFileName)).href.replace(
72
+ /.ts$/,
73
+ '.js',
74
+ );
75
+ // create package.json with type commonjs and save it to the output dir
76
+ await fs.writeFile(path.join(outputPath, 'package.json'), JSON.stringify({ type: 'commonjs' }, null, 2));
77
+
78
+ // We need to figure out the symbol exported by the config file by
79
+ // analyzing the AST and finding an export with the type "VendureConfig"
80
+ const sourceFile = ts.createSourceFile(
81
+ vendureConfigPath,
82
+ await fs.readFile(vendureConfigPath, 'utf-8'),
83
+ ts.ScriptTarget.Latest,
84
+ true,
85
+ );
86
+ const detectedExportedSymbolName = findConfigExport(sourceFile);
87
+ const configExportedSymbolName = detectedExportedSymbolName || vendureConfigExport;
88
+ if (!configExportedSymbolName) {
89
+ throw new Error(
90
+ `Could not find a variable exported as VendureConfig. Please specify the name of the exported variable using the "vendureConfigExport" option.`,
91
+ );
92
+ }
93
+
94
+ // Register path aliases from tsconfig before importing
95
+ const tsConfigInfo = await findTsConfigPaths(vendureConfigPath, logger);
96
+ if (tsConfigInfo) {
97
+ tsConfigPaths.register({
98
+ baseUrl: outputPath,
99
+ paths: tsConfigInfo.paths,
100
+ });
101
+ }
102
+
103
+ const config = await import(compiledConfigFilePath).then(m => m[configExportedSymbolName]);
104
+ if (!config) {
105
+ throw new Error(
106
+ `Could not find a variable exported as VendureConfig with the name "${configExportedSymbolName}".`,
107
+ );
108
+ }
109
+ return { vendureConfig: config, exportedSymbolName: configExportedSymbolName, pluginInfo };
110
+ }
111
+
112
+ /**
113
+ * Finds and parses tsconfig files in the given directory and its parent directories.
114
+ * Returns the paths configuration if found.
115
+ */
116
+ async function findTsConfigPaths(
117
+ configPath: string,
118
+ logger: Logger,
119
+ ): Promise<{ baseUrl: string; paths: Record<string, string[]> } | undefined> {
120
+ const configDir = path.dirname(configPath);
121
+ let currentDir = configDir;
122
+
123
+ while (currentDir !== path.parse(currentDir).root) {
124
+ try {
125
+ const files = await fs.readdir(currentDir);
126
+ const tsConfigFiles = files.filter(file => /^tsconfig(\..*)?\.json$/.test(file));
127
+
128
+ for (const fileName of tsConfigFiles) {
129
+ const tsConfigPath = path.join(currentDir, fileName);
130
+ try {
131
+ const tsConfigContent = await fs.readFile(tsConfigPath, 'utf-8');
132
+ // Use JSON5 or similar parser if comments are expected in tsconfig.json
133
+ // For simplicity, assuming standard JSON here. Handle parse errors.
134
+ const tsConfig = JSON.parse(tsConfigContent);
135
+ const compilerOptions = tsConfig.compilerOptions || {};
136
+
137
+ if (compilerOptions.paths) {
138
+ // Determine the effective baseUrl: explicitly set or the directory of tsconfig.json
139
+ const tsConfigBaseUrl = path.resolve(currentDir, compilerOptions.baseUrl || '.');
140
+ const paths: Record<string, string[]> = {};
141
+
142
+ for (const [alias, patterns] of Object.entries(compilerOptions.paths)) {
143
+ // Store paths as defined in tsconfig, they will be relative to baseUrl
144
+ paths[alias] = (patterns as string[]).map(pattern =>
145
+ // Normalize slashes for consistency, keep relative
146
+ pattern.replace(/\\/g, '/'),
147
+ );
148
+ }
149
+ logger.debug(
150
+ `Found tsconfig paths in ${tsConfigPath}: ${JSON.stringify({ baseUrl: tsConfigBaseUrl, paths }, null, 2)}`,
151
+ );
152
+ return { baseUrl: tsConfigBaseUrl, paths };
153
+ }
154
+ } catch (e: any) {
155
+ logger.warn(
156
+ `Could not read or parse tsconfig file ${tsConfigPath}: ${e.message as string}`,
157
+ );
158
+ }
159
+ }
160
+ } catch (e: any) {
161
+ // If we can't read the directory, just continue to the parent
162
+ logger.warn(`Could not read directory ${currentDir}: ${e.message as string}`);
163
+ }
164
+ currentDir = path.dirname(currentDir);
165
+ }
166
+ logger.debug(`No tsconfig paths found traversing up from ${configDir}`);
167
+ return undefined;
168
+ }
169
+
170
+ export async function compileFile(
171
+ inputRootDir: string,
172
+ inputPath: string,
173
+ outputDir: string,
174
+ logger: Logger = defaultLogger,
175
+ compiledFiles = new Set<string>(),
176
+ isRoot = true,
177
+ pluginInfo: PluginInfo[] = [],
178
+ ): Promise<PluginInfo[]> {
179
+ const absoluteInputPath = path.resolve(inputPath);
180
+ if (compiledFiles.has(absoluteInputPath)) {
181
+ return pluginInfo;
182
+ }
183
+ compiledFiles.add(absoluteInputPath);
184
+
185
+ // Ensure output directory exists
186
+ await fs.ensureDir(outputDir);
187
+
188
+ // Read the source file
189
+ const source = await fs.readFile(inputPath, 'utf-8');
190
+
191
+ // Parse the source to find relative imports
192
+ const sourceFile = ts.createSourceFile(absoluteInputPath, source, ts.ScriptTarget.Latest, true);
193
+
194
+ const importPaths = new Set<string>();
195
+ let tsConfigInfo: { baseUrl: string; paths: Record<string, string[]> } | undefined;
196
+
197
+ if (isRoot) {
198
+ tsConfigInfo = await findTsConfigPaths(absoluteInputPath, logger);
199
+ if (tsConfigInfo) {
200
+ logger?.debug(`Using TypeScript configuration: ${JSON.stringify(tsConfigInfo, null, 2)}`);
201
+ }
202
+ }
203
+
204
+ async function collectImports(node: ts.Node) {
205
+ if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
206
+ const importPath = node.moduleSpecifier.text;
207
+
208
+ // Handle relative imports
209
+ if (importPath.startsWith('.')) {
210
+ const resolvedPath = path.resolve(path.dirname(absoluteInputPath), importPath);
211
+ let resolvedTsPath = resolvedPath + '.ts';
212
+ // Also check for .tsx if .ts doesn't exist
213
+ if (!(await fs.pathExists(resolvedTsPath))) {
214
+ const resolvedTsxPath = resolvedPath + '.tsx';
215
+ if (await fs.pathExists(resolvedTsxPath)) {
216
+ resolvedTsPath = resolvedTsxPath;
217
+ } else {
218
+ // If neither exists, maybe it's an index file?
219
+ const resolvedIndexPath = path.join(resolvedPath, 'index.ts');
220
+ if (await fs.pathExists(resolvedIndexPath)) {
221
+ resolvedTsPath = resolvedIndexPath;
222
+ } else {
223
+ const resolvedIndexTsxPath = path.join(resolvedPath, 'index.tsx');
224
+ if (await fs.pathExists(resolvedIndexTsxPath)) {
225
+ resolvedTsPath = resolvedIndexTsxPath;
226
+ } else {
227
+ // If still not found, log a warning or let TS handle it later
228
+ logger?.warn(
229
+ `Could not resolve relative import "${importPath}" from "${absoluteInputPath}" to an existing .ts/.tsx file.`,
230
+ );
231
+ // Do not add to importPaths if we can't verify existence
232
+ return;
233
+ }
234
+ }
235
+ }
236
+ }
237
+ importPaths.add(resolvedTsPath);
238
+ }
239
+ // Handle path aliases if tsConfigInfo exists
240
+ else if (tsConfigInfo) {
241
+ // Attempt to resolve using path aliases
242
+ let resolved = false;
243
+ for (const [alias, patterns] of Object.entries(tsConfigInfo.paths)) {
244
+ const aliasPrefix = alias.replace('*', '');
245
+ const aliasSuffix = alias.endsWith('*') ? '*' : '';
246
+
247
+ if (
248
+ importPath.startsWith(aliasPrefix) &&
249
+ (aliasSuffix === '*' || importPath === aliasPrefix)
250
+ ) {
251
+ const remainingImportPath = importPath.slice(aliasPrefix.length);
252
+ for (const pattern of patterns) {
253
+ const patternPrefix = pattern.replace('*', '');
254
+ const patternSuffix = pattern.endsWith('*') ? '*' : '';
255
+ // Ensure suffix match consistency (* vs exact)
256
+ if (aliasSuffix !== patternSuffix) continue;
257
+
258
+ const potentialPathBase = path.resolve(tsConfigInfo.baseUrl, patternPrefix);
259
+ const resolvedPath = path.join(potentialPathBase, remainingImportPath);
260
+
261
+ let resolvedTsPath = resolvedPath + '.ts';
262
+ // Similar existence checks as relative paths
263
+ if (!(await fs.pathExists(resolvedTsPath))) {
264
+ const resolvedTsxPath = resolvedPath + '.tsx';
265
+ if (await fs.pathExists(resolvedTsxPath)) {
266
+ resolvedTsPath = resolvedTsxPath;
267
+ } else {
268
+ const resolvedIndexPath = path.join(resolvedPath, 'index.ts');
269
+ if (await fs.pathExists(resolvedIndexPath)) {
270
+ resolvedTsPath = resolvedIndexPath;
271
+ } else {
272
+ const resolvedIndexTsxPath = path.join(resolvedPath, 'index.tsx');
273
+ if (await fs.pathExists(resolvedIndexTsxPath)) {
274
+ resolvedTsPath = resolvedIndexTsxPath;
275
+ } else {
276
+ // Path doesn't resolve to a file for this pattern
277
+ continue;
278
+ }
279
+ }
280
+ }
281
+ }
282
+ // Add the first successful resolution for this alias
283
+ importPaths.add(resolvedTsPath);
284
+ resolved = true;
285
+ break; // Stop checking patterns for this alias
286
+ }
287
+ }
288
+ if (resolved) break; // Stop checking other aliases if resolved
289
+ }
290
+ }
291
+ // For all other imports (node_modules, etc), we should still add them to be processed
292
+ // by the TypeScript compiler, even if we can't resolve them to a file
293
+ else {
294
+ // Add the import path as is - TypeScript will handle resolution
295
+ // importPaths.add(importPath);
296
+ }
297
+ } else {
298
+ const children = node.getChildren();
299
+ for (const child of children) {
300
+ // Only process nodes that could contain import statements
301
+ if (
302
+ ts.isSourceFile(child) ||
303
+ ts.isModuleBlock(child) ||
304
+ ts.isModuleDeclaration(child) ||
305
+ ts.isImportDeclaration(child) ||
306
+ child.kind === ts.SyntaxKind.SyntaxList
307
+ ) {
308
+ await collectImports(child);
309
+ }
310
+ }
311
+ }
312
+ }
313
+
314
+ // Start collecting imports from the source file
315
+ await collectImports(sourceFile);
316
+
317
+ const extractedPluginInfo = getPluginInfo(sourceFile);
318
+ if (extractedPluginInfo) {
319
+ pluginInfo.push(extractedPluginInfo);
320
+ }
321
+
322
+ // Store the tsConfigInfo on the first call if found
323
+ const rootTsConfigInfo = isRoot ? tsConfigInfo : undefined;
324
+
325
+ // Recursively collect all files that need to be compiled
326
+ for (const importPath of importPaths) {
327
+ // Pass rootTsConfigInfo down, but set isRoot to false
328
+ await compileFile(inputRootDir, importPath, outputDir, logger, compiledFiles, false, pluginInfo);
329
+ }
330
+
331
+ // If this is the root file (the one that started the compilation),
332
+ // use the TypeScript compiler API to compile all files together
333
+ if (isRoot) {
334
+ logger.info(`Starting compilation for ${compiledFiles.size} files...`);
335
+ const allFiles = Array.from(compiledFiles);
336
+ const compilerOptions: ts.CompilerOptions = {
337
+ // Base options
338
+ target: ts.ScriptTarget.ES2020,
339
+ module: ts.ModuleKind.CommonJS, // Output CommonJS for Node compatibility
340
+ experimentalDecorators: true,
341
+ emitDecoratorMetadata: true,
342
+ esModuleInterop: true,
343
+ skipLibCheck: true, // Faster compilation
344
+ forceConsistentCasingInFileNames: true,
345
+ moduleResolution: ts.ModuleResolutionKind.NodeJs, // Use Node.js module resolution
346
+ incremental: false, // No need for incremental compilation
347
+ noEmitOnError: false, // Continue emitting even with errors
348
+ isolatedModules: true, // Treat files as separate modules
349
+ strict: false, // Disable strict type checking for speed
350
+ noUnusedLocals: false, // Skip unused locals check
351
+ noUnusedParameters: false, // Skip unused parameters check
352
+
353
+ // Output options
354
+ outDir: outputDir, // Output directory for all compiled files
355
+ sourceMap: false, // Generate source maps
356
+ declaration: false, // Don't generate .d.ts files
357
+
358
+ // Path resolution options - use info found from tsconfig
359
+ baseUrl: rootTsConfigInfo ? rootTsConfigInfo.baseUrl : undefined, // Let TS handle resolution if no baseUrl
360
+ paths: rootTsConfigInfo ? rootTsConfigInfo.paths : undefined,
361
+ // rootDir: inputRootDir, // Often inferred correctly, can cause issues if set explicitly sometimes
362
+ allowJs: true, // Allow JS files if needed, though we primarily collect TS
363
+ resolveJsonModule: true, // Allow importing JSON
364
+ };
365
+
366
+ logger.debug(`compilerOptions: ${JSON.stringify(compilerOptions, null, 2)}`);
367
+
368
+ // Create a Program to represent the compilation context
369
+ const program = ts.createProgram(allFiles, compilerOptions);
370
+ logger.info(`Emitting compiled files to ${outputDir}`);
371
+ const emitResult = program.emit();
372
+
373
+ const hasEmitErrors = reportDiagnostics(program, emitResult, logger);
374
+
375
+ if (hasEmitErrors) {
376
+ throw new Error('TypeScript compilation failed with errors.');
377
+ }
378
+
379
+ logger.info(`Successfully compiled ${allFiles.length} files to ${outputDir}`);
380
+ }
381
+ return pluginInfo;
382
+ }
383
+
384
+ function reportDiagnostics(program: ts.Program, emitResult: ts.EmitResult, logger: Logger) {
385
+ const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
386
+ let hasEmitErrors = emitResult.emitSkipped;
387
+ allDiagnostics.forEach(diagnostic => {
388
+ if (diagnostic.file && diagnostic.start) {
389
+ const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
390
+ const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
391
+ const logFn = diagnostic.category === ts.DiagnosticCategory.Error ? logger.warn : logger.info;
392
+ // eslint-disable-next-line no-console
393
+ console.log(
394
+ `TS${diagnostic.code} ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`,
395
+ );
396
+ if (diagnostic.category === ts.DiagnosticCategory.Error) {
397
+ hasEmitErrors = true;
398
+ }
399
+ } else {
400
+ const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
401
+ const logFn = diagnostic.category === ts.DiagnosticCategory.Error ? logger.warn : logger.info;
402
+ // eslint-disable-next-line no-console
403
+ console.log(`TS${diagnostic.code}: ${message}`);
404
+ if (diagnostic.category === ts.DiagnosticCategory.Error) {
405
+ hasEmitErrors = true;
406
+ }
407
+ }
408
+ });
409
+ return hasEmitErrors;
410
+ }
@@ -11,14 +11,20 @@ import {
11
11
  import { buildSchema } from 'graphql';
12
12
  import { GraphQLSchema } from 'graphql';
13
13
 
14
- let schemaPromise: Promise<GraphQLSchema>;
14
+ let schemaPromise: Promise<GraphQLSchema> | undefined;
15
15
 
16
+ /**
17
+ * @description
18
+ * This function generates a GraphQL schema from the Vendure config.
19
+ * It is used to generate the schema for the dashboard.
20
+ */
16
21
  export async function generateSchema({
17
22
  vendureConfig,
18
23
  }: {
19
24
  vendureConfig: VendureConfig;
20
25
  }): Promise<GraphQLSchema> {
21
26
  if (!schemaPromise) {
27
+ /* eslint-disable-next-line @typescript-eslint/no-misused-promises */
22
28
  schemaPromise = new Promise(async (resolve, reject) => {
23
29
  resetConfig();
24
30
  await setConfig(vendureConfig ?? {});