Skip to content

Commit

Permalink
- writing out types for vue virtual modules #97
Browse files Browse the repository at this point in the history
  • Loading branch information
ezolenko committed Jul 17, 2018
1 parent 54570f6 commit 433c8fa
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 80 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.

47 changes: 23 additions & 24 deletions dist/rollup-plugin-typescript2.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20168,31 +20168,30 @@ function typescript(options) {
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file ? file : dest; // rollup 0.48+ has 'file' https://github.com/rollup/rollup/issues/1479
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = path.dirname(bundleFile);
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_2(declarations, ({ type, map }, key) => {
lodash_2([type, map], (e) => {
if (!e)
return;
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = e.name;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = path.dirname(bundleFile);
const destDirectory = path.isAbsolute(destDirname) ? destDirname : path.join(process.cwd(), destDirname);
writeToPath = path.join(destDirectory, path.relative(process.cwd(), e.name));
}
if (writeToPath.includes("?")) {
// HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.d.ts'
context.debug(() => `${safe_4("skipping declarations")} for '${key}', invalid file path`);
}
else {
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, e.text, e.writeByteOrderMark);
}
});
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".map.d.ts", map);
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.cjs.js.map

Large diffs are not rendered by default.

47 changes: 23 additions & 24 deletions dist/rollup-plugin-typescript2.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -20164,31 +20164,30 @@ function typescript(options) {
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file ? file : dest; // rollup 0.48+ has 'file' https://github.com/rollup/rollup/issues/1479
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = dirname(bundleFile);
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_2(declarations, ({ type, map }, key) => {
lodash_2([type, map], (e) => {
if (!e)
return;
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = e.name;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = dirname(bundleFile);
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), e.name));
}
if (writeToPath.includes("?")) {
// HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.d.ts'
context.debug(() => `${safe_4("skipping declarations")} for '${key}', invalid file path`);
}
else {
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, e.text, e.writeByteOrderMark);
}
});
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".map.d.ts", map);
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.es.js.map

Large diffs are not rendered by default.

57 changes: 28 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,39 +308,38 @@ export default function typescript(options?: Partial<IOptions>)

const bundleFile = file ? file : dest; // rollup 0.48+ has 'file' https://github.com/rollup/rollup/issues/1479

_.each(declarations, ({ type, map }, key) =>
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
{
_.each([type, map], (e) =>
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = fileName;
else
{
if (!e)
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = e.name;
else
{
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = dirname(bundleFile);
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), e.name));
}
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = dirname(bundleFile);
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
}

if (writeToPath.includes("?"))
{
// HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.d.ts'
context.debug(() => `${yellow("skipping declarations")} for '${key}', invalid file path`);
}
else
{
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);

// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, e.text, e.writeByteOrderMark);
}
});
// 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, ".map.d.ts", map);
});
}
},
Expand Down

0 comments on commit 433c8fa

Please sign in to comment.