Skip to content

Commit a569848

Browse files
authored
fix(typescript): revert #1653 (#1880)
revert(typescript): reverts #1653 This reverts commit 40a38b1 This commit caused certain files to no longer be included in the project.
1 parent a209058 commit a569848

File tree

9 files changed

+52
-74
lines changed

9 files changed

+52
-74
lines changed

packages/typescript/src/index.ts

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from 'path';
22

33
import { createFilter } from '@rollup/pluginutils';
44

5-
import type { Plugin, PluginContext, SourceDescription } from 'rollup';
5+
import type { Plugin, SourceDescription } from 'rollup';
66
import type { Watch } from 'typescript';
77

88
import type { RollupTypescriptOptions } from '../types';
@@ -37,31 +37,6 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
3737
tslib,
3838
typescript: ts
3939
} = getPluginOptions(options);
40-
const createProgram = (context: PluginContext) =>
41-
createWatchProgram(ts, context, {
42-
formatHost,
43-
resolveModule,
44-
parsedOptions,
45-
writeFile(fileName, data, _writeByteOrderMark, _onError, sourceFiles) {
46-
if (sourceFiles) {
47-
for (const sourceFile of sourceFiles) {
48-
if (!parsedOptions.fileNames.includes(sourceFile.fileName)) {
49-
parsedOptions.fileNames.push(sourceFile.fileName);
50-
}
51-
}
52-
}
53-
54-
if (parsedOptions.options.composite || parsedOptions.options.incremental) {
55-
tsCache.cacheCode(fileName, data);
56-
}
57-
emittedFiles.set(fileName, data);
58-
},
59-
status(diagnostic) {
60-
watchProgramHelper.handleStatus(diagnostic);
61-
},
62-
transformers
63-
});
64-
6540
const tsCache = new TSCache(cacheDir);
6641
const emittedFiles = new Map<string, string>();
6742
const watchProgramHelper = new WatchProgramHelper();
@@ -81,14 +56,6 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
8156
name: 'typescript',
8257

