Skip to content

Commit 3d0b844

Browse files
committed
refactor: enable isolatedDeclarations
1 parent 186f894 commit 3d0b844

File tree

10 files changed

+62
-43
lines changed

10 files changed

+62
-43
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"build": "tsdown",
7474
"dev": "pnpm run -C playground dev",
7575
"test": "vitest",
76+
"typecheck": "tsc --noEmit",
7677
"release": "bumpp && pnpm publish",
7778
"benchmark": "node benchmark/index.js",
7879
"prepublishOnly": "pnpm run build"

src/core/convert.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ export function transformJsxToString(
444444
plugins,
445445
id = '',
446446
}: Pick<OptionsResolved, 'debug' | 'plugins'> & { id?: string },
447-
) {
447+
): { code: string; map: any } {
448448
const s = new MagicString(code)
449449

450450
if (id.endsWith('.tsx')) plugins.push('typescript')
@@ -468,8 +468,9 @@ export function transformJsxToString(
468468
code: s.toString(),
469469
get map() {
470470
return s.generateMap({
471-
hires: 'boundary',
471+
source: id,
472472
includeContent: true,
473+
hires: 'boundary',
473474
})
474475
},
475476
}

src/core/options.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FilterPattern } from '@rollup/pluginutils'
33

44
export interface Options {
55
include?: FilterPattern
6-
exclude?: FilterPattern | undefined
6+
exclude?: FilterPattern
77
debug?: boolean
88
/**
99
* Plugins for `@babel/parser`
@@ -12,15 +12,22 @@ export interface Options {
1212
* @default `['jsx']` or `['jsx', 'typescript']`.
1313
*/
1414
plugins?: ParserPlugin[]
15+
enforce?: 'pre' | 'post' | undefined
1516
}
1617

17-
export type OptionsResolved = Required<Options>
18+
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
1819

19-
export function resolveOption(options: Options): OptionsResolved {
20+
export type OptionsResolved = Overwrite<
21+
Required<Options>,
22+
Pick<Options, 'enforce'>
23+
>
24+
25+
export function resolveOptions(options: Options): OptionsResolved {
2026
return {
2127
include: options.include || [/\.[jt]sx$/],
22-
exclude: options.exclude || undefined,
28+
exclude: options.exclude || [/node_modules/],
2329
debug: options.debug ?? false,
2430
plugins: ['jsx'],
31+
enforce: 'enforce' in options ? options.enforce : 'pre',
2532
}
2633
}

src/core/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ export type Primitive =
1010
| bigint
1111

1212
const KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g
13-
export function kebabCase(str: string) {
13+
export function kebabCase(str: string): string {
1414
return str.replaceAll(KEBAB_REGEX, (match) => {
1515
return `-${match.toLowerCase()}`
1616
})
1717
}
1818

19-
export const styleToString = (styles: Record<string, string>) =>
20-
Object.entries(styles)
19+
export function styleToString(styles: Record<string, string>): string {
20+
return Object.entries(styles)
2121
.map(([key, value]) => `${kebabCase(key)}:${value}`)
2222
.join(';')
23+
}
2324

2425
const RAW_RE = /__RAW_(.*?)_RAW/g
2526

26-
export const escapeString = (str: string) => {
27+
export function escapeString(str: string): string {
2728
const text = jsesc(str, {
2829
quotes: 'backtick',
2930
wrap: true,

src/esbuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import unplugin from '.'
22

3-
export default unplugin.esbuild
3+
export default unplugin.esbuild as typeof unplugin.esbuild

src/index.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
import { createFilter } from '@rollup/pluginutils'
2-
import { createUnplugin } from 'unplugin'
2+
import { createUnplugin, type UnpluginInstance } from 'unplugin'
33
import { transformJsxToString } from './core/convert'
4-
import { resolveOption, type Options } from './core/options'
4+
import { resolveOptions, type Options } from './core/options'
55

66
declare global {
77
// @ts-expect-error missing JSX
88
const jsxToString: (element: JSX.Element) => string
99
const jsxRaw: (variable: any) => any
1010
}
1111

12-
export default createUnplugin<Options | undefined, false>((options = {}) => {
13-
const opt = resolveOption(options)
14-
const filter = createFilter(opt.include, opt.exclude)
12+
const JsxString: UnpluginInstance<Options | undefined, false> = createUnplugin(
13+
(options = {}) => {
14+
const opt = resolveOptions(options)
15+
const filter = createFilter(opt.include, opt.exclude)
1516

16-
const name = 'unplugin-jsx-string'
17-
return {
18-
name,
19-
enforce: 'pre',
17+
const name = 'unplugin-jsx-string'
18+
return {
19+
name,
20+
enforce: 'pre',
2021

21-
transformInclude(id) {
22-
return filter(id)
23-
},
22+
transformInclude(id) {
23+
return filter(id)
24+
},
2425

25-
transform(code, id) {
26-
try {
27-
return transformJsxToString(code, {
28-
...opt,
29-
id,
30-
})
31-
} catch (error: unknown) {
32-
this.error(`${name} ${error}`)
33-
}
34-
},
35-
}
36-
})
26+
transform(code, id) {
27+
try {
28+
return transformJsxToString(code, {
29+
...opt,
30+
id,
31+
})
32+
} catch (error: unknown) {
33+
this.error(`${name} ${error}`)
34+
}
35+
},
36+
}
37+
},
38+
)
39+
40+
export default JsxString

src/rollup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import unplugin from '.'
22

3-
export default unplugin.rollup
3+
export default unplugin.rollup as typeof unplugin.rollup

src/vite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import unplugin from '.'
22

3-
export default unplugin.vite
3+
export default unplugin.vite as typeof unplugin.vite

src/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import unplugin from '.'
22

3-
export default unplugin.webpack
3+
export default unplugin.webpack as typeof unplugin.webpack

tsconfig.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
{
22
"compilerOptions": {
3-
"target": "es2022",
3+
"target": "esnext",
44
"jsx": "preserve",
55
"lib": ["es2022"],
6-
"module": "esnext",
7-
"moduleResolution": "node",
6+
"moduleDetection": "force",
7+
"module": "preserve",
8+
"moduleResolution": "bundler",
89
"resolveJsonModule": true,
910
"types": ["node", "react"],
1011
"strict": true,
11-
"exactOptionalPropertyTypes": true,
1212
"noUnusedLocals": true,
13+
"declaration": true,
14+
"isolatedDeclarations": true,
1315
"esModuleInterop": true,
16+
"isolatedModules": true,
17+
"verbatimModuleSyntax": true,
1418
"skipLibCheck": true
1519
},
16-
"include": ["src", "tests"]
20+
"include": ["src", "tests"],
21+
"exclude": ["tests/fixtures"]
1722
}

0 commit comments

Comments
 (0)