Skip to content

Commit

Permalink
feat(bundling): generate type definitions in rollup executor
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jul 26, 2023
1 parent 1dadb3d commit d629bd4
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 146 deletions.
14 changes: 14 additions & 0 deletions e2e/rollup/src/rollup.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
checkFilesExist,
cleanupProject,
newProject,
readJson,
rmDist,
runCLI,
runCommand,
Expand All @@ -26,6 +28,10 @@ describe('Rollup Plugin', () => {
runCLI(`build ${myPkg}`);
let output = runCommand(`node dist/libs/${myPkg}/index.cjs`);
expect(output).toMatch(/Hello/);
expect(readJson(`dist/libs/${myPkg}/package.json`).types).toBe(
'./index.d.ts'
);
checkFilesExist(`dist/libs/${myPkg}/index.d.ts`);

updateProjectConfig(myPkg, (config) => {
delete config.targets.build;
Expand All @@ -40,6 +46,10 @@ describe('Rollup Plugin', () => {
runCLI(`build ${myPkg}`);
output = runCommand(`node dist/libs/${myPkg}/index.cjs`);
expect(output).toMatch(/Hello/);
expect(readJson(`dist/libs/${myPkg}/package.json`).types).toBe(
'./index.d.ts'
);
checkFilesExist(`dist/libs/${myPkg}/index.d.ts`);

updateProjectConfig(myPkg, (config) => {
delete config.targets.build;
Expand All @@ -54,6 +64,10 @@ describe('Rollup Plugin', () => {
runCLI(`build ${myPkg}`);
output = runCommand(`node dist/libs/${myPkg}/index.cjs`);
expect(output).toMatch(/Hello/);
expect(readJson(`dist/libs/${myPkg}/package.json`).types).toBe(
'./index.d.ts'
);
checkFilesExist(`dist/libs/${myPkg}/index.d.ts`);
}, 500000);

it('should be able to build libs generated with @nx/js:lib --bundler rollup', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
"resolve.exports": "1.1.0",
"rollup": "^2.56.2",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-dts": "^5.3.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-postcss": "^4.0.1",
"rollup-plugin-typescript2": "0.34.1",
Expand Down Expand Up @@ -359,4 +360,3 @@
}
}
}

3 changes: 2 additions & 1 deletion packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"rxjs": "^7.8.0",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js"
"@nx/js": "file:../js",
"rollup-plugin-dts": "^5.3.0"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 2 additions & 0 deletions packages/rollup/src/executors/rollup/lib/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface NormalizedRollupExecutorOptions extends RollupExecutorOptions {
projectRoot: string;
assets: AssetGlobPattern[];
rollupConfig: string[];
typesTmpDir: string;
}

export function normalizeRollupExecutorOptions(
Expand Down Expand Up @@ -45,6 +46,7 @@ export function normalizeRollupExecutorOptions(
projectRoot,
outputPath,
skipTypeCheck: options.skipTypeCheck || false,
typesTmpDir: join(root, 'tmp', '__nx__types__'),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('updatePackageJson', () => {
projectRoot: '.',
assets: [],
rollupConfig: [],
typesTmpDir: '',
};

const sharedContext = {
Expand Down
20 changes: 10 additions & 10 deletions packages/rollup/src/executors/rollup/lib/update-package-json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { relative } from 'path';
import { parse } from 'path';
import { ExecutorContext } from 'nx/src/config/misc-interfaces';
import { ProjectGraphProjectNode } from 'nx/src/config/project-graph';
import {
Expand All @@ -19,10 +19,10 @@ export function updatePackageJson(
const hasEsmFormat = options.format.includes('esm');
const hasCjsFormat = options.format.includes('cjs');

const types = `./${relative(options.projectRoot, options.main).replace(
/\.[jt]sx?$/,
'.d.ts'
)}`;
const entryPointName = parse(options.main).name;
const cjsEntryPoint = `./${entryPointName}.cjs`;
const esmEntryPoint = `./${entryPointName}.js`;
const types = `./${entryPointName}.d.ts`;
const exports = {
// TS 4.5+
'.': {
Expand All @@ -33,17 +33,17 @@ export function updatePackageJson(
if (hasEsmFormat) {
// `module` field is used by bundlers like rollup and webpack to detect ESM.
// May not be required in the future if type is already "module".
packageJson.module = './index.js';
exports['.']['import'] = './index.js';
packageJson.module = esmEntryPoint;
exports['.']['import'] = esmEntryPoint;

if (!hasCjsFormat) {
packageJson.main = './index.js';
packageJson.main = esmEntryPoint;
}
}

if (hasCjsFormat) {
packageJson.main = './index.cjs';
exports['.']['require'] = './index.cjs';
packageJson.main = cjsEntryPoint;
exports['.']['require'] = cjsEntryPoint;
}
if (!options.skipTypeField) {
packageJson.type = options.format.includes('esm') ? 'module' : 'commonjs';
Expand Down
20 changes: 0 additions & 20 deletions packages/rollup/src/executors/rollup/lib/validate-types.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/rollup/src/executors/rollup/rollup.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('rollupExecutor', () => {
chunkFileNames: '[name].cjs',
entryFileNames: '[name].cjs',
},
{
file: '/root/dist/ui/index.d.ts',
format: 'es',
},
]);
});

Expand Down
Loading

0 comments on commit d629bd4

Please sign in to comment.