Skip to content

Commit bdcbd40

Browse files
alan-agius4dgp1130
authored andcommitted
refactor: force ESM resolution of rxjs when in server bundles
Rxjs conditional export for node points to the CJS version. This commits adds a resolver to replace the resolved path to the ESM version.
1 parent 565179e commit bdcbd40

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import type { Plugin } from 'esbuild';
10+
11+
const RXJS_ESM_RESOLUTION = Symbol('RXJS_ESM_RESOLUTION');
12+
13+
/**
14+
* Creates a plugin that forces ESM resolution of rxjs.
15+
* This is needed as when targeting node, the CJS version is used to the current package conditional exports.
16+
* @see: https://github.com/ReactiveX/rxjs/blob/2947583bb33e97f3db9e6d9f6cea70c62a173060/package.json#L19.
17+
*
18+
* NOTE: This can be removed when and if rxjs adds an import condition that allows ESM usage on Node.js.
19+
*
20+
* @returns An esbuild plugin.
21+
*/
22+
export function createRxjsEsmResolutionPlugin(): Plugin {
23+
return {
24+
name: 'angular-rxjs-resolution',
25+
setup(build) {
26+
build.onResolve({ filter: /^rxjs/ }, async (args) => {
27+
if (args.pluginData?.[RXJS_ESM_RESOLUTION]) {
28+
return null;
29+
}
30+
31+
const { importer, kind, resolveDir, namespace, pluginData = {} } = args;
32+
pluginData[RXJS_ESM_RESOLUTION] = true;
33+
34+
const result = await build.resolve(args.path, {
35+
importer,
36+
kind,
37+
namespace,
38+
pluginData,
39+
resolveDir,
40+
});
41+
42+
result.path = result.path.replace('/dist/cjs/', '/dist/esm/');
43+
44+
return result;
45+
});
46+
},
47+
};
48+
}

packages/angular_devkit/build_angular/src/tools/esbuild/server-code-bundle.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { NormalizedApplicationBuildOptions } from '../../builders/applicati
1313
import { SourceFileCache, createCompilerPlugin } from './angular/compiler-plugin';
1414
import { createCompilerPluginOptions } from './compiler-plugin';
1515
import { createExternalPackagesPlugin } from './external-packages-plugin';
16+
import { createRxjsEsmResolutionPlugin } from './rxjs-esm-resolution-plugin';
1617
import { createSourcemapIngorelistPlugin } from './sourcemap-ignorelist-plugin';
1718
import { getEsBuildCommonOptions, getFeatureSupport } from './utils';
1819
import { createVirtualModulePlugin } from './virtual-module-plugin';
@@ -96,9 +97,11 @@ export function createServerCodeBundleOptions(
9697
],
9798
};
9899

100+
buildOptions.plugins ??= [];
99101
if (options.externalPackages) {
100-
buildOptions.plugins ??= [];
101102
buildOptions.plugins.push(createExternalPackagesPlugin());
103+
} else {
104+
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
102105
}
103106

104107
return buildOptions;

0 commit comments

Comments
 (0)