Skip to content

Commit 70e10dd

Browse files
jaysooFrozenPandaz
authored andcommitted
fix(js): non-buildable js libs specify type=module (#29620)
In the new TS solution setup, non-buildable libraries should still`type: module`. This can lead to problem since ESM is used but the type will default to CJS. This PR also updates `nx sync` for TS references such that transitive deps are not sycned by default, unless `NX_ENABLE_TS_SYNC_TRANSITIVE_DEPENDENCIES=true` env var is setup. Previously, the transitive deps are synced unless the env var disables it. There isn't a good reason to enable it by default, and it is much cleaner to not sync by default. This means that if we have libs `a`, `b`, and `c`, where `a -> b` and `b -> c` dependency edges are formed, then running: ``` nx sync ``` Will update `a/tsconfig.json` to contain refs to `b` but not `c`. Whereas: ``` NX_ENABLE_TS_SYNC_TRANSITIVE_DEPENDENCIES=true nx sync ``` Will update `a/tsconfig.json` to contain refs to both `b` and `c`. <!-- 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 `type` is missing ## Expected Behavior `type: module` is set in `package.json` ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 311204a commit 70e10dd

File tree

4 files changed

+62
-131
lines changed

4 files changed

+62
-131
lines changed

packages/js/src/generators/library/library.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,7 @@ describe('lib', () => {
17081708
"main": "./src/index.ts",
17091709
"name": "@proj/my-ts-lib",
17101710
"private": true,
1711+
"type": "module",
17111712
"types": "./src/index.ts",
17121713
"version": "0.0.1",
17131714
}
@@ -1722,6 +1723,7 @@ describe('lib', () => {
17221723
"main": "./src/index.js",
17231724
"name": "@proj/my-js-lib",
17241725
"private": true,
1726+
"type": "module",
17251727
"types": "./src/index.js",
17261728
"version": "0.0.1",
17271729
}
@@ -1870,5 +1872,35 @@ describe('lib', () => {
18701872
tree.read('my-ts-lib/src/lib/my-ts-lib.spec.ts', 'utf-8')
18711873
).toContain(`import { myTsLib } from './my-ts-lib.js';`);
18721874
});
1875+
1876+
it('should generate non-buildable setup', async () => {
1877+
await libraryGenerator(tree, {
1878+
...defaultOptions,
1879+
directory: 'my-lib',
1880+
bundler: 'none',
1881+
unitTestRunner: 'none',
1882+
linter: 'none',
1883+
});
1884+
1885+
expect(readJson(tree, 'my-lib/package.json')).toMatchInlineSnapshot(`
1886+
{
1887+
"dependencies": {},
1888+
"exports": {
1889+
".": {
1890+
"default": "./src/index.ts",
1891+
"import": "./src/index.ts",
1892+
"types": "./src/index.ts",
1893+
},
1894+
"./package.json": "./package.json",
1895+
},
1896+
"main": "./src/index.ts",
1897+
"name": "@proj/my-lib",
1898+
"private": true,
1899+
"type": "module",
1900+
"types": "./src/index.ts",
1901+
"version": "0.0.1",
1902+
}
1903+
`);
1904+
});
18731905
});
18741906
});

packages/js/src/generators/library/library.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,7 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
499499
createProjectTsConfigs(tree, options);
500500

501501
let fileNameImport = options.fileName;
502-
if (
503-
options.bundler === 'vite' ||
504-
(options.isUsingTsSolutionConfig && options.bundler !== 'none')
505-
) {
502+
if (options.bundler === 'vite' || options.isUsingTsSolutionConfig) {
506503
const tsConfig = readTsConfigFromTree(
507504
tree,
508505
join(options.projectRoot, 'tsconfig.lib.json')
@@ -613,10 +610,9 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
613610
...determineEntryFields(options),
614611
};
615612

616-
if (
617-
options.isUsingTsSolutionConfig &&
618-
!['none', 'rollup', 'vite'].includes(options.bundler)
619-
) {
613+
if (options.bundler === 'none') {
614+
updatedPackageJson.type = 'module';
615+
} else if (options.bundler !== 'vite' && options.bundler !== 'rollup') {
620616
return getUpdatedPackageJsonContent(updatedPackageJson, {
621617
main: join(options.projectRoot, 'src/index.ts'),
622618
outputPath: joinPathFragments(options.projectRoot, 'dist'),
@@ -646,19 +642,20 @@ function createFiles(tree: Tree, options: NormalizedLibraryGeneratorOptions) {
646642
packageJson.files = ['dist', '!**/*.tsbuildinfo'];
647643
}
648644

649-
if (
650-
options.isUsingTsSolutionConfig &&
651-
!['none', 'rollup', 'vite'].includes(options.bundler)
652-
) {
653-
packageJson = getUpdatedPackageJsonContent(packageJson, {
654-
main: join(options.projectRoot, 'src/index.ts'),
655-
outputPath: joinPathFragments(options.projectRoot, 'dist'),
656-
projectRoot: options.projectRoot,
657-
rootDir: join(options.projectRoot, 'src'),
658-
generateExportsField: true,
659-
packageJsonPath,
660-
format: ['esm'],
661-
});
645+
if (options.isUsingTsSolutionConfig) {
646+
if (options.bundler === 'none') {
647+
packageJson.type = 'module';
648+
} else if (options.bundler !== 'vite' && options.bundler !== 'rollup') {
649+
packageJson = getUpdatedPackageJsonContent(packageJson, {
650+
main: join(options.projectRoot, 'src/index.ts'),
651+
outputPath: joinPathFragments(options.projectRoot, 'dist'),
652+
projectRoot: options.projectRoot,
653+
rootDir: join(options.projectRoot, 'src'),
654+
generateExportsField: true,
655+
packageJsonPath,
656+
format: ['esm'],
657+
});
658+
}
662659
}
663660

664661
writeJson<PackageJson>(tree, packageJsonPath, packageJson);

0 commit comments

Comments
 (0)