Skip to content

Commit

Permalink
fix(js): handle case where tslib or @swc/helpers are missing from ext…
Browse files Browse the repository at this point in the history
…ernalNodes (#22523)
  • Loading branch information
jaysoo authored Mar 26, 2024
1 parent 7914496 commit 82dc703
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
67 changes: 67 additions & 0 deletions packages/js/src/utils/find-npm-dependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,73 @@ describe('findNpmDependencies', () => {
});
});

it('should handle missing ts/swc helper packages from externalNodes', () => {
vol.fromJSON(
{
'./nx.json': JSON.stringify(nxJson),
'./libs/my-lib/tsconfig.json': JSON.stringify({
compilerOptions: {
importHelpers: true,
},
}),
'./libs/my-lib/.swcrc': JSON.stringify({
jsc: {
externalHelpers: true,
},
}),
},
'/root'
);
const libWithHelpers = {
name: 'my-lib',
type: 'lib' as const,
data: {
root: 'libs/my-lib',
targets: {
build1: {
executor: '@nx/js:tsc',
options: {
tsConfig: 'libs/my-lib/tsconfig.json',
},
},
build2: {
executor: '@nx/js:swc',
options: {},
},
},
},
};
const projectGraph = {
nodes: {
'my-lib': libWithHelpers,
},
externalNodes: {},
dependencies: {},
};
const projectFileMap = {
'my-lib': [],
};

expect(
findNpmDependencies(
'/root',
libWithHelpers,
projectGraph,
projectFileMap,
'build1'
)
).toEqual({});
expect(
findNpmDependencies(
'/root',
libWithHelpers,
projectGraph,
projectFileMap,
'build2'
)
).toEqual({});
});

it('should not pick up helper npm dependencies if not required', () => {
vol.fromJSON(
{
Expand Down
14 changes: 10 additions & 4 deletions packages/js/src/utils/find-npm-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ function collectHelperDependencies(

if (target.executor === '@nx/js:tsc' && target.options?.tsConfig) {
const tsConfig = readTsConfig(join(workspaceRoot, target.options.tsConfig));
if (tsConfig?.options['importHelpers']) {
npmDeps['tslib'] = projectGraph.externalNodes['npm:tslib']?.data.version;
if (
tsConfig?.options['importHelpers'] &&
projectGraph.externalNodes['npm:tslib']?.type === 'npm'
) {
npmDeps['tslib'] = projectGraph.externalNodes['npm:tslib'].data.version;
}
}
if (target.executor === '@nx/js:swc') {
Expand All @@ -212,9 +215,12 @@ function collectHelperDependencies(
const swcConfig = fileExists(swcConfigPath)
? readJsonFile(swcConfigPath)
: {};
if (swcConfig?.jsc?.externalHelpers) {
if (
swcConfig?.jsc?.externalHelpers &&
projectGraph.externalNodes['npm:@swc/helpers']?.type === 'npm'
) {
npmDeps['@swc/helpers'] =
projectGraph.externalNodes['npm:@swc/helpers']?.data.version;
projectGraph.externalNodes['npm:@swc/helpers'].data.version;
}
}
}

0 comments on commit 82dc703

Please sign in to comment.