Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-vue): respect __VUE_PROD_DEVTOOLS__ setting #4984

Merged
merged 7 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ test('/about', async () => {

test('/external', async () => {
await page.goto(url + '/external')
expect(await page.textContent('div')).toMatch('Example external component content')
expect(await page.textContent('div')).toMatch(
'Example external component content'
)
// should not have hydration mismatch
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('mismatch')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<template>
<div>Example external component content</div>
</template>
<template>
<div>Example external component content</div>
</template>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import ExampleExternalComponent from './ExampleExternalComponent.vue'
export default ExampleExternalComponent
import ExampleExternalComponent from './ExampleExternalComponent.vue'

export default ExampleExternalComponent
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "example-external-component",
"private": true,
"version": "0.0.0",
"main": "index.js"
}
{
"name": "example-external-component",
"private": true,
"version": "0.0.0",
"main": "index.js"
}
26 changes: 13 additions & 13 deletions packages/playground/ssr-vue/src/pages/External.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<template>
<ExampleExternalComponent />
</template>
<script>
import ExampleExternalComponent from 'example-external-component'
export default {
components: {
ExampleExternalComponent
}
}
</script>
<template>
<ExampleExternalComponent />
</template>

<script>
import ExampleExternalComponent from 'example-external-component'

export default {
components: {
ExampleExternalComponent
}
}
</script>
8 changes: 6 additions & 2 deletions packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface ResolvedOptions extends Options {
root: string
sourceMap: boolean
devServer?: ViteDevServer
devToolsEnabled?: boolean
}

export default function vuePlugin(rawOptions: Options = {}): Plugin {
Expand Down Expand Up @@ -101,7 +102,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
customElement,
refTransform,
root: process.cwd(),
sourceMap: true
sourceMap: true,
devToolsEnabled: process.env.NODE_ENV !== 'production'
}

return {
Expand Down Expand Up @@ -132,7 +134,9 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
...options,
root: config.root,
sourceMap: config.command === 'build' ? !!config.build.sourcemap : true,
isProduction: config.isProduction
isProduction: config.isProduction,
devToolsEnabled:
!!config.define!.__VUE_PROD_DEVTOOLS__ || !config.isProduction
}
},

Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function transformMain(
ssr: boolean,
asCustomElement: boolean
) {
const { devServer, isProduction } = options
const { devServer, isProduction, devToolsEnabled } = options

// prev descriptor is only set and used for hmr
const prevDescriptor = getPrevDescriptor(filename)
Expand Down Expand Up @@ -58,6 +58,7 @@ export async function transformMain(
// inlined template cannot be individually hot updated.
const useInlineTemplate =
!devServer &&
!devToolsEnabled &&
Copy link
Member

@sodatea sodatea Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems exposing __file is enough for the devtool to display the component name, why do you want to disable inlineTemplate?

Copy link
Contributor Author

@oliverzy oliverzy Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because for component with script setup, inlineTemplate as true doesn't expose any setup binding. Vue devtools relies on this to display component setup binding correctly.

Copy link
Member

@sodatea sodatea Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.
I have discussed this with Evan today.
As the inlineTemplate option has an impact on the app performance, we believe that it shouldn't be disabled.
We need to find another way to fix this issue. Maybe in another package.

descriptor.scriptSetup &&
!(descriptor.template && descriptor.template.src)
const hasTemplateImport = descriptor.template && !useInlineTemplate
Expand Down Expand Up @@ -108,7 +109,7 @@ export async function transformMain(
if (hasScoped) {
attachedProps.push([`__scopeId`, JSON.stringify(`data-v-${descriptor.id}`)])
}
if (devServer && !isProduction) {
if (devToolsEnabled || (devServer && !isProduction)) {
// expose filename during serve for devtools to pickup
attachedProps.push([`__file`, JSON.stringify(filename)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to safety concerns, the full path should not be used in production. Please use path.basename(filename).
The development bundle should still use the full path so that users can open the file directly from devtools.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I've just started using @vitejs/plugin-vue2 in my rollup project, and was surprised to see the full filepath to my modules included in the generated output (as __component__.options.__file), and have not found a way to disable this. Can you advise on how to remove these filepaths?

}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function resolveScript(
...options.script,
id: descriptor.id,
isProd: options.isProduction,
inlineTemplate: !options.devServer,
inlineTemplate: !options.devServer && !options.devToolsEnabled,
refTransform: options.refTransform !== false,
templateOptions: resolveTemplateCompilerOptions(descriptor, options, ssr),
// @ts-ignore TODO remove ignore when we support this in @vue/compiler-sfc
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export function arraify<T>(target: T | T[]): T[] {
}

export function toUpperCaseDriveLetter(pathName: string): string {
return pathName.replace(/^\w:/, letter => letter.toUpperCase())
return pathName.replace(/^\w:/, (letter) => letter.toUpperCase())
}

export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
Expand Down