From 4153a6ecf6707729a4f0c6a620eeb8d6916588df Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 11 Sep 2024 12:05:01 +0000 Subject: [PATCH] fix(@angular/build): prevent transformation of Node.js internal dependencies by Vite This commit excludes Node.js module imports from being processed by Vite when prebundling is enabled. Closes #28390 --- .../src/builders/dev-server/vite-server.ts | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/angular/build/src/builders/dev-server/vite-server.ts b/packages/angular/build/src/builders/dev-server/vite-server.ts index 9706b94d36c6..cd9a07e81fc8 100644 --- a/packages/angular/build/src/builders/dev-server/vite-server.ts +++ b/packages/angular/build/src/builders/dev-server/vite-server.ts @@ -42,6 +42,11 @@ interface OutputFileRecord { type: BuildOutputFileType; } +interface DevServerExternalResultMetadata extends Omit { + explicitBrowser: string[]; + explicitServer: string[]; +} + export type BuilderAction = ( options: ApplicationBuilderInternalOptions, context: BuilderContext, @@ -140,10 +145,11 @@ export async function* serveWithVite( let hadError = false; const generatedFiles = new Map(); const assetFiles = new Map(); - const externalMetadata: ExternalResultMetadata = { + const externalMetadata: DevServerExternalResultMetadata = { implicitBrowser: [], implicitServer: [], - explicit: [], + explicitBrowser: [], + explicitServer: [], }; // Add cleanup logic via a builder teardown. @@ -236,17 +242,20 @@ export async function* serveWithVite( } // Empty Arrays to avoid growing unlimited with every re-build. - externalMetadata.explicit.length = 0; + externalMetadata.explicitBrowser.length = 0; + externalMetadata.explicitServer.length = 0; externalMetadata.implicitServer.length = 0; externalMetadata.implicitBrowser.length = 0; - externalMetadata.explicit.push(...explicit); + externalMetadata.explicitBrowser.push(...explicit); + externalMetadata.explicitServer.push(...explicit, ...nodeJsBuiltinModules); externalMetadata.implicitServer.push(...implicitServerFiltered); externalMetadata.implicitBrowser.push(...implicitBrowserFiltered); // The below needs to be sorted as Vite uses these options are part of the hashing invalidation algorithm. // See: https://github.com/vitejs/vite/blob/0873bae0cfe0f0718ad2f5743dd34a17e4ab563d/packages/vite/src/node/optimizer/index.ts#L1203-L1239 - externalMetadata.explicit.sort(); + externalMetadata.explicitBrowser.sort(); + externalMetadata.explicitServer.sort(); externalMetadata.implicitServer.sort(); externalMetadata.implicitBrowser.sort(); } @@ -494,7 +503,7 @@ export async function setupServer( outputFiles: Map, assets: Map, preserveSymlinks: boolean | undefined, - externalMetadata: ExternalResultMetadata, + externalMetadata: DevServerExternalResultMetadata, ssr: boolean, prebundleTransformer: JavaScriptTransformer, target: string[], @@ -573,18 +582,18 @@ export async function setupServer( }, // This is needed when `externalDependencies` is used to prevent Vite load errors. // NOTE: If Vite adds direct support for externals, this can be removed. - preTransformRequests: externalMetadata.explicit.length === 0, + preTransformRequests: externalMetadata.explicitBrowser.length === 0, }, ssr: { // Note: `true` and `/.*/` have different sematics. When true, the `external` option is ignored. noExternal: /.*/, // Exclude any Node.js built in module and provided dependencies (currently build defined externals) - external: externalMetadata.explicit, + external: externalMetadata.explicitServer, optimizeDeps: getDepOptimizationConfig({ // Only enable with caching since it causes prebundle dependencies to be cached disabled: serverOptions.prebundle === false, // Exclude any explicitly defined dependencies (currently build defined externals and node.js built-ins) - exclude: externalMetadata.explicit, + exclude: externalMetadata.explicitServer, // Include all implict dependencies from the external packages internal option include: externalMetadata.implicitServer, ssr: true, @@ -603,19 +612,19 @@ export async function setupServer( outputFiles, assets, ssr, - external: externalMetadata.explicit, + external: externalMetadata.explicitBrowser, indexHtmlTransformer, extensionMiddleware, normalizePath, }), - createRemoveIdPrefixPlugin(externalMetadata.explicit), + createRemoveIdPrefixPlugin(externalMetadata.explicitBrowser), ], // Browser only optimizeDeps. (This does not run for SSR dependencies). optimizeDeps: getDepOptimizationConfig({ // Only enable with caching since it causes prebundle dependencies to be cached disabled: serverOptions.prebundle === false, // Exclude any explicitly defined dependencies (currently build defined externals) - exclude: externalMetadata.explicit, + exclude: externalMetadata.explicitBrowser, // Include all implict dependencies from the external packages internal option include: externalMetadata.implicitBrowser, ssr: false,