Skip to content

Commit

Permalink
feat(js): create package.json with proper defaults (#18091)
Browse files Browse the repository at this point in the history
Co-authored-by: Miroslav Jonaš <[email protected]>
  • Loading branch information
jaysoo and meeroslav authored Jul 18, 2023
1 parent 57b2a76 commit b07d24e
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 66 deletions.
36 changes: 17 additions & 19 deletions e2e/esbuild/src/esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,24 @@ describe('EsBuild Plugin', () => {
name: `@proj/${myPkg}`,
version: '0.0.1',
type: 'commonjs',
main: './index.cjs',
dependencies: {},
});

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

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

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

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

/* CJS format is not used by default, but passing --format=esm,cjs generates with it.
*/
checkFilesDoNotExist(`dist/libs/${myPkg}/index.cjs`);
runCLI(`build ${myPkg} --format=esm,cjs`);
checkFilesExist(`dist/libs/${myPkg}/index.cjs`);

/* Metafile is not generated by default, but passing --metafile generates it.
*/
checkFilesDoNotExist(`dist/libs/${myPkg}/meta.json`);
Expand All @@ -81,7 +77,7 @@ describe('EsBuild Plugin', () => {
);
expect(() => runCLI(`build ${myPkg}`)).toThrow();
expect(() => runCLI(`build ${myPkg} --skipTypeCheck`)).not.toThrow();
expect(runCommand(`node dist/libs/${myPkg}/index.js`)).toMatch(/Bye/);
expect(runCommand(`node dist/libs/${myPkg}/index.cjs`)).toMatch(/Bye/);
// Reset file
updateFile(
`libs/${myPkg}/src/index.ts`,
Expand Down Expand Up @@ -141,7 +137,7 @@ describe('EsBuild Plugin', () => {
expect(
readJson(`dist/libs/${parentLib}/package.json`).dependencies?.['dayjs']
).not.toBeDefined();
let runResult = runCommand(`node dist/libs/${parentLib}/index.js`);
let runResult = runCommand(`node dist/libs/${parentLib}/index.cjs`);
expect(runResult).toMatch(/Hello world/);
expect(runResult).toMatch(/Hello from child lib/);

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

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

checkFilesExist(
`dist/libs/${myPkg}/lib/${myPkg}.js`,
`dist/libs/${myPkg}/index.js`
`dist/libs/${myPkg}/libs/${myPkg}/src/lib/${myPkg}.cjs`,
`dist/libs/${myPkg}/index.cjs`
);
// Test files are excluded in tsconfig (e.g. tsconfig.lib.json)
checkFilesDoNotExist(`dist/libs/${myPkg}/lib/${myPkg}.spec.js`);
checkFilesDoNotExist(
`dist/libs/${myPkg}/libs/${myPkg}/src/lib/${myPkg}.spec.cjs`
);
// Can run package (package.json fields are correctly generated)
expect(runCommand(`node dist/libs/${myPkg}`)).toMatch(/Hello/);
}, 300_000);
Expand All @@ -193,14 +191,14 @@ describe('EsBuild Plugin', () => {
runCLI(`build ${myPkg}`);

checkFilesExist(
`dist/libs/${myPkg}/index.js`,
`dist/libs/${myPkg}/extra.js`
`dist/libs/${myPkg}/index.cjs`,
`dist/libs/${myPkg}/extra.cjs`
);
expect(
runCommand(`node dist/libs/${myPkg}/index.js`, { failOnError: true })
runCommand(`node dist/libs/${myPkg}/index.cjs`, { failOnError: true })
).toMatch(/main/);
expect(
runCommand(`node dist/libs/${myPkg}/extra.js`, { failOnError: true })
runCommand(`node dist/libs/${myPkg}/extra.cjs`, { failOnError: true })
).toMatch(/extra/);
}, 120_000);

Expand Down
33 changes: 0 additions & 33 deletions e2e/js/src/js-bundling.test.ts

This file was deleted.

171 changes: 171 additions & 0 deletions e2e/js/src/js-packaging.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import {
checkFilesExist,
cleanupProject,
newProject,
runCLI,
tmpProjPath,
runCommand,
createFile,
uniq,
getPackageManagerCommand,
} from '@nx/e2e/utils';
import { join } from 'path';
import { ensureDirSync } from 'fs-extra';

describe('bundling libs', () => {
let scope: string;

beforeEach(() => {
scope = newProject();
});

afterEach(() => cleanupProject());

it('should support esbuild, rollup, vite bundlers for building libs', () => {
const esbuildLib = uniq('esbuildlib');
const viteLib = uniq('vitelib');
const rollupLib = uniq('rolluplib');

runCLI(
`generate @nx/js:lib ${esbuildLib} --bundler=esbuild --no-interactive`
);
runCLI(`generate @nx/js:lib ${viteLib} --bundler=vite --no-interactive`);
runCLI(
`generate @nx/js:lib ${rollupLib} --bundler=rollup --no-interactive`
);

runCLI(`build ${esbuildLib}`);
runCLI(`build ${viteLib}`);
runCLI(`build ${rollupLib}`);

const pmc = getPackageManagerCommand();
let output: string;

// Make sure outputs in commonjs project
createFile(
'test-cjs/package.json',
JSON.stringify(
{
name: 'test-cjs',
private: true,
type: 'commonjs',
dependencies: {
[`@proj/${esbuildLib}`]: `file:../dist/libs/${esbuildLib}`,
[`@proj/${viteLib}`]: `file:../dist/libs/${viteLib}`,
[`@proj/${rollupLib}`]: `file:../dist/libs/${rollupLib}`,
},
},
null,
2
)
);
createFile(
'test-cjs/index.js',
`
const { ${esbuildLib} } = require('@proj/${esbuildLib}');
const { ${viteLib} } = require('@proj/${viteLib}');
const { ${rollupLib} } = require('@proj/${rollupLib}');
console.log(${esbuildLib}());
console.log(${viteLib}());
console.log(${rollupLib}());
`
);
runCommand(pmc.install, {
cwd: join(tmpProjPath(), 'test-cjs'),
});
output = runCommand('node index.js', {
cwd: join(tmpProjPath(), 'test-cjs'),
});
expect(output).toContain(esbuildLib);
expect(output).toContain(viteLib);
expect(output).toContain(rollupLib);

// Make sure outputs in esm project
createFile(
'test-esm/package.json',
JSON.stringify(
{
name: 'test-esm',
private: true,
type: 'module',
dependencies: {
[`@proj/${esbuildLib}`]: `file:../dist/libs/${esbuildLib}`,
[`@proj/${viteLib}`]: `file:../dist/libs/${viteLib}`,
[`@proj/${rollupLib}`]: `file:../dist/libs/${rollupLib}`,
},
},
null,
2
)
);
createFile(
'test-esm/index.js',
`
import { ${esbuildLib} } from '@proj/${esbuildLib}';
import { ${viteLib} } from '@proj/${viteLib}';
import { ${rollupLib} } from '@proj/${rollupLib}';
console.log(${esbuildLib}());
console.log(${viteLib}());
console.log(${rollupLib}());
`
);
runCommand(pmc.install, {
cwd: join(tmpProjPath(), 'test-esm'),
});
output = runCommand('node index.js', {
cwd: join(tmpProjPath(), 'test-esm'),
});
expect(output).toContain(esbuildLib);
expect(output).toContain(viteLib);
expect(output).toContain(rollupLib);
}, 500_000);

it('should support tsc and swc for building libs', () => {
const tscLib = uniq('tsclib');
const swcLib = uniq('swclib');

runCLI(`generate @nx/js:lib ${tscLib} --bundler=tsc --no-interactive`);
runCLI(`generate @nx/js:lib ${swcLib} --bundler=swc --no-interactive`);

runCLI(`build ${tscLib}`);
runCLI(`build ${swcLib}`);

const pmc = getPackageManagerCommand();
let output: string;

// Make sure outputs in commonjs project
createFile(
'test-cjs/package.json',
JSON.stringify(
{
name: 'test-cjs',
private: true,
type: 'commonjs',
dependencies: {
[`@proj/${tscLib}`]: `file:../dist/libs/${tscLib}`,
[`@proj/${swcLib}`]: `file:../dist/libs/${swcLib}`,
},
},
null,
2
)
);
createFile(
'test-cjs/index.js',
`
const { ${tscLib} } = require('@proj/${tscLib}');
const { ${swcLib} } = require('@proj/${swcLib}');
console.log(${tscLib}());
console.log(${swcLib}());
`
);
runCommand(pmc.install, {
cwd: join(tmpProjPath(), 'test-cjs'),
});
output = runCommand('node index.js', {
cwd: join(tmpProjPath(), 'test-cjs'),
});
expect(output).toContain(tscLib);
expect(output).toContain(swcLib);
}, 500_000);
});
6 changes: 6 additions & 0 deletions e2e/js/src/js-swc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ describe('js e2e', () => {
return json;
});

updateJson(`libs/${lib}/package.json`, (json) => {
// Delete automatically generated helper dependency to test legacy behavior.
delete json.dependencies['@swc/helpers'];
return json;
});

runCLI(
`build ${lib} --generateLockfile=true --updateBuildableProjectDepsInPackageJson`
);
Expand Down
12 changes: 12 additions & 0 deletions e2e/js/src/js-tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ describe('js e2e', () => {
return json;
});

updateJson(`libs/${lib}/package.json`, (json) => {
// Delete automatically generated helper dependency to test legacy behavior.
delete json.dependencies.tslib;
return json;
});

runCLI(`build ${lib} --updateBuildableProjectDepsInPackageJson`);

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

updateJson(`libs/${lib}/package.json`, (json) => {
// Delete automatically generated helper dependency to test legacy behavior.
delete json.dependencies.tslib;
return json;
});

runCLI(`build ${lib} --updateBuildableProjectDepsInPackageJson`);

// Check that only 'react' exists, don't care about version
Expand Down
Loading

1 comment on commit b07d24e

@vercel
Copy link

@vercel vercel bot commented on b07d24e Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.