Skip to content

Commit

Permalink
fix: switch types rollup feature to opt-in (#117)
Browse files Browse the repository at this point in the history
* fix: switch types rollup feature to opt-in

* fix: typo in README
  • Loading branch information
igordanchenko committed Dec 23, 2021
1 parent 8c0a145 commit 56a078e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ package-lock.json
!.yarn/sdks
!.yarn/versions
.pnp.*

# WebStorm project files
.idea
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,14 @@ _TODO: Simple guide to host error codes to be completed_

### Types rollup

DTS automatically rolls up TypeScript type definitions into a single `index.d.ts` file via [rollup-plugin-dts](https://github.com/Swatinem/rollup-plugin-dts) plugin. If this approach does not meet your project's needs, you can disable it via `--noTypesRollup` flag.
DTS can automatically rollup TypeScript type definitions into a single `index.d.ts` file via [rollup-plugin-dts](https://github.com/Swatinem/rollup-plugin-dts) plugin. To enable types rollup, add `--rollupTypes` flag to your `build` and `watch` scripts.

```json
"build": "dts build --rollupTypes",
"start": "dts watch --rollupTypes",
```

[rollup-plugin-dts](https://github.com/Swatinem/rollup-plugin-dts) was seen to cause issues when using json and less imports. Use with caution.

## Customization

Expand Down Expand Up @@ -450,7 +457,7 @@ Options
--onFailure Run a command on a failed build
--noClean Don't clean the dist folder
--transpileOnly Skip type checking
--noTypesRollup Skip types rollup
--rollupTypes Enable types rollup
-h, --help Displays this message
Examples
Expand Down Expand Up @@ -483,7 +490,7 @@ Options
--extractErrors Opt-in to extracting invariant error codes
--tsconfig Specify your custom tsconfig path (default <root-folder>/tsconfig.json)
--transpileOnly Skip type checking
--noTypesRollup Skip types rollup
--rollupTypes Enable types rollup
-h, --help Displays this message
Examples
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ prog
.example('watch --transpileOnly')
.option('--extractErrors', 'Extract invariant errors to ./errors/codes.json.')
.example('watch --extractErrors')
.option('--noTypesRollup', 'Skip types rollup')
.example('watch --noTypesRollup')
.option('--rollupTypes', 'Enable types rollup')
.example('watch --rollupTypes')
.action(async (dirtyOpts: WatchOpts) => {
const opts = await normalizeOpts(dirtyOpts);
const buildConfigs = await createBuildConfigs(opts, appPackageJson);
Expand Down Expand Up @@ -348,7 +348,7 @@ prog
console.log(`
${chalk.dim('Watching for changes')}
`);
if (!opts.noTypesRollup) {
if (opts.rollupTypes) {
await rollupTypes(opts.tsconfig, appPackageJson);
}

Expand Down Expand Up @@ -383,8 +383,8 @@ prog
.example('build --tsconfig ./tsconfig.foo.json')
.option('--transpileOnly', 'Skip type checking')
.example('build --transpileOnly')
.option('--noTypesRollup', 'Skip types rollup')
.example('build --noTypesRollup')
.option('--rollupTypes', 'Enable types rollup')
.example('build --rollupTypes')
.option(
'--extractErrors',
'Extract errors to ./errors/codes.json and provide a url for decoding.'
Expand Down Expand Up @@ -416,7 +416,7 @@ prog
throw e;
})
.then(async () => {
if (!opts.noTypesRollup) {
if (opts.rollupTypes) {
await rollupTypes(opts.tsconfig, appPackageJson);
}
})
Expand Down
8 changes: 6 additions & 2 deletions src/rollupTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export async function rollupTypes(
plugins: [dts(), del({ hook: 'buildEnd', targets: declarationDir })],
} as RollupOptions & { output: OutputOptions };

const bundle = await rollup(config);
await bundle.write(config.output);
try {
const bundle = await rollup(config);
await bundle.write(config.output);
} catch (e) {
console.log('Failed to rollup types:', e);
}
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface BuildOpts extends SharedOpts {
format: 'cjs,esm';
target: 'browser';
noClean?: boolean;
noTypesRollup?: boolean;
rollupTypes?: boolean;
}

export interface WatchOpts extends BuildOpts {
Expand Down
24 changes: 15 additions & 9 deletions test/e2e/dts-build-withTypesRollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('dts build :: types rollup', () => {
});

it('should rollup types into index.d.ts', () => {
const output = execWithCache('node ../dist/index.js build');
const output = execWithCache('node ../dist/index.js build --rollupTypes');

expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();

Expand All @@ -27,8 +27,8 @@ describe('dts build :: types rollup', () => {
expect(output.code).toBe(0);
});

it('should honor --noTypesRollup flag', () => {
const output = execWithCache('node ../dist/index.js build --noTypesRollup');
it('should not run by default', () => {
const output = execWithCache('node ../dist/index.js build');

expect(shell.test('-f', 'dist/types/index.d.ts')).toBeTruthy();

Expand All @@ -47,9 +47,12 @@ describe('dts build :: types rollup', () => {
);

try {
const output = execWithCache('node ../dist/index.js build', {
noCache: true,
});
const output = execWithCache(
'node ../dist/index.js build --rollupTypes',
{
noCache: true,
}
);

expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();

Expand All @@ -70,9 +73,12 @@ describe('dts build :: types rollup', () => {
shell.sed('-i', '"types"', '"types-disabled"', 'package.json');

try {
const output = execWithCache('node ../dist/index.js build', {
noCache: true,
});
const output = execWithCache(
'node ../dist/index.js build --rollupTypes',
{
noCache: true,
}
);

expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();
expect(shell.test('-f', 'dist/foo/foo.d.ts')).toBeTruthy();
Expand Down

0 comments on commit 56a078e

Please sign in to comment.