Skip to content

Commit cc55c44

Browse files
authored
fix(typescript): allow for files to be nested in folders within outDir (#1783)
fix(typescript): allow for files to be nested within outDir This change corrects a regression introduced by #1728, preventing dual bundle configs from writing output files to directories nested within the directory defined in the tsconfig's outDir property. Example: ```js export default { input: 'src/index.ts', // Replace with the path to your entry TypeScript file output: [ { dir: 'dist/cjs', format: 'cjs', sourcemap: true, preserveModules: true }, { dir: 'dist/esm', format: 'esm', sourcemap: true, preserveModules: true } ], plugins: [ json(), resolve(), commonjs(), typescript({ tsconfig: 'tsconfig.json', compilerOptions: { /* Basic Options */ target: 'ES5', module: 'ES2020', lib: ['ES2019', 'ES2020'], allowJs: true, checkJs: false, /* Strict Type-Checking Options */ strict: true, /* Module Resolution Options */ moduleResolution: 'node', esModuleInterop: true, /* Advanced Options */ forceConsistentCasingInFileNames: true, skipDefaultLibCheck: true, skipLibCheck: true, outDir: path.join(__dirname, 'dist') } }) ], external: deps // Add any external dependencies you don't want to bundle }; ```
1 parent 4799541 commit cc55c44

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

packages/typescript/src/options/validate.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ export function validatePaths(
5858
for (const dirProperty of DIRECTORY_PROPS) {
5959
if (compilerOptions[dirProperty] && outputDir) {
6060
// Checks if the given path lies within Rollup output dir
61-
const fromRollupDirToTs = relative(outputDir, compilerOptions[dirProperty]!);
62-
if (fromRollupDirToTs.startsWith('..')) {
63-
if (outputOptions.dir) {
61+
if (outputOptions.dir) {
62+
const fromRollupDirToTs = relative(outputDir, compilerOptions[dirProperty]!);
63+
if (fromRollupDirToTs.startsWith('..')) {
6464
context.error(
6565
`@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside Rollup 'dir' option.`
6666
);
67-
} else {
67+
}
68+
} else {
69+
const fromTsDirToRollup = relative(compilerOptions[dirProperty]!, outputDir);
70+
if (fromTsDirToRollup.startsWith('..')) {
6871
context.error(
6972
`@rollup/plugin-typescript: Path of Typescript compiler option '${dirProperty}' must be located inside the same directory as the Rollup 'file' option.`
7073
);

packages/typescript/test/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,36 @@ test.serial(
129129
}
130130
);
131131

132+
test.serial(
133+
'ensures output files can be written to subdirectories within the tsconfig outDir',
134+
async (t) => {
135+
const warnings = [];
136+
const outputOpts = { format: 'es', file: 'fixtures/basic/dist/esm/main.js' };
137+
const bundle = await rollup({
138+
input: 'fixtures/basic/main.ts',
139+
output: outputOpts,
140+
plugins: [
141+
typescript({
142+
tsconfig: 'fixtures/basic/tsconfig.json',
143+
outDir: 'fixtures/basic/dist'
144+
})
145+
],
146+
onwarn(warning) {
147+
warnings.push(warning);
148+
}
149+
});
150+
151+
// This should not throw an error
152+
const output = await getFiles(bundle, outputOpts);
153+
154+
t.deepEqual(
155+
output.map((out) => out.fileName),
156+
['fixtures/basic/dist/esm/main.js']
157+
);
158+
t.is(warnings.length, 0);
159+
}
160+
);
161+
132162
test.serial('ensures multiple outputs can be built', async (t) => {
133163
// In a rollup.config.js we would pass an array
134164
// The rollup method that's exported as a library won't do that so we must make two calls

0 commit comments

Comments
 (0)