diff --git a/src/filename.ts b/src/filename.ts index 141a156..10e0281 100644 --- a/src/filename.ts +++ b/src/filename.ts @@ -7,6 +7,7 @@ export const RE_DTS_MAP: RegExp = /\.d\.([cm]?)ts\.map$/ export const RE_NODE_MODULES: RegExp = /[\\/]node_modules[\\/]/ export const RE_CSS: RegExp = /\.css$/ export const RE_VUE: RegExp = /\.vue$/ +export const RE_JSON: RegExp = /\.json$/ export function filename_js_to_dts(id: string): string { return id.replace(RE_JS, '.d.$1ts') @@ -16,6 +17,7 @@ export function filename_to_dts(id: string): string { .replace(RE_VUE, '.vue.ts') .replace(RE_TS, '.d.$1ts') .replace(RE_JS, '.d.$1ts') + .replace(RE_JSON, '.d.ts') } export function filename_dts_to(id: string, ext: 'js' | 'ts'): string { return id.replace(RE_DTS, `.$1${ext}`) diff --git a/src/generate.ts b/src/generate.ts index 15a7f6e..4032f1c 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -10,6 +10,7 @@ import { RE_DTS, RE_DTS_MAP, RE_JS, + RE_JSON, RE_NODE_MODULES, RE_TS, RE_VUE, @@ -181,7 +182,7 @@ export function createGeneratePlugin({ order: 'pre', filter: { id: { - include: [RE_JS, RE_TS, RE_VUE], + include: [RE_JS, RE_TS, RE_VUE, RE_JSON], exclude: [RE_DTS, RE_NODE_MODULES], }, }, @@ -206,6 +207,7 @@ export function createGeneratePlugin({ } if (emitDtsOnly) { + if (RE_JSON.test(id)) return '{}' return 'export { }' } }, diff --git a/src/index.ts b/src/index.ts index 8a1e0b8..5c9a281 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ export { RE_DTS, RE_DTS_MAP, RE_JS, + RE_JSON, RE_NODE_MODULES, RE_TS, RE_VUE, diff --git a/src/resolver.ts b/src/resolver.ts index 312e922..900c8ce 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -5,6 +5,7 @@ import { filename_to_dts, RE_CSS, RE_DTS, + RE_JSON, RE_NODE_MODULES, RE_TS, RE_VUE, @@ -13,7 +14,7 @@ import type { OptionsResolved } from './options.ts' import type { Plugin, ResolvedId } from 'rolldown' function isSourceFile(id: string) { - return RE_TS.test(id) || RE_VUE.test(id) + return RE_TS.test(id) || RE_VUE.test(id) || RE_JSON.test(id) } export function createDtsResolvePlugin({ diff --git a/tests/__snapshots__/tsc.test.ts.snap b/tests/__snapshots__/tsc.test.ts.snap index 35a20ca..82f5604 100644 --- a/tests/__snapshots__/tsc.test.ts.snap +++ b/tests/__snapshots__/tsc.test.ts.snap @@ -102,6 +102,21 @@ const input2 = "input2"; export { input2 };" `; +exports[`tsc > import JSON 1`] = ` +"// index.d.ts +declare namespace foo_d_exports { + export { age, name }; +} +declare let name: string; +declare let age: number; +declare namespace bar_d_exports { + export { _exports as default }; +} +declare const _exports: string[]; +//#endregion +export { age, _exports as bar, bar_d_exports as barNs, foo_d_exports as fooNs, name };" +`; + exports[`tsc > jsdoc 1`] = ` "// jsdoc.d.ts import { Plugin } from "rolldown"; diff --git a/tests/fixtures/import-json/bar.json b/tests/fixtures/import-json/bar.json new file mode 100644 index 0000000..ff194f5 --- /dev/null +++ b/tests/fixtures/import-json/bar.json @@ -0,0 +1 @@ +["bar"] diff --git a/tests/rollup-plugin-dts/import-json/foo.json b/tests/fixtures/import-json/foo.json similarity index 93% rename from tests/rollup-plugin-dts/import-json/foo.json rename to tests/fixtures/import-json/foo.json index 498536d..cabe16d 100644 --- a/tests/rollup-plugin-dts/import-json/foo.json +++ b/tests/fixtures/import-json/foo.json @@ -1,4 +1,4 @@ { "name": "foo", "age": 42 -} \ No newline at end of file +} diff --git a/tests/fixtures/import-json/index.ts b/tests/fixtures/import-json/index.ts new file mode 100644 index 0000000..bfd9fb0 --- /dev/null +++ b/tests/fixtures/import-json/index.ts @@ -0,0 +1,8 @@ +// import foo from './foo.json' // TODO not supported yet +import * as fooNs from './foo.json' + +import bar from './bar.json' +import * as barNs from './bar.json' + +export { name, age } from './foo.json' +export { bar, fooNs, barNs } diff --git a/tests/rollup-plugin-dts/import-json/bar.json b/tests/rollup-plugin-dts/import-json/bar.json deleted file mode 100644 index 678abc1..0000000 --- a/tests/rollup-plugin-dts/import-json/bar.json +++ /dev/null @@ -1 +0,0 @@ -["bar"] \ No newline at end of file diff --git a/tests/rollup-plugin-dts/import-json/diff.patch b/tests/rollup-plugin-dts/import-json/diff.patch deleted file mode 100644 index adc5b6a..0000000 --- a/tests/rollup-plugin-dts/import-json/diff.patch +++ /dev/null @@ -1,15 +0,0 @@ -Index: diff.patch -=================================================================== ---- diff.patch -+++ diff.patch -@@ -1,9 +1,2 @@ - // index.d.ts --declare let name: string --declare let age: number --declare const export_default: { -- name: typeof name -- age: typeof age --} --declare const _exports: string[] --export { age, _exports as bar, export_default as foo, name } -+export { age, bar_default as bar, foo_default as foo, name } diff --git a/tests/rollup-plugin-dts/import-json/index.ts b/tests/rollup-plugin-dts/import-json/index.ts deleted file mode 100644 index 305e8bc..0000000 --- a/tests/rollup-plugin-dts/import-json/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -import foo from './foo.json' -import bar from './bar.json' - -export { name, age } from './foo.json' - -export { foo, bar } diff --git a/tests/rollup-plugin-dts/import-json/tsconfig.json b/tests/rollup-plugin-dts/import-json/tsconfig.json deleted file mode 100644 index 6c1b530..0000000 --- a/tests/rollup-plugin-dts/import-json/tsconfig.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "compilerOptions": { - "resolveJsonModule": true, - - "lib": ["es2018", "esnext", "dom"], - "skipDefaultLibCheck": true, - - "strict": true, - - "allowSyntheticDefaultImports": true, - "moduleResolution": "node", - "module": "esnext", - "stripInternal": true, - - "newLine": "lf", - - "noEmit": true, - "declaration": true, - "emitDeclarationOnly": true - } -} diff --git a/tests/tsc.test.ts b/tests/tsc.test.ts index d5032ff..9317d75 100644 --- a/tests/tsc.test.ts +++ b/tests/tsc.test.ts @@ -283,4 +283,19 @@ describe('tsc', () => { ) expect(snapshot).toMatchSnapshot() }) + + test('import JSON', async () => { + const { snapshot } = await rolldownBuild( + path.resolve(dirname, 'fixtures/import-json/index.ts'), + [ + dts({ + compilerOptions: { + isolatedDeclarations: false, + }, + emitDtsOnly: true, + }), + ], + ) + expect(snapshot).toMatchSnapshot() + }) })