Skip to content

Commit

Permalink
fix(@angular/build): automatically resolve .mjs files when using Vite
Browse files Browse the repository at this point in the history
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 2324d5a)
  • Loading branch information
alan-agius4 committed Jun 13, 2024
1 parent df783bb commit 405c148
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ function getDepOptimizationConfig({
supported: getFeatureSupport(target, zoneless),
plugins,
loader,
resolveExtensions: ['.mjs', '.js', '.cjs'],
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/build/src/tools/esbuild/global-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 405c148

Please sign in to comment.