Skip to content

Commit a514b4a

Browse files
sxzzSunsetTechuila
andauthored
feat: support JSON partiality (#114)
Co-authored-by: Grigory <techuila.sunset@gmail.com>
1 parent 0e78923 commit a514b4a

File tree

13 files changed

+48
-46
lines changed

13 files changed

+48
-46
lines changed

src/filename.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const RE_DTS_MAP: RegExp = /\.d\.([cm]?)ts\.map$/
77
export const RE_NODE_MODULES: RegExp = /[\\/]node_modules[\\/]/
88
export const RE_CSS: RegExp = /\.css$/
99
export const RE_VUE: RegExp = /\.vue$/
10+
export const RE_JSON: RegExp = /\.json$/
1011

1112
export function filename_js_to_dts(id: string): string {
1213
return id.replace(RE_JS, '.d.$1ts')
@@ -16,6 +17,7 @@ export function filename_to_dts(id: string): string {
1617
.replace(RE_VUE, '.vue.ts')
1718
.replace(RE_TS, '.d.$1ts')
1819
.replace(RE_JS, '.d.$1ts')
20+
.replace(RE_JSON, '.d.ts')
1921
}
2022
export function filename_dts_to(id: string, ext: 'js' | 'ts'): string {
2123
return id.replace(RE_DTS, `.$1${ext}`)

src/generate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
RE_DTS,
1111
RE_DTS_MAP,
1212
RE_JS,
13+
RE_JSON,
1314
RE_NODE_MODULES,
1415
RE_TS,
1516
RE_VUE,
@@ -181,7 +182,7 @@ export function createGeneratePlugin({
181182
order: 'pre',
182183
filter: {
183184
id: {
184-
include: [RE_JS, RE_TS, RE_VUE],
185+
include: [RE_JS, RE_TS, RE_VUE, RE_JSON],
185186
exclude: [RE_DTS, RE_NODE_MODULES],
186187
},
187188
},
@@ -206,6 +207,7 @@ export function createGeneratePlugin({
206207
}
207208

208209
if (emitDtsOnly) {
210+
if (RE_JSON.test(id)) return '{}'
209211
return 'export { }'
210212
}
211213
},

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {
1212
RE_DTS,
1313
RE_DTS_MAP,
1414
RE_JS,
15+
RE_JSON,
1516
RE_NODE_MODULES,
1617
RE_TS,
1718
RE_VUE,

src/resolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
filename_to_dts,
66
RE_CSS,
77
RE_DTS,
8+
RE_JSON,
89
RE_NODE_MODULES,
910
RE_TS,
1011
RE_VUE,
@@ -13,7 +14,7 @@ import type { OptionsResolved } from './options.ts'
1314
import type { Plugin, ResolvedId } from 'rolldown'
1415

1516
function isSourceFile(id: string) {
16-
return RE_TS.test(id) || RE_VUE.test(id)
17+
return RE_TS.test(id) || RE_VUE.test(id) || RE_JSON.test(id)
1718
}
1819

1920
export function createDtsResolvePlugin({

tests/__snapshots__/tsc.test.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ const input2 = "input2";
102102
export { input2 };"
103103
`;
104104

105+
exports[`tsc > import JSON 1`] = `
106+
"// index.d.ts
107+
declare namespace foo_d_exports {
108+
export { age, name };
109+
}
110+
declare let name: string;
111+
declare let age: number;
112+
declare namespace bar_d_exports {
113+
export { _exports as default };
114+
}
115+
declare const _exports: string[];
116+
//#endregion
117+
export { age, _exports as bar, bar_d_exports as barNs, foo_d_exports as fooNs, name };"
118+
`;
119+
105120
exports[`tsc > jsdoc 1`] = `
106121
"// jsdoc.d.ts
107122
import { Plugin } from "rolldown";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["bar"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"name": "foo",
33
"age": 42
4-
}
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// import foo from './foo.json' // TODO not supported yet
2+
import * as fooNs from './foo.json'
3+
4+
import bar from './bar.json'
5+
import * as barNs from './bar.json'
6+
7+
export { name, age } from './foo.json'
8+
export { bar, fooNs, barNs }

tests/rollup-plugin-dts/import-json/bar.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/rollup-plugin-dts/import-json/diff.patch

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)