8000 feat: add `features` option · unplugin/unplugin-vue@623ab79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 623ab79

Browse files
committed
feat: add features option
1 parent e83ae1d commit 623ab79

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

src/core/index.ts

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export interface Options {
5454
| 'genDefaultAs'
5555
| 'customElement'
5656
| 'defineModel'
57+
| 'propsDestructure'
5758
>
5859
> & {
5960
/**
@@ -97,11 +98,7 @@ export interface Options {
9798
>
9899

99100
/**
100-
* Transform Vue SFCs into custom elements.
101-
* - `true`: all `*.vue` imports are converted into custom elements
102-
* - `string | RegExp`: matched files are converted into custom elements
103-
*
104-
* @default /\.ce\.vue$/
101+
* @deprecated moved to `features.customElement`.
105102
*/
106103
customElement?: boolean | string | RegExp | (string | RegExp)[]
107104

@@ -114,11 +111,32 @@ export interface Options {
114111
* @default true
115112
*/
116113
inlineTemplate?: boolean
114+
115+
features?: {
116+
optionsAPI?: boolean
117+
prodDevtools?: boolean
118+
prodHydrationMismatchDetails?: boolean
119+
/**
120+
* Enable reactive destructure for `defineProps`.
121+
* - Available in Vue 3.4 and later.
122+
* - Defaults to true in Vue 3.5+
123+
* - Defaults to false in Vue 3.4 (**experimental**)
124+
*/
125+
propsDestructure?: boolean
126+
/**
127+
* Transform Vue SFCs into custom elements.
128+
* - `true`: all `*.vue` imports are converted into custom elements
129+
* - `string | RegExp`: matched files are converted into custom elements
130+
*
131+
* @default /\.ce\.vue$/
132+
*/
133+
customElement?: boolean | string | RegExp | (string | RegExp)[]
134+
}
117135
}
118136

119137
export type Context = UnpluginContext & UnpluginContextMeta
120138

121-
export type ResolvedOptions = Options &
139+
export type ResolvedOptions = Omit<Options, 'customElement'> &
122140
Required<
123141
Pick<
124142
Options,
@@ -127,7 +145,6 @@ export type ResolvedOptions = Options &
127145
| 'ssr'
128146
| 'sourceMap'
129147
| 'root'
130-
| 'customElement'
131148
| 'compiler'
132149
| 'inlineTemplate'
133150
>
@@ -136,6 +153,7 @@ export type ResolvedOptions = Options &
136153
devServer?: ViteDevServer
137154
devToolsEnabled?: boolean
138155
cssDevSourcemap: boolean
156+
features: NonNullable<Options['features']>
139157
}
140158

141159
function resolveOptions(rawOptions: Options): ResolvedOptions {
@@ -149,11 +167,20 @@ function resolveOptions(rawOptions: Options): ResolvedOptions {
149167
ssr: rawOptions.ssr ?? false,
150168
sourceMap: rawOptions.sourceMap ?? true,
151169
root,
152-
customElement: rawOptions.customElement ?? /\.ce\.vue$/,
153170
compiler: rawOptions.compiler as any, // to be set in buildStart
154171
devToolsEnabled: !isProduction,
155172
cssDevSourcemap: false,
156173
inlineTemplate: rawOptions.inlineTemplate ?? true,
174+
features: {
175+
optionsAPI: true,
176+
prodDevtools: false,
177+
prodHydrationMismatchDetails: false,
178+
propsDestructure: false,
179+
...rawOptions.features,
180+
customElement:
181+
(rawOptions.features?.customElement || rawOptions.customElement) ??
182+
/\.ce\.vue$/,
183+
},
157184
}
158185
}
159186

@@ -165,11 +192,12 @@ export const plugin = createUnplugin<Options | undefined, false>(
165192
createFilter(options.value.include, options.value.exclude),
166193
)
167194

168-
const customElementFilter = computed(() =>
169-
typeof options.value.customElement === 'boolean'
170-
? () => options.value.customElement as boolean
171-
: createFilter(options.value.customElement),
172-
)
195+
const customElementFilter = computed(() => {
196+
const customElement = options.value.features.customElement
197+
return typeof customElement === 'boolean'
198+
? () => customElement as boolean
199+
: createFilter(customElement)
200+
})
173201

174202
const api = {
175203
get options() {
@@ -208,11 +236,18 @@ export const plugin = createUnplugin<Options | undefined, false>(
208236
dedupe: config.build?.ssr ? [] : ['vue'],
209237
},
210238
define: {
211-
__VUE_OPTIONS_API__: config.define?.__VUE_OPTIONS_API__ ?? true,
239+
__VUE_OPTIONS_API__:
240+
(options.value.features.optionsAPI ||
241+
config.define?.__VUE_OPTIONS_API__) ??
242+
true,
212243
__VUE_PROD_DEVTOOLS__:
213-
config.define?.__VUE_PROD_DEVTOOLS__ ?? false,
244+
(options.value.features.prodDevtools ||
245+
config.define?.__VUE_PROD_DEVTOOLS__) ??
246+
false,
214247
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__:
215-
config.define?.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ ?? false,
248+
(options.value.features.prodHydrationMismatchDetails ||
249+
config.define?.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__) ??
250+
false,
216251
},
217252
ssr: {
218253
// @ts-ignore -- config.legacy.buildSsrCjsExternalHeuristics will be removed in Vite 5
@@ -232,8 +267,11 @@ export const plugin = createUnplugin<Options | undefined, false>(
232267
cssDevSourcemap: config.css?.devSourcemap ?? false,
233268
isProduction: config.isProduction,
234269
compiler: options.value.compiler || resolveCompiler(config.root),
235-
devToolsEnabled:
236-
!!config.define!.__VUE_PROD_DEVTOOLS__ || !config.isProduction,
270+
devToolsEnabled: !!(
271+
options.value.features.prodDevtools ||
272+
config.define!.__VUE_PROD_DEVTOOLS__ ||
273+
!config.isProduction
274+
),
237275
}
238276
},
239277

src/core/script.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function resolveScript(
8080
? scriptIdentifier
8181
: undefined,
8282
customElement,
83+
propsDestructure: options.features.propsDestructure,
8384
})
8485

8586
if (!options.isProduction && resolved?.deps) {

0 commit comments

Comments
 (0)
0