mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): detect non-functional imports within page meta (#8881)
This commit is contained in:
parent
be8036098f
commit
9227361027
@ -2,7 +2,7 @@ import { pathToFileURL } from 'node:url'
|
|||||||
import { createUnplugin } from 'unplugin'
|
import { createUnplugin } from 'unplugin'
|
||||||
import { parseQuery, parseURL, stringifyQuery } from 'ufo'
|
import { parseQuery, parseURL, stringifyQuery } from 'ufo'
|
||||||
import { findStaticImports, findExports, StaticImport, parseStaticImport } from 'mlly'
|
import { findStaticImports, findExports, StaticImport, parseStaticImport } from 'mlly'
|
||||||
import type { CallExpression, Expression } from 'estree'
|
import type { CallExpression, Identifier, Expression } from 'estree'
|
||||||
import { walk } from 'estree-walker'
|
import { walk } from 'estree-walker'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import { isAbsolute, normalize } from 'pathe'
|
import { isAbsolute, normalize } from 'pathe'
|
||||||
@ -93,6 +93,7 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
const importMap = new Map<string, StaticImport>()
|
const importMap = new Map<string, StaticImport>()
|
||||||
|
const addedImports = new Set()
|
||||||
for (const i of imports) {
|
for (const i of imports) {
|
||||||
const parsed = parseStaticImport(i)
|
const parsed = parseStaticImport(i)
|
||||||
for (const name of [
|
for (const name of [
|
||||||
@ -118,14 +119,25 @@ export const PageMetaPlugin = createUnplugin((options: PageMetaPluginOptions) =>
|
|||||||
|
|
||||||
let contents = `const __nuxt_page_meta = ${code!.slice(meta.start, meta.end) || '{}'}\nexport default __nuxt_page_meta`
|
let contents = `const __nuxt_page_meta = ${code!.slice(meta.start, meta.end) || '{}'}\nexport default __nuxt_page_meta`
|
||||||
|
|
||||||
|
function addImport (name: string | false) {
|
||||||
|
if (name && importMap.has(name)) {
|
||||||
|
const importValue = importMap.get(name)!.code
|
||||||
|
if (!addedImports.has(importValue)) {
|
||||||
|
contents = importMap.get(name)!.code + '\n' + contents
|
||||||
|
addedImports.add(importValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
walk(meta, {
|
walk(meta, {
|
||||||
enter (_node) {
|
enter (_node) {
|
||||||
if (_node.type === 'CallExpression') {
|
if (_node.type === 'CallExpression') {
|
||||||
const node = _node as CallExpression & { start: number, end: number }
|
const node = _node as CallExpression & { start: number, end: number }
|
||||||
const name = 'name' in node.callee && node.callee.name
|
addImport('name' in node.callee && node.callee.name)
|
||||||
if (name && importMap.has(name)) {
|
}
|
||||||
contents = importMap.get(name)!.code + '\n' + contents
|
if (_node.type === 'Identifier') {
|
||||||
}
|
const node = _node as Identifier & { start: number, end: number }
|
||||||
|
addImport(node.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
5
test/fixtures/basic/pages/index.vue
vendored
5
test/fixtures/basic/pages/index.vue
vendored
@ -23,6 +23,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { setupDevtoolsPlugin } from '@vue/devtools-api'
|
import { setupDevtoolsPlugin } from '@vue/devtools-api'
|
||||||
import { useRuntimeConfig } from '#imports'
|
import { useRuntimeConfig } from '#imports'
|
||||||
|
import { importedValue, importedRE } from '~/some-exports'
|
||||||
|
|
||||||
setupDevtoolsPlugin({}, () => {}) as any
|
setupDevtoolsPlugin({}, () => {}) as any
|
||||||
|
|
||||||
@ -30,7 +31,9 @@ const config = useRuntimeConfig()
|
|||||||
|
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
alias: '/some-alias',
|
alias: '/some-alias',
|
||||||
other: ref('test')
|
other: ref('test'),
|
||||||
|
imported: importedValue,
|
||||||
|
something: importedRE.test('an imported regex')
|
||||||
})
|
})
|
||||||
|
|
||||||
// reset title template example
|
// reset title template example
|
||||||
|
2
test/fixtures/basic/some-exports.ts
vendored
Normal file
2
test/fixtures/basic/some-exports.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export const importedValue = 'an imported value'
|
||||||
|
export const importedRE = /an imported regex/
|
Loading…
Reference in New Issue
Block a user