Skip to content

Commit

Permalink
fix(js): @nx/js:init ensures tslib is installed if importHelpers is true
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Sep 25, 2024
1 parent 04cf92a commit 9be5ec0
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 @@ -506,7 +506,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 @@ -547,20 +549,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 @@ -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,
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 @@ -170,10 +170,6 @@ export async function libraryGeneratorInternal(
]);
}

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

if (!options.skipFormat) {
await formatFiles(tree);
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9be5ec0

Please sign in to comment.