|
| 1 | +import { getLang } from '@vue-macros/common' |
| 2 | +import type { UnpluginContextMeta } from 'unplugin' |
| 3 | + |
1 | 4 | // vue-router/auto/routes was more natural but didn't work well with TS |
2 | 5 | export const MODULE_ROUTES_PATH = `vue-router/auto-routes` |
3 | 6 | export const MODULE_RESOLVER_PATH = `vue-router/auto-resolver` |
@@ -33,4 +36,30 @@ export function asVirtualId(id: string) { |
33 | 36 | return VIRTUAL_PREFIX + id |
34 | 37 | } |
35 | 38 |
|
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 | +} |
0 commit comments