Skip to content

Commit b07d24e

Browse files
jaysoomeeroslav
andauthored
feat(js): create package.json with proper defaults (#18091)
Co-authored-by: Miroslav Jonaš <meeroslav@users.noreply.github.com>
1 parent 57b2a76 commit b07d24e

File tree

8 files changed

+299
-66
lines changed

8 files changed

+299
-66
lines changed

e2e/esbuild/src/esbuild.test.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,24 @@ describe('EsBuild Plugin', () => {
4242
name: `@proj/${myPkg}`,
4343
version: '0.0.1',
4444
type: 'commonjs',
45+
main: './index.cjs',
46+
dependencies: {},
4547
});
4648

4749
// Build normally with package.json generation.
4850
runCLI(`build ${myPkg}`);
4951

50-
expect(runCommand(`node dist/libs/${myPkg}/index.js`)).toMatch(/Hello/);
52+
expect(runCommand(`node dist/libs/${myPkg}/index.cjs`)).toMatch(/Hello/);
5153
// main field should be set correctly in package.json
5254
checkFilesExist(`dist/libs/${myPkg}/package.json`);
5355
expect(runCommand(`node dist/libs/${myPkg}`)).toMatch(/Hello/);
5456

55-
expect(runCommand(`node dist/libs/${myPkg}/index.js`)).toMatch(/Hello/);
57+
expect(runCommand(`node dist/libs/${myPkg}/index.cjs`)).toMatch(/Hello/);
5658
// main field should be set correctly in package.json
5759

5860
expect(readFile(`dist/libs/${myPkg}/assets/a.md`)).toMatch(/file a/);
5961
expect(readFile(`dist/libs/${myPkg}/assets/b.md`)).toMatch(/file b/);
6062

61-
/* CJS format is not used by default, but passing --format=esm,cjs generates with it.
62-
*/
63-
checkFilesDoNotExist(`dist/libs/${myPkg}/index.cjs`);
64-
runCLI(`build ${myPkg} --format=esm,cjs`);
65-
checkFilesExist(`dist/libs/${myPkg}/index.cjs`);
66-
6763
/* Metafile is not generated by default, but passing --metafile generates it.
6864
*/
6965
checkFilesDoNotExist(`dist/libs/${myPkg}/meta.json`);
@@ -81,7 +77,7 @@ describe('EsBuild Plugin', () => {
8177
);
8278
expect(() => runCLI(`build ${myPkg}`)).toThrow();
8379
expect(() => runCLI(`build ${myPkg} --skipTypeCheck`)).not.toThrow();
84-
expect(runCommand(`node dist/libs/${myPkg}/index.js`)).toMatch(/Bye/);
80+
expect(runCommand(`node dist/libs/${myPkg}/index.cjs`)).toMatch(/Bye/);
8581
// Reset file
8682
updateFile(
8783
`libs/${myPkg}/src/index.ts`,
@@ -141,7 +137,7 @@ describe('EsBuild Plugin', () => {
141137
expect(
142138
readJson(`dist/libs/${parentLib}/package.json`).dependencies?.['dayjs']
143139
).not.toBeDefined();
144-
let runResult = runCommand(`node dist/libs/${parentLib}/index.js`);
140+
let runResult = runCommand(`node dist/libs/${parentLib}/index.cjs`);
145141
expect(runResult).toMatch(/Hello world/);
146142
expect(runResult).toMatch(/Hello from child lib/);
147143

@@ -155,7 +151,7 @@ describe('EsBuild Plugin', () => {
155151
rambda: expect.any(String),
156152
lodash: expect.any(String),
157153
});
158-
runResult = runCommand(`node dist/libs/${parentLib}/index.js`);
154+
runResult = runCommand(`node dist/libs/${parentLib}/index.cjs`);
159155
expect(runResult).toMatch(/Hello world/);
160156
expect(runResult).toMatch(/Hello from child lib/);
161157
}, 300_000);
@@ -164,16 +160,18 @@ describe('EsBuild Plugin', () => {
164160
const myPkg = uniq('my-pkg');
165161
runCLI(`generate @nx/js:lib ${myPkg} --bundler=esbuild`);
166162
updateFile(`libs/${myPkg}/src/lib/${myPkg}.ts`, `console.log('Hello');\n`);
167-
updateFile(`libs/${myPkg}/src/index.ts`, `import './lib/${myPkg}.js';\n`);
163+
updateFile(`libs/${myPkg}/src/index.ts`, `import './lib/${myPkg}.cjs';\n`);
168164

169165
runCLI(`build ${myPkg} --bundle=false`);
170166

171167
checkFilesExist(
172-
`dist/libs/${myPkg}/lib/${myPkg}.js`,
173-
`dist/libs/${myPkg}/index.js`
168+
`dist/libs/${myPkg}/libs/${myPkg}/src/lib/${myPkg}.cjs`,
169+
`dist/libs/${myPkg}/index.cjs`
174170
);
175171
// Test files are excluded in tsconfig (e.g. tsconfig.lib.json)
176-
checkFilesDoNotExist(`dist/libs/${myPkg}/lib/${myPkg}.spec.js`);
172+
checkFilesDoNotExist(
173+
`dist/libs/${myPkg}/libs/${myPkg}/src/lib/${myPkg}.spec.cjs`
174+
);
177175
// Can run package (package.json fields are correctly generated)
178176
expect(runCommand(`node dist/libs/${myPkg}`)).toMatch(/Hello/);
179177
}, 300_000);
@@ -193,14 +191,14 @@ describe('EsBuild Plugin', () => {
193191
runCLI(`build ${myPkg}`);
194192

195193
checkFilesExist(
196-
`dist/libs/${myPkg}/index.js`,
197-
`dist/libs/${myPkg}/extra.js`
194+
`dist/libs/${myPkg}/index.cjs`,
195+
`dist/libs/${myPkg}/extra.cjs`
198196
);
199197
expect(
200-
runCommand(`node dist/libs/${myPkg}/index.js`, { failOnError: true })
198+
runCommand(`node dist/libs/${myPkg}/index.cjs`, { failOnError: true })
201199
).toMatch(/main/);
202200
expect(
203-
runCommand(`node dist/libs/${myPkg}/extra.js`, { failOnError: true })
201+
runCommand(`node dist/libs/${myPkg}/extra.cjs`, { failOnError: true })
204202
).toMatch(/extra/);
205203
}, 120_000);
206204

e2e/js/src/js-bundling.test.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

e2e/js/src/js-packaging.test.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import {
2+
checkFilesExist,
3+
cleanupProject,
4+
newProject,
5+
runCLI,
6+
tmpProjPath,
7+
runCommand,
8+
createFile,
9+
uniq,
10+
getPackageManagerCommand,
11+
} from '@nx/e2e/utils';
12+
import { join } from 'path';
13+
import { ensureDirSync } from 'fs-extra';
14+
15+
describe('bundling libs', () => {
16+
let scope: string;
17+
18+
beforeEach(() => {
19+
scope = newProject();
20+
});
21+
22+
afterEach(() => cleanupProject());
23+
24+
it('should support esbuild, rollup, vite bundlers for building libs', () => {
25+
const esbuildLib = uniq('esbuildlib');
26+
const viteLib = uniq('vitelib');
27+
const rollupLib = uniq('rolluplib');
28+
29+
runCLI(
30+
`generate @nx/js:lib ${esbuildLib} --bundler=esbuild --no-interactive`
31+
);
32+
runCLI(`generate @nx/js:lib ${viteLib} --bundler=vite --no-interactive`);
33+
runCLI(
34+
`generate @nx/js:lib ${rollupLib} --bundler=rollup --no-interactive`
35+
);
36+
37+
runCLI(`build ${esbuildLib}`);
38+
runCLI(`build ${viteLib}`);
39+
runCLI(`build ${rollupLib}`);
40+
41+
const pmc = getPackageManagerCommand();
42+
let output: string;
43+
44+
// Make sure outputs in commonjs project
45+
createFile(
46+
'test-cjs/package.json',
47+
JSON.stringify(
48+
{
49+
name: 'test-cjs',
50+
private: true,
51+
type: 'commonjs',
52+
dependencies: {
53+
[`@proj/${esbuildLib}`]: `file:../dist/libs/${esbuildLib}`,
54+
[`@proj/${viteLib}`]: `file:../dist/libs/${viteLib}`,
55+
[`@proj/${rollupLib}`]: `file:../dist/libs/${rollupLib}`,
56+
},
57+
},
58+
null,
59+
2
60+
)
61+
);
62+
createFile(
63+
'test-cjs/index.js',
64+
`
65+
const { ${esbuildLib} } = require('@proj/${esbuildLib}');
66+
const { ${viteLib} } = require('@proj/${viteLib}');
67+
const { ${rollupLib} } = require('@proj/${rollupLib}');
68+
console.log(${esbuildLib}());
69+
console.log(${viteLib}());
70+
console.log(${rollupLib}());
71+
`
72+
);
73+
runCommand(pmc.install, {
74+
cwd: join(tmpProjPath(), 'test-cjs'),
75+
});
76+
output = runCommand('node index.js', {
77+
cwd: join(tmpProjPath(), 'test-cjs'),
78+
});
79+
expect(output).toContain(esbuildLib);
80+
expect(output).toContain(viteLib);
81+
expect(output).toContain(rollupLib);
82+
83+
// Make sure outputs in esm project
84+
createFile(
85+
'test-esm/package.json',
86+
JSON.stringify(
87+
{
88+
name: 'test-esm',
89+
private: true,
90+
type: 'module',
91+
dependencies: {
92+
[`@proj/${esbuildLib}`]: `file:../dist/libs/${esbuildLib}`,
93+
[`@proj/${viteLib}`]: `file:../dist/libs/${viteLib}`,
94+
[`@proj/${rollupLib}`]: `file:../dist/libs/${rollupLib}`,
95+
},
96+
},
97+
null,
98+
2
99+
)
100+
);
101+
createFile(
102+
'test-esm/index.js',
103+
`
104+
import { ${esbuildLib} } from '@proj/${esbuildLib}';
105+
import { ${viteLib} } from '@proj/${viteLib}';
106+
import { ${rollupLib} } from '@proj/${rollupLib}';
107+
console.log(${esbuildLib}());
108+
console.log(${viteLib}());
109+
console.log(${rollupLib}());
110+
`
111+
);
112+
runCommand(pmc.install, {
113+
cwd: join(tmpProjPath(), 'test-esm'),
114+
});
115+
output = runCommand('node index.js', {
116+
cwd: join(tmpProjPath(), 'test-esm'),
117+
});
118+
expect(output).toContain(esbuildLib);
119+
expect(output).toContain(viteLib);
120+
expect(output).toContain(rollupLib);
121+
}, 500_000);
122+
123+
it('should support tsc and swc for building libs', () => {
124+
const tscLib = uniq('tsclib');
125+
const swcLib = uniq('swclib');
126+
127+
runCLI(`generate @nx/js:lib ${tscLib} --bundler=tsc --no-interactive`);
128+
runCLI(`generate @nx/js:lib ${swcLib} --bundler=swc --no-interactive`);
129+
130+
runCLI(`build ${tscLib}`);
131+
runCLI(`build ${swcLib}`);
132+
133+
const pmc = getPackageManagerCommand();
134+
let output: string;
135+
136+
// Make sure outputs in commonjs project
137+
createFile(
138+
'test-cjs/package.json',
139+
JSON.stringify(
140+
{
141+
name: 'test-cjs',
142+
private: true,
143+
type: 'commonjs',
144+
dependencies: {
145+
[`@proj/${tscLib}`]: `file:../dist/libs/${tscLib}`,
146+
[`@proj/${swcLib}`]: `file:../dist/libs/${swcLib}`,
147+
},
148+
},
149+
null,
150+
2
151+
)
152+
);
153+
createFile(
154+
'test-cjs/index.js',
155+
`
156+
const { ${tscLib} } = require('@proj/${tscLib}');
157+
const { ${swcLib} } = require('@proj/${swcLib}');
158+
console.log(${tscLib}());
159+
console.log(${swcLib}());
160+
`
161+
);
162+
runCommand(pmc.install, {
163+
cwd: join(tmpProjPath(), 'test-cjs'),
164+
});
165+
output = runCommand('node index.js', {
166+
cwd: join(tmpProjPath(), 'test-cjs'),
167+
});
168+
expect(output).toContain(tscLib);
169+
expect(output).toContain(swcLib);
170+
}, 500_000);
171+
});

e2e/js/src/js-swc.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ describe('js e2e', () => {
9191
return json;
9292
});
9393

94+
updateJson(`libs/${lib}/package.json`, (json) => {
95+
// Delete automatically generated helper dependency to test legacy behavior.
96+
delete json.dependencies['@swc/helpers'];
97+
return json;
98+
});
99+
94100
runCLI(
95101
`build ${lib} --generateLockfile=true --updateBuildableProjectDepsInPackageJson`
96102
);

e2e/js/src/js-tsc.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ describe('js e2e', () => {
129129
return json;
130130
});
131131

132+
updateJson(`libs/${lib}/package.json`, (json) => {
133+
// Delete automatically generated helper dependency to test legacy behavior.
134+
delete json.dependencies.tslib;
135+
return json;
136+
});
137+
132138
runCLI(`build ${lib} --updateBuildableProjectDepsInPackageJson`);
133139

134140
expect(readJson(`dist/libs/${lib}/package.json`)).toHaveProperty(
@@ -228,6 +234,12 @@ describe('package.json updates', () => {
228234
`;
229235
});
230236

237+
updateJson(`libs/${lib}/package.json`, (json) => {
238+
// Delete automatically generated helper dependency to test legacy behavior.
239+
delete json.dependencies.tslib;
240+
return json;
241+
});
242+
231243
runCLI(`build ${lib} --updateBuildableProjectDepsInPackageJson`);
232244

233245
// Check that only 'react' exists, don't care about version

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy