Skip to content

Commit 23420c4

Browse files
committed
- not generating typings for files that are not actually imported
#162, #136
1 parent a39ecfc commit 23420c4

6 files changed

+41
-13
lines changed

dist/index.d.ts.map

+1-1
Original file line numberDiff line numberDiff line change

dist/rollup-plugin-typescript2.cjs.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -26714,6 +26714,7 @@ const typescript = (options) => {
2671426714
let service;
2671526715
let noErrors = true;
2671626716
const declarations = {};
26717+
const allImportedFiles = new Set();
2671726718
let _cache;
2671826719
const cache = () => {
2671926720
if (!_cache)
@@ -26808,6 +26809,7 @@ const typescript = (options) => {
2680826809
generateRound = 0; // in watch mode transform call resets generate count (used to avoid printing too many copies of the same error messages)
2680926810
if (!filter(id))
2681026811
return undefined;
26812+
allImportedFiles.add(normalize(id));
2681126813
const contextWrapper = new RollupContext(pluginOptions.verbosity, pluginOptions.abortOnError, this, "rpt2: ");
2681226814
const snapshot = servicesHost.setSnapshot(id, code);
2681326815
// getting compiled file from cache or from ts
@@ -26841,6 +26843,8 @@ const typescript = (options) => {
2684126843
printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true);
2684226844
}
2684326845
if (result) {
26846+
if (result.references)
26847+
result.references.map(normalize).map(allImportedFiles.add, allImportedFiles);
2684426848
if (watchMode && this.addWatchFile && result.references) {
2684526849
if (tsConfigPath)
2684626850
this.addWatchFile(tsConfigPath);
@@ -26891,11 +26895,16 @@ const typescript = (options) => {
2689126895
generateRound++;
2689226896
},
2689326897
_onwrite({ file, dir }) {
26894-
if (parsedConfig.options.declaration) {
26898+
if (!parsedConfig.options.declaration)
26899+
return;
2689526900
lodash_3(parsedConfig.fileNames, (name) => {
2689626901
const key = normalize(name);
26897-
if (lodash_9(declarations, key) || !filter(key))
26902+
if (lodash_9(declarations, key))
26903+
return;
26904+
if (!allImportedFiles.has(key)) {
26905+
context.debug(() => `skipping declarations for unused '${key}'`);
2689826906
return;
26907+
}
2689926908
context.debug(() => `generating missed declarations for '${key}'`);
2690026909
const output = service.getEmitOutput(key, true);
2690126910
const out = convertEmitOutput(output);
@@ -26929,7 +26938,6 @@ const typescript = (options) => {
2692926938
writeDeclaration(key, ".d.ts", type);
2693026939
writeDeclaration(key, ".d.ts.map", map);
2693126940
});
26932-
}
2693326941
},
2693426942
};
2693526943
return self;

dist/rollup-plugin-typescript2.cjs.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rollup-plugin-typescript2.es.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -26710,6 +26710,7 @@ const typescript = (options) => {
2671026710
let service;
2671126711
let noErrors = true;
2671226712
const declarations = {};
26713+
const allImportedFiles = new Set();
2671326714
let _cache;
2671426715
const cache = () => {
2671526716
if (!_cache)
@@ -26804,6 +26805,7 @@ const typescript = (options) => {
2680426805
generateRound = 0; // in watch mode transform call resets generate count (used to avoid printing too many copies of the same error messages)
2680526806
if (!filter(id))
2680626807
return undefined;
26808+
allImportedFiles.add(normalize(id));
2680726809
const contextWrapper = new RollupContext(pluginOptions.verbosity, pluginOptions.abortOnError, this, "rpt2: ");
2680826810
const snapshot = servicesHost.setSnapshot(id, code);
2680926811
// getting compiled file from cache or from ts
@@ -26837,6 +26839,8 @@ const typescript = (options) => {
2683726839
printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true);
2683826840
}
2683926841
if (result) {
26842+
if (result.references)
26843+
result.references.map(normalize).map(allImportedFiles.add, allImportedFiles);
2684026844
if (watchMode && this.addWatchFile && result.references) {
2684126845
if (tsConfigPath)
2684226846
this.addWatchFile(tsConfigPath);
@@ -26887,11 +26891,16 @@ const typescript = (options) => {
2688726891
generateRound++;
2688826892
},
2688926893
_onwrite({ file, dir }) {
26890-
if (parsedConfig.options.declaration) {
26894+
if (!parsedConfig.options.declaration)
26895+
return;
2689126896
lodash_3(parsedConfig.fileNames, (name) => {
2689226897
const key = normalize(name);
26893-
if (lodash_9(declarations, key) || !filter(key))
26898+
if (lodash_9(declarations, key))
26899+
return;
26900+
if (!allImportedFiles.has(key)) {
26901+
context.debug(() => `skipping declarations for unused '${key}'`);
2689426902
return;
26903+
}
2689526904
context.debug(() => `generating missed declarations for '${key}'`);
2689626905
const output = service.getEmitOutput(key, true);
2689726906
const out = convertEmitOutput(output);
@@ -26925,7 +26934,6 @@ const typescript = (options) => {
2692526934
writeDeclaration(key, ".d.ts", type);
2692626935
writeDeclaration(key, ".d.ts.map", map);
2692726936
});
26928-
}
2692926937
},
2693026938
};
2693126939
return self;

dist/rollup-plugin-typescript2.es.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
3232
let service: tsTypes.LanguageService;
3333
let noErrors = true;
3434
const declarations: { [name: string]: { type: tsTypes.OutputFile; map?: tsTypes.OutputFile } } = {};
35+
const allImportedFiles = new Set();
3536

3637
let _cache: TsCache;
3738
const cache = (): TsCache =>
@@ -170,6 +171,8 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
170171
if (!filter(id))
171172
return undefined;
172173

174+
allImportedFiles.add(normalize(id));
175+
173176
const contextWrapper = new RollupContext(pluginOptions.verbosity, pluginOptions.abortOnError, this, "rpt2: ");
174177

175178
const snapshot = servicesHost.setSnapshot(id, code);
@@ -227,6 +230,9 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
227230

228231
if (result)
229232
{
233+
if (result.references)
234+
result.references.map(normalize).map(allImportedFiles.add, allImportedFiles);
235+
230236
if (watchMode && this.addWatchFile && result.references)
231237
{
232238
if (tsConfigPath)
@@ -306,13 +312,20 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
306312

307313
_onwrite({ file, dir }: OutputOptions): void
308314
{
309-
if (parsedConfig.options.declaration)
310-
{
315+
if (!parsedConfig.options.declaration)
316+
return;
317+
311318
_.each(parsedConfig.fileNames, (name) =>
312319
{
313320
const key = normalize(name);
314-
if (_.has(declarations, key) || !filter(key))
321+
if (_.has(declarations, key))
315322
return;
323+
if (!allImportedFiles.has(key))
324+
{
325+
context.debug(() => `skipping declarations for unused '${key}'`);
326+
return;
327+
}
328+
316329
context.debug(() => `generating missed declarations for '${key}'`);
317330
const output = service.getEmitOutput(key, true);
318331
const out = convertEmitOutput(output);
@@ -356,7 +369,6 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
356369
writeDeclaration(key, ".d.ts", type);
357370
writeDeclaration(key, ".d.ts.map", map);
358371
});
359-
}
360372
},
361373
};
362374

0 commit comments

Comments
 (0)