Skip to content

Commit

Permalink
docs(typescript): More informative error messages (#619)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Holthausen <[email protected]>
  • Loading branch information
dummdidumm and Simon Holthausen authored Oct 24, 2020
1 parent df56bfb commit 5d292b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 12 additions & 7 deletions packages/typescript/src/options/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,28 @@ export function validatePaths(
) {
if (compilerOptions.out) {
context.error(
`@rollup/plugin-typescript: Deprecated 'out' option is not supported. Use 'outDir' instead.`
`@rollup/plugin-typescript: Deprecated Typescript compiler option 'out' is not supported. Use 'outDir' instead.`
);
} else if (compilerOptions.outFile) {
context.error(
`@rollup/plugin-typescript: 'outFile' option is not supported. Use 'outDir' instead.`
`@rollup/plugin-typescript: Typescript compiler option 'outFile' is not supported. Use 'outDir' instead.`
);
}

for (const dirProperty of DIRECTORY_PROPS) {
if (compilerOptions[dirProperty]) {
if (!outputOptions.dir) {
context.error(
`@rollup/plugin-typescript: 'dir' must be used when '${dirProperty}' is specified.`
`@rollup/plugin-typescript: Rollup 'dir' option must be used when Typescript compiler option '${dirProperty}' is specified.`
);
}

// Checks if the given path lies within Rollup output dir
const fromRollupDirToTs = relative(outputOptions.dir, compilerOptions[dirProperty]!);
if (fromRollupDirToTs.startsWith('..')) {
context.error(`@rollup/plugin-typescript: '${dirProperty}' must be located inside 'dir'.`);
context.error(
`@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside Rollup 'dir' option.`
);
}
}
}
Expand All @@ -72,21 +74,24 @@ export function validatePaths(
if (tsBuildInfoPath && compilerOptions.incremental) {
if (!outputOptions.dir) {
context.error(
`@rollup/plugin-typescript: 'dir' must be used when 'tsBuildInfoFile' or 'incremental' are specified.`
`@rollup/plugin-typescript: Rollup 'dir' option must be used when Typescript compiler options 'tsBuildInfoFile' or 'incremental' are specified.`
);
}

// Checks if the given path lies within Rollup output dir
const fromRollupDirToTs = relative(outputOptions.dir, tsBuildInfoPath);
if (fromRollupDirToTs.startsWith('..')) {
context.error(`@rollup/plugin-typescript: 'tsBuildInfoFile' must be located inside 'dir'.`);
context.error(
`@rollup/plugin-typescript: Path of Typescript compiler option 'tsBuildInfoFile' must be located inside Rollup 'dir' option.`
);
}
}

if (compilerOptions.declaration || compilerOptions.declarationMap || compilerOptions.composite) {
if (DIRECTORY_PROPS.every((dirProperty) => !compilerOptions[dirProperty])) {
context.error(
`@rollup/plugin-typescript: 'outDir' or 'declarationDir' must be specified to generate declaration files.`
`@rollup/plugin-typescript: You are using one of Typescript's compiler options 'declaration', 'declarationMap' or 'composite'. ` +
`In this case 'outDir' or 'declarationDir' must be specified to generate declaration files.`
);
}
}
Expand Down
16 changes: 12 additions & 4 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,19 @@ test.serial('ensures outDir is located in Rollup output dir', async (t) => {
getCode(bundle, { format: 'esm', file: 'fixtures/basic/other/out.js' }, true)
);
t.true(
noDirError.message.includes(`'dir' must be used when 'outDir' is specified`),
noDirError.message.includes(
`Rollup 'dir' option must be used when Typescript compiler option 'outDir' is specified`
),
`Unexpected error message: ${noDirError.message}`
);

const wrongDirError = await t.throwsAsync(() =>
getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true)
);
t.true(
wrongDirError.message.includes(`'outDir' must be located inside 'dir'`),
wrongDirError.message.includes(
`Path of Typescript compiler option 'outDir' must be located inside Rollup 'dir' option`
),
`Unexpected error message: ${wrongDirError.message}`
);
});
Expand All @@ -239,15 +243,19 @@ test.serial('ensures declarationDir is located in Rollup output dir', async (t)
getCode(bundle, { format: 'esm', file: 'fixtures/basic/other/out.js' }, true)
);
t.true(
noDirError.message.includes(`'dir' must be used when 'declarationDir' is specified`),
noDirError.message.includes(
`Rollup 'dir' option must be used when Typescript compiler option 'declarationDir' is specified`
),
`Unexpected error message: ${noDirError.message}`
);

const wrongDirError = await t.throwsAsync(() =>
getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true)
);
t.true(
wrongDirError.message.includes(`'declarationDir' must be located inside 'dir'`),
wrongDirError.message.includes(
`Path of Typescript compiler option 'declarationDir' must be located inside Rollup 'dir' option`
),
`Unexpected error message: ${wrongDirError.message}`
);
});
Expand Down

0 comments on commit 5d292b5

Please sign in to comment.