Merge branch 'marijnh-restore-emit-file'

This commit is contained in:
Eugene Zolenko 2019-11-05 10:34:58 -07:00
commit c03143b560
9 changed files with 83 additions and 84 deletions

View File

@ -165,6 +165,8 @@ See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108)
* `useTsconfigDeclarationDir`: false
If true, declaration files will be emitted in the directory given in the tsconfig. If false, the declaration files will be placed inside the destination directory given in the Rollup configuration.
Set to false if any other rollup plugins need access to declaration files.
* `typescript`: typescript module installed with the plugin
@ -206,7 +208,7 @@ Otherwise the plugin should work in watch mode. Make sure to run a normal build
### Requirements
TypeScript `2.4+`
Rollup `1.26.0+`
Rollup `1.26.3+`
Node `6.4.0+` (basic es6 support)
### Reporting bugs

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,CAwW7C,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,CAqW7C,CAAC;AAEF,eAAe,UAAU,CAAC"}

View File

@ -27396,11 +27396,9 @@ const typescript = (options) => {
}
return undefined;
},
generateBundle(bundleOptions, _bundle, isWrite) {
generateBundle(bundleOptions) {
self._ongenerate();
if (isWrite) {
self._onwrite(bundleOptions);
}
self._onwrite.call(this, bundleOptions);
},
_ongenerate() {
context.debug(() => `generating target ${generateRound + 1}`);
@ -27424,7 +27422,7 @@ const typescript = (options) => {
cache().done();
generateRound++;
},
_onwrite({ file, dir }) {
_onwrite(_output) {
if (!parsedConfig.options.declaration)
return;
lodash_3(parsedConfig.fileNames, (name) => {
@ -27441,32 +27439,34 @@ const typescript = (options) => {
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key, extension, entry) => {
const emitDeclaration = (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));
// If 'useTsconfigDeclarationDir' is given in the
// plugin options, directly write to the path provided
// by Typescript's LanguageService (which may not be
// under Rollup's output directory, and thus can't be
// emitted as an asset).
if (pluginOptions.useTsconfigDeclarationDir) {
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${fileName}'`);
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
}
else {
const relativePath = path.relative(process.cwd(), fileName);
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${relativePath}'`);
this.emitFile({
type: "asset",
source: entry.text,
fileName: relativePath,
});
}
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);
emitDeclaration(key, ".d.ts", type);
emitDeclaration(key, ".d.ts.map", map);
});
},
};

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@ import { emptyDirSync, readJsonSync, writeJsonSync, ensureFileSync, removeSync,
import fs, { existsSync, readdirSync, renameSync, readFileSync } from 'fs';
import util from 'util';
import os from 'os';
import path__default, { normalize as normalize$1, join, dirname, isAbsolute, relative } from 'path';
import path__default, { normalize as normalize$1, join, dirname, relative } from 'path';
import { sync as sync$4 } from 'resolve';
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@ -27390,11 +27390,9 @@ const typescript = (options) => {
}
return undefined;
},
generateBundle(bundleOptions, _bundle, isWrite) {
generateBundle(bundleOptions) {
self._ongenerate();
if (isWrite) {
self._onwrite(bundleOptions);
}
self._onwrite.call(this, bundleOptions);
},
_ongenerate() {
context.debug(() => `generating target ${generateRound + 1}`);
@ -27418,7 +27416,7 @@ const typescript = (options) => {
cache().done();
generateRound++;
},
_onwrite({ file, dir }) {
_onwrite(_output) {
if (!parsedConfig.options.declaration)
return;
lodash_3(parsedConfig.fileNames, (name) => {
@ -27435,32 +27433,34 @@ const typescript = (options) => {
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key, extension, entry) => {
const emitDeclaration = (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));
// If 'useTsconfigDeclarationDir' is given in the
// plugin options, directly write to the path provided
// by Typescript's LanguageService (which may not be
// under Rollup's output directory, and thus can't be
// emitted as an asset).
if (pluginOptions.useTsconfigDeclarationDir) {
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${fileName}'`);
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
}
else {
const relativePath = relative(process.cwd(), fileName);
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${relativePath}'`);
this.emitFile({
type: "asset",
source: entry.text,
fileName: relativePath,
});
}
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);
emitDeclaration(key, ".d.ts", type);
emitDeclaration(key, ".d.ts.map", map);
});
},
};

File diff suppressed because one or more lines are too long

6
package-lock.json generated
View File

@ -1014,9 +1014,9 @@
}
},
"rollup": {
"version": "1.26.0",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-1.26.0.tgz",
"integrity": "sha512-5HljNYn9icFvXX+Oe97qY5TWvnWhKqgGT0HGeWWqFPx7w7+Anzg7dfHMtUif7YYy6QxAgynDSwK6uxbgcrVUxw==",
"version": "1.26.3",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-1.26.3.tgz",
"integrity": "sha512-8MhY/M8gnv3Q/pQQSWYWzbeJ5J1C5anCNY5BK1kV8Yzw9RFS0FF4lbLt+uyPO3wLKWXSXrhAL5pWL85TZAh+Sw==",
"dev": true,
"requires": {
"@types/estree": "0.0.39",

View File

@ -36,7 +36,7 @@
"tslib": "1.10.0"
},
"peerDependencies": {
"rollup": ">=1.26.0",
"rollup": ">=1.26.3",
"typescript": ">=2.4.0"
},
"devDependencies": {
@ -54,7 +54,7 @@
"lodash": "4.17.15",
"object-hash": "1.3.1",
"rimraf": "3.0.0",
"rollup": "^1.26.0 ",
"rollup": "^1.26.3 ",
"rollup-plugin-commonjs": "10.1.0",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-re": "1.0.7",

View File

@ -12,7 +12,7 @@ import { parseTsConfig } from "./parse-tsconfig";
import { printDiagnostics } from "./print-diagnostics";
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
import { blue, red, yellow, green } from "colors/safe";
import { dirname, isAbsolute, join, relative } from "path";
import { relative } from "path";
import { normalize } from "./normalize";
import { satisfies } from "semver";
import findCacheDir from "find-cache-dir";
@ -269,13 +269,10 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
return undefined;
},
generateBundle(bundleOptions: OutputOptions, _bundle: any, isWrite: boolean): void
generateBundle(this: PluginContext, bundleOptions: OutputOptions): void
{
self._ongenerate();
if (isWrite)
{
self._onwrite(bundleOptions);
}
self._onwrite.call(this, bundleOptions);
},
_ongenerate(): void
@ -316,7 +313,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
generateRound++;
},
_onwrite({ file, dir }: OutputOptions): void
_onwrite(this: PluginContext, _output: OutputOptions): void
{
if (!parsedConfig.options.declaration)
return;
@ -339,10 +336,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const bundleFile = file;
const outputDir = dir;
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
const emitDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
{
if (!entry)
return;
@ -351,29 +345,32 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
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 'useTsconfigDeclarationDir' is given in the
// plugin options, directly write to the path provided
// by Typescript's LanguageService (which may not be
// under Rollup's output directory, and thus can't be
// emitted as an asset).
if (pluginOptions.useTsconfigDeclarationDir)
{
// 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));
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${fileName}'`);
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
}
else
{
const relativePath = relative(process.cwd(), fileName);
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${relativePath}'`);
this.emitFile({
type: "asset",
source: entry.text,
fileName: relativePath,
});
}
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);
emitDeclaration(key, ".d.ts", type);
emitDeclaration(key, ".d.ts.map", map);
});
},
};