8358
buildStart(rollupOptions) {
84-
if (typeof rollupOptions.input === 'string') {
85-
rollupOptions.input = [rollupOptions.input];
86-
}
87-
88-
if (Array.isArray(rollupOptions.input)) {
89-
parsedOptions.fileNames = rollupOptions.input.map((fileName) => path.resolve(fileName));
90-
}
91-
9259
emitParsedOptionsErrors(ts, this, parsedOptions);
9360

9461
preflight({
@@ -107,7 +74,21 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
10774
program = null;
10875
}
10976
if (!program) {
110-
program = createProgram(this);
77+
program = createWatchProgram(ts, this, {
78+
formatHost,
79+
resolveModule,
80+
parsedOptions,
81+
writeFile(fileName, data) {
82+
if (parsedOptions.options.composite || parsedOptions.options.incremental) {
83+
tsCache.cacheCode(fileName, data);
84+
}
85+
emittedFiles.set(fileName, data);
86+
},
87+
status(diagnostic) {
88+
watchProgramHelper.handleStatus(diagnostic);
89+
},
90+
transformers
91+
});
11192
}
11293
},
11394

@@ -158,6 +139,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
158139

159140
if (resolved) {
160141
if (/\.d\.[cm]?ts/.test(resolved.extension)) return null;
142+
if (!filter(resolved.resolvedFileName)) return null;
161143
return path.normalize(resolved.resolvedFileName);
162144
}
163145

@@ -167,20 +149,16 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
167149
async load(id) {
168150
if (!filter(id)) return null;
169151

170-
const resolvedId = path.resolve(id);
171-
172-
this.addWatchFile(resolvedId);
152+
this.addWatchFile(id);
173153
await watchProgramHelper.wait();
174154

175-
const fileName = normalizePath(resolvedId);
155+
const fileName = normalizePath(id);
176156
if (!parsedOptions.fileNames.includes(fileName)) {
177157
// Discovered new file that was not known when originally parsing the TypeScript config
178-
parsedOptions.fileNames.push(path.resolve(fileName));
179-
180-
createProgram(this).close();
158+
parsedOptions.fileNames.push(fileName);
181159
}
182160

183-
const output = findTypescriptOutput(ts, parsedOptions, resolvedId, emittedFiles, tsCache);
161+
const output = findTypescriptOutput(ts, parsedOptions, id, emittedFiles, tsCache);
184162

185163
return output.code != null ? (output as SourceDescription) : null;
186164
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'declared-module' {
2+
export function declaredFunction(): void;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { declaredFunction } from 'declared-module';
2+
3+
declaredFunction();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"include": ["main.ts", "declarations.d.ts"],
3+
}

packages/typescript/test/fixtures/with-invalid-sources-inside-cwd/invalid.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/typescript/test/fixtures/with-invalid-sources-inside-cwd/main.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/typescript/test/fixtures/with-invalid-sources-inside-cwd/valid.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/typescript/test/test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,7 @@ test.serial('does it support tsconfig.rootDir for filtering', async (t) => {
11541154
t.is(files.length, 1);
11551155
});
11561156

1157-
// todo: why would want to deliberately forbid resolution from outside of CWD? What problem does it solve to add such a constraint?
1158-
test.skip('does it fail for filtering with incorrect rootDir in nested projects', async (t) => {
1157+
test.serial('does it fail for filtering with incorrect rootDir in nested projects', async (t) => {
11591158
process.chdir('fixtures/root-dir/packages/test-2');
11601159
const error = await t.throwsAsync(
11611160
rollup({
@@ -1615,17 +1614,18 @@ test.serial('compiled external library', async (t) => {
16151614
t.pass();
16161615
});
16171616

1618-
test.serial(
1619-
'do not consider files that are not part of the entry point dependency graph',
1620-
async (t) => {
1621-
process.chdir('fixtures/with-invalid-sources-inside-cwd');
1622-
const input = 'main.ts';
1623-
1624-
const build = await rollup({
1625-
input,
1626-
plugins: [typescript()]
1627-
});
1617+
test.serial('observes included declarations', async (t) => {
1618+
const warnings = [];
1619+
const bundle = await rollup({
1620+
input: 'fixtures/included-declarations/main.ts',
1621+
external: ['declared-module'],
1622+
plugins: [typescript({ tsconfig: 'fixtures/included-declarations/tsconfig.json' })],
1623+
onwarn(warning) {
1624+
warnings.push(warning);
1625+
}
1626+
});
1627+
t.deepEqual(warnings, []);
16281628

1629-
t.deepEqual(build.watchFiles, [path.resolve('main.ts'), path.resolve('valid.ts')]);
1630-
}
1631-
);
1629+
const files = await getCode(bundle, { format: 'es' }, true);
1630+
t.is(files.length, 1);
1631+
});

packages/typescript/test/tslib.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { platform } from 'os';
2-
import { resolve } from 'path';
32

43
import test from 'ava';
54
import type { RollupError } from 'rollup';
@@ -63,15 +62,13 @@ test.serial('fails on bad tslib path', async (t) => {
6362
return;
6463
}
6564

66-
t.deepEqual(
67-
error.message,
68-
`Could not load fixtures/joker/tslib.js (imported by fixtures/overriding-tslib/main.ts): ENOENT: no such file or directory, open 'fixtures/joker/tslib.js'`
69-
);
70-
t.deepEqual(error.watchFiles, [
71-
resolve('fixtures/overriding-tslib/main.ts'),
72-
'fixtures/joker/tslib.js'
73-
]);
74-
t.deepEqual(error.code, 'ENOENT');
65+
if (error.watchFiles) {
66+
let [filePath] = error.watchFiles;
67+
filePath = filePath.substring(filePath.indexOf('packages'));
68+
error.watchFiles[0] = filePath;
69+
}
70+
71+
t.snapshot(error);
7572
});
7673

7774
test.serial('fails without tslib installed', async (t) => {

0 commit comments

Comments
 (0)