From 405c1480917d50c677be178c817b845f30cc8cce Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 13 Jun 2024 19:47:41 +0000 Subject: [PATCH] fix(@angular/build): automatically resolve `.mjs` files when using Vite Previously, ESM file resolution without extensions failed when using Vite, causing issues in module loading. This commit addresses the problem by automatically resolving `.mjs` files, aligning the behavior with the application builder and ensuring consistent module resolution across different build tools. **NB**: This is a workaround as valid ESM imports should always have an extension. Closes #27841 (cherry picked from commit 2324d5a2ecedad4330a9cb0e9e66909f95d2ea98) --- packages/angular/build/src/builders/dev-server/vite-server.ts | 1 + .../angular/build/src/tools/esbuild/application-code-bundle.ts | 2 +- packages/angular/build/src/tools/esbuild/global-scripts.ts | 2 +- 3 files changed, 3 insertions(+), 2 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 1b97ea661c70..c39395dfa4a9 100644 --- a/packages/angular/build/src/builders/dev-server/vite-server.ts +++ b/packages/angular/build/src/builders/dev-server/vite-server.ts @@ -675,6 +675,7 @@ function getDepOptimizationConfig({ supported: getFeatureSupport(target, zoneless), plugins, loader, + resolveExtensions: ['.mjs', '.js', '.cjs'], }, }; } diff --git a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts index 696784fd3702..27cc5cccbaf0 100644 --- a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts +++ b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts @@ -360,7 +360,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu format: 'esm', assetNames: outputNames.media, conditions: ['es2020', 'es2015', 'module'], - resolveExtensions: ['.ts', '.tsx', '.mjs', '.js'], + resolveExtensions: ['.ts', '.tsx', '.mjs', '.js', '.cjs'], metafile: true, legalComments: options.extractLicenses ? 'none' : 'eof', logLevel: options.verbose && !jsonLogs ? 'debug' : 'silent', diff --git a/packages/angular/build/src/tools/esbuild/global-scripts.ts b/packages/angular/build/src/tools/esbuild/global-scripts.ts index 7fa5c4d9fb80..2c178c29953a 100644 --- a/packages/angular/build/src/tools/esbuild/global-scripts.ts +++ b/packages/angular/build/src/tools/esbuild/global-scripts.ts @@ -63,7 +63,7 @@ export function createGlobalScriptsBundleOptions( assetNames: outputNames.media, mainFields: ['script', 'browser', 'main'], conditions: ['script'], - resolveExtensions: ['.mjs', '.js'], + resolveExtensions: ['.mjs', '.js', '.cjs'], logLevel: options.verbose && !jsonLogs ? 'debug' : 'silent', metafile: true, minify: optimizationOptions.scripts,