@hyrious/dts
Advanced tools
Comparing version 0.1.3 to 0.1.4
11
cli.js
@@ -52,3 +52,3 @@ #!/usr/bin/env node | ||
// package.json | ||
var version = "0.1.3"; | ||
var version = "0.1.4"; | ||
@@ -77,3 +77,3 @@ // 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").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) => { | ||
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").option("--expand-star", "Expand namespace imports to named imports (experimental)").example("src/index.ts dist/index.d.ts").action(async (entry, options) => { | ||
entry ||= guess_entry(process.cwd()); | ||
@@ -83,2 +83,3 @@ const outfile = options.outfile && String(options.outfile) || entry.replace(/\.tsx?$/, ".d.ts"); | ||
const exclude = to_array(options.exclude); | ||
const expandStar = !!options["expand-star"]; | ||
try { | ||
@@ -88,3 +89,7 @@ if (include?.some((e) => exclude?.includes(e))) { | ||
} | ||
const { output, elapsed } = await build(entry, outfile, { include, exclude }); | ||
const { output, elapsed } = await build(entry, outfile, { | ||
include, | ||
exclude, | ||
experimental: { expandStar } | ||
}); | ||
console.log(`Built ${output.map((e) => e.fileName).join(", ")} in ${Math.floor(elapsed)}ms`); | ||
@@ -91,0 +96,0 @@ } catch (err) { |
@@ -8,2 +8,6 @@ import { RollupOutput } from 'rollup'; | ||
exclude?: string[]; | ||
experimental?: { | ||
/** Post process the result and replace all `* as` to `{...names}` */ | ||
expandStar?: boolean; | ||
}; | ||
} | ||
@@ -10,0 +14,0 @@ interface BuildResult { |
43
index.js
@@ -43,2 +43,3 @@ // src/index.ts | ||
const exclude = options.exclude || []; | ||
const expandStar = options.experimental?.expandStar; | ||
const start = Date.now(); | ||
@@ -64,3 +65,4 @@ rmSync(outfile, { force: true }); | ||
}), | ||
fix_trivia() | ||
fix_trivia(), | ||
expandStar && expand_star() | ||
], | ||
@@ -108,4 +110,43 @@ external: [...get_external(entry, new Set(include)), ...exclude] | ||
} | ||
function expand_star() { | ||
return { | ||
name: "expand-star", | ||
renderChunk(code) { | ||
const namespaces = []; | ||
code.replace(/^import \* as (\S+) from ['"]([-@\w]+)/gm, (_, ns, external) => { | ||
namespaces.push([ns, external]); | ||
return ""; | ||
}); | ||
if (namespaces.length) { | ||
const names = {}; | ||
for (const [ns, module] of namespaces) { | ||
names[ns] ||= {}; | ||
const re = new RegExp(`^import {(.+)} from ['"]${module}['"];$`, "gm"); | ||
code = code.replace(re, (_, imports) => { | ||
for (let name of imports.split(",")) { | ||
name = name.trim(); | ||
if (name) | ||
names[ns][name] = true; | ||
} | ||
return ""; | ||
}); | ||
} | ||
for (const [ns] of namespaces) { | ||
names[ns] ||= {}; | ||
const re = new RegExp(`\\b${ns.replace(/\$/g, "\\$")}\\.(\\w+)\\b`, "g"); | ||
code = code.replace(re, (_, name) => { | ||
names[ns][name] = true; | ||
return name; | ||
}); | ||
} | ||
code = code.replace(/^import \* as (\S+) from\b/gm, (_, ns) => { | ||
return `import { ${Object.keys(names[ns]).join(", ")} } from`; | ||
}); | ||
return code; | ||
} | ||
} | ||
}; | ||
} | ||
export { | ||
build | ||
}; |
{ | ||
"name": "@hyrious/dts", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Invoke rollup-plugin-dts to generate bundled .d.ts file", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -41,4 +41,5 @@ import cleanStack from 'clean-stack' | ||
.option('-e, --exclude', 'Force exclude a module from the bundle') | ||
.option('--expand-star', 'Expand namespace imports to named imports (experimental)') | ||
.example('src/index.ts dist/index.d.ts') | ||
.action(<SadeHandler1<'outfile' | 'include' | 'exclude'>>(async (entry, options) => { | ||
.action(<SadeHandler1<'outfile' | 'include' | 'exclude' | 'expand-star'>>(async (entry, options) => { | ||
entry ||= guess_entry(process.cwd()) | ||
@@ -48,2 +49,3 @@ const outfile = (options.outfile && String(options.outfile)) || entry.replace(/\.tsx?$/, '.d.ts') | ||
const exclude = to_array(options.exclude) | ||
const expandStar = !!options['expand-star'] | ||
try { | ||
@@ -53,3 +55,7 @@ if (include?.some(e => exclude?.includes(e))) { | ||
} | ||
const { output, elapsed } = await build(entry, outfile, { include, exclude }) | ||
const { output, elapsed } = await build(entry, outfile, { | ||
include, | ||
exclude, | ||
experimental: { expandStar }, | ||
}) | ||
console.log(`Built ${output.map(e => e.fileName).join(', ')} in ${Math.floor(elapsed)}ms`) | ||
@@ -56,0 +62,0 @@ } catch (err) { |
@@ -28,2 +28,6 @@ import type ts from 'typescript' | ||
exclude?: string[] | ||
experimental?: { | ||
/** Post process the result and replace all `* as` to `{...names}` */ | ||
expandStar?: boolean | ||
} | ||
} | ||
@@ -44,2 +48,3 @@ | ||
const exclude = options.exclude || [] | ||
const expandStar = options.experimental?.expandStar | ||
@@ -73,2 +78,3 @@ const start = Date.now() | ||
fix_trivia(), | ||
expandStar && expand_star(), | ||
], | ||
@@ -121,1 +127,40 @@ external: [...get_external(entry, new Set(include)), ...exclude], | ||
} | ||
function expand_star(): Plugin { | ||
return { | ||
name: 'expand-star', | ||
renderChunk(code) { | ||
const namespaces: [variable: string, module: string][] = [] | ||
code.replace(/^import \* as (\S+) from ['"]([-@\w]+)/gm, (_, ns, external) => { | ||
namespaces.push([ns, external]) | ||
return '' | ||
}) | ||
if (namespaces.length) { | ||
const names: Record<string, Record<string, true>> = {} | ||
for (const [ns, module] of namespaces) { | ||
names[ns] ||= {} | ||
const re = new RegExp(`^import {(.+)} from ['"]${module}['"];$`, 'gm') | ||
code = code.replace(re, (_, imports: string) => { | ||
for (let name of imports.split(',')) { | ||
name = name.trim() | ||
if (name) names[ns][name] = true | ||
} | ||
return '' | ||
}) | ||
} | ||
for (const [ns] of namespaces) { | ||
names[ns] ||= {} | ||
const re = new RegExp(`\\b${ns.replace(/\$/g, '\\$')}\\.(\\w+)\\b`, 'g') | ||
code = code.replace(re, (_, name) => { | ||
names[ns][name] = true | ||
return name | ||
}) | ||
} | ||
code = code.replace(/^import \* as (\S+) from\b/gm, (_, ns) => { | ||
return `import { ${Object.keys(names[ns]).join(', ')} } from` | ||
}) | ||
return code | ||
} | ||
}, | ||
} | ||
} |
17385
457