- not generating typings for files that are not actually imported

#162, #136
This commit is contained in:
Eugene Zolenko 2019-08-01 15:45:37 -06:00
parent a39ecfc9eb
commit 23420c4b89
6 changed files with 152 additions and 124 deletions

2
dist/index.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AASpC,OAAO,EAAE,UAAU,EAAgG,MAAM,QAAQ,CAAC;AAGlI,QAAA,MAAM,UAAU,EAAE,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAsV7C,CAAC;AAEF,eAAe,UAAU,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AASpC,OAAO,EAAE,UAAU,EAAgG,MAAM,QAAQ,CAAC;AAGlI,QAAA,MAAM,UAAU,EAAE,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAkW7C,CAAC;AAEF,eAAe,UAAU,CAAC"}

View File

@ -26714,6 +26714,7 @@ const typescript = (options) => {
let service;
let noErrors = true;
const declarations = {};
const allImportedFiles = new Set();
let _cache;
const cache = () => {
if (!_cache)
@ -26808,6 +26809,7 @@ const typescript = (options) => {
generateRound = 0; // in watch mode transform call resets generate count (used to avoid printing too many copies of the same error messages)
if (!filter(id))
return undefined;
allImportedFiles.add(normalize(id));
const contextWrapper = new RollupContext(pluginOptions.verbosity, pluginOptions.abortOnError, this, "rpt2: ");
const snapshot = servicesHost.setSnapshot(id, code);
// getting compiled file from cache or from ts
@ -26841,6 +26843,8 @@ const typescript = (options) => {
printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true);
}
if (result) {
if (result.references)
result.references.map(normalize).map(allImportedFiles.add, allImportedFiles);
if (watchMode && this.addWatchFile && result.references) {
if (tsConfigPath)
this.addWatchFile(tsConfigPath);
@ -26891,45 +26895,49 @@ const typescript = (options) => {
generateRound++;
},
_onwrite({ file, dir }) {
if (parsedConfig.options.declaration) {
lodash_3(parsedConfig.fileNames, (name) => {
const key = normalize(name);
if (lodash_9(declarations, key) || !filter(key))
return;
context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key, extension, entry) => {
if (!entry)
return;
let fileName = entry.name;
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
fileName = fileName.split("?", 1) + extension;
let writeToPath;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = bundleFile ? path.dirname(bundleFile) : outputDir;
const destDirectory = path.isAbsolute(destDirname) ? destDirname : path.join(process.cwd(), destDirname);
writeToPath = path.join(destDirectory, path.relative(process.cwd(), fileName));
}
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
};
lodash_3(declarations, ({ type, map }, key) => {
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
}
if (!parsedConfig.options.declaration)
return;
lodash_3(parsedConfig.fileNames, (name) => {
const key = normalize(name);
if (lodash_9(declarations, key))
return;
if (!allImportedFiles.has(key)) {
context.debug(() => `skipping declarations for unused '${key}'`);
return;
}
context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key, extension, entry) => {
if (!entry)
return;
let fileName = entry.name;
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
fileName = fileName.split("?", 1) + extension;
let writeToPath;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = bundleFile ? path.dirname(bundleFile) : outputDir;
const destDirectory = path.isAbsolute(destDirname) ? destDirname : path.join(process.cwd(), destDirname);
writeToPath = path.join(destDirectory, path.relative(process.cwd(), fileName));
}
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
};
lodash_3(declarations, ({ type, map }, key) => {
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
},
};
return self;

File diff suppressed because one or more lines are too long

View File

@ -26710,6 +26710,7 @@ const typescript = (options) => {
let service;
let noErrors = true;
const declarations = {};
const allImportedFiles = new Set();
let _cache;
const cache = () => {
if (!_cache)
@ -26804,6 +26805,7 @@ const typescript = (options) => {
generateRound = 0; // in watch mode transform call resets generate count (used to avoid printing too many copies of the same error messages)
if (!filter(id))
return undefined;
allImportedFiles.add(normalize(id));
const contextWrapper = new RollupContext(pluginOptions.verbosity, pluginOptions.abortOnError, this, "rpt2: ");
const snapshot = servicesHost.setSnapshot(id, code);
// getting compiled file from cache or from ts
@ -26837,6 +26839,8 @@ const typescript = (options) => {
printDiagnostics(contextWrapper, diagnostics, parsedConfig.options.pretty === true);
}
if (result) {
if (result.references)
result.references.map(normalize).map(allImportedFiles.add, allImportedFiles);
if (watchMode && this.addWatchFile && result.references) {
if (tsConfigPath)
this.addWatchFile(tsConfigPath);
@ -26887,45 +26891,49 @@ const typescript = (options) => {
generateRound++;
},
_onwrite({ file, dir }) {
if (parsedConfig.options.declaration) {
lodash_3(parsedConfig.fileNames, (name) => {
const key = normalize(name);
if (lodash_9(declarations, key) || !filter(key))
return;
context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key, extension, entry) => {
if (!entry)
return;
let fileName = entry.name;
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
fileName = fileName.split("?", 1) + extension;
let writeToPath;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = bundleFile ? dirname(bundleFile) : outputDir;
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
}
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
};
lodash_3(declarations, ({ type, map }, key) => {
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
}
if (!parsedConfig.options.declaration)
return;
lodash_3(parsedConfig.fileNames, (name) => {
const key = normalize(name);
if (lodash_9(declarations, key))
return;
if (!allImportedFiles.has(key)) {
context.debug(() => `skipping declarations for unused '${key}'`);
return;
}
context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key, extension, entry) => {
if (!entry)
return;
let fileName = entry.name;
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
fileName = fileName.split("?", 1) + extension;
let writeToPath;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = bundleFile ? dirname(bundleFile) : outputDir;
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
}
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
};
lodash_3(declarations, ({ type, map }, key) => {
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
},
};
return self;

File diff suppressed because one or more lines are too long

View File

@ -32,6 +32,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
let service: tsTypes.LanguageService;
let noErrors = true;
const declarations: { [name: string]: { type: tsTypes.OutputFile; map?: tsTypes.OutputFile } } = {};
const allImportedFiles = new Set();
let _cache: TsCache;
const cache = (): TsCache =>
@ -170,6 +171,8 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
if (!filter(id))
return undefined;
allImportedFiles.add(normalize(id));
const contextWrapper = new RollupContext(pluginOptions.verbosity, pluginOptions.abortOnError, this, "rpt2: ");
const snapshot = servicesHost.setSnapshot(id, code);
@ -227,6 +230,9 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
if (result)
{
if (result.references)
result.references.map(normalize).map(allImportedFiles.add, allImportedFiles);
if (watchMode && this.addWatchFile && result.references)
{
if (tsConfigPath)
@ -306,57 +312,63 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
_onwrite({ file, dir }: OutputOptions): void
{
if (parsedConfig.options.declaration)
if (!parsedConfig.options.declaration)
return;
_.each(parsedConfig.fileNames, (name) =>
{
_.each(parsedConfig.fileNames, (name) =>
const key = normalize(name);
if (_.has(declarations, key))
return;
if (!allImportedFiles.has(key))
{
const key = normalize(name);
if (_.has(declarations, key) || !filter(key))
return;
context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
context.debug(() => `skipping declarations for unused '${key}'`);
return;
}
const bundleFile = file;
const outputDir = dir;
context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
{
if (!entry)
return;
let fileName = entry.name;
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
fileName = fileName.split("?", 1) + extension;
let writeToPath: string;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else
{
if (!entry)
return;
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = bundleFile ? dirname(bundleFile) : outputDir as string;
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
}
let fileName = entry.name;
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
fileName = fileName.split("?", 1) + extension;
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);
let writeToPath: string;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else
{
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = bundleFile ? dirname(bundleFile) : outputDir as string;
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
}
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
};
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
};
_.each(declarations, ({ type, map }, key) =>
{
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
}
_.each(declarations, ({ type, map }, key) =>
{
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
},
};