Skip to content

Commit 6f4d5ec

Browse files
committed
fix: fix lang matching
1 parent 7de82cb commit 6f4d5ec

File tree

5 files changed

+73
-39
lines changed

5 files changed

+73
-39
lines changed

Diff for: src/index.ts

+7-32
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import { formatError } from './formatError'
2020
import VueLoaderPlugin from './plugin'
2121
import { canInlineTemplate } from './resolveScript'
2222
import { setDescriptor } from './descriptorCache'
23-
import { getOptions, stringifyRequest as _stringifyRequest } from './util'
23+
import {
24+
getOptions,
25+
stringifyRequest as _stringifyRequest,
26+
genMatchResource,
27+
testWebpack5,
28+
} from './util'
2429

2530
export { VueLoaderPlugin }
2631

@@ -94,14 +99,11 @@ export default function loader(
9499
_compiler,
95100
} = loaderContext
96101

97-
const webpackVersion = _compiler?.webpack?.version
98-
const isWebpack5 = webpackVersion && webpackVersion[0] > '4'
99-
102+
const isWebpack5 = testWebpack5(_compiler)
100103
const rawQuery = _resourceQuery.slice(1)
101104
const incomingQuery = qs.parse(rawQuery)
102105
const resourceQuery = rawQuery ? `&${rawQuery}` : ''
103106
const options = (getOptions(loaderContext) || {}) as VueLoaderOptions
104-
105107
const enableInlineMatchResource =
106108
isWebpack5 && Boolean(options.experimentalInlineMatchResource)
107109

@@ -392,30 +394,3 @@ function attrsToQuery(attrs: SFCBlock['attrs'], langFallback?: string): string {
392394
}
393395
return query
394396
}
395-
396-
function genMatchResource(
397-
context: LoaderContext<VueLoaderOptions>,
398-
resourcePath: string,
399-
resourceQuery?: string,
400-
lang?: string,
401-
additionalLoaders?: string[]
402-
) {
403-
resourceQuery = resourceQuery || ''
404-
additionalLoaders = additionalLoaders || []
405-
406-
const loaders = [...additionalLoaders]
407-
const parsedQuery = qs.parse(resourceQuery.slice(1))
408-
409-
// process non-external resources
410-
if ('vue' in parsedQuery && !('external' in parsedQuery)) {
411-
const currentRequest = context.loaders
412-
.slice(context.loaderIndex)
413-
.map((obj) => obj.request)
414-
loaders.push(...currentRequest)
415-
}
416-
const loaderString = loaders.join('!')
417-
418-
return `${resourcePath}${lang ? `.${lang}` : ''}${resourceQuery}!=!${
419-
loaderString ? `${loaderString}!` : ''
420-
}${resourcePath}${resourceQuery}`
421-
}

Diff for: src/pitcher.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { LoaderDefinitionFunction, LoaderContext } from 'webpack'
22
import * as qs from 'querystring'
3-
import { stringifyRequest } from './util'
3+
import { getOptions, stringifyRequest, testWebpack5 } from './util'
44
import { VueLoaderOptions } from '.'
55

66
const selfPath = require.resolve('./index')
@@ -58,8 +58,7 @@ export const pitch = function () {
5858
})
5959

