Просмотр исходного кода

fix(dashboard): Fix plugin detection from barrel files

Michael Bromley 6 месяцев назад
Родитель
Сommit
aa12ac7801

+ 1 - 0
packages/dashboard/.gitignore

@@ -26,3 +26,4 @@ dist-ssr
 *.sw?
 
 src/app/routeTree.gen.ts
+vite/tests/__temp

+ 17 - 0
packages/dashboard/vite/tests/barrel-exports.spec.ts

@@ -0,0 +1,17 @@
+import { join } from 'path';
+import { describe, expect, it } from 'vitest';
+
+import { loadVendureConfig } from '../utils/config-loader.js';
+
+describe('detecting plugins in barrel exports', () => {
+    it('should detect plugins in barrel exports', async () => {
+        const result = await loadVendureConfig({
+            tempDir: join(__dirname, './__temp'),
+            vendureConfigPath: join(__dirname, 'barrel-exports', 'vendure-config.ts'),
+        });
+
+        expect(result.pluginInfo).toHaveLength(1);
+        expect(result.pluginInfo[0].name).toBe('MyPlugin');
+        expect(result.pluginInfo[0].dashboardEntryPath).toBe('./dashboard/index.tsx');
+    });
+});

+ 1 - 0
packages/dashboard/vite/tests/barrel-exports/my-plugin/index.ts

@@ -0,0 +1 @@
+export * from './src/my.plugin';

+ 8 - 0
packages/dashboard/vite/tests/barrel-exports/my-plugin/src/my.plugin.ts

@@ -0,0 +1,8 @@
+import { PluginCommonModule, VendurePlugin } from '@vendure/core';
+
+@VendurePlugin({
+    imports: [PluginCommonModule],
+    providers: [],
+    dashboard: './dashboard/index.tsx',
+})
+export class MyPlugin {}

+ 6 - 0
packages/dashboard/vite/tests/barrel-exports/package.json

@@ -0,0 +1,6 @@
+{
+    "type": "commonjs",
+    "name": "barrel-exports",
+    "version": "0.0.1", 
+    "main": "index.ts"
+}

+ 19 - 0
packages/dashboard/vite/tests/barrel-exports/vendure-config.ts

@@ -0,0 +1,19 @@
+import { VendureConfig } from '@vendure/core';
+
+import { MyPlugin } from './my-plugin';
+
+export const config: VendureConfig = {
+    apiOptions: {
+        port: 3000,
+    },
+    authOptions: {
+        tokenMethod: 'bearer',
+    },
+    dbConnectionOptions: {
+        type: 'postgres',
+    },
+    paymentOptions: {
+        paymentMethodHandlers: [],
+    },
+    plugins: [MyPlugin],
+};

+ 10 - 2
packages/dashboard/vite/utils/config-loader.ts

@@ -328,12 +328,17 @@ export async function compileFile({
     }
 
     async function collectImports(node: ts.Node) {
-        if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
+        if (
+            (ts.isExportDeclaration(node) || ts.isImportDeclaration(node)) &&
+            node.moduleSpecifier &&
+            ts.isStringLiteral(node.moduleSpecifier)
+        ) {
             const importPath = node.moduleSpecifier.text;
 
             // Handle relative imports
             if (importPath.startsWith('.')) {
                 const resolvedPath = path.resolve(path.dirname(absoluteInputPath), importPath);
+                // TODO: does this handle index files correctly?
                 let resolvedTsPath = resolvedPath + '.ts';
                 // Also check for .tsx if .ts doesn't exist
                 if (!(await fs.pathExists(resolvedTsPath))) {
@@ -384,7 +389,9 @@ export async function compileFile({
                             const potentialPathBase = path.resolve(tsConfigInfo.baseUrl, patternPrefix);
                             const resolvedPath = path.join(potentialPathBase, remainingImportPath);
 
-                            let resolvedTsPath = resolvedPath + '.ts';
+                            let resolvedTsPath = resolvedPath.endsWith('.ts')
+                                ? resolvedPath
+                                : resolvedPath + '.ts';
                             // Similar existence checks as relative paths
                             if (!(await fs.pathExists(resolvedTsPath))) {
                                 const resolvedTsxPath = resolvedPath + '.tsx';
@@ -429,6 +436,7 @@ export async function compileFile({
                     ts.isModuleBlock(child) ||
                     ts.isModuleDeclaration(child) ||
                     ts.isImportDeclaration(child) ||
+                    ts.isExportDeclaration(child) ||
                     child.kind === ts.SyntaxKind.SyntaxList
                 ) {
                     await collectImports(child);