Skip to content

Commit

Permalink
- reverting #181
Browse files Browse the repository at this point in the history
  • Loading branch information
ezolenko committed Oct 29, 2019
2 parents f330ba5 + c35e35d commit 330a0ce
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 75 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108)

* `useTsconfigDeclarationDir`: false

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.

Set to false if you want other plugins to see type declarations.
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.

* `typescript`: typescript module installed with the plugin

Expand Down
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.

44 changes: 22 additions & 22 deletions dist/rollup-plugin-typescript2.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27396,9 +27396,11 @@ const typescript = (options) => {
}
return undefined;
},
generateBundle(bundleOptions) {
generateBundle(bundleOptions, _bundle, isWrite) {
self._ongenerate();
self._onwrite.call(this, bundleOptions);
if (isWrite) {
self._onwrite(bundleOptions);
}
},
_ongenerate() {
context.debug(() => `generating target ${generateRound + 1}`);
Expand All @@ -27422,7 +27424,7 @@ const typescript = (options) => {
cache().done();
generateRound++;
},
_onwrite(_output) {
_onwrite({ file, dir }) {
if (!parsedConfig.options.declaration)
return;
lodash_3(parsedConfig.fileNames, (name) => {
Expand All @@ -27439,34 +27441,32 @@ const typescript = (options) => {
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const emitDeclaration = (key, extension, entry) => {
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;
// 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);
}
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 {
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,
});
// 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) => {
emitDeclaration(key, ".d.ts", type);
emitDeclaration(key, ".d.ts.map", map);
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
},
};
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.cjs.js.map

Large diffs are not rendered by default.

46 changes: 23 additions & 23 deletions dist/rollup-plugin-typescript2.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, relative } from 'path';
import path__default, { normalize as normalize$1, join, dirname, isAbsolute, 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 : {};
Expand Down Expand Up @@ -27390,9 +27390,11 @@ const typescript = (options) => {
}
return undefined;
},
generateBundle(bundleOptions) {
generateBundle(bundleOptions, _bundle, isWrite) {
self._ongenerate();
self._onwrite.call(this, bundleOptions);
if (isWrite) {
self._onwrite(bundleOptions);
}
},
_ongenerate() {
context.debug(() => `generating target ${generateRound + 1}`);
Expand All @@ -27416,7 +27418,7 @@ const typescript = (options) => {
cache().done();
generateRound++;
},
_onwrite(_output) {
_onwrite({ file, dir }) {
if (!parsedConfig.options.declaration)
return;
lodash_3(parsedConfig.fileNames, (name) => {
Expand All @@ -27433,34 +27435,32 @@ const typescript = (options) => {
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
});
const emitDeclaration = (key, extension, entry) => {
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;
// 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);
}
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 {
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,
});
// 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) => {
emitDeclaration(key, ".d.ts", type);
emitDeclaration(key, ".d.ts.map", map);
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
},
};
Expand Down
2 changes: 1 addition & 1 deletion dist/rollup-plugin-typescript2.es.js.map

Large diffs are not rendered by default.

51 changes: 27 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { relative } from "path";
import { dirname, isAbsolute, join, relative } from "path";
import { normalize } from "./normalize";
import { satisfies } from "semver";
import findCacheDir from "find-cache-dir";
Expand Down Expand Up @@ -269,10 +269,13 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
return undefined;
},

generateBundle(this: PluginContext, bundleOptions: OutputOptions): void
generateBundle(bundleOptions: OutputOptions, _bundle: any, isWrite: boolean): void
{
self._ongenerate();
self._onwrite.call(this, bundleOptions);
if (isWrite)
{
self._onwrite(bundleOptions);
}
},

_ongenerate(): void
Expand Down Expand Up @@ -313,7 +316,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
generateRound++;
},

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

const emitDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
const bundleFile = file;
const outputDir = dir;

const writeDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
{
if (!entry)
return;
Expand All @@ -345,32 +351,29 @@ 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;

// 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(() => `${blue("emitting declarations")} for '${key}' to '${fileName}'`);
tsModule.sys.writeFile(fileName, entry.text, entry.writeByteOrderMark);
}
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
{
const relativePath = relative(process.cwd(), fileName);
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${relativePath}'`);
this.emitFile({
type: "asset",
source: entry.text,
fileName: relativePath,
});
// 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("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) =>
{
emitDeclaration(key, ".d.ts", type);
emitDeclaration(key, ".d.ts.map", map);
writeDeclaration(key, ".d.ts", type);
writeDeclaration(key, ".d.ts.map", map);
});
},
};
Expand Down

0 comments on commit 330a0ce

Please sign in to comment.