6060
// Inject style-post-loader before css-loader for scoped CSS and trimming
61-
const webpackVersion = context._compiler?.webpack?.version
62-
const isWebpack5 = webpackVersion && webpackVersion[0] > '4'
61+
const isWebpack5 = testWebpack5(context._compiler)
6362
if (query.type === `style`) {
6463
if (isWebpack5 && context._compiler?.options.experiments.css) {
6564
if (query.inline || query.module) {
@@ -103,15 +102,21 @@ export const pitch = function () {
103102

104103
// Rewrite request. Technically this should only be done when we have deduped
105104
// loaders. But somehow this is required for block source maps to work.
106-
return genProxyModule(loaders, context, query.type !== 'template')
105+
return genProxyModule(
106+
loaders,
107+
context,
108+
query.type !== 'template',
109+
query.ts ? 'ts' : (query.lang as string)
110+
)
107111
}
108112

109113
function genProxyModule(
110114
loaders: (Loader | string)[],
111115
context: LoaderContext<VueLoaderOptions>,
112-
exportDefault = true
116+
exportDefault = true,
117+
lang = 'js'
113118
) {
114-
const request = genRequest(loaders, context)
119+
const request = genRequest(loaders, lang, context)
115120
// return a proxy module which simply re-exports everything from the
116121
// actual request. Note for template blocks the compiled module has no
117122
// default export.
@@ -123,12 +128,28 @@ function genProxyModule(
123128

124129
function genRequest(
125130
loaders: (Loader | string)[],
131+
lang: string,
126132
context: LoaderContext<VueLoaderOptions>
127133
) {
134+
const isWebpack5 = testWebpack5(context._compiler)
135+
const options = (getOptions(context) || {}) as VueLoaderOptions
136+
const enableInlineMatchResource =
137+
isWebpack5 && options.experimentalInlineMatchResource
138+
128139
const loaderStrings = loaders.map((loader) => {
129140
return typeof loader === 'string' ? loader : loader.request
130141
})
131142
const resource = context.resourcePath + context.resourceQuery
143+
144+
if (enableInlineMatchResource) {
145+
return stringifyRequest(
146+
context,
147+
`${context.resourcePath}${lang ? `.${lang}` : ''}${
148+
context.resourceQuery
149+
}!=!-!${[...loaderStrings, resource].join('!')}`
150+
)
151+
}
152+
132153
return stringifyRequest(
133154
context,
134155
'-!' + [...loaderStrings, resource].join('!')

Diff for: src/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Compiler } from 'webpack'
2+
import { testWebpack5 } from './util'
23

34
declare class VueLoaderPlugin {
45
static NS: string
@@ -11,7 +12,7 @@ class Plugin {
1112
static NS = NS
1213
apply(compiler: Compiler) {
1314
let Ctor: typeof VueLoaderPlugin
14-
if (compiler.webpack?.version && compiler.webpack.version[0] > '4') {
15+
if (testWebpack5(compiler)) {
1516
// webpack5 and upper
1617
Ctor = require('./pluginWebpack5').default
1718
} else {

Diff for: src/pluginWebpack5.ts

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class VueLoaderPlugin {
223223
const parsed = qs.parse(query.slice(1))
224224
return parsed.vue != null
225225
},
226+
options: vueLoaderOptions,
226227
}
227228

228229
// replace original rules

Diff for: src/util.ts

+36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Compiler, LoaderContext } from 'webpack'
2+
import qs from 'querystring'
23
import type { SFCDescriptor, CompilerOptions } from 'vue/compiler-sfc'
34
import type { VueLoaderOptions } from '.'
45
import * as path from 'path'
@@ -163,3 +164,38 @@ export function stringifyRequest(
163164
.join('!')
164165
)
165166
}
167+
168+
export function genMatchResource(
169+
context: LoaderContext<VueLoaderOptions>,
170+
resourcePath: string,
171+
resourceQuery?: string,
172+
lang?: string,
173+
additionalLoaders?: string[]
174+
) {
175+
resourceQuery = resourceQuery || ''
176+
additionalLoaders = additionalLoaders || []
177+
178+
const loaders = [...additionalLoaders]
179+
const parsedQuery = qs.parse(resourceQuery.slice(1))
180+
181+
// process non-external resources
182+
if ('vue' in parsedQuery && !('external' in parsedQuery)) {
183+
const currentRequest = context.loaders
184+
.slice(context.loaderIndex)
185+
.map((obj) => obj.request)
186+
loaders.push(...currentRequest)
187+
}
188+
const loaderString = loaders.join('!')
189+
190+
return `${resourcePath}${lang ? `.${lang}` : ''}${resourceQuery}!=!${
191+
loaderString ? `${loaderString}!` : ''
192+
}${resourcePath}${resourceQuery}`
193+
}
194+
195+
export const testWebpack5 = (compiler?: Compiler) => {
196+
if (!compiler) {
197+
return false
198+
}
199+
const webpackVersion = compiler?.webpack?.version
200+
return Boolean(webpackVersion && webpackVersion[0] > '4')
201+
}

0 commit comments

Comments
 (0)