Skip to content

Commit

Permalink
- not generating typings for files that are not actually imported
Browse files Browse the repository at this point in the history
  • Loading branch information
ezolenko committed Aug 1, 2019
1 parent a39ecfc commit 23420c4
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 123 deletions.
2 changes: 1 addition & 1 deletion dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 47 additions & 39 deletions dist/rollup-plugin-typescript2.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26714,6 +26714,7 @@ const typescript = (options) => {
let service;
let noErrors = true;
const declarations = {};
const allImportedFiles = new Set();
let _cache;
const cache = () => {
if (!_cache)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.cjs.js.map

Large diffs are not rendered by default.

86 changes: 47 additions & 39 deletions dist/rollup-plugin-typescript2.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -26710,6 +26710,7 @@ const typescript = (options) => {
let service;
let noErrors = true;
const declarations = {};
const allImportedFiles = new Set();
let _cache;
const cache = () => {
if (!_cache)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.es.js.map

Large diffs are not rendered by default.

96 changes: 54 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) =>
{
if (!entry)
return;
const bundleFile = file;
const outputDir = dir;

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;
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
{
if (!entry)
return;

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));
}
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}'`);

_.each(declarations, ({ type, map }, key) =>
{
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
}
// 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);
});
},
};

Expand Down

0 comments on commit 23420c4

Please sign in to comment.