From 49a15e1098eaffd042a40a8c81049711acc14ac4 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 24 Sep 2024 13:08:32 -0400 Subject: [PATCH] fix(js): @nx/js:init ensures tslib is installed if importHelpers is true --- e2e/eslint/src/linter.test.ts | 26 +++++++++--------- packages/js/src/generators/init/init.spec.ts | 27 +++++++++++++++++++ packages/js/src/generators/init/init.ts | 11 ++++++++ packages/js/src/generators/library/library.ts | 21 --------------- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/e2e/eslint/src/linter.test.ts b/e2e/eslint/src/linter.test.ts index 84f00c9b6ce09f..03bc9e978cddfd 100644 --- a/e2e/eslint/src/linter.test.ts +++ b/e2e/eslint/src/linter.test.ts @@ -547,20 +547,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) => { diff --git a/packages/js/src/generators/init/init.spec.ts b/packages/js/src/generators/init/init.spec.ts index b6c7a9fb33e11c..a7470593658b22 100644 --- a/packages/js/src/generators/init/init.spec.ts +++ b/packages/js/src/generators/init/init.spec.ts @@ -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); + } + ); }); diff --git a/packages/js/src/generators/init/init.ts b/packages/js/src/generators/init/init.ts index 17a89b7f60723b..854ce6bb805ac1 100644 --- a/packages/js/src/generators/init/init.ts +++ b/packages/js/src/generators/init/init.ts @@ -21,6 +21,7 @@ import { swcCoreVersion, swcHelpersVersion, swcNodeVersion, + tsLibVersion, typescriptVersion, } from '../../utils/versions'; import { InitSchema } from './schema'; @@ -110,6 +111,16 @@ export async function initGeneratorInternal( tasks.push(prettierTask); } + 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, diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index aacafe2b8e03ec..845a65daa746b0 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -170,10 +170,6 @@ export async function libraryGeneratorInternal( ]); } - if (options.bundler !== 'none') { - addBundlerDependencies(tree, options); - } - if (!options.skipFormat) { await formatFiles(tree); } @@ -411,23 +407,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) {