Skip to content

Commit

Permalink
fix(typescript): only emit tsbuildinfo file when there is something t…
Browse files Browse the repository at this point in the history
…o emit (#771)

* fix(typescript): only emit tsbuildinfo file when there is something to emit (fixes #681)

* test(typescript): fix test failing on Node.js v10
  • Loading branch information
cherryblossom000 authored Jan 28, 2021
1 parent 6b4b7b6 commit 2711aa8
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 54 deletions.
14 changes: 9 additions & 5 deletions packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi

const tsBuildInfoPath = ts.getTsBuildInfoEmitOutputFilePath(parsedOptions.options);
if (tsBuildInfoPath) {
this.emitFile({
type: 'asset',
fileName: normalizePath(path.relative(outputOptions.dir!, tsBuildInfoPath)),
source: emittedFiles.get(tsBuildInfoPath)
});
const tsBuildInfoSource = emittedFiles.get(tsBuildInfoPath);
// https://github.com/rollup/plugins/issues/681
if (tsBuildInfoSource) {
this.emitFile({
type: 'asset',
fileName: normalizePath(path.relative(outputOptions.dir!, tsBuildInfoPath)),
source: tsBuildInfoSource
});
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type AnswerToQuestion = string | undefined;

const answer: AnswerToQuestion = '42';

// eslint-disable-next-line no-console
console.log(`the answer is ${answer}`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"incremental": true,
"outDir": "./dist",
"tsBuildInfoFile": "./dist/.tsbuildinfo"
}
}
150 changes: 101 additions & 49 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,29 @@ test.serial('ensures multiple outputs can be built', async (t) => {
plugins: [typescript({ tsconfig: 'fixtures/multiple-files/tsconfig.json' })]
});

const output1 = await getCode(bundle1, { file: 'fixtures/multiple-files/index.js', format: 'cjs' }, true);
const output1 = await getCode(
bundle1,
{ file: 'fixtures/multiple-files/index.js', format: 'cjs' },
true
);

const bundle2 = await rollup({
input: 'fixtures/multiple-files/src/server.ts',
plugins: [typescript({ tsconfig: 'fixtures/multiple-files/tsconfig.json' })]
});

const output2 = await getCode(bundle2, { file: 'fixtures/multiple-files/server.js', format: 'cjs' }, true);

t.deepEqual(
[...new Set(output1.concat(output2).map((out) => out.fileName))].sort(),
['index.d.ts', 'index.js', 'server.d.ts', 'server.js']
const output2 = await getCode(
bundle2,
{ file: 'fixtures/multiple-files/server.js', format: 'cjs' },
true
);

t.deepEqual([...new Set(output1.concat(output2).map((out) => out.fileName))].sort(), [
'index.d.ts',
'index.js',
'server.d.ts',
'server.js'
]);
});

test.serial('relative paths in tsconfig.json are resolved relative to the file', async (t) => {
Expand Down Expand Up @@ -877,6 +887,50 @@ test.serial('supports consecutive incremental rebuilds', async (t) => {
);
});

// https://github.com/rollup/plugins/issues/681
test.serial('supports incremental rebuilds with no change to cache', async (t) => {
process.chdir('fixtures/incremental-output-cache');
const cleanup = () => {
let files;
try {
files = fs.readdirSync('dist');
} catch (error) {
if (error.code === 'ENOENT') return;
throw error;
}
files.forEach((file) => fs.unlinkSync(path.join('dist', file)));
};

cleanup();

const firstBundle = await rollup({
input: 'main.ts',
plugins: [typescript()],
onwarn
});

const firstRun = await getCode(firstBundle, { format: 'esm', dir: 'dist' }, true);
t.deepEqual(
firstRun.map((out) => out.fileName),
['main.js', '.tsbuildinfo']
);
await firstBundle.write({ dir: 'dist' });

const secondBundle = await rollup({
input: 'main.ts',
plugins: [typescript()],
onwarn
});
const secondRun = await getCode(secondBundle, { format: 'esm', dir: 'dist' }, true);
t.deepEqual(
secondRun.map((out) => out.fileName),
// .tsbuildinfo should not be emitted
['main.js']
);

cleanup();
});

test.serial.skip('supports project references', async (t) => {
process.chdir('fixtures/project-references');

Expand Down Expand Up @@ -1088,50 +1142,48 @@ test('supports custom transformers', async (t) => {
});

function fakeTypescript(custom) {
return Object.assign(
{
sys: ts.sys,
createModuleResolutionCache: ts.createModuleResolutionCache,
ModuleKind: ts.ModuleKind,

transpileModule() {
return {
outputText: '',
diagnostics: [],
sourceMapText: JSON.stringify({ mappings: '' })
};
},

createWatchCompilerHost() {
return {
afterProgramCreate() {}
};
},

createWatchProgram() {
return {};
},

parseJsonConfigFileContent(json, host, basePath, existingOptions) {
return {
options: {
...json.compilerOptions,
...existingOptions
},
fileNames: [],
errors: []
};
},

getOutputFileNames(_, id) {
return [id.replace(/\.tsx?/, '.js')];
},

// eslint-disable-next-line no-undefined
getTsBuildInfoEmitOutputFilePath: () => undefined
return {
sys: ts.sys,
createModuleResolutionCache: ts.createModuleResolutionCache,
ModuleKind: ts.ModuleKind,

transpileModule() {
return {
outputText: '',
diagnostics: [],
sourceMapText: JSON.stringify({ mappings: '' })
};
},
custom
);

createWatchCompilerHost() {
return {
afterProgramCreate() {}
};
},

createWatchProgram() {
return {};
},

parseJsonConfigFileContent(json, host, basePath, existingOptions) {
return {
options: {
...json.compilerOptions,
...existingOptions
},
fileNames: [],
errors: []
};
},

getOutputFileNames(_, id) {
return [id.replace(/\.tsx?/, '.js')];
},

// eslint-disable-next-line no-undefined
getTsBuildInfoEmitOutputFilePath: () => undefined,
...custom
};
}

test.serial('picks up on newly included typescript files in watch mode', async (t) => {
Expand Down

0 comments on commit 2711aa8

Please sign in to comment.