Skip to content

Commit f4d573d

Browse files
committed
Add option to use full path
Assists with vue devtools plugin when using Module Federation
1 parent 6986365 commit f4d573d

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface VueLoaderOptions {
3434
transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
3535
compiler?: TemplateCompiler | string
3636
compilerOptions?: CompilerOptions
37+
useAbsolutePath?: boolean
3738
/**
3839
* TODO remove in 3.4
3940
* @deprecated
@@ -314,10 +315,8 @@ export default function loader(
314315
if (!isProduction) {
315316
// Expose the file's full path in development, so that it can be opened
316317
// from the devtools.
317-
propsToAttach.push([
318-
`__file`,
319-
JSON.stringify(rawShortFilePath.replace(/\\/g, '/')),
320-
])
318+
const base = options.useAbsolutePath ? filename : rawShortFilePath
319+
propsToAttach.push([`__file`, JSON.stringify(base.replace(/\\/g, '/'))])
321320
} else if (options.exposeFilename) {
322321
// Libraries can opt-in to expose their components' filenames in production builds.
323322
// For security reasons, only expose the file's basename in production.

src/pluginWebpack5.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const NormalModule = require('webpack/lib/NormalModule')
1313
const BasicEffectRulePlugin = require('webpack/lib/rules/BasicEffectRulePlugin')
1414
const BasicMatcherRulePlugin = require('webpack/lib/rules/BasicMatcherRulePlugin')
1515
const UseEffectRulePlugin = require('webpack/lib/rules/UseEffectRulePlugin')
16-
const RuleSetCompiler =
17-
require('webpack/lib/rules/RuleSetCompiler') as RuleSetCompiler
16+
const RuleSetCompiler = require('webpack/lib/rules/RuleSetCompiler') as RuleSetCompiler
1817

1918
let objectMatcherRulePlugins = []
2019
try {

test/advanced.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
genId,
99
DEFAULT_VUE_USE,
1010
} from './utils'
11+
import path from 'path'
1112

1213
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
1314

@@ -55,7 +56,6 @@ test('no __file in production when exposeFilename disabled', async () => {
5556

5657
expect(componentModule.__file).toBe(undefined)
5758
})
58-
5959
test('expose file basename as __file in production when exposeFilename enabled', async () => {
6060
const { componentModule } = await mockBundleAndRun({
6161
mode: 'production',
@@ -67,6 +67,35 @@ test('expose file basename as __file in production when exposeFilename enabled',
6767
expect(componentModule.__file).toBe('basic.vue')
6868
})
6969

70+
test('use absolute path', async () => {
71+
const { componentModule } = await mockBundleAndRun({
72+
mode: 'development',
73+
entry: 'basic.vue',
74+
vue: {
75+
useAbsolutePath: true,
76+
},
77+
})
78+
79+
expect(componentModule.__file).toBe(
80+
path.join(__dirname, 'fixtures/basic.vue')
81+
)
82+
})
83+
84+
test('use absolute path and expose filename in production', async () => {
85+
const { componentModule } = await mockBundleAndRun({
86+
mode: 'development',
87+
entry: 'basic.vue',
88+
vue: {
89+
useAbsolutePath: true,
90+
exposeFilename: true,
91+
},
92+
})
93+
94+
expect(componentModule.__file).toBe(
95+
path.join(__dirname, 'fixtures/basic.vue')
96+
)
97+
})
98+
7099
test.skip('source map', async () => {
71100
const { code } = await bundle({
72101
entry: 'basic.vue',

0 commit comments

Comments
 (0)