Răsfoiți Sursa

fix(dashboard): Do not throw on compilation errors by default

Michael Bromley 7 luni în urmă
părinte
comite
b1df8490d8

+ 48 - 13
packages/dashboard/vite/utils/config-loader.ts

@@ -36,6 +36,7 @@ export interface ConfigLoaderOptions {
     tempDir: string;
     vendureConfigExport?: string;
     logger?: Logger;
+    reportCompilationErrors?: boolean;
 }
 
 export interface LoadVendureConfigResult {
@@ -67,7 +68,12 @@ export async function loadVendureConfig(options: ConfigLoaderOptions): Promise<L
     const configFileName = path.basename(vendureConfigPath);
     const inputRootDir = path.dirname(vendureConfigPath);
     await fs.remove(outputPath);
-    const pluginInfo = await compileFile(inputRootDir, vendureConfigPath, outputPath, logger);
+    const pluginInfo = await compileFile({
+        inputRootDir,
+        inputPath: vendureConfigPath,
+        outputDir: outputPath,
+        logger,
+    });
     const compiledConfigFilePath = pathToFileURL(path.join(outputPath, configFileName)).href.replace(
         /.ts$/,
         '.js',
@@ -147,7 +153,14 @@ async function findTsConfigPaths(
                             );
                         }
                         logger.debug(
-                            `Found tsconfig paths in ${tsConfigPath}: ${JSON.stringify({ baseUrl: tsConfigBaseUrl, paths }, null, 2)}`,
+                            `Found tsconfig paths in ${tsConfigPath}: ${JSON.stringify(
+                                {
+                                    baseUrl: tsConfigBaseUrl,
+                                    paths,
+                                },
+                                null,
+                                2,
+                            )}`,
                         );
                         return { baseUrl: tsConfigBaseUrl, paths };
                     }
@@ -167,15 +180,27 @@ async function findTsConfigPaths(
     return undefined;
 }
 
-export async function compileFile(
-    inputRootDir: string,
-    inputPath: string,
-    outputDir: string,
-    logger: Logger = defaultLogger,
+type CompileFileOptions = {
+    inputRootDir: string;
+    inputPath: string;
+    outputDir: string;
+    logger?: Logger;
+    compiledFiles?: Set<string>;
+    isRoot?: boolean;
+    pluginInfo?: PluginInfo[];
+    reportCompilationErrors?: boolean;
+};
+
+export async function compileFile({
+    inputRootDir,
+    inputPath,
+    outputDir,
+    logger = defaultLogger,
     compiledFiles = new Set<string>(),
     isRoot = true,
-    pluginInfo: PluginInfo[] = [],
-): Promise<PluginInfo[]> {
+    pluginInfo = [],
+    reportCompilationErrors = false,
+}: CompileFileOptions): Promise<PluginInfo[]> {
     const absoluteInputPath = path.resolve(inputPath);
     if (compiledFiles.has(absoluteInputPath)) {
         return pluginInfo;
@@ -325,7 +350,15 @@ export async function compileFile(
     // Recursively collect all files that need to be compiled
     for (const importPath of importPaths) {
         // Pass rootTsConfigInfo down, but set isRoot to false
-        await compileFile(inputRootDir, importPath, outputDir, logger, compiledFiles, false, pluginInfo);
+        await compileFile({
+            inputRootDir,
+            inputPath: importPath,
+            outputDir,
+            logger,
+            compiledFiles,
+            isRoot: false,
+            pluginInfo,
+        });
     }
 
     // If this is the root file (the one that started the compilation),
@@ -370,10 +403,12 @@ export async function compileFile(
         logger.info(`Emitting compiled files to ${outputDir}`);
         const emitResult = program.emit();
 
-        const hasEmitErrors = reportDiagnostics(program, emitResult, logger);
+        if (reportCompilationErrors) {
+            const hasEmitErrors = reportDiagnostics(program, emitResult, logger);
 
-        if (hasEmitErrors) {
-            throw new Error('TypeScript compilation failed with errors.');
+            if (hasEmitErrors) {
+                throw new Error('TypeScript compilation failed with errors.');
+            }
         }
 
         logger.info(`Successfully compiled ${allFiles.length} files to ${outputDir}`);

+ 14 - 4
packages/dashboard/vite/vite-plugin-vendure-dashboard.ts

@@ -1,4 +1,3 @@
-import { lingui } from '@lingui/vite-plugin';
 import tailwindcss from '@tailwindcss/vite';
 import { TanStackRouterVite } from '@tanstack/router-plugin/vite';
 import react from '@vitejs/plugin-react';
@@ -10,8 +9,7 @@ import { configLoaderPlugin } from './vite-plugin-config-loader.js';
 import { viteConfigPlugin } from './vite-plugin-config.js';
 import { dashboardMetadataPlugin } from './vite-plugin-dashboard-metadata.js';
 import { gqlTadaPlugin } from './vite-plugin-gql-tada.js';
-import { themeVariablesPlugin } from './vite-plugin-theme.js';
-import { ThemeVariablesPluginOptions } from './vite-plugin-theme.js';
+import { ThemeVariablesPluginOptions, themeVariablesPlugin } from './vite-plugin-theme.js';
 import { UiConfigPluginOptions, uiConfigPlugin } from './vite-plugin-ui-config.js';
 
 /**
@@ -37,6 +35,14 @@ export type VitePluginVendureDashboardOptions = {
     gqlTadaOutputPath?: string;
     tempCompilationDir?: string;
     disableTansStackRouterPlugin?: boolean;
+    /**
+     * @description
+     * If set to `true`, compilation errors during the build process will be reported and
+     * the build will fail.
+     *
+     * @default false
+     */
+    reportCompilationErrors?: boolean;
 } & UiConfigPluginOptions &
     ThemeVariablesPluginOptions;
 
@@ -74,7 +80,11 @@ export function vendureDashboardPlugin(options: VitePluginVendureDashboardOption
         }),
         themeVariablesPlugin({ theme: options.theme }),
         tailwindcss(),
-        configLoaderPlugin({ vendureConfigPath: normalizedVendureConfigPath, tempDir }),
+        configLoaderPlugin({
+            vendureConfigPath: normalizedVendureConfigPath,
+            tempDir,
+            reportCompilationErrors: options.reportCompilationErrors,
+        }),
         viteConfigPlugin({ packageRoot }),
         adminApiSchemaPlugin(),
         dashboardMetadataPlugin({ rootDir: tempDir }),