Skip to content

Commit c03143b

Browse files
committed
Merge branch 'marijnh-restore-emit-file'
2 parents 330a0ce + bc5b419 commit c03143b

9 files changed

+81
-82
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108)
165165
* `useTsconfigDeclarationDir`: false
166166

167167
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.
168+
169+
Set to false if any other rollup plugins need access to declaration files.
168170

169171
* `typescript`: typescript module installed with the plugin
170172

@@ -206,7 +208,7 @@ Otherwise the plugin should work in watch mode. Make sure to run a normal build
206208
### Requirements
207209

208210
TypeScript `2.4+`
209-
Rollup `1.26.0+`
211+
Rollup `1.26.3+`
210212
Node `6.4.0+` (basic es6 support)
211213

212214
### Reporting bugs

dist/index.d.ts.map

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

dist/rollup-plugin-typescript2.cjs.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -27396,11 +27396,9 @@ const typescript = (options) => {
2739627396
}
2739727397
return undefined;
2739827398
},
27399-
generateBundle(bundleOptions, _bundle, isWrite) {
27399+
generateBundle(bundleOptions) {
2740027400
self._ongenerate();
27401-
if (isWrite) {
27402-
self._onwrite(bundleOptions);
27403-
}
27401+
self._onwrite.call(this, bundleOptions);
2740427402
},
2740527403
_ongenerate() {
2740627404
context.debug(() => `generating target ${generateRound + 1}`);
@@ -27424,7 +27422,7 @@ const typescript = (options) => {
2742427422
cache().done();
2742527423
generateRound++;
2742627424
},
27427-
_onwrite({ file, dir }) {
27425+
_onwrite(_output) {
2742827426
if (!parsedConfig.options.declaration)
2742927427
return;
2743027428
lodash_3(parsedConfig.fileNames, (name) => {
@@ -27441,32 +27439,34 @@ const typescript = (options) => {
2744127439
if (out.dts)
2744227440
declarations[key] = { type: out.dts, map: out.dtsmap };
2744327441
});
27444-
const bundleFile = file;
27445-
const outputDir = dir;
27446-
const writeDeclaration = (key, extension, entry) => {
27442+
const emitDeclaration = (key, extension, entry) => {
2744727443
if (!entry)
2744827444
return;
2744927445
let fileName = entry.name;
2745027446
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
2745127447
fileName = fileName.split("?", 1) + extension;
27452-
let writeToPath;
27453-
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
27454-
// use the path provided by Typescript's LanguageService.
27455-
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
27456-
writeToPath = fileName;
27448+
// If 'useTsconfigDeclarationDir' is given in the
27449+
// plugin options, directly write to the path provided
27450+
// by Typescript's LanguageService (which may not be
27451+
// under Rollup's output directory, and thus can't be
27452+
// emitted as an asset).
27453+
if (pluginOptions.useTsconfigDeclarationDir) {
27454+
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${fileName}'`);
27455+
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
27456+
}
2745727457
else {
27458-
// Otherwise, take the directory name from the path and make sure it is absolute.
27459-
const destDirname = bundleFile ? path.dirname(bundleFile) : outputDir;
27460-
const destDirectory = path.isAbsolute(destDirname) ? destDirname : path.join(process.cwd(), destDirname);
27461-
writeToPath = path.join(destDirectory, path.relative(process.cwd(), fileName));
27458+
const relativePath = path.relative(process.cwd(), fileName);
27459+
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${relativePath}'`);
27460+
this.emitFile({
27461+
type: "asset",
27462+
source: entry.text,
27463+
fileName: relativePath,
27464+
});
2746227465
}
27463-
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
27464-
// Write the declaration file to disk.
27465-
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
2746627466
};
2746727467
lodash_3(declarations, ({ type, map }, key) => {
27468-
writeDeclaration(key, ".d.ts", type);
27469-
writeDeclaration(key, ".d.ts.map", map);
27468+
emitDeclaration(key, ".d.ts", type);
27469+
emitDeclaration(key, ".d.ts.map", map);
2747027470
});
2747127471
},
2747227472
};

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { emptyDirSync, readJsonSync, writeJsonSync, ensureFileSync, removeSync,
44
import fs, { existsSync, readdirSync, renameSync, readFileSync } from 'fs';
55
import util from 'util';
66
import os from 'os';
7-
import path__default, { normalize as normalize$1, join, dirname, isAbsolute, relative } from 'path';
7+
import path__default, { normalize as normalize$1, join, dirname, relative } from 'path';
88
import { sync as sync$4 } from 'resolve';
99

1010
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) => {
2739027390
}
2739127391
return undefined;
2739227392
},
27393-
generateBundle(bundleOptions, _bundle, isWrite) {
27393+
generateBundle(bundleOptions) {
2739427394
self._ongenerate();
27395-
if (isWrite) {
27396-
self._onwrite(bundleOptions);
27397-
}
27395+
self._onwrite.call(this, bundleOptions);
2739827396
},
2739927397
_ongenerate() {
2740027398
context.debug(() => `generating target ${generateRound + 1}`);
@@ -27418,7 +27416,7 @@ const typescript = (options) => {
2741827416
cache().done();
2741927417
generateRound++;
2742027418
},
27421-
_onwrite({ file, dir }) {
27419+
_onwrite(_output) {
2742227420
if (!parsedConfig.options.declaration)
2742327421
return;
2742427422
lodash_3(parsedConfig.fileNames, (name) => {
@@ -27435,32 +27433,34 @@ const typescript = (options) => {
2743527433
if (out.dts)
2743627434
declarations[key] = { type: out.dts, map: out.dtsmap };
2743727435
});
27438-
const bundleFile = file;
27439-
const outputDir = dir;
27440-
const writeDeclaration = (key, extension, entry) => {
27436+
const emitDeclaration = (key, extension, entry) => {
2744127437
if (!entry)
2744227438
return;
2744327439
let fileName = entry.name;
2744427440
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
2744527441
fileName = fileName.split("?", 1) + extension;
27446-
let writeToPath;
27447-
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
27448-
// use the path provided by Typescript's LanguageService.
27449-
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
27450-
writeToPath = fileName;
27442+
// If 'useTsconfigDeclarationDir' is given in the
27443+
// plugin options, directly write to the path provided
27444+
// by Typescript's LanguageService (which may not be
27445+
// under Rollup's output directory, and thus can't be
27446+
// emitted as an asset).
27447+
if (pluginOptions.useTsconfigDeclarationDir) {
27448+
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${fileName}'`);
27449+
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
27450+
}
2745127451
else {
27452-
// Otherwise, take the directory name from the path and make sure it is absolute.
27453-
const destDirname = bundleFile ? dirname(bundleFile) : outputDir;
27454-
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
27455-
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
27452+
const relativePath = relative(process.cwd(), fileName);
27453+
context.debug(() => `${safe_5("emitting declarations")} for '${key}' to '${relativePath}'`);
27454+
this.emitFile({
27455+
type: "asset",
27456+
source: entry.text,
27457+
fileName: relativePath,
27458+
});
2745627459
}
27457-
context.debug(() => `${safe_5("writing declarations")} for '${key}' to '${writeToPath}'`);
27458-
// Write the declaration file to disk.
27459-
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
2746027460
};
2746127461
lodash_3(declarations, ({ type, map }, key) => {
27462-
writeDeclaration(key, ".d.ts", type);
27463-
writeDeclaration(key, ".d.ts.map", map);
27462+
emitDeclaration(key, ".d.ts", type);
27463+
emitDeclaration(key, ".d.ts.map", map);
2746427464
});
2746527465
},
2746627466
};

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.

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"tslib": "1.10.0"
3737
},
3838
"peerDependencies": {
39-
"rollup": ">=1.26.0",
39+
"rollup": ">=1.26.3",
4040
"typescript": ">=2.4.0"
4141
},
4242
"devDependencies": {
@@ -54,7 +54,7 @@
5454
"lodash": "4.17.15",
5555
"object-hash": "1.3.1",
5656
"rimraf": "3.0.0",
57-
"rollup": "^1.26.0 ",
57+
"rollup": "^1.26.3 ",
5858
"rollup-plugin-commonjs": "10.1.0",
5959
"rollup-plugin-node-resolve": "5.2.0",
6060
"rollup-plugin-re": "1.0.7",

src/index.ts

+25-28
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { parseTsConfig } from "./parse-tsconfig";
1212
import { printDiagnostics } from "./print-diagnostics";
1313
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
1414
import { blue, red, yellow, green } from "colors/safe";
15-
import { dirname, isAbsolute, join, relative } from "path";
15+
import { relative } from "path";
1616
import { normalize } from "./normalize";
1717
import { satisfies } from "semver";
1818
import findCacheDir from "find-cache-dir";
@@ -269,13 +269,10 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
269269
return undefined;
270270
},
271271

272-
generateBundle(bundleOptions: OutputOptions, _bundle: any, isWrite: boolean): void
272+
generateBundle(this: PluginContext, bundleOptions: OutputOptions): void
273273
{
274274
self._ongenerate();
275-
if (isWrite)
276-
{
277-
self._onwrite(bundleOptions);
278-
}
275+
self._onwrite.call(this, bundleOptions);
279276
},
280277

281278
_ongenerate(): void
@@ -316,7 +313,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
316313
generateRound++;
317314
},
318315

319-
_onwrite({ file, dir }: OutputOptions): void
316+
_onwrite(this: PluginContext, _output: OutputOptions): void
320317
{
321318
if (!parsedConfig.options.declaration)
322319
return;
@@ -339,10 +336,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
339336
declarations[key] = { type: out.dts, map: out.dtsmap };
340337
});
341338

342-
const bundleFile = file;
343-
const outputDir = dir;
344-
345-
const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
339+
const emitDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
346340
{
347341
if (!entry)
348342
return;
@@ -351,29 +345,32 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
351345
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
352346
fileName = fileName.split("?", 1) + extension;
353347

354-
let writeToPath: string;
355-
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
356-
// use the path provided by Typescript's LanguageService.
357-
if ((!bundleFile && !outputDir) || pluginOptions.useTsconfigDeclarationDir)
358-
writeToPath = fileName;
359-
else
348+
// If 'useTsconfigDeclarationDir' is given in the
349+
// plugin options, directly write to the path provided
350+
// by Typescript's LanguageService (which may not be
351+
// under Rollup's output directory, and thus can't be
352+
// emitted as an asset).
353+
if (pluginOptions.useTsconfigDeclarationDir)
360354
{
361-
// Otherwise, take the directory name from the path and make sure it is absolute.
362-
const destDirname = bundleFile ? dirname(bundleFile) : outputDir as string;
363-
const destDirectory = isAbsolute(destDirname) ? destDirname : join(process.cwd(), destDirname);
364-
writeToPath = join(destDirectory, relative(process.cwd(), fileName));
355+
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${fileName}'`);
356+
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
357+
}
358+
else
359+
{
360+
const relativePath = relative(process.cwd(), fileName);
361+
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${relativePath}'`);
362+
this.emitFile({
363+
type: "asset",
364+
source: entry.text,
365+
fileName: relativePath,
366+
});
365367
}
366-
367-
context.debug(() => `${blue("writing declarations")} for '${key}' to '${writeToPath}'`);
368-
369-
// Write the declaration file to disk.
370-
tsModule.sys.writeFile(writeToPath, entry.text, entry.writeByteOrderMark);
371368
};
372369

373370
_.each(declarations, ({ type, map }, key) =>
374371
{
375-
writeDeclaration(key, ".d.ts", type);
376-
writeDeclaration(key, ".d.ts.map", map);
372+
emitDeclaration(key, ".d.ts", type);
373+
emitDeclaration(key, ".d.ts.map", map);
377374
});
378375
},
379376
};

0 commit comments

Comments
 (0)