Skip to content

Commit

Permalink
rip out all the .ts -> .js logic
Browse files Browse the repository at this point in the history
the plugin now only supports `.d.ts` files as inputs, #29
  • Loading branch information
Swatinem committed Jun 3, 2019
1 parent 672f8be commit 6ec64da
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 282 deletions.
194 changes: 0 additions & 194 deletions src/compiler.ts

This file was deleted.

103 changes: 42 additions & 61 deletions src/index.ts
@@ -1,43 +1,12 @@
import * as ts from "typescript";
import { PluginImpl } from "rollup";
import { getCachedCompiler, CompileMode } from "./compiler";
import { version } from "../package.json";

export interface Options {
tsconfig?: string;
compilerOptions?: ts.CompilerOptions;
compileMode?: CompileMode;
banner?: boolean;
}

const TSLIB_ID = "\0tslib";

const BANNER =
`
// FILE GENERATED BY \`rollup-plugin-dts@${version}\`
// https://github.com/Swatinem/rollup-plugin-dts
`.trim() + "\n";

const plugin: PluginImpl<Options> = (options = {}) => {
const tslibFileName = require.resolve("tslib").replace("tslib.js", "tslib.es6.js");
const tslib = ts.sys.readFile(tslibFileName, "utf-8") || null;

const mode = options.compileMode || CompileMode.Types;
const compiler = getCachedCompiler({
tsconfig: options.tsconfig || process.cwd(),
compilerOptions: options.compilerOptions || {},
mode,
});
import { Transformer } from "./Transformer";

const plugin: PluginImpl<{}> = () => {
return {
name: "dts",

banner: mode === CompileMode.Types && options.banner !== false ? BANNER : undefined,

options(options) {
if (mode === CompileMode.Js) {
return options;
}
return {
...options,
treeshake: {
Expand All @@ -48,9 +17,6 @@ const plugin: PluginImpl<Options> = (options = {}) => {
},

outputOptions(options) {
if (mode === CompileMode.Js) {
return options;
}
return {
...options,
chunkFileNames: options.chunkFileNames || "[name]-[hash].d.ts",
Expand All @@ -65,26 +31,53 @@ const plugin: PluginImpl<Options> = (options = {}) => {
};
},

resolveId(importee, importer) {
// istanbul ignore if
if (importee === "tslib") {
return TSLIB_ID;
}
resolveId(source, importer) {
if (!importer) {
return;
}
importer = importer.split("\\").join("/");
return compiler.resolve(importee, importer);

// resolve this via typescript
const { resolvedModule } = ts.nodeModuleNameResolver(source, importer, {}, ts.sys);
if (!resolvedModule) {
return;
}

// here, we define everything that comes from `node_modules` as `external`.
// maybe its a good idea to introduce an option for this?
if (resolvedModule.isExternalLibraryImport) {
return { id: source, external: true };
}
let id = resolvedModule.resolvedFileName;
const { extension } = resolvedModule;
if (extension !== ".d.ts") {
// ts resolves `.ts`/`.tsx` files before `.d.ts`
id = id.slice(0, id.length - extension.length) + ".d.ts";
}

return { id };
},

async load(id) {
if (id === TSLIB_ID) {
return tslib;
transform(code, id) {
if (!id.endsWith(".d.ts")) {
this.error("`rollup-plugin-dts` can only deal with `.d.ts` files.");
return;
}
if (id.endsWith(".ts") || id.endsWith(".tsx")) {
return compiler.load(id);

const dtsSource = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);
const converter = new Transformer(dtsSource);
const { ast, fixups } = converter.transform();

// NOTE(swatinem):
// hm, typescript generates `export default` without a declare,
// but rollup moves the `export default` to a different place, which leaves
// the function declaration without a `declare`.
// Well luckily both words have the same length, haha :-D
code = code.replace(/(export\s+)default(\s+(function|class))/m, "$1declare$2");
for (const fixup of fixups) {
code = code.slice(0, fixup.range.start) + fixup.identifier + code.slice(fixup.range.end);
}
return null;

return { code, ast };
},

// TODO: figure out if we could use this to "fix" namespace-re-exports
Expand All @@ -97,16 +90,4 @@ const plugin: PluginImpl<Options> = (options = {}) => {
};
};

const dts: PluginImpl<Options> = (options = {}) => {
options.compileMode = options.compileMode || CompileMode.Types;
return plugin(options);
};

const js: PluginImpl<Options> = (options = {}) => {
options.compileMode = options.compileMode || CompileMode.Js;
return plugin(options);
};

export { CompileMode, plugin, dts, js, js as ts };

export default plugin;

0 comments on commit 6ec64da

Please sign in to comment.