Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): only emit tsbuildinfo file when there is something to emit #771

Merged
merged 2 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
shellscape marked this conversation as resolved.
Show resolved Hide resolved
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