UNPKG

@wrench/roll-typescript

Version:

plugin for bundling TypeScript with support of modular output and declaration bundle

63 lines (61 loc) 2.22 kB
Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const bundle_dts_1 = require("./bundle-dts"); const host_1 = require("./host"); const project_1 = require("./project"); const util_1 = require("./util"); const NAME = "@wrench/typescript-dts"; const NULL_NAME = `\0${NAME}`; const EXPORT_NULL = "exports = null;"; function typescriptDts(options) { options = Object.assign({}, options); let entry; let project; return { name: NAME, options(input) { if (typeof input.input === "string") entry = input.input; else throw new Error("input should be a single file!"); return null; }, buildStart(input) { project = util_1.lazy(this, "project", createProject, options); const dependencies = host_1.collectDependencies(project, input.input); host_1.addFileNames(project, dependencies); }, resolveId(specifier, importer) { ensureDeclarationFileName(importer || specifier); return NULL_NAME; }, load(id) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (id === NULL_NAME) return EXPORT_NULL; }); }, generateBundle(output, bundle) { for (const key of Object.keys(bundle)) if (util_1.isOutputChunkWithId(bundle[key], NULL_NAME)) delete bundle[key]; bundle_dts_1.bundleDts({ entry, program: project.getProgram(), output: output.file, }); }, }; } exports.typescriptDts = typescriptDts; function ensureDeclarationFileName(fileName) { if (!fileName.endsWith(".d.ts")) throw new Error("should only contain '.d.ts.' files, but found: " + fileName); } function createProject(options) { const ts = options.typescript || require("typescript"); return new project_1.Project({ ts, options: Object.assign(Object.assign({}, ts.getDefaultCompilerOptions()), { allowJs: false, sourceMap: false, declaration: true }), }); }