Skip to content

Commit 23420c4

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

6 files changed

+151
-123
lines changed

dist/index.d.ts.map

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

dist/rollup-plugin-typescript2.cjs.js

+47-39
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,45 +26895,49 @@ const typescript = (options) => {
2689126895
generateRound++;
2689226896
},
2689326897
_onwrite({ file, dir }) {
26894-
if (parsedConfig.options.declaration) {
26895-
lodash_3(parsedConfig.fileNames, (name) => {
26896-
const key = normalize(name);
26897-
if (lodash_9(declarations, key) || !filter(key))
26898-
return;
26899-
context.debug(() => `generating missed declarations for '${key}'`);
26900-
const output = service.getEmitOutput(key, true);
26901-
const out = convertEmitOutput(output);
26902-
if (out.dts)
26903-
declarations[key] = { type: out.dts, map: out.dtsmap };
26904-
});
26905-
const bundleFile = file;
26906-
const outputDir = dir;
26907-
const writeDeclaration = (key, extension, entry) => {
26908-
if (!entry)
26909-
return;
26910-
let fileName = entry.name;
26911-
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
26912-
fileName = fileName.split("?", 1) + extension;
26913-
let writeToPath;
26914-
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
26915-
// use the path provided by Typescript's LanguageService.
26916-
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
26917-
writeToPath = fileName;
26918-
else {
26919-
// Otherwise, take the directory name from the path and make sure it is absolute.
26920-
const destDirname = bundleFile ? path.dirname(bundleFile) : outputDir;
26921-
const destDirectory = path.isAbsolute(destDirname) ? destDirname : path.join(process.cwd(), destDirname);
26922-
writeToPath = path.join(destDirectory, path.relative(process.cwd(), fileName));
26923-
}
26924-
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
26925-
// Write the declaration file to disk.
26926-
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
26927-
};
26928-
lodash_3(declarations, ({ type, map }, key) => {
26929-
writeDeclaration(key, ".d.ts", type);
26930-
writeDeclaration(key, ".d.ts.map", map);
26931-
});
26932-
}
26898+
if (!parsedConfig.options.declaration)
26899+
return;
26900+
lodash_3(parsedConfig.fileNames, (name) => {
26901+
const key = normalize(name);
26902+
if (lodash_9(declarations, key))
26903+
return;
26904+
if (!allImportedFiles.has(key)) {
26905+
context.debug(() => `skipping declarations for unused '${key}'`);
26906+
return;
26907+
}
26908+
context.debug(() => `generating missed declarations for '${key}'`);
26909+
const output = service.getEmitOutput(key, true);
26910+
const out = convertEmitOutput(output);
26911+
if (out.dts)
26912+
declarations[key] = { type: out.dts, map: out.dtsmap };
26913+
});
26914+
const bundleFile = file;
26915+
const outputDir = dir;
26916+
const writeDeclaration = (key, extension, entry) => {
26917+
if (!entry)
26918+
return;
26919+
let fileName = entry.name;
26920+
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
26921+
fileName = fileName.split("?", 1) + extension;
26922+
let writeToPath;
26923+
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
26924+
// use the path provided by Typescript's LanguageService.
26925+
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
26926+
writeToPath = fileName;
26927+
else {
26928+
// Otherwise, take the directory name from the path and make sure it is absolute.
26929+
const destDirname = bundleFile ? path.dirname(bundleFile) : outputDir;
26930+
const destDirectory = path.isAbsolute(destDirname) ? destDirname : path.join(process.cwd(), destDirname);
26931+
writeToPath = path.join(destDirectory, path.relative(process.cwd(), fileName));
26932+
}
26933+
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
26934+
// Write the declaration file to disk.
26935+
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
26936+
};
26937+
lodash_3(declarations, ({ type, map }, key) => {
26938+
writeDeclaration(key, ".d.ts", type);
26939+
writeDeclaration(key, ".d.ts.map", map);
26940+
});
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

+47-39
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,45 +26891,49 @@ const typescript = (options) => {
2688726891
generateRound++;
2688826892
},
2688926893
_onwrite({ file, dir }) {
26890-
if (parsedConfig.options.declaration) {
26891-
lodash_3(parsedConfig.fileNames, (name) => {
26892-
const key = normalize(name);
26893-
if (lodash_9(declarations, key) || !filter(key))
26894-
return;
26895-
context.debug(() => `generating missed declarations for '${key}'`);
26896-
const output = service.getEmitOutput(key, true);
26897-
const out = convertEmitOutput(output);
26898-
if (out.dts)
26899-
declarations[key] = { type: out.dts, map: out.dtsmap };
26900-
});
26901-
const bundleFile = file;
26902-
const outputDir = dir;
26903-
const writeDeclaration = (key, extension, entry) => {
26904-
if (!entry)
26905-
return;
26906-
let fileName = entry.name;
26907-
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
26908-
fileName = fileName.split("?", 1) + extension;
26909-
let writeToPath;
26910-
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
26911-
// use the path provided by Typescript's LanguageService.
26912-
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
26913-
writeToPath = fileName;
26914-
else {
26915-
// Otherwise, take the directory name from the path and make sure it is absolute.
26916-
const destDirname = bundleFile ? dirname(bundleFile) : outputDir;
26917-
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
26918-
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
26919-
}
26920-
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
26921-
// Write the declaration file to disk.
26922-
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
26923-
};
26924-
lodash_3(declarations, ({ type, map }, key) => {
26925-
writeDeclaration(key, ".d.ts", type);
26926-
writeDeclaration(key, ".d.ts.map", map);
26927-
});
26928-
}
26894+
if (!parsedConfig.options.declaration)
26895+
return;
26896+
lodash_3(parsedConfig.fileNames, (name) => {
26897+
const key = normalize(name);
26898+
if (lodash_9(declarations, key))
26899+
return;
26900+
if (!allImportedFiles.has(key)) {
26901+
context.debug(() => `skipping declarations for unused '${key}'`);
26902+
return;
26903+
}
26904+
context.debug(() => `generating missed declarations for '${key}'`);
26905+
const output = service.getEmitOutput(key, true);
26906+
const out = convertEmitOutput(output);
26907+
if (out.dts)
26908+
declarations[key] = { type: out.dts, map: out.dtsmap };
26909+
});
26910+
const bundleFile = file;
26911+
const outputDir = dir;
26912+
const writeDeclaration = (key, extension, entry) => {
26913+
if (!entry)
26914+
return;
26915+
let fileName = entry.name;
26916+
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
26917+
fileName = fileName.split("?", 1) + extension;
26918+
let writeToPath;
26919+
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
26920+
// use the path provided by Typescript's LanguageService.
26921+
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
26922+
writeToPath = fileName;
26923+
else {
26924+
// Otherwise, take the directory name from the path and make sure it is absolute.
26925+
const destDirname = bundleFile ? dirname(bundleFile) : outputDir;
26926+
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
26927+
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
26928+
}
26929+
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
26930+
// Write the declaration file to disk.
26931+
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
26932+
};
26933+
lodash_3(declarations, ({ type, map }, key) => {
26934+
writeDeclaration(key, ".d.ts", type);
26935+
writeDeclaration(key, ".d.ts.map", map);
26936+
});
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

+54-42
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,57 +312,63 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
306312

307313
_onwrite({ file, dir }: OutputOptions): void
308314
{
309-
if (parsedConfig.options.declaration)
315+
if (!parsedConfig.options.declaration)
316+
return;
317+
318+
_.each(parsedConfig.fileNames, (name) =>
310319
{
311-
_.each(parsedConfig.fileNames, (name) =>
320+
const key = normalize(name);
321+
if (_.has(declarations, key))
322+
return;
323+
if (!allImportedFiles.has(key))
312324
{
313-
const key = normalize(name);
314-
if (_.has(declarations, key) || !filter(key))
315-
return;
316-
context.debug(() => `generating missed declarations for '${key}'`);
317-
const output = service.getEmitOutput(key, true);
318-
const out = convertEmitOutput(output);
319-
if (out.dts)
320-
declarations[key] = { type: out.dts, map: out.dtsmap };
321-
});
325+
context.debug(() => `skipping declarations for unused '${key}'`);
326+
return;
327+
}
322328

323-
const bundleFile = file;
324-
const outputDir = dir;
329+
context.debug(() => `generating missed declarations for '${key}'`);
330+
const output = service.getEmitOutput(key, true);
331+
const out = convertEmitOutput(output);
332+
if (out.dts)
333+
declarations[key] = { type: out.dts, map: out.dtsmap };
334+
});
325335

326-
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
327-
{
328-
if (!entry)
329-
return;
336+
const bundleFile = file;
337+
const outputDir = dir;
330338

331-
let fileName = entry.name;
332-
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
333-
fileName = fileName.split("?", 1) + extension;
339+
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
340+
{
341+
if (!entry)
342+
return;
334343

335-
let writeToPath: string;
336-
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
337-
// use the path provided by Typescript's LanguageService.
338-
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
339-
writeToPath = fileName;
340-
else
341-
{
342-
// Otherwise, take the directory name from the path and make sure it is absolute.
343-
const destDirname = bundleFile ? dirname(bundleFile) : outputDir as string;
344-
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
345-
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
346-
}
344+
let fileName = entry.name;
345+
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
346+
fileName = fileName.split("?", 1) + extension;
347347

348-
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);
348+
let writeToPath: string;
349+
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
350+
// use the path provided by Typescript's LanguageService.
351+
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
352+
writeToPath = fileName;
353+
else
354+
{
355+
// Otherwise, take the directory name from the path and make sure it is absolute.
356+
const destDirname = bundleFile ? dirname(bundleFile) : outputDir as string;
357+
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
358+
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
359+
}
349360

350-
// Write the declaration file to disk.
351-
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
352-
};
361+
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);
353362

354-
_.each(declarations, ({ type, map }, key) =>
355-
{
356-
writeDeclaration(key, ".d.ts", type);
357-
writeDeclaration(key, ".d.ts.map", map);
358-
});
359-
}
363+
// Write the declaration file to disk.
364+
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
365+
};
366+
367+
_.each(declarations, ({ type, map }, key) =>
368+
{
369+
writeDeclaration(key, ".d.ts", type);
370+
writeDeclaration(key, ".d.ts.map", map);
371+
});
360372
},
361373
};
362374

0 commit comments

Comments
 (0)