Skip to content

Commit 91feea1

Browse files
committed
refactor: enable isolatedDeclarations
1 parent 58fd186 commit 91feea1

File tree

11 files changed

+232
-84
lines changed

11 files changed

+232
-84
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"build": "tsdown",
7272
"dev": "tsdown --watch",
7373
"test": "vitest",
74-
"typecheck": "vue-tsc --noEmit",
74+
"typecheck": "tsc --noEmit",
7575
"release": "bumpp && pnpm publish",
7676
"prepublishOnly": "pnpm run build"
7777
},

src/core/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import { pascalCase } from 'change-case'
33

4-
export function resolveName(id: string) {
4+
export function resolveName(id: string): string {
55
return pascalCase(path.basename(id, 'vue'))
66
}

src/esbuild.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for esbuild plugin.
3+
*
4+
* @module
5+
*/
26

3-
export default unplugin.esbuild
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Esbuild plugin
11+
*
12+
* @example
13+
* ```ts
14+
* import { build } from 'esbuild'
15+
* import Starter from 'unplugin-starter/esbuild'
16+
*
17+
* build({ plugins: [Starter()] })
18+
```
19+
*/
20+
const esbuild = VueNamedExport.esbuild as typeof VueNamedExport.esbuild
21+
export default esbuild

src/farm.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* This entry file is for Farm plugin.
3+
*
4+
* @module
5+
*/
6+
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Farm plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // farm.config.js
15+
* import Starter from 'unplugin-starter/farm'
16+
*
17+
* export default {
18+
* plugins: [Starter()],
19+
* }
20+
* ```
21+
*/
22+
const farm = VueNamedExport.farm as typeof VueNamedExport.farm
23+
export default farm

src/index.ts

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createFilter } from '@rollup/pluginutils'
22
import { babelParse, getLang } from 'ast-kit'
33
import { generateTransform, MagicString } from 'magic-string-ast'
4-
import { createUnplugin } from 'unplugin'
4+
import { createUnplugin, type UnpluginInstance } from 'unplugin'
55
import { resolveOption, type Options } from './core/options'
66
import { resolveName } from './core/utils'
77
import type * as t from '@babel/types'
@@ -13,78 +13,80 @@ function getNodeStart(node: t.Node) {
1313
return node.start!
1414
}
1515

16-
export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
17-
const options = resolveOption(rawOptions)
18-
const filter = createFilter(options.include, options.exclude)
16+
const VueNamedExport: UnpluginInstance<Options | undefined, false> =
17+
createUnplugin((rawOptions = {}) => {
18+
const options = resolveOption(rawOptions)
19+
const filter = createFilter(options.include, options.exclude)
1920

20-
const name = 'unplugin-vue-named-export'
21-
return {
22-
name,
23-
enforce: 'post',
21+
const name = 'unplugin-vue-named-export'
22+
return {
23+
name,
24+
enforce: 'post',
2425

25-
transformInclude(id) {
26-
return filter(id)
27-
},
28-
29-
async transform(code, id) {
30-
const lang = getLang(id)
26+
transformInclude(id) {
27+
return filter(id)
28+
},
3129

32-
const program = babelParse(code, lang)
33-
const defaultExport = program.body.find(
34-
(node): node is t.ExportDefaultDeclaration =>
35-
node.type === 'ExportDefaultDeclaration',
36-
)
37-
if (!defaultExport) return
30+
async transform(code, id) {
31+
const lang = getLang(id)
3832

39-
const s = new MagicString(code)
40-
const resolvedName = await (options.resolveName || resolveName)(id)
33+
const program = babelParse(code, lang)
34+
const defaultExport = program.body.find(
35+
(node): node is t.ExportDefaultDeclaration =>
36+
node.type === 'ExportDefaultDeclaration',
37+
)
38+
if (!defaultExport) return
4139

42-
s.overwrite(
43-
defaultExport.start!,
44-
getNodeStart(defaultExport.declaration),
45-
`export const ${resolvedName} = `,
46-
)
40+
const s = new MagicString(code)
41+
const resolvedName = await (options.resolveName || resolveName)(id)
4742

48-
if (!options.removeDefault) {
49-
s.appendLeft(defaultExport.end!, `\nexport default ${resolvedName};`)
50-
} else {
51-
// hack Vite HMR
52-
s.replace(
53-
/const \{ default: updated, (.*) \} = mod/,
54-
(_, $1) => `const { "${resolvedName}": updated, ${$1} } = mod`,
43+
s.overwrite(
44+
defaultExport.start!,
45+
getNodeStart(defaultExport.declaration),
46+
`export const ${resolvedName} = `,
5547
)
56-
}
5748

58-
return generateTransform(s, id)
59-
},
49+
if (!options.removeDefault) {
50+
s.appendLeft(defaultExport.end!, `\nexport default ${resolvedName};`)
51+
} else {
52+
// hack Vite HMR
53+
s.replace(
54+
/const \{ default: updated, (.*) \} = mod/,
55+
(_, $1) => `const { "${resolvedName}": updated, ${$1} } = mod`,
56+
)
57+
}
58+
59+
return generateTransform(s, id)
60+
},
6061

61-
vite: {
62-
config(config, { command }) {
63-
if (command !== 'serve') return
64-
return {
65-
optimizeDeps: {
66-
esbuildOptions: {
67-
plugins: [
68-
{
69-
name: `${name}-optimize-deps`,
70-
setup(build) {
71-
build.onLoad({ filter: /\.vue($|\?)/ }, async (args) => {
72-
const resolvedName = await (
73-
options.resolveName || resolveName
74-
)(args.path)
75-
let js = `export const ${resolvedName} = {}`
76-
if (!options.removeDefault) {
77-
js += `\nexport default ${resolvedName}`
78-
}
79-
return { contents: js }
80-
})
62+
vite: {
63+
config(config, { command }) {
64+
if (command !== 'serve') return
65+
return {
66+
optimizeDeps: {
67+
esbuildOptions: {
68+
plugins: [
69+
{
70+
name: `${name}-optimize-deps`,
71+
setup(build) {
72+
build.onLoad({ filter: /\.vue($|\?)/ }, async (args) => {
73+
const resolvedName = await (
74+
options.resolveName || resolveName
75+
)(args.path)
76+
let js = `export const ${resolvedName} = {}`
77+
if (!options.removeDefault) {
78+
js += `\nexport default ${resolvedName}`
79+
}
80+
return { contents: js }
81+
})
82+
},
8183
},
82-
},
83-
],
84+
],
85+
},
8486
},
85-
},
86-
}
87+
}
88+
},
8789
},
88-
},
89-
}
90-
})
90+
}
91+
})
92+
export default VueNamedExport

src/rolldown.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* This entry file is for Rolldown plugin.
3+
*
4+
* @module
5+
*/
6+
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Rolldown plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // rolldown.config.js
15+
* import Starter from 'unplugin-starter/rolldown'
16+
*
17+
* export default {
18+
* plugins: [Starter()],
19+
* }
20+
* ```
21+
*/
22+
const rolldown = VueNamedExport.rolldown as typeof VueNamedExport.rolldown
23+
export default rolldown

src/rollup.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for Rollup plugin.
3+
*
4+
* @module
5+
*/
26

3-
export default unplugin.rollup
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Rollup plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // rollup.config.js
15+
* import Starter from 'unplugin-starter/rollup'
16+
*
17+
* export default {
18+
* plugins: [Starter()],
19+
* }
20+
* ```
21+
*/
22+
const rollup = VueNamedExport.rollup as typeof VueNamedExport.rollup
23+
export default rollup

src/rspack.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* This entry file is for Rspack plugin.
3+
*
4+
* @module
5+
*/
6+
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Rspack plugin
11+
*
12+
* @example
13+
* ```js
14+
* // rspack.config.js
15+
* import Starter from 'unplugin-starter/rspack'
16+
*
17+
* default export {
18+
* plugins: [Starter()],
19+
* }
20+
* ```
21+
*/
22+
const rspack = VueNamedExport.rspack as typeof VueNamedExport.rspack
23+
export default rspack

src/vite.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for Vite plugin.
3+
*
4+
* @module
5+
*/
26

3-
export default unplugin.vite
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Vite plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // vite.config.ts
15+
* import Starter from 'unplugin-starter/vite'
16+
*
17+
* export default defineConfig({
18+
* plugins: [Starter()],
19+
* })
20+
* ```
21+
*/
22+
const vite = VueNamedExport.vite as typeof VueNamedExport.vite
23+
export default vite

src/webpack.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for webpack plugin.
3+
*
4+
* @module
5+
*/
26

3-
export default unplugin.webpack
7+
import VueNamedExport from './index'
8+
9+
/**
10+
* Webpack plugin
11+
*
12+
* @example
13+
* ```js
14+
* // webpack.config.js
15+
* import Starter from 'unplugin-starter/webpack'
16+
*
17+
* default export {
18+
* plugins: [Starter()],
19+
* }
20+
* ```
21+
*/
22+
const webpack = VueNamedExport.webpack as typeof VueNamedExport.webpack
23+
export default webpack

0 commit comments

Comments
 (0)