Skip to content

Commit 8c91f98

Browse files
authored
fix(unplugin): support definePage with webpack and rspack (#2829)
The `?definePage` import of `.vue` pages still matched the `.vue` rule in webpack and rspack, so vue-loader tried to compile the extracted route data as an SFC and failed. Use an inline match resource so the module goes through the tsx rules instead. See posva/unplugin-vue-router#531
1 parent 596d481 commit 8c91f98

6 files changed

Lines changed: 66 additions & 13 deletions

File tree

‎packages/router/src/unplugin/codegen/generateRouteRecords.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ describe('generateRouteRecord', () => {
115115
expect(routes).toContain('_definePage_nav_2e_top_1\n')
116116
})
117117

118+
it.each(['webpack', 'rspack'] as const)(
119+
'keeps definePage imports away from vue-loader with %s',
120+
framework => {
121+
const tree = new PrefixTree(DEFAULT_OPTIONS)
122+
tree.insert('foo', 'foo.vue').setDefinePageImport('foo.vue', true)
123+
tree.insert('bar', 'bar.tsx').setDefinePageImport('bar.tsx', true)
124+
125+
const importsMap = new ImportsMap()
126+
generateRouteRecords(
127+
tree,
128+
{ ...DEFAULT_OPTIONS, _framework: framework },
129+
importsMap
130+
)
131+
132+
expect(importsMap.toString()).toMatchInlineSnapshot(`
133+
"import _definePage_default_0 from 'bar.tsx?definePage&lang.tsx'
134+
import { _mergeRouteRecord } from 'vue-router/experimental'
135+
import _definePage_default_2 from 'foo.vue.definePage.tsx?definePage!=!foo.vue?definePage&vue&lang.tsx'
136+
"
137+
`)
138+
}
139+
)
140+
118141
it('nested children', () => {
119142
const tree = new PrefixTree(DEFAULT_OPTIONS)
120143
tree.insert('a/a', 'a/a.vue')

‎packages/router/src/unplugin/codegen/generateRouteRecords.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getLang } from '@vue-macros/common'
1+
import { getDefinePageImportId } from '../core/moduleConstants'
22
import type { TreeNode } from '../core/tree'
33
import type { ImportsMap } from '../core/utils'
44
import { type ResolvedOptions } from '../options'
@@ -41,11 +41,8 @@ ${node
4141
if (!node.fileNeedsDefinePageImport(filePath)) continue
4242
const pageDataImport = `_definePage_${encodeImportIdentifierPart(name)}_${importsMap.size}`
4343
definePageDataList.push(pageDataImport)
44-
const lang = getLang(filePath)
4544
importsMap.addDefault(
46-
// TODO: apply the language used in the sfc
47-
`${filePath}?definePage&` +
48-
(lang === 'vue' ? 'vue&lang.tsx' : `lang.${lang}`),
45+
getDefinePageImportId(filePath, options._framework),
4946
pageDataImport
5047
)
5148
}

‎packages/router/src/unplugin/codegen/generateRouteResolver.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getLang } from '@vue-macros/common'
1+
import { getDefinePageImportId } from '../core/moduleConstants'
22
import { PrefixTree, type TreeNode } from '../core/tree'
33
import type { ImportsMap } from '../core/utils'
44
import { type ResolvedOptions } from '../options'
@@ -166,11 +166,8 @@ export function generateRouteRecord({
166166
if (!node.fileNeedsDefinePageImport(filePath)) continue
167167
const pageDataImport = `_definePage_${encodeImportIdentifierPart(name)}_${importsMap.size}`
168168
definePageDataList.push(pageDataImport)
169-
const lang = getLang(filePath)
170169
importsMap.addDefault(
171-
// TODO: apply the language used in the sfc
172-
`${filePath}?definePage&` +
173-
(lang === 'vue' ? 'vue&lang.tsx' : `lang.${lang}`),
170+
getDefinePageImportId(filePath, options._framework),
174171
pageDataImport
175172
)
176173
}

‎packages/router/src/unplugin/core/moduleConstants.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { getLang } from '@vue-macros/common'
2+
import type { UnpluginContextMeta } from 'unplugin'
3+
14
// vue-router/auto/routes was more natural but didn't work well with TS
25
export const MODULE_ROUTES_PATH = `vue-router/auto-routes`
36
export const MODULE_RESOLVER_PATH = `vue-router/auto-resolver`
@@ -33,4 +36,30 @@ export function asVirtualId(id: string) {
3336
return VIRTUAL_PREFIX + id
3437
}
3538

36-
export const DEFINE_PAGE_QUERY_RE = /\?.*\bdefinePage&vue\b/
39+
export const DEFINE_PAGE_QUERY_RE = /\?.*\bdefinePage\b/
40+
41+
/**
42+
* Generates the id used to import the `definePage()` route data of a page
43+
* component.
44+
*
45+
* @param filePath - path of the page component
46+
* @param framework - bundler the plugin is running in
47+
*/
48+
export function getDefinePageImportId(
49+
filePath: string,
50+
framework?: UnpluginContextMeta['framework']
51+
): string {
52+
const lang = getLang(filePath)
53+
// TODO: apply the language used in the sfc
54+
if (lang !== 'vue') return `${filePath}?definePage&lang.${lang}`
55+
56+
const id = `${filePath}?definePage&vue&lang.tsx`
57+
// webpack and rspack match rules against the file path, ignoring the query,
58+
// so vue-loader would try to compile the extracted `definePage()` as an SFC.
59+
// Using a match resource makes the module go through the tsx rules instead
60+
// https://webpack.js.org/api/loaders/#inline-matchresource
61+
if (framework === 'webpack' || framework === 'rspack') {
62+
return `${filePath}.definePage.tsx?definePage!=!${id}`
63+
}
64+
return id
65+
}

‎packages/router/src/unplugin/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export { DEFAULT_OPTIONS }
4040
export { AutoExportLoaders } from '../experimental/data-loaders/auto-exports'
4141
export type { AutoExportLoadersOptions } from '../experimental/data-loaders/auto-exports'
4242

43-
export default createUnplugin<Options | undefined>((opt = {}, _meta) => {
44-
const options = resolveOptions(opt)
43+
export default createUnplugin<Options | undefined>((opt = {}, meta) => {
44+
const options = resolveOptions({ ...opt, _framework: meta.framework })
4545
const ctx = createRoutesContext(options)
4646

4747
function getVirtualId(id: string) {

‎packages/router/src/unplugin/options.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { resolve } from 'pathe'
66
import type { EditableTreeNode } from './core/extendRoutes'
77
import { type ParseSegmentOptions } from './core/treeNodeValue'
88
import { type _Awaitable } from './utils'
9+
import type { UnpluginContextMeta } from 'unplugin'
910

1011
/**
1112
* Options for a routes folder.
@@ -213,6 +214,12 @@ export interface Options {
213214
*/
214215
_inspect?: boolean
215216

217+
/**
218+
* Bundler the plugin is running in. Set automatically by the plugin.
219+
* @internal
220+
*/
221+
_framework?: UnpluginContextMeta['framework']
222+
216223
/**
217224
* Activates debug logs.
218225
*/

0 commit comments

Comments
 (0)