@hyrious/dts
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -52,3 +52,3 @@ #!/usr/bin/env node | ||
// package.json | ||
var version = "0.1.4"; | ||
var version = "0.1.5"; | ||
@@ -55,0 +55,0 @@ // src/cli.ts |
import { RollupOutput } from 'rollup'; | ||
import { Options } from 'rollup-plugin-dts'; | ||
declare const version = "0.1.5"; | ||
interface BuildOptions { | ||
@@ -19,2 +21,2 @@ dts?: Options; | ||
export { BuildOptions, BuildResult, build }; | ||
export { BuildOptions, BuildResult, build, version }; |
70
index.js
@@ -24,5 +24,12 @@ // src/index.ts | ||
// src/index.ts | ||
import { readFileSync, rmSync } from "fs"; | ||
import { readFileSync, rmSync, writeFileSync } from "fs"; | ||
import { rollup } from "rollup"; | ||
import dts from "rollup-plugin-dts"; | ||
import { tmpdir } from "os"; | ||
import { join, relative } from "path"; | ||
// package.json | ||
var version = "0.1.5"; | ||
// src/index.ts | ||
var CommonExts = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|png|jpe?g|gif|svg|ico|webp|avif|mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf|wasm)$/; | ||
@@ -46,3 +53,7 @@ var _options = { | ||
const start = Date.now(); | ||
rmSync(outfile, { force: true }); | ||
const dts_ = dts({ | ||
respectExternal: true, | ||
...options.dts, | ||
compilerOptions | ||
}); | ||
const bundle = await rollup({ | ||
@@ -56,17 +67,6 @@ input: entry, | ||
}, | ||
plugins: [ | ||
ignore(CommonExts), | ||
json({ | ||
preferConst: true | ||
}), | ||
dts({ | ||
respectExternal: true, | ||
...options.dts, | ||
compilerOptions | ||
}), | ||
fix_trivia(), | ||
expandStar && expand_star() | ||
], | ||
plugins: [ignore(CommonExts), wrap(json, dts_), dts_, expandStar && expand_star()], | ||
external: [...get_external(entry, new Set(include)), ...exclude] | ||
}); | ||
rmSync(outfile, { force: true }); | ||
const result = await bundle.write({ | ||
@@ -79,2 +79,26 @@ file: outfile, | ||
} | ||
function wrap(json2, dts2) { | ||
const pwd = process.cwd(); | ||
const jsonPlugin = json2({ | ||
preferConst: true | ||
}); | ||
const tempfiles = []; | ||
return { | ||
name: "wrap(json)", | ||
transform(code, id) { | ||
const result = jsonPlugin.transform.call(this, code, id); | ||
if (!result || typeof result === "string") | ||
return result; | ||
const tempfile = join(tmpdir(), relative(pwd, id).replace(/[\/\\]/g, "+") + ".ts"); | ||
writeFileSync(tempfile, result.code); | ||
tempfiles.push(tempfile); | ||
return dts2.transform.call(this, result.code, tempfile); | ||
}, | ||
generateBundle() { | ||
for (const file of tempfiles) | ||
rmSync(file); | ||
tempfiles.length = 0; | ||
} | ||
}; | ||
} | ||
function get_external(file, reject) { | ||
@@ -94,13 +118,10 @@ const pkg = sync_default(file, (_, names) => { | ||
} | ||
function fix_trivia() { | ||
return { | ||
name: "fix-trivia", | ||
renderChunk(code) { | ||
return code.replace(/^(\s*)(const|enum)\s/gm, "$1declare $2 "); | ||
} | ||
}; | ||
} | ||
function ignore(re) { | ||
return { | ||
name: "ignore", | ||
resolveId(id) { | ||
if (re.test(id)) { | ||
return id; | ||
} | ||
}, | ||
load(id) { | ||
@@ -153,3 +174,4 @@ if (re.test(id)) { | ||
export { | ||
build | ||
build, | ||
version | ||
}; |
{ | ||
"name": "@hyrious/dts", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Invoke rollup-plugin-dts to generate bundled .d.ts file", | ||
@@ -17,2 +17,5 @@ "keywords": [ | ||
"bin": "./cli.js", | ||
"scripts": { | ||
"build": "esbuild-dev scripts/build.ts" | ||
}, | ||
"files": [ | ||
@@ -37,6 +40,3 @@ "cli.js", | ||
"escalade": "^3.1.1" | ||
}, | ||
"scripts": { | ||
"build": "esbuild-dev scripts/build.ts" | ||
} | ||
} | ||
} |
import type ts from 'typescript' | ||
import json from '@rollup/plugin-json' | ||
import json, { RollupJsonOptions } from '@rollup/plugin-json' | ||
import escalade from 'escalade/sync' | ||
import { readFileSync, rmSync } from 'fs' | ||
import { Plugin, rollup, RollupOutput } from 'rollup' | ||
import { readFileSync, rmSync, writeFileSync } from 'fs' | ||
import { Plugin, rollup, RollupOutput, TransformHook } from 'rollup' | ||
import dts, { Options } from 'rollup-plugin-dts' | ||
import { tmpdir } from 'os' | ||
import { join, relative } from 'path' | ||
export { version } from '../package.json' | ||
const CommonExts = | ||
@@ -51,4 +55,7 @@ /\.(css|less|sass|scss|styl|stylus|pcss|postcss|png|jpe?g|gif|svg|ico|webp|avif|mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf|wasm)$/ | ||
rmSync(outfile, { force: true }) | ||
const dts_ = dts({ | ||
respectExternal: true, | ||
...options.dts, | ||
compilerOptions, | ||
}) | ||
const bundle = await rollup({ | ||
@@ -66,18 +73,8 @@ input: entry, | ||
}, | ||
plugins: [ | ||
ignore(CommonExts), | ||
json({ | ||
preferConst: true, | ||
}), | ||
dts({ | ||
respectExternal: true, | ||
...options.dts, | ||
compilerOptions, | ||
}), | ||
fix_trivia(), | ||
expandStar && expand_star(), | ||
], | ||
plugins: [ignore(CommonExts), wrap(json, dts_), dts_, expandStar && expand_star()], | ||
external: [...get_external(entry, new Set(include)), ...exclude], | ||
}) | ||
rmSync(outfile, { force: true }) | ||
const result = await bundle.write({ | ||
@@ -92,2 +89,30 @@ file: outfile, | ||
function wrap(json: (options?: RollupJsonOptions) => Plugin, dts: Plugin): Plugin { | ||
const pwd = process.cwd() | ||
const jsonPlugin = json({ | ||
preferConst: true, | ||
}) | ||
const tempfiles: string[] = [] | ||
return { | ||
name: 'wrap(json)', | ||
transform(code, id) { | ||
const result = (jsonPlugin.transform as TransformHook).call(this, code, id) | ||
if (!result || typeof result === 'string') return result | ||
const tempfile = join(tmpdir(), relative(pwd, id).replace(/[\/\\]/g, '+') + '.ts') | ||
// rollup-plugin-dts uses `ts.sys.readFile` to create a new program for this file | ||
// so we have to write this virtual file to disk | ||
writeFileSync(tempfile, result.code!) | ||
tempfiles.push(tempfile) | ||
return (dts.transform as TransformHook).call(this, result.code!, tempfile) | ||
}, | ||
generateBundle() { | ||
for (const file of tempfiles) rmSync(file) | ||
tempfiles.length = 0 | ||
}, | ||
} | ||
} | ||
function get_external(file: string, reject: Set<string>) { | ||
@@ -108,14 +133,10 @@ const pkg = escalade(file, (_, names) => { | ||
function fix_trivia(): Plugin { | ||
return { | ||
name: 'fix-trivia', | ||
renderChunk(code) { | ||
return code.replace(/^(\s*)(const|enum)\s/gm, '$1declare $2 ') | ||
}, | ||
} | ||
} | ||
function ignore(re: RegExp): Plugin { | ||
return { | ||
name: 'ignore', | ||
resolveId(id) { | ||
if (re.test(id)) { | ||
return id | ||
} | ||
}, | ||
load(id) { | ||
@@ -122,0 +143,0 @@ if (re.test(id)) { |
19028
495