@hyrious/dts
Advanced tools
Comparing version 0.1.1 to 0.1.2
23
cli.js
@@ -52,3 +52,3 @@ #!/usr/bin/env node | ||
// package.json | ||
var version = "0.1.1"; | ||
var version = "0.1.2"; | ||
@@ -70,8 +70,23 @@ // src/cli.ts | ||
} | ||
sade("dts").version(version).describe("Invoke rollup-plugin-dts to generate bundled .d.ts file").command("build [index.ts]", "Build a .d.ts file from a .ts file", { default: true }).option("-o, --outfile", "Output file").example("src/index.ts dist/index.d.ts").action((entry, options) => { | ||
function to_array(e) { | ||
if (Array.isArray(e)) | ||
return e; | ||
if (typeof e === "string") | ||
return [e]; | ||
return void 0; | ||
} | ||
sade("dts").version(version).describe("Invoke rollup-plugin-dts to generate bundled .d.ts file").command("build [index.ts]", "Build a .d.ts file from a .ts file", { default: true }).option("-o, --outfile", "Output file").option("-i, --include", "Force include a module in the bundle").option("-e, --exclude", "Force exclude a module from the bundle").example("src/index.ts dist/index.d.ts").action(async (entry, options) => { | ||
entry ||= guess_entry(process.cwd()); | ||
const outfile = options.outfile && String(options.outfile) || entry.replace(/\.tsx?$/, ".d.ts"); | ||
build(entry, outfile).then(({ output, elapsed }) => { | ||
const include = to_array(options.include); | ||
const exclude = to_array(options.exclude); | ||
try { | ||
if (include?.some((e) => exclude?.includes(e))) { | ||
throw new Error("Cannot both include and exclude a module"); | ||
} | ||
const { output, elapsed } = await build(entry, outfile, { include, exclude }); | ||
console.log(`Built ${output.map((e) => e.fileName).join(", ")} in ${Math.floor(elapsed)}ms`); | ||
}).catch(error_exit); | ||
} catch (err) { | ||
error_exit(err); | ||
} | ||
}).parse(process.argv); |
@@ -6,2 +6,4 @@ import { RollupOutput } from 'rollup'; | ||
dts?: Options; | ||
include?: string[]; | ||
exclude?: string[]; | ||
} | ||
@@ -8,0 +10,0 @@ interface BuildResult { |
10
index.js
@@ -42,2 +42,4 @@ // src/index.ts | ||
const compilerOptions = Object.assign({}, (options.dts || {}).compilerOptions, _options); | ||
const include = options.include || []; | ||
const exclude = options.exclude || []; | ||
const start = Date.now(); | ||
@@ -57,2 +59,3 @@ rmSync(outfile, { force: true }); | ||
dts({ | ||
respectExternal: true, | ||
...options.dts, | ||
@@ -62,3 +65,3 @@ compilerOptions | ||
], | ||
external: [CommonExts, ...get_external(entry)] | ||
external: [CommonExts, ...get_external(entry, new Set(include)), ...exclude] | ||
}); | ||
@@ -72,3 +75,3 @@ const result = await bundle.write({ | ||
} | ||
function get_external(file) { | ||
function get_external(file, reject) { | ||
const pkg = sync_default(file, (_, names) => { | ||
@@ -81,3 +84,4 @@ if (names.includes("package.json")) { | ||
const json2 = JSON.parse(readFileSync(pkg, "utf8")); | ||
return Object.keys(Object.assign({}, json2.dependencies, json2.peerDependencies)); | ||
const deps = Object.assign({}, json2.dependencies, json2.peerDependencies); | ||
return Object.keys(deps).filter((e) => !reject.has(e)); | ||
} else { | ||
@@ -84,0 +88,0 @@ return []; |
{ | ||
"name": "@hyrious/dts", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Invoke rollup-plugin-dts to generate bundled .d.ts file", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -12,3 +12,3 @@ import cleanStack from 'clean-stack' | ||
interface SadeHandler1<Keys extends string> { | ||
(entry: string | undefined, options: Record<Keys, boolean | number | string | undefined>): void | ||
(entry: string | undefined, options: Record<Keys, boolean | number | string | string[] | undefined>): void | ||
} | ||
@@ -28,2 +28,8 @@ | ||
function to_array(e: boolean | number | string | string[] | undefined) { | ||
if (Array.isArray(e)) return e | ||
if (typeof e === 'string') return [e] | ||
return undefined | ||
} | ||
sade('dts') | ||
@@ -35,13 +41,21 @@ .version(version) | ||
.option('-o, --outfile', 'Output file') | ||
.option('-i, --include', 'Force include a module in the bundle') | ||
.option('-e, --exclude', 'Force exclude a module from the bundle') | ||
.example('src/index.ts dist/index.d.ts') | ||
.action(<SadeHandler1<'outfile'>>((entry, options) => { | ||
.action(<SadeHandler1<'outfile' | 'include' | 'exclude'>>(async (entry, options) => { | ||
entry ||= guess_entry(process.cwd()) | ||
const outfile = (options.outfile && String(options.outfile)) || entry.replace(/\.tsx?$/, '.d.ts') | ||
build(entry, outfile) | ||
.then(({ output, elapsed }) => { | ||
console.log(`Built ${output.map(e => e.fileName).join(', ')} in ${Math.floor(elapsed)}ms`) | ||
}) | ||
.catch(error_exit) | ||
const include = to_array(options.include) | ||
const exclude = to_array(options.exclude) | ||
try { | ||
if (include?.some(e => exclude?.includes(e))) { | ||
throw new Error('Cannot both include and exclude a module') | ||
} | ||
const { output, elapsed } = await build(entry, outfile, { include, exclude }) | ||
console.log(`Built ${output.map(e => e.fileName).join(', ')} in ${Math.floor(elapsed)}ms`) | ||
} catch (err) { | ||
error_exit(err) | ||
} | ||
})) | ||
.parse(process.argv) |
@@ -27,2 +27,4 @@ import type ts from 'typescript' | ||
dts?: Options | ||
include?: string[] | ||
exclude?: string[] | ||
} | ||
@@ -41,2 +43,4 @@ | ||
const compilerOptions = Object.assign({}, (options.dts || {}).compilerOptions, _options) | ||
const include = options.include || [] | ||
const exclude = options.exclude || [] | ||
@@ -63,2 +67,3 @@ const start = Date.now() | ||
dts({ | ||
respectExternal: true, | ||
...options.dts, | ||
@@ -68,3 +73,3 @@ compilerOptions, | ||
], | ||
external: [CommonExts, ...get_external(entry)], | ||
external: [CommonExts, ...get_external(entry, new Set(include)), ...exclude], | ||
}) | ||
@@ -81,3 +86,3 @@ | ||
function get_external(file: string) { | ||
function get_external(file: string, reject: Set<string>) { | ||
const pkg = escalade(file, (_, names) => { | ||
@@ -90,3 +95,4 @@ if (names.includes('package.json')) { | ||
const json = JSON.parse(readFileSync(pkg, 'utf8')) | ||
return Object.keys(Object.assign({}, json.dependencies, json.peerDependencies)) | ||
const deps = Object.assign({}, json.dependencies, json.peerDependencies) | ||
return Object.keys(deps).filter(e => !reject.has(e)) | ||
} else { | ||
@@ -93,0 +99,0 @@ return [] |
13420
317