Skip to content

Commit

Permalink
fix(js): @nx/js:init ensures tslib is installed if importHelpers is t…
Browse files Browse the repository at this point in the history
…rue (#28083)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
Sometimes tslib is missing even though it is needed for typechecks.

## Expected Behavior
tslib is installed when needed. i.e. `importHelpers: true`

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27656
  • Loading branch information
jaysoo authored and FrozenPandaz committed Sep 26, 2024
1 parent 25c6f2c commit 9e51589
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
30 changes: 15 additions & 15 deletions e2e/eslint/src/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ describe('Linter', () => {
it('should report dependency check issues', () => {
const rootPackageJson = readJson('package.json');
const nxVersion = rootPackageJson.devDependencies.nx;
const tslibVersion = rootPackageJson.dependencies['tslib'];
const tslibVersion =
rootPackageJson.dependencies['tslib'] ||
rootPackageJson.devDependencies['tslib'];

let out = runCLI(`lint ${mylib}`, {
silenceError: true,
Expand Down Expand Up @@ -535,20 +537,18 @@ describe('Linter', () => {
`Successfully ran target lint for project ${mylib}`
);
const packageJson = readJson(`libs/${mylib}/package.json`);
expect(packageJson).toMatchInlineSnapshot(`
{
"dependencies": {
"@nx/devkit": "${nxVersion}",
"tslib": "${tslibVersion}",
},
"main": "./src/index.js",
"name": "@proj/${mylib}",
"private": true,
"type": "commonjs",
"typings": "./src/index.d.ts",
"version": "0.0.1",
}
`);
expect(packageJson).toMatchObject({
dependencies: {
'@nx/devkit': nxVersion,
tslib: tslibVersion,
},
main: './src/index.js',
name: `@proj/${mylib}`,
private: true,
type: 'commonjs',
typings: './src/index.d.ts',
version: '0.0.1',
});

// intentionally set the invalid version
updateJson(`libs/${mylib}/package.json`, (json) => {
Expand Down
27 changes: 27 additions & 0 deletions packages/js/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,31 @@ describe('js init generator', () => {
expect(tree.exists('.prettierignore')).toBeFalsy();
expect(tree.exists('.prettierrc')).toBeFalsy();
});

it.each`
fileName | importHelpers | shouldAdd
${'tsconfig.json'} | ${true} | ${true}
${'tsconfig.base.json'} | ${true} | ${true}
${'tsconfig.json'} | ${false} | ${false}
${'tsconfig.base.json'} | ${false} | ${false}
${null} | ${false} | ${false}
`(
'should add tslib if importHelpers is true in base tsconfig',
async ({ fileName, importHelpers, shouldAdd }) => {
if (fileName) {
writeJson(tree, fileName, {
compilerOptions: {
importHelpers,
},
});
}

await init(tree, {
addTsConfigBase: false,
});

const packageJson = readJson(tree, 'package.json');
expect(!!packageJson.devDependencies?.['tslib']).toBe(shouldAdd);
}
);
});
11 changes: 11 additions & 0 deletions packages/js/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
swcCoreVersion,
swcHelpersVersion,
swcNodeVersion,
tsLibVersion,
typescriptVersion,
} from '../../utils/versions';
import { InitSchema } from './schema';
Expand Down Expand Up @@ -154,6 +155,16 @@ export async function initGeneratorInternal(
});
}

const rootTsConfigFileName = getRootTsConfigFileName(tree);
// If the root tsconfig file uses `importHelpers` then we must install tslib
// in order to run tsc for build and typecheck.
if (rootTsConfigFileName) {
const rootTsConfig = readJson(tree, rootTsConfigFileName);
if (rootTsConfig.compilerOptions?.importHelpers) {
devDependencies['tslib'] = tsLibVersion;
}
}

const installTask = !schema.skipPackageJson
? addDependenciesToPackageJson(
tree,
Expand Down
21 changes: 0 additions & 21 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ export async function libraryGeneratorInternal(
]);
}

if (options.bundler !== 'none') {
addBundlerDependencies(tree, options);
}

if (!options.skipFormat) {
await formatFiles(tree);
}
Expand Down Expand Up @@ -413,23 +409,6 @@ export async function addLint(
return task;
}

function addBundlerDependencies(tree: Tree, options: NormalizedSchema) {
updateJson(tree, `${options.projectRoot}/package.json`, (json) => {
if (options.bundler === 'tsc') {
json.dependencies = {
...json.dependencies,
tslib: tsLibVersion,
};
} else if (options.bundler === 'swc') {
json.dependencies = {
...json.dependencies,
'@swc/helpers': swcHelpersVersion,
};
}
return json;
});
}

function updateTsConfig(tree: Tree, options: NormalizedSchema) {
updateJson(tree, join(options.projectRoot, 'tsconfig.json'), (json) => {
if (options.strict) {
Expand Down

0 comments on commit 9e51589

Please sign in to comment.