From e0e0ca901513e1acc645e7f00f5dc1485a43ecba Mon Sep 17 00:00:00 2001 From: shellscape Date: Fri, 5 Feb 2021 11:13:06 -0500 Subject: [PATCH 1/2] feat(typescript): error when no tsconfig and no rootDir --- packages/typescript/src/preflight.ts | 7 +++ .../typescript/test/snapshots/tsconfig.ts.md | 25 +++++++++ .../test/snapshots/tsconfig.ts.snap | Bin 0 -> 390 bytes packages/typescript/test/tsconfig.ts | 50 ++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 packages/typescript/test/snapshots/tsconfig.ts.md create mode 100644 packages/typescript/test/snapshots/tsconfig.ts.snap create mode 100644 packages/typescript/test/tsconfig.ts diff --git a/packages/typescript/src/preflight.ts b/packages/typescript/src/preflight.ts index 3b7e70e2d..74945ee8b 100644 --- a/packages/typescript/src/preflight.ts +++ b/packages/typescript/src/preflight.ts @@ -17,6 +17,8 @@ ${pluginName}: Rollup requires that TypeScript produces ES Modules. Unfortunatel "module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext" in the target tsconfig.json file or plugin options.`.replace(/\n/g, ''); +const rootDirErrorMessage = `${pluginName}: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified.`; + const tsLibErrorMessage = `${pluginName}: Could not find module 'tslib', which is required by this plugin. Is it installed?`; let undef; @@ -28,6 +30,11 @@ export const preflight = ({ config, context, rollupOptions, tslib }: PreflightOp context.warn(moduleErrorMessage); } + const { options } = config; + if (!options.configFilePath && !options.rootDir && !options.rootDirs) { + context.error(rootDirErrorMessage); + } + if (!rollupOptions.preserveModules && tslib === null) { context.error(tsLibErrorMessage); } diff --git a/packages/typescript/test/snapshots/tsconfig.ts.md b/packages/typescript/test/snapshots/tsconfig.ts.md new file mode 100644 index 000000000..9073553e5 --- /dev/null +++ b/packages/typescript/test/snapshots/tsconfig.ts.md @@ -0,0 +1,25 @@ +# Snapshot report for `test/tsconfig.ts` + +The actual snapshot is saved in `tsconfig.ts.snap`. + +Generated by [AVA](https://avajs.dev). + +## inline config without tsconfig + rootDir + +> Snapshot 1 + + [ + 'main.js', + 'types/main.d.ts', + ] + +## inline config without tsconfig without rootDir fails + +> Snapshot 1 + + Error { + code: 'PLUGIN_ERROR', + hook: 'buildStart', + plugin: 'typescript', + message: '@rollup/plugin-typescript: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified.', + } diff --git a/packages/typescript/test/snapshots/tsconfig.ts.snap b/packages/typescript/test/snapshots/tsconfig.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..e893a73c26d9ae8d4dc800f0363119bbdac0a07f GIT binary patch literal 390 zcmV;10eSvGRzVTB)kh@=1|!5ftt&z~etRb~W>9tUD( z26nJ2MhQk%XCUnf#6>_n6NnE1u`nZ(AS0`5QBi)8Fe4j`$;b#}2?z)R<{5*xsVuhmA!qUv5)D(sCjMO}Z5|Eyf;^h3iw9Is5WqJ7} z3dIGf$(d=HsVRE!aOGfRHUQFQK process.chdir(__dirname)); + +test.serial('inline config without tsconfig + rootDir', async (t) => { + const bundle = await rollup({ + input: 'fixtures/basic/main.ts', + plugins: [ + typescript({ + declaration: true, + declarationDir: 'fixtures/basic/dist/types', + exclude: 'fixtures/basic/dist/types', + include: 'fixtures/basic/*.ts', + tsconfig: false, + rootDir: 'fixtures/basic' + }) + ], + onwarn + }); + const files = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); + const [, { source }] = files; + + t.snapshot(files.map(({ fileName }) => fileName)); + t.true((source as string)?.includes('declare const answer = 42;')); +}); + +test.serial('inline config without tsconfig without rootDir fails', async (t) => { + const fail = () => + rollup({ + input: 'fixtures/basic/main.ts', + plugins: [ + typescript({ + declaration: true, + declarationDir: 'fixtures/basic/dist/types', + exclude: 'fixtures/basic/dist/types', + include: 'fixtures/basic/*.ts', + tsconfig: false + }) + ], + onwarn + }); + + const error = await t.throwsAsync(fail); + t.snapshot(error); +}); From 90f4ebae8be741c3a8a7dcde56f78f9524ab006e Mon Sep 17 00:00:00 2001 From: shellscape Date: Fri, 5 Feb 2021 11:23:40 -0500 Subject: [PATCH 2/2] fix: only emit when declaration: true --- packages/typescript/src/preflight.ts | 4 ++-- .../typescript/test/snapshots/tsconfig.ts.md | 2 +- .../typescript/test/snapshots/tsconfig.ts.snap | Bin 390 -> 409 bytes 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/typescript/src/preflight.ts b/packages/typescript/src/preflight.ts index 74945ee8b..39cc1731c 100644 --- a/packages/typescript/src/preflight.ts +++ b/packages/typescript/src/preflight.ts @@ -17,7 +17,7 @@ ${pluginName}: Rollup requires that TypeScript produces ES Modules. Unfortunatel "module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext" in the target tsconfig.json file or plugin options.`.replace(/\n/g, ''); -const rootDirErrorMessage = `${pluginName}: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified.`; +const rootDirErrorMessage = `${pluginName}: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".`; const tsLibErrorMessage = `${pluginName}: Could not find module 'tslib', which is required by this plugin. Is it installed?`; @@ -31,7 +31,7 @@ export const preflight = ({ config, context, rollupOptions, tslib }: PreflightOp } const { options } = config; - if (!options.configFilePath && !options.rootDir && !options.rootDirs) { + if (options.declaration && !options.configFilePath && !options.rootDir && !options.rootDirs) { context.error(rootDirErrorMessage); } diff --git a/packages/typescript/test/snapshots/tsconfig.ts.md b/packages/typescript/test/snapshots/tsconfig.ts.md index 9073553e5..cd5733ae1 100644 --- a/packages/typescript/test/snapshots/tsconfig.ts.md +++ b/packages/typescript/test/snapshots/tsconfig.ts.md @@ -21,5 +21,5 @@ Generated by [AVA](https://avajs.dev). code: 'PLUGIN_ERROR', hook: 'buildStart', plugin: 'typescript', - message: '@rollup/plugin-typescript: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified.', + message: '@rollup/plugin-typescript: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".', } diff --git a/packages/typescript/test/snapshots/tsconfig.ts.snap b/packages/typescript/test/snapshots/tsconfig.ts.snap index e893a73c26d9ae8d4dc800f0363119bbdac0a07f..ecf6b0f74f435539fbb9f3bb652a5458bd699c59 100644 GIT binary patch literal 409 zcmV;K0cQR|RzVqf00000000AB zkiSX;K@i4wF8?G%E+Hu_m$T8r#L6O-VuA!DhNxg8vbozNtLN>xyN3j^vG4%|3tKCn zzy}blY%G0%fSrw4*|>XpiDcnp=9~S^E;Dt6kcB>8KO5J}7mxQ3--lPvl`$Z5A|1Xi zUk-X5r}Q>jdLNV9MTn~gNTFOz!2)D9b!=$~tvjtJtuk08$TWrEq72z21xT`Dv4~(E zK^lQ=;0mM=WJ-{BxknRI=xpzAwc1C`Zg;0U3l2FyNld;MG2h!&M5q}o8~V|J1rtz> zLMmOs!dWnPN~I)&CPr!(D#ioMBo_26 zVuE`3d`JVVOplT-5BhBI9}Bp`GNdl+GtH8~!;VK?p9o?I#~_Xp5p}AQrO!cXRmU~0 zcD!t};SC|kDBf_K*|v=!kHA7uFoUA$q=W_4WBFGHNG|Mn#9DmqRh9e!Cr80{9s&RW DgmJNa literal 390 zcmV;10eSvGRzVTB)kh@=1|!5ftt&z~etRb~W>9tUD( z26nJ2MhQk%XCUnf#6>_n6NnE1u`nZ(AS0`5QBi)8Fe4j`$;b#}2?z)R<{5*xsVuhmA!qUv5)D(sCjMO}Z5|Eyf;^h3iw9Is5WqJ7} z3dIGf$(d=HsVRE!aOGfRHUQFQK