Skip to content

Commit

Permalink
fix(js): do not generate package.json file for non-buildable js libra…
Browse files Browse the repository at this point in the history
…ries in non-ts solution setup (#29646)

## Current Behavior

When generating a non-buildable js library in a workspace using the
integrated setup, a `package.json` file is generated.

## Expected Behavior

When generating a non-buildable js library in a workspace using the
integrated setup, a `package.json` file should not be generated.

## Related Issue(s)

Fixes #

(cherry picked from commit ad96cc1)
  • Loading branch information
leosvelperez authored and FrozenPandaz committed Jan 17, 2025
1 parent 00b0b38 commit a963fa9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
22 changes: 14 additions & 8 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,19 @@ describe('lib', () => {
});
});

describe('--bundler=none', () => {
it('should not generate a package.json when bundler is none', async () => {
await libraryGenerator(tree, {
...defaultOptions,
directory: 'my-lib',
bundler: 'none',
unitTestRunner: 'none',
});

expect(tree.exists('my-lib/package.json')).toBe(false);
});
});

describe('--minimal', () => {
it('should generate a README.md when minimal is set to false', async () => {
await libraryGenerator(tree, {
Expand Down Expand Up @@ -1602,14 +1615,7 @@ describe('lib', () => {
},
}
`);
expect(readJson(tree, 'my-lib/package.json')).toMatchInlineSnapshot(`
{
"dependencies": {},
"name": "@proj/my-lib",
"private": true,
"version": "0.0.1",
}
`);
expect(tree.exists('my-lib/package.json')).toBe(false);
});
});

Expand Down
7 changes: 7 additions & 0 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
};
return json;
});
} else if (
!options.isUsingTsSolutionConfig &&
options.useProjectJson &&
(!options.bundler || options.bundler === 'none') &&
!(options.projectRoot === '.')
) {
tree.delete(packageJsonPath);
}

if (options.minimal && !(options.projectRoot === '.')) {
Expand Down
17 changes: 9 additions & 8 deletions packages/node/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,21 @@ function ensureDependencies(tree: Tree): GeneratorCallback {
}

function updatePackageJson(tree: Tree, options: NormalizedSchema) {
const packageJson = readJson(
tree,
joinPathFragments(options.projectRoot, 'package.json')
const packageJsonPath = joinPathFragments(
options.projectRoot,
'package.json'
);
if (!tree.exists(packageJsonPath)) {
return;
}

const packageJson = readJson(tree, packageJsonPath);

if (packageJson.type === 'module') {
// The @nx/js:lib generator can set the type to 'module' which would
// potentially break consumers of the library.
delete packageJson.type;
}

writeJson(
tree,
joinPathFragments(options.projectRoot, 'package.json'),
packageJson
);
writeJson(tree, packageJsonPath, packageJson);
}

0 comments on commit a963fa9

Please sign in to comment.