Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): do not generate package.json file for non-buildable js libraries in non-ts solution setup #29646

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -685,6 +685,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);
}
Loading