From 9d0f4849e11443aa73dd82726ddac451b456b811 Mon Sep 17 00:00:00 2001 From: JohMun Date: Wed, 3 Nov 2021 17:04:29 +0100 Subject: [PATCH] Add option 'exposeFilename' to plugin-vue --- packages/plugin-vue/README.md | 13 +++++++++++++ packages/plugin-vue/src/index.ts | 17 ++++++++++++++++- packages/plugin-vue/src/main.ts | 4 ++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/plugin-vue/README.md b/packages/plugin-vue/README.md index b29039da2cbef0..f2d9bd0c5c66df 100644 --- a/packages/plugin-vue/README.md +++ b/packages/plugin-vue/README.md @@ -46,6 +46,19 @@ export interface Options { */ refTransform?: boolean | string | RegExp | (string | RegExp)[] + /** + * In non-production environments, plugin-vue injects a __file property to components + * for better debugging experience. If the name property is missing in a component, + * Vue will infer it from the __file field to display in console warnings. + * This property is stripped in production builds by default. + * But you may want to retain it if you are developing a component library + * and don't want to bother specifying name in each component. + * Then you can turn this option on. + * + * @default false + */ + exposeFilename?: boolean, + // options to pass on to vue/compiler-sfc script?: Partial template?: Partial diff --git a/packages/plugin-vue/src/index.ts b/packages/plugin-vue/src/index.ts index 01a27ea9959beb..3a89fd74bb0552 100644 --- a/packages/plugin-vue/src/index.ts +++ b/packages/plugin-vue/src/index.ts @@ -56,6 +56,19 @@ export interface Options { */ refTransform?: boolean | string | RegExp | (string | RegExp)[] + /** + * In non-production environments, plugin-vue injects a __file property to components + * for better debugging experience. If the name property is missing in a component, + * Vue will infer it from the __file field to display in console warnings. + * This property is stripped in production builds by default. + * But you may want to retain it if you are developing a component library + * and don't want to bother specifying name in each component. + * Then you can turn this option on. + * + * @default false + */ + exposeFilename?: boolean, + /** * @deprecated the plugin now auto-detects whether it's being invoked for ssr. */ @@ -73,7 +86,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { include = /\.vue$/, exclude, customElement = /\.ce\.vue$/, - refTransform = false + refTransform = false, + exposeFilename = false } = rawOptions const filter = createFilter(include, exclude) @@ -100,6 +114,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { exclude, customElement, refTransform, + exposeFilename, root: process.cwd(), sourceMap: true } diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index ef82b3a05551d2..aedcd6753f92ed 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -27,7 +27,7 @@ export async function transformMain( ssr: boolean, asCustomElement: boolean ) { - const { devServer, isProduction } = options + const { devServer, isProduction, exposeFilename } = options // prev descriptor is only set and used for hmr const prevDescriptor = getPrevDescriptor(filename) @@ -101,7 +101,7 @@ export async function transformMain( if (hasScoped) { attachedProps.push([`__scopeId`, JSON.stringify(`data-v-${descriptor.id}`)]) } - if (devServer && !isProduction) { + if (devServer && !isProduction || exposeFilename) { // expose filename during serve for devtools to pickup attachedProps.push([`__file`, JSON.stringify(filename)]) }