fix(js): detect helpers correctly when pnpm external nodes are suffixed with version (#17694)

This commit is contained in:
Jack Hsu 2023-06-21 14:16:52 -04:00 committed by GitHub
parent 99265e404b
commit 335c2d5160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 10 deletions

View File

@ -133,12 +133,9 @@ describe('js e2e', () => {
const rootPackageJson = readJson(`package.json`); const rootPackageJson = readJson(`package.json`);
expect( expect(readJson(`dist/libs/${lib}/package.json`)).toHaveProperty(
satisfies( 'peerDependencies.tslib'
readJson(`dist/libs/${lib}/package.json`).peerDependencies.tslib, );
rootPackageJson.dependencies.tslib
)
).toBeTruthy();
updateJson(`libs/${lib}/tsconfig.json`, (json) => { updateJson(`libs/${lib}/tsconfig.json`, (json) => {
json.compilerOptions = { ...json.compilerOptions, importHelpers: false }; json.compilerOptions = { ...json.compilerOptions, importHelpers: false };

View File

@ -0,0 +1,44 @@
import {
getHelperDependency,
HelperDependency,
} from './compiler-helper-dependency';
import { join } from 'path';
describe('getHelperDependency', () => {
it('should support pnpm external nodes where the name is suffixed with the version', () => {
const helperDependency = HelperDependency.tsc;
const configPath = join(__dirname, 'test-fixtures', 'tsconfig.json');
const dependencies = [];
const projectGraph = {
nodes: {},
externalNodes: {
'tslib@2.0.0': {
name: 'npm:tslib@2.0.0' as const,
type: 'npm' as const,
data: {
packageName: 'tslib',
version: '2.0.0',
},
},
},
dependencies: {},
};
const result = getHelperDependency(
helperDependency,
configPath,
dependencies,
projectGraph
);
expect(result).toEqual({
name: 'npm:tslib',
outputs: [],
node: {
name: 'npm:tslib@2.0.0',
type: 'npm',
data: { packageName: 'tslib', version: '2.0.0' },
},
});
});
});

View File

@ -1,7 +1,8 @@
import { import {
logger, logger,
ProjectGraph, type ProjectGraph,
ProjectGraphDependency, type ProjectGraphDependency,
type ProjectGraphExternalNode,
readJsonFile, readJsonFile,
} from '@nx/devkit'; } from '@nx/devkit';
import { DependentBuildableProjectNode } from './buildable-libs-utils'; import { DependentBuildableProjectNode } from './buildable-libs-utils';
@ -38,6 +39,7 @@ const jsExecutors = {
* @param {HelperDependency} helperDependency * @param {HelperDependency} helperDependency
* @param {string} configPath * @param {string} configPath
* @param {DependentBuildableProjectNode[]} dependencies * @param {DependentBuildableProjectNode[]} dependencies
* @param {ProjectGraph} projectGraph
* @param {boolean=false} returnDependencyIfFound * @param {boolean=false} returnDependencyIfFound
*/ */
export function getHelperDependency( export function getHelperDependency(
@ -71,7 +73,18 @@ export function getHelperDependency(
if (!isHelperNeeded) return null; if (!isHelperNeeded) return null;
const libNode = projectGraph.externalNodes[helperDependency]; let libNode: ProjectGraphExternalNode | null = projectGraph[helperDependency];
// If libNode is not found due to the version suffix from pnpm lockfile, try to match it by package name.
if (!libNode) {
for (const nodeName of Object.keys(projectGraph.externalNodes)) {
const node = projectGraph.externalNodes[nodeName];
if (`npm:${node.data.packageName}` === helperDependency) {
libNode = node;
break;
}
}
}
if (!libNode) { if (!libNode) {
logger.warn( logger.warn(

View File

@ -0,0 +1,5 @@
{
"compilerOptions": {
"importHelpers": true
}
}

View File

@ -9,7 +9,7 @@
"exclude": [ "exclude": [
"**/*.spec.ts", "**/*.spec.ts",
"**/*.test.ts", "**/*.test.ts",
"./src/utils/typescript/test-fixtures/**/*.ts", "./src/**/test-fixtures/**/*",
"jest.config.ts" "jest.config.ts"
], ],
"include": ["**/*.ts"] "include": ["**/*.ts"]