Skip to content

Commit 330a0ce

Browse files
committed
- reverting #181
2 parents f330ba5 + c35e35d commit 330a0ce

7 files changed

+76
-75
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108)
164164

165165
* `useTsconfigDeclarationDir`: false
166166

167-
If true, declaration files will be written in the directory given in the tsconfig. If false, the declaration files will be emitted and rollup will place them inside the destination directory given in the Rollup configuration.
168-
169-
Set to false if you want other plugins to see type declarations.
167+
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.
170168

171169
* `typescript`: typescript module installed with the plugin
172170

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,9 +27396,11 @@ const typescript = (options) => {
2739627396
}
2739727397
return undefined;
2739827398
},
27399-
generateBundle(bundleOptions) {
27399+
generateBundle(bundleOptions, _bundle, isWrite) {
2740027400
self._ongenerate();
27401-
self._onwrite.call(this, bundleOptions);
27401+
if (isWrite) {
27402+
self._onwrite(bundleOptions);
27403+
}
2740227404
},
2740327405
_ongenerate() {
2740427406
context.debug(() => `generating target ${generateRound + 1}`);
@@ -27422,7 +27424,7 @@ const typescript = (options) => {
2742227424
cache().done();
2742327425
generateRound++;
2742427426
},
27425-
_onwrite(_output) {
27427+
_onwrite({ file, dir }) {
2742627428
if (!parsedConfig.options.declaration)
2742727429
return;
2742827430
lodash_3(parsedConfig.fileNames, (name) => {
@@ -27439,34 +27441,32 @@ const typescript = (options) => {
2743927441
if (out.dts)
2744027442
declarations[key] = { type: out.dts, map: out.dtsmap };
2744127443
});
27442-
const emitDeclaration = (key, extension, entry) => {
27444+
const bundleFile = file;
27445+
const outputDir = dir;
27446+
const writeDeclaration = (key, extension, entry) => {
2744327447
if (!entry)
2744427448
return;
2744527449
let fileName = entry.name;
2744627450
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
2744727451
fileName = fileName.split("?", 1) + extension;
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-
}
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;
2745727457
else {
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-
});
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));
2746527462
}
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-
emitDeclaration(key, ".d.ts", type);
27469-
emitDeclaration(key, ".d.ts.map", map);
27468+
writeDeclaration(key, ".d.ts", type);
27469+
writeDeclaration(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, relative } from 'path';
7+
import path__default, { normalize as normalize$1, join, dirname, isAbsolute, 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,9 +27390,11 @@ const typescript = (options) => {
2739027390
}
2739127391
return undefined;
2739227392
},
27393-
generateBundle(bundleOptions) {
27393+
generateBundle(bundleOptions, _bundle, isWrite) {
2739427394
self._ongenerate();
27395-
self._onwrite.call(this, bundleOptions);
27395+
if (isWrite) {
27396+
self._onwrite(bundleOptions);
27397+
}
2739627398
},
2739727399
_ongenerate() {
2739827400
context.debug(() => `generating target ${generateRound + 1}`);
@@ -27416,7 +27418,7 @@ const typescript = (options) => {
2741627418
cache().done();
2741727419
generateRound++;
2741827420
},
27419-
_onwrite(_output) {
27421+
_onwrite({ file, dir }) {
2742027422
if (!parsedConfig.options.declaration)
2742127423
return;
2742227424
lodash_3(parsedConfig.fileNames, (name) => {
@@ -27433,34 +27435,32 @@ const typescript = (options) => {
2743327435
if (out.dts)
2743427436
declarations[key] = { type: out.dts, map: out.dtsmap };
2743527437
});
27436-
const emitDeclaration = (key, extension, entry) => {
27438+
const bundleFile = file;
27439+
const outputDir = dir;
27440+
const writeDeclaration = (key, extension, entry) => {
2743727441
if (!entry)
2743827442
return;
2743927443
let fileName = entry.name;
2744027444
if (fileName.includes("?")) // HACK for rollup-plugin-vue, it creates virtual modules in form 'file.vue?rollup-plugin-vue=script.ts'
2744127445
fileName = fileName.split("?", 1) + extension;
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-
}
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;
2745127451
else {
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-
});
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));
2745927456
}
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-
emitDeclaration(key, ".d.ts", type);
27463-
emitDeclaration(key, ".d.ts.map", map);
27462+
writeDeclaration(key, ".d.ts", type);
27463+
writeDeclaration(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.

src/index.ts

+27-24
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 { relative } from "path";
15+
import { dirname, isAbsolute, join, relative } from "path";
1616
import { normalize } from "./normalize";
1717
import { satisfies } from "semver";
1818
import findCacheDir from "find-cache-dir";
@@ -269,10 +269,13 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
269269
return undefined;
270270
},
271271

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

278281
_ongenerate(): void
@@ -313,7 +316,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
313316
generateRound++;
314317
},
315318

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

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

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)
354-
{
355-
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${fileName}'`);
356-
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
357-
}
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;
358359
else
359360
{
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-
});
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));
367365
}
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);
368371
};
369372

370373
_.each(declarations, ({ type, map }, key) =>
371374
{
372-
emitDeclaration(key, ".d.ts", type);
373-
emitDeclaration(key, ".d.ts.map", map);
375+
writeDeclaration(key, ".d.ts", type);
376+
writeDeclaration(key, ".d.ts.map", map);
374377
});
375378
},
376379
};

0 commit comments

Comments
 (0)