Skip to content

Commit 001382d

Browse files
authored
feat: make __file injection opt-in in production (#1475)
1 parent adc6dd6 commit 001382d

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

Diff for: docs/options.md

+9
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,12 @@ When both options are specified, enables file-system-based template compilation
9292
- default: `true`
9393

9494
In development mode, we use [prettier](https://prettier.io/) to format the compiled render function for ease of debugging by default. However, if you encounter any obscure bug of prettier, such as [exponential compilation time for deeply nested functions](https://github.com/prettier/prettier/issues/4672), you can disable this option to circumvent it.
95+
96+
## exposeFilename
97+
98+
- type: `boolean`
99+
- default: `false`
100+
101+
In non-production environments, vue-loader 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.
102+
103+
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.

Diff for: lib/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,15 @@ var component = normalizer(
176176
}
177177

178178
// Expose filename. This is used by the devtools and Vue runtime warnings.
179-
code += `\ncomponent.options.__file = ${
180-
isProduction
181-
// For security reasons, only expose the file's basename in production.
182-
? JSON.stringify(filename)
183-
// Expose the file's full path in development, so that it can be opened
184-
// from the devtools.
185-
: JSON.stringify(rawShortFilePath.replace(/\\/g, '/'))
186-
}`
179+
if (!isProduction) {
180+
// Expose the file's full path in development, so that it can be opened
181+
// from the devtools.
182+
code += `\ncomponent.options.__file = ${JSON.stringify(rawShortFilePath.replace(/\\/g, '/'))}`
183+
} else if (options.exposeFilename) {
184+
// Libraies can opt-in to expose their components' filenames in production builds.
185+
// For security reasons, only expose the file's basename in production.
186+
code += `\ncomponent.options.__file = ${JSON.stringify(filename)}`
187+
}
187188

188189
code += `\nexport default component.exports`
189190
// console.log(code)

Diff for: test/advanced.spec.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,31 @@ test('expose file path as __file outside production', done => {
5454
})
5555
})
5656

57-
test('expose file basename as __file in production', done => {
57+
test('no __file in production when exposeFilename disabled', done => {
5858
const origNodeEnv = process.env.NODE_ENV
5959
process.env.NODE_ENV = 'production'
6060
mockBundleAndRun(
6161
{
6262
entry: 'basic.vue'
6363
},
64+
({ module }) => {
65+
expect(module.__file).toBe(undefined)
66+
process.env.NODE_ENV = origNodeEnv
67+
done()
68+
}
69+
)
70+
})
71+
72+
test('expose file basename as __file in production when exposeFilename enabled', done => {
73+
const origNodeEnv = process.env.NODE_ENV
74+
process.env.NODE_ENV = 'production'
75+
mockBundleAndRun(
76+
{
77+
entry: 'basic.vue',
78+
vue: {
79+
exposeFilename: true
80+
}
81+
},
6482
({ module }) => {
6583
expect(module.__file).toBe('basic.vue')
6684
process.env.NODE_ENV = origNodeEnv

0 commit comments

Comments
 (0)