mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-24 22:55:13 +00:00
chore: add more checks around indexed access (#29060)
This commit is contained in:
parent
da019ba39a
commit
6a39c657f5
@ -33,7 +33,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
|
||||
}
|
||||
const pkg = await readPackageJSON(nearestNuxtPkg)
|
||||
const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]) : ''
|
||||
const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]!) : ''
|
||||
|
||||
const rootDir = pathToFileURL(opts.cwd || process.cwd()).href
|
||||
|
||||
|
@ -54,8 +54,10 @@ const NuxtClientFallbackServer = defineComponent({
|
||||
const defaultSlot = ctx.slots.default?.()
|
||||
const ssrVNodes = createBuffer()
|
||||
|
||||
for (let i = 0; i < (defaultSlot?.length || 0); i++) {
|
||||
ssrRenderVNode(ssrVNodes.push, defaultSlot![i], vm!)
|
||||
if (defaultSlot) {
|
||||
for (let i = 0; i < defaultSlot.length; i++) {
|
||||
ssrRenderVNode(ssrVNodes.push, defaultSlot[i]!, vm!)
|
||||
}
|
||||
}
|
||||
|
||||
const buffer = ssrVNodes.getBuffer()
|
||||
|
@ -29,17 +29,20 @@ const getId = import.meta.client ? () => (id++).toString() : randomUUID
|
||||
const components = import.meta.client ? new Map<string, Component>() : undefined
|
||||
|
||||
async function loadComponents (source = appBaseURL, paths: NuxtIslandResponse['components']) {
|
||||
if (!paths) { return }
|
||||
|
||||
const promises: Array<Promise<void>> = []
|
||||
|
||||
for (const component in paths) {
|
||||
for (const [component, item] of Object.entries(paths)) {
|
||||
if (!(components!.has(component))) {
|
||||
promises.push((async () => {
|
||||
const chunkSource = join(source, paths[component].chunk)
|
||||
const chunkSource = join(source, item.chunk)
|
||||
const c = await import(/* @vite-ignore */ chunkSource).then(m => m.default || m)
|
||||
components!.set(component, c)
|
||||
})())
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(promises)
|
||||
}
|
||||
|
||||
@ -276,7 +279,7 @@ export default defineComponent({
|
||||
teleports.push(createVNode(Teleport,
|
||||
// use different selectors for even and odd teleportKey to force trigger the teleport
|
||||
{ to: import.meta.client ? `${isKeyOdd ? 'div' : ''}[data-island-uid="${uid.value}"][data-island-slot="${slot}"]` : `uid=${uid.value};slot=${slot}` },
|
||||
{ default: () => (payloads.slots?.[slot].props?.length ? payloads.slots[slot].props : [{}]).map((data: any) => slots[slot]?.(data)) }),
|
||||
{ default: () => (payloads.slots?.[slot]?.props?.length ? payloads.slots[slot].props : [{}]).map((data: any) => slots[slot]?.(data)) }),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ export default defineComponent({
|
||||
const islandContext = nuxtApp.ssrContext!.islandContext!
|
||||
|
||||
return () => {
|
||||
const slot = slots.default!()[0]
|
||||
const slot = slots.default!()[0]!
|
||||
const slotType = slot.type as ExtendedComponent
|
||||
const name = (slotType.__name || slotType.name) as string
|
||||
|
||||
|
@ -100,7 +100,7 @@ export function vforToArray (source: any): any[] {
|
||||
const keys = Object.keys(source)
|
||||
const array = new Array(keys.length)
|
||||
for (let i = 0, l = keys.length; i < l; i++) {
|
||||
const key = keys[i]
|
||||
const key = keys[i]!
|
||||
array[i] = source[key]
|
||||
}
|
||||
return array
|
||||
|
@ -23,7 +23,7 @@ export async function loadPayload (url: string, opts: LoadPayloadOptions = {}):
|
||||
const nuxtApp = useNuxtApp()
|
||||
const cache = nuxtApp._payloadCache = nuxtApp._payloadCache || {}
|
||||
if (payloadURL in cache) {
|
||||
return cache[payloadURL]
|
||||
return cache[payloadURL] || null
|
||||
}
|
||||
cache[payloadURL] = isPrerendered(url).then((prerendered) => {
|
||||
if (!prerendered) {
|
||||
|
@ -14,7 +14,12 @@ export const preloadComponents = async (components: string | string[]) => {
|
||||
const nuxtApp = useNuxtApp()
|
||||
|
||||
components = toArray(components)
|
||||
await Promise.all(components.map(name => _loadAsyncComponent(nuxtApp.vueApp._context.components[name])))
|
||||
await Promise.all(components.map((name) => {
|
||||
const component = nuxtApp.vueApp._context.components[name]
|
||||
if (component) {
|
||||
return _loadAsyncComponent(component)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,18 +7,18 @@ import { defineNuxtPlugin, useNuxtApp } from '../nuxt'
|
||||
// @ts-expect-error Virtual file.
|
||||
import { componentIslands } from '#build/nuxt.config.mjs'
|
||||
|
||||
const revivers: Record<string, (data: any) => any> = {
|
||||
NuxtError: data => createError(data),
|
||||
EmptyShallowRef: data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
|
||||
EmptyRef: data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data)),
|
||||
ShallowRef: data => shallowRef(data),
|
||||
ShallowReactive: data => shallowReactive(data),
|
||||
Ref: data => ref(data),
|
||||
Reactive: data => reactive(data),
|
||||
}
|
||||
const revivers: [string, (data: any) => any][] = [
|
||||
['NuxtError', data => createError(data)],
|
||||
['EmptyShallowRef', data => shallowRef(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data))],
|
||||
['EmptyRef', data => ref(data === '_' ? undefined : data === '0n' ? BigInt(0) : destr(data))],
|
||||
['ShallowRef', data => shallowRef(data)],
|
||||
['ShallowReactive', data => shallowReactive(data)],
|
||||
['Ref', data => ref(data)],
|
||||
['Reactive', data => reactive(data)],
|
||||
]
|
||||
|
||||
if (componentIslands) {
|
||||
revivers.Island = ({ key, params, result }: any) => {
|
||||
revivers.push(['Island', ({ key, params, result }: any) => {
|
||||
const nuxtApp = useNuxtApp()
|
||||
if (!nuxtApp.isHydrating) {
|
||||
nuxtApp.payload.data[key] = nuxtApp.payload.data[key] || $fetch(`/__nuxt_island/${key}.json`, {
|
||||
@ -33,15 +33,15 @@ if (componentIslands) {
|
||||
html: '',
|
||||
...result,
|
||||
}
|
||||
}
|
||||
}])
|
||||
}
|
||||
|
||||
export default defineNuxtPlugin({
|
||||
name: 'nuxt:revive-payload:client',
|
||||
order: -30,
|
||||
async setup (nuxtApp) {
|
||||
for (const reviver in revivers) {
|
||||
definePayloadReviver(reviver, revivers[reviver as keyof typeof revivers])
|
||||
for (const [reviver, fn] of revivers) {
|
||||
definePayloadReviver(reviver, fn)
|
||||
}
|
||||
Object.assign(nuxtApp.payload, await nuxtApp.runWithContext(getNuxtClientPayload))
|
||||
// For backwards compatibility - TODO: remove later
|
||||
|
@ -78,7 +78,7 @@ export const islandsTransform = createUnplugin((options: ServerOnlyComponentTran
|
||||
|
||||
if (attributes.name) { delete attributes.name }
|
||||
if (attributes['v-bind']) {
|
||||
attributes._bind = extractAttributes(attributes, ['v-bind'])['v-bind']
|
||||
attributes._bind = extractAttributes(attributes, ['v-bind'])['v-bind']!
|
||||
}
|
||||
const teleportAttributes = extractAttributes(attributes, ['v-if', 'v-else-if', 'v-else'])
|
||||
const bindings = getPropsToString(attributes)
|
||||
@ -137,7 +137,7 @@ function extractAttributes (attributes: Record<string, string>, names: string[])
|
||||
const extracted: Record<string, string> = {}
|
||||
for (const name of names) {
|
||||
if (name in attributes) {
|
||||
extracted[name] = attributes[name]
|
||||
extracted[name] = attributes[name]!
|
||||
delete attributes[name]
|
||||
}
|
||||
}
|
||||
|
@ -140,10 +140,10 @@ export default defineNuxtModule<ComponentsOptions>({
|
||||
nuxt.hook('build:manifest', (manifest) => {
|
||||
const sourceFiles = getComponents().filter(c => c.global).map(c => relative(nuxt.options.srcDir, c.filePath))
|
||||
|
||||
for (const key in manifest) {
|
||||
if (manifest[key].isEntry) {
|
||||
manifest[key].dynamicImports =
|
||||
manifest[key].dynamicImports?.filter(i => !sourceFiles.includes(i))
|
||||
for (const chunk of Object.values(manifest)) {
|
||||
if (chunk.isEntry) {
|
||||
chunk.dynamicImports =
|
||||
chunk.dynamicImports?.filter(i => !sourceFiles.includes(i))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -56,6 +56,8 @@ export const TreeShakeTemplatePlugin = createUnplugin((options: TreeShakeTemplat
|
||||
const node = _node as AcornNode<Node>
|
||||
if (isSsrRender(node)) {
|
||||
const [componentCall, _, children] = node.arguments
|
||||
if (!componentCall) { return }
|
||||
|
||||
if (componentCall.type === 'Identifier' || componentCall.type === 'MemberExpression' || componentCall.type === 'CallExpression') {
|
||||
const componentName = getComponentName(node)
|
||||
const isClientComponent = COMPONENTS_IDENTIFIERS_RE.test(componentName)
|
||||
@ -137,11 +139,13 @@ function removeFromSetupReturn (codeAst: Program, name: string, magicString: Mag
|
||||
const variableList = node.value.body.body.filter((statement): statement is VariableDeclaration => statement.type === 'VariableDeclaration')
|
||||
const returnedVariableDeclaration = variableList.find(declaration => declaration.declarations[0]?.id.type === 'Identifier' && declaration.declarations[0]?.id.name === '__returned__' && declaration.declarations[0]?.init?.type === 'ObjectExpression')
|
||||
if (returnedVariableDeclaration) {
|
||||
const init = returnedVariableDeclaration.declarations[0].init as ObjectExpression
|
||||
const init = returnedVariableDeclaration.declarations[0]?.init as ObjectExpression | undefined
|
||||
if (init) {
|
||||
removePropertyFromObject(init, name, magicString)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) {
|
||||
const _plugins: Array<NuxtPlugin & Omit<PluginMeta, 'enforce'>> = []
|
||||
for (const plugin of plugins) {
|
||||
try {
|
||||
const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src] : await fsp.readFile(plugin.src!, 'utf-8')
|
||||
const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src]! : await fsp.readFile(plugin.src!, 'utf-8')
|
||||
_plugins.push({
|
||||
...await extractMetadata(code, IS_TSX.test(plugin.src) ? 'tsx' : 'ts'),
|
||||
...plugin,
|
||||
|
@ -142,9 +142,9 @@ function createGranularWatcher () {
|
||||
delete watchers[path]
|
||||
}
|
||||
if (event === 'addDir' && path !== dir && !ignoredDirs.has(path) && !pathsToWatch.includes(path) && !(path in watchers) && !isIgnored(path)) {
|
||||
watchers[path] = chokidar.watch(path, { ...nuxt.options.watchers.chokidar, ignored: [isIgnored] })
|
||||
const pathWatcher = watchers[path] = chokidar.watch(path, { ...nuxt.options.watchers.chokidar, ignored: [isIgnored] })
|
||||
// TODO: consider moving to emit absolute path in 3.8 or 4.0
|
||||
watchers[path].on('all', (event, p) => nuxt.callHook('builder:watch', event, nuxt.options.experimental.relativeWatchPaths ? normalize(relative(nuxt.options.srcDir, p)) : normalize(p)))
|
||||
pathWatcher.on('all', (event, p) => nuxt.callHook('builder:watch', event, nuxt.options.experimental.relativeWatchPaths ? normalize(relative(nuxt.options.srcDir, p)) : normalize(p)))
|
||||
nuxt.hook('close', () => watchers[path]?.close())
|
||||
}
|
||||
})
|
||||
|
@ -345,10 +345,10 @@ async function initNuxt (nuxt: Nuxt) {
|
||||
// TODO: [Experimental] Avoid emitting assets when flag is enabled
|
||||
if (nuxt.options.features.noScripts && !nuxt.options.dev) {
|
||||
nuxt.hook('build:manifest', async (manifest) => {
|
||||
for (const file in manifest) {
|
||||
if (manifest[file].resourceType === 'script') {
|
||||
await rm(resolve(nuxt.options.buildDir, 'dist/client', withoutLeadingSlash(nuxt.options.app.buildAssetsDir), manifest[file].file), { force: true })
|
||||
manifest[file].file = ''
|
||||
for (const chunk of Object.values(manifest)) {
|
||||
if (chunk.resourceType === 'script') {
|
||||
await rm(resolve(nuxt.options.buildDir, 'dist/client', withoutLeadingSlash(nuxt.options.app.buildAssetsDir), chunk.file), { force: true })
|
||||
chunk.file = ''
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -64,7 +64,7 @@ export const LayerAliasingPlugin = createUnplugin((options: LayerAliasingOptions
|
||||
if (!layer || !ALIAS_RE_SINGLE.test(code)) { return }
|
||||
|
||||
const s = new MagicString(code)
|
||||
s.replace(ALIAS_RE, r => aliases[layer][r as '~'] || r)
|
||||
s.replace(ALIAS_RE, r => aliases[layer]?.[r as '~'] || r)
|
||||
|
||||
if (s.hasChanged()) {
|
||||
return {
|
||||
|
@ -70,7 +70,7 @@ export async function extractMetadata (code: string, loader = 'ts' as 'ts' | 'ts
|
||||
}
|
||||
|
||||
const plugin = node.arguments[0]
|
||||
if (plugin.type === 'ObjectExpression') {
|
||||
if (plugin?.type === 'ObjectExpression') {
|
||||
meta = defu(extractMetaFromObject(plugin.properties), meta)
|
||||
}
|
||||
|
||||
@ -122,7 +122,7 @@ export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
|
||||
name: 'nuxt:remove-plugin-metadata',
|
||||
transform (code, id) {
|
||||
id = normalize(id)
|
||||
const plugin = nuxt.apps.default.plugins.find(p => p.src === id)
|
||||
const plugin = nuxt.apps.default?.plugins.find(p => p.src === id)
|
||||
if (!plugin) { return }
|
||||
|
||||
const s = new MagicString(code)
|
||||
@ -163,7 +163,7 @@ export const RemovePluginMetadataPlugin = (nuxt: Nuxt) => createUnplugin(() => {
|
||||
if (!name || !wrapperNames.has(name)) { return }
|
||||
wrapped = true
|
||||
|
||||
if (node.arguments[0].type !== 'ObjectExpression') {
|
||||
if (node.arguments[0] && node.arguments[0].type !== 'ObjectExpression') {
|
||||
// TODO: Warn if legacy plugin format is detected
|
||||
if ('params' in node.arguments[0] && node.arguments[0].params.length > 1) {
|
||||
logger.warn(`Plugin \`${plugin.src}\` is in legacy Nuxt 2 format (context, inject) which is likely to be broken and will be ignored.`)
|
||||
|
@ -32,6 +32,7 @@ export function prehydrateTransformPlugin (nuxt: Nuxt) {
|
||||
const node = _node as SimpleCallExpression & { start: number, end: number }
|
||||
const name = 'name' in node.callee && node.callee.name
|
||||
if (name === 'onPrehydrate') {
|
||||
if (!node.arguments[0]) { return }
|
||||
if (node.arguments[0].type !== 'ArrowFunctionExpression' && node.arguments[0].type !== 'FunctionExpression') { return }
|
||||
|
||||
const needsAttr = node.arguments[0].params.length > 0
|
||||
|
@ -326,8 +326,10 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
|
||||
// Render 103 Early Hints
|
||||
if (process.env.NUXT_EARLY_HINTS && !isRenderingPayload && !import.meta.prerender) {
|
||||
const { link } = renderResourceHeaders({}, renderer.rendererContext)
|
||||
if (link) {
|
||||
writeEarlyHints(event, link)
|
||||
}
|
||||
}
|
||||
|
||||
if (process.env.NUXT_INLINE_STYLES && !isRenderingIsland) {
|
||||
for (const id of await getEntryIds()) {
|
||||
@ -395,8 +397,7 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
|
||||
}
|
||||
if (!isRenderingIsland || import.meta.dev) {
|
||||
const link: Link[] = []
|
||||
for (const style in styles) {
|
||||
const resource = styles[style]
|
||||
for (const resource of Object.values(styles)) {
|
||||
// Do not add links to resources that are inlined (vite v5+)
|
||||
if (import.meta.dev && 'inline' in getURLQuery(resource.file)) {
|
||||
continue
|
||||
@ -495,7 +496,7 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
|
||||
const islandResponse: NuxtIslandResponse = {
|
||||
id: islandContext.id,
|
||||
head: islandHead,
|
||||
html: getServerComponentHTML(htmlContext.body),
|
||||
html: getServerComponentHTML(htmlContext.body as [string, ...string[]]),
|
||||
components: getClientIslandResponse(ssrContext),
|
||||
slots: getSlotIslandResponse(ssrContext),
|
||||
}
|
||||
@ -567,7 +568,7 @@ async function renderInlineStyles (usedModules: Set<string> | string[]): Promise
|
||||
const styleMap = await getSSRStyles()
|
||||
const inlinedStyles = new Set<string>()
|
||||
for (const mod of usedModules) {
|
||||
if (mod in styleMap) {
|
||||
if (mod in styleMap && styleMap[mod]) {
|
||||
for (const style of await styleMap[mod]()) {
|
||||
inlinedStyles.add(style)
|
||||
}
|
||||
@ -649,9 +650,9 @@ function splitPayload (ssrContext: NuxtSSRContext) {
|
||||
/**
|
||||
* remove the root node from the html body
|
||||
*/
|
||||
function getServerComponentHTML (body: string[]): string {
|
||||
function getServerComponentHTML (body: [string, ...string[]]): string {
|
||||
const match = body[0].match(ROOT_NODE_REGEX)
|
||||
return match ? match[1] : body[0]
|
||||
return match?.[1] || body[0]
|
||||
}
|
||||
|
||||
const SSR_SLOT_TELEPORT_MARKER = /^uid=([^;]*);slot=(.*)$/
|
||||
@ -661,10 +662,10 @@ const SSR_CLIENT_SLOT_MARKER = /^island-slot=[^;]*;(.*)$/
|
||||
function getSlotIslandResponse (ssrContext: NuxtSSRContext): NuxtIslandResponse['slots'] {
|
||||
if (!ssrContext.islandContext || !Object.keys(ssrContext.islandContext.slots).length) { return undefined }
|
||||
const response: NuxtIslandResponse['slots'] = {}
|
||||
for (const slot in ssrContext.islandContext.slots) {
|
||||
response[slot] = {
|
||||
...ssrContext.islandContext.slots[slot],
|
||||
fallback: ssrContext.teleports?.[`island-fallback=${slot}`],
|
||||
for (const [name, slot] of Object.entries(ssrContext.islandContext.slots)) {
|
||||
response[name] = {
|
||||
...slot,
|
||||
fallback: ssrContext.teleports?.[`island-fallback=${name}`],
|
||||
}
|
||||
}
|
||||
return response
|
||||
@ -674,11 +675,11 @@ function getClientIslandResponse (ssrContext: NuxtSSRContext): NuxtIslandRespons
|
||||
if (!ssrContext.islandContext || !Object.keys(ssrContext.islandContext.components).length) { return undefined }
|
||||
const response: NuxtIslandResponse['components'] = {}
|
||||
|
||||
for (const clientUid in ssrContext.islandContext.components) {
|
||||
for (const [clientUid, component] of Object.entries(ssrContext.islandContext.components)) {
|
||||
// remove teleport anchor to avoid hydration issues
|
||||
const html = ssrContext.teleports?.[clientUid].replaceAll('<!--teleport start anchor-->', '') || ''
|
||||
const html = ssrContext.teleports?.[clientUid]?.replaceAll('<!--teleport start anchor-->', '') || ''
|
||||
response[clientUid] = {
|
||||
...ssrContext.islandContext.components[clientUid],
|
||||
...component,
|
||||
html,
|
||||
slots: getComponentSlotTeleport(ssrContext.teleports ?? {}),
|
||||
}
|
||||
|
@ -28,11 +28,12 @@ export function resolveComponentNameSegments (fileName: string, prefixParts: str
|
||||
let index = prefixParts.length - 1
|
||||
const matchedSuffix: string[] = []
|
||||
while (index >= 0) {
|
||||
matchedSuffix.unshift(...splitByCase(prefixParts[index]).map(p => p.toLowerCase()))
|
||||
const prefixPart = prefixParts[index]!
|
||||
matchedSuffix.unshift(...splitByCase(prefixPart).map(p => p.toLowerCase()))
|
||||
const matchedSuffixContent = matchedSuffix.join('/')
|
||||
if ((fileNamePartsContent === matchedSuffixContent || fileNamePartsContent.startsWith(matchedSuffixContent + '/')) ||
|
||||
// e.g Item/Item/Item.vue -> Item
|
||||
(prefixParts[index].toLowerCase() === fileNamePartsContent &&
|
||||
(prefixPart.toLowerCase() === fileNamePartsContent &&
|
||||
prefixParts[index + 1] &&
|
||||
prefixParts[index] === prefixParts[index + 1])) {
|
||||
componentNameParts.length = index
|
||||
|
@ -160,7 +160,7 @@ export const Title = defineComponent({
|
||||
if (import.meta.dev) {
|
||||
const defaultSlot = slots.default?.()
|
||||
|
||||
if (defaultSlot && (defaultSlot.length > 1 || typeof defaultSlot[0].children !== 'string')) {
|
||||
if (defaultSlot && (defaultSlot.length > 1 || (defaultSlot[0] && typeof defaultSlot[0].children !== 'string'))) {
|
||||
console.error('<Title> can take only one string in its default slot.')
|
||||
}
|
||||
|
||||
|
@ -242,7 +242,7 @@ export default defineNuxtModule({
|
||||
]
|
||||
})
|
||||
|
||||
function isPage (file: string, pages = nuxt.apps.default.pages): boolean {
|
||||
function isPage (file: string, pages = nuxt.apps.default?.pages): boolean {
|
||||
if (!pages) { return false }
|
||||
return pages.some(page => page.file === file) || pages.some(page => page.children && isPage(file, page.children))
|
||||
}
|
||||
@ -354,7 +354,7 @@ export default defineNuxtModule({
|
||||
|
||||
const updatePage = async function updatePage (path: string) {
|
||||
const glob = pageToGlobMap[path]
|
||||
const code = path in nuxt.vfs ? nuxt.vfs[path] : await readFile(path!, 'utf-8')
|
||||
const code = path in nuxt.vfs ? nuxt.vfs[path]! : await readFile(path!, 'utf-8')
|
||||
try {
|
||||
const extractedRule = await extractRouteRules(code)
|
||||
if (extractedRule) {
|
||||
@ -403,8 +403,7 @@ export default defineNuxtModule({
|
||||
nuxt.hook('pages:extend', (routes) => {
|
||||
const nitro = useNitro()
|
||||
let resolvedRoutes: string[]
|
||||
for (const path in nitro.options.routeRules) {
|
||||
const rule = nitro.options.routeRules[path]
|
||||
for (const [path, rule] of Object.entries(nitro.options.routeRules)) {
|
||||
if (!rule.redirect) { continue }
|
||||
resolvedRoutes ||= routes.flatMap(route => resolveRoutePaths(route))
|
||||
// skip if there's already a route matching this path
|
||||
@ -450,14 +449,14 @@ export default defineNuxtModule({
|
||||
if (nuxt.options.dev) { return }
|
||||
const sourceFiles = nuxt.apps.default?.pages?.length ? getSources(nuxt.apps.default.pages) : []
|
||||
|
||||
for (const key in manifest) {
|
||||
if (manifest[key].src && Object.values(nuxt.apps).some(app => app.pages?.some(page => page.mode === 'server' && page.file === join(nuxt.options.srcDir, manifest[key].src!)))) {
|
||||
for (const [key, chunk] of Object.entries(manifest)) {
|
||||
if (chunk.src && Object.values(nuxt.apps).some(app => app.pages?.some(page => page.mode === 'server' && page.file === join(nuxt.options.srcDir, chunk.src!)))) {
|
||||
delete manifest[key]
|
||||
continue
|
||||
}
|
||||
if (manifest[key].isEntry) {
|
||||
manifest[key].dynamicImports =
|
||||
manifest[key].dynamicImports?.filter(i => !sourceFiles.includes(i))
|
||||
if (chunk.isEntry) {
|
||||
chunk.dynamicImports =
|
||||
chunk.dynamicImports?.filter(i => !sourceFiles.includes(i))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -14,7 +14,7 @@ const ruleCache: Record<string, NitroRouteConfig | null> = {}
|
||||
|
||||
export async function extractRouteRules (code: string): Promise<NitroRouteConfig | null> {
|
||||
if (code in ruleCache) {
|
||||
return ruleCache[code]
|
||||
return ruleCache[code] || null
|
||||
}
|
||||
if (!ROUTE_RULE_RE.test(code)) { return null }
|
||||
|
||||
|
@ -102,7 +102,7 @@ export function generateRoutesFromFiles (files: ScannedFile[], options: Generate
|
||||
// Array where routes should be added, useful when adding child routes
|
||||
let parent = routes
|
||||
|
||||
const lastSegment = segments[segments.length - 1]
|
||||
const lastSegment = segments[segments.length - 1]!
|
||||
if (lastSegment.endsWith('.server')) {
|
||||
segments[segments.length - 1] = lastSegment.replace('.server', '')
|
||||
if (options.shouldUseServerComponents) {
|
||||
@ -116,7 +116,7 @@ export function generateRoutesFromFiles (files: ScannedFile[], options: Generate
|
||||
for (let i = 0; i < segments.length; i++) {
|
||||
const segment = segments[i]
|
||||
|
||||
const tokens = parseSegment(segment)
|
||||
const tokens = parseSegment(segment!)
|
||||
|
||||
// Skip group segments
|
||||
if (tokens.every(token => token.type === SegmentTokenType.group)) {
|
||||
@ -151,7 +151,7 @@ export function generateRoutesFromFiles (files: ScannedFile[], options: Generate
|
||||
export async function augmentPages (routes: NuxtPage[], vfs: Record<string, string>, augmentedPages = new Set<string>()) {
|
||||
for (const route of routes) {
|
||||
if (route.file && !augmentedPages.has(route.file)) {
|
||||
const fileContent = route.file in vfs ? vfs[route.file] : fs.readFileSync(await resolvePath(route.file), 'utf-8')
|
||||
const fileContent = route.file in vfs ? vfs[route.file]! : fs.readFileSync(await resolvePath(route.file), 'utf-8')
|
||||
const routeMeta = await getRouteMeta(fileContent, route.file)
|
||||
if (route.meta) {
|
||||
routeMeta.meta = { ...routeMeta.meta, ...route.meta }
|
||||
@ -174,7 +174,7 @@ export function extractScriptContent (html: string) {
|
||||
for (const match of html.matchAll(SFC_SCRIPT_RE)) {
|
||||
if (match?.groups?.content) {
|
||||
contents.push({
|
||||
loader: match.groups.attrs.includes('tsx') ? 'tsx' : 'ts',
|
||||
loader: match.groups.attrs?.includes('tsx') ? 'tsx' : 'ts',
|
||||
code: match.groups.content.trim(),
|
||||
})
|
||||
}
|
||||
@ -196,7 +196,9 @@ export async function getRouteMeta (contents: string, absolutePath: string): Pro
|
||||
delete metaCache[absolutePath]
|
||||
}
|
||||
|
||||
if (absolutePath in metaCache) { return metaCache[absolutePath] }
|
||||
if (absolutePath in metaCache && metaCache[absolutePath]) {
|
||||
return metaCache[absolutePath]
|
||||
}
|
||||
|
||||
const loader = getLoader(absolutePath)
|
||||
const scriptBlocks = !loader ? null : loader === 'vue' ? extractScriptContent(contents) : [{ code: contents, loader }]
|
||||
@ -403,7 +405,7 @@ function parseSegment (segment: string) {
|
||||
consumeBuffer()
|
||||
}
|
||||
state = SegmentParserState.initial
|
||||
} else if (PARAM_CHAR_RE.test(c)) {
|
||||
} else if (c && PARAM_CHAR_RE.test(c)) {
|
||||
buffer += c
|
||||
} else {
|
||||
// console.debug(`[pages]Ignored character "${c}" while building param "${buffer}" from "segment"`)
|
||||
|
@ -297,8 +297,8 @@ async function getResolvedApp (files: Array<string | { name: string, contents: s
|
||||
mw.path = normaliseToRepo(mw.path)!
|
||||
}
|
||||
|
||||
for (const layout in app.layouts) {
|
||||
app.layouts[layout].file = normaliseToRepo(app.layouts[layout].file)!
|
||||
for (const layout of Object.values(app.layouts)) {
|
||||
layout.file = normaliseToRepo(layout.file)!
|
||||
}
|
||||
|
||||
await nuxt.close()
|
||||
|
@ -654,7 +654,7 @@ describe('pages:generateRoutesFromFiles', () => {
|
||||
}))).map((route, index) => {
|
||||
return {
|
||||
...route,
|
||||
meta: test.files![index].meta,
|
||||
meta: test.files![index]!.meta,
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -5,7 +5,8 @@ import { relative, resolve } from 'pathe'
|
||||
import { withTrailingSlash, withoutLeadingSlash } from 'ufo'
|
||||
import escapeRE from 'escape-string-regexp'
|
||||
import { normalizeViteManifest } from 'vue-bundle-renderer'
|
||||
import type { Manifest } from 'vue-bundle-renderer'
|
||||
import type { Manifest as RendererManifest } from 'vue-bundle-renderer'
|
||||
import type { Manifest as ViteClientManifest } from 'vite'
|
||||
import type { ViteBuildContext } from './vite'
|
||||
|
||||
export async function writeManifest (ctx: ViteBuildContext, css: string[] = []) {
|
||||
@ -13,7 +14,7 @@ export async function writeManifest (ctx: ViteBuildContext, css: string[] = [])
|
||||
const clientDist = resolve(ctx.nuxt.options.buildDir, 'dist/client')
|
||||
const serverDist = resolve(ctx.nuxt.options.buildDir, 'dist/server')
|
||||
|
||||
const devClientManifest: Manifest = {
|
||||
const devClientManifest: RendererManifest = {
|
||||
'@vite/client': {
|
||||
isEntry: true,
|
||||
file: '@vite/client',
|
||||
@ -32,17 +33,18 @@ export async function writeManifest (ctx: ViteBuildContext, css: string[] = [])
|
||||
const manifestFile = resolve(clientDist, 'manifest.json')
|
||||
const clientManifest = ctx.nuxt.options.dev
|
||||
? devClientManifest
|
||||
: JSON.parse(readFileSync(manifestFile, 'utf-8'))
|
||||
: JSON.parse(readFileSync(manifestFile, 'utf-8')) as ViteClientManifest
|
||||
|
||||
const manifestEntries = Object.values(clientManifest)
|
||||
|
||||
const buildAssetsDir = withTrailingSlash(withoutLeadingSlash(ctx.nuxt.options.app.buildAssetsDir))
|
||||
const BASE_RE = new RegExp(`^${escapeRE(buildAssetsDir)}`)
|
||||
|
||||
for (const key in clientManifest) {
|
||||
const entry = clientManifest[key]
|
||||
for (const entry of manifestEntries) {
|
||||
if (entry.file) {
|
||||
entry.file = entry.file.replace(BASE_RE, '')
|
||||
}
|
||||
for (const item of ['css', 'assets']) {
|
||||
for (const item of ['css', 'assets'] as const) {
|
||||
if (entry[item]) {
|
||||
entry[item] = entry[item].map((i: string) => i.replace(BASE_RE, ''))
|
||||
}
|
||||
@ -52,12 +54,11 @@ export async function writeManifest (ctx: ViteBuildContext, css: string[] = [])
|
||||
await mkdir(serverDist, { recursive: true })
|
||||
|
||||
if (ctx.config.build?.cssCodeSplit === false) {
|
||||
for (const key in clientManifest as Record<string, { file?: string }>) {
|
||||
const val = clientManifest[key]
|
||||
if (val.file?.endsWith('.css')) {
|
||||
for (const entry of manifestEntries) {
|
||||
if (entry.file?.endsWith('.css')) {
|
||||
const key = relative(ctx.config.root!, ctx.entry)
|
||||
clientManifest[key].css ||= []
|
||||
clientManifest[key].css!.push(val.file)
|
||||
clientManifest[key]!.css ||= []
|
||||
;(clientManifest[key]!.css as string[]).push(entry.file)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,7 @@ export function analyzePlugin (ctx: ViteBuildContext): Plugin[] {
|
||||
const bundle = outputBundle[_bundleId]
|
||||
if (!bundle || bundle.type !== 'chunk') { continue }
|
||||
const minifiedModuleEntryPromises: Array<Promise<[string, RenderedModule]>> = []
|
||||
for (const moduleId in bundle.modules) {
|
||||
const module = bundle.modules[moduleId]!
|
||||
for (const [moduleId, module] of Object.entries(bundle.modules)) {
|
||||
minifiedModuleEntryPromises.push(
|
||||
transform(module.code || '', { minify: true })
|
||||
.then(result => [moduleId, { ...module, code: result.code }]),
|
||||
|
@ -86,8 +86,7 @@ export const VitePublicDirsPlugin = createUnplugin((options: VitePublicDirsPlugi
|
||||
}
|
||||
},
|
||||
generateBundle (_outputOptions, bundle) {
|
||||
for (const file in bundle) {
|
||||
const chunk = bundle[file]!
|
||||
for (const [file, chunk] of Object.entries(bundle)) {
|
||||
if (!file.endsWith('.css') || chunk.type !== 'asset') { continue }
|
||||
|
||||
let css = chunk.source.toString()
|
||||
|
@ -62,8 +62,7 @@ export function ssrStylesPlugin (options: SSRStylePluginOptions): Plugin {
|
||||
if (options.mode === 'client') { return }
|
||||
|
||||
const emitted: Record<string, string> = {}
|
||||
for (const file in cssMap) {
|
||||
const { files, inBundle } = cssMap[file]!
|
||||
for (const [file, { files, inBundle }] of Object.entries(cssMap)) {
|
||||
// File has been tree-shaken out of build (or there are no styles to inline)
|
||||
if (!files.length || !inBundle) { continue }
|
||||
const fileName = filename(file)
|
||||
|
@ -208,8 +208,7 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
|
||||
|
||||
// Remove CSS entries for files that will have inlined styles
|
||||
ctx.nuxt.hook('build:manifest', (manifest) => {
|
||||
for (const key in manifest) {
|
||||
const entry = manifest[key]!
|
||||
for (const [key, entry] of Object.entries(manifest)) {
|
||||
const shouldRemoveCSS = chunksWithInlinedCSS.has(key) && !entry.isEntry
|
||||
if (entry.isEntry && chunksWithInlinedCSS.has(key)) {
|
||||
// @ts-expect-error internal key
|
||||
|
@ -33,9 +33,10 @@ export default class VueSSRClientPlugin {
|
||||
const stats = compilation.getStats().toJson()
|
||||
|
||||
const initialFiles = new Set<string>()
|
||||
for (const name in stats.entrypoints!) {
|
||||
const entryAssets = stats.entrypoints![name]!.assets!
|
||||
for (const asset of entryAssets) {
|
||||
for (const { assets } of Object.values(stats.entrypoints!)) {
|
||||
if (!assets) { continue }
|
||||
|
||||
for (const asset of assets) {
|
||||
const file = asset.name
|
||||
if ((isJS(file) || isCSS(file)) && !isHotUpdate(file)) {
|
||||
initialFiles.add(file)
|
||||
@ -71,15 +72,15 @@ export default class VueSSRClientPlugin {
|
||||
}
|
||||
|
||||
const { entrypoints = {}, namedChunkGroups = {} } = stats
|
||||
const assetModules = stats.modules!.filter(m => m.assets!.length)
|
||||
const fileToIndex = (file: string) => webpackManifest.all.indexOf(file)
|
||||
stats.modules!.forEach((m) => {
|
||||
const fileToIndex = (file: string | number) => webpackManifest.all.indexOf(String(file))
|
||||
for (const m of stats.modules!) {
|
||||
// Ignore modules duplicated in multiple chunks
|
||||
if (m.chunks!.length === 1) {
|
||||
const [cid] = m.chunks!
|
||||
if (m.chunks?.length !== 1) { continue }
|
||||
|
||||
const [cid] = m.chunks
|
||||
const chunk = stats.chunks!.find(c => c.id === cid)
|
||||
if (!chunk || !chunk.files || !cid) {
|
||||
return
|
||||
continue
|
||||
}
|
||||
const id = m.identifier!.replace(/\s\w+$/, '') // remove appended hash
|
||||
const filesSet = new Set(chunk.files.map(fileToIndex).filter(i => i !== -1))
|
||||
@ -110,13 +111,14 @@ export default class VueSSRClientPlugin {
|
||||
}
|
||||
|
||||
// Find all asset modules associated with the same chunk
|
||||
assetModules.forEach((m) => {
|
||||
if (m.chunks!.includes(cid)) {
|
||||
files.push(...(m.assets as string[]).map(fileToIndex))
|
||||
if (stats.modules) {
|
||||
for (const m of stats.modules) {
|
||||
if (m.assets?.length && m.chunks?.includes(cid)) {
|
||||
files.push(...m.assets.map(fileToIndex))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const manifest = normalizeWebpackManifest(webpackManifest as any)
|
||||
await this.options.nuxt.callHook('build:manifest', manifest)
|
||||
|
@ -18,13 +18,13 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM
|
||||
// Identical behaviour between inline/external vue options as this should only affect the server build
|
||||
|
||||
it('default client bundle size', async () => {
|
||||
const [clientStats, clientStatsInlined] = await Promise.all(['.output', '.output-inline']
|
||||
const [clientStats, clientStatsInlined] = await Promise.all((['.output', '.output-inline'])
|
||||
.map(outputDir => analyzeSizes(['**/*.js'], join(rootDir, outputDir, 'public'))))
|
||||
|
||||
expect.soft(roundToKilobytes(clientStats.totalBytes)).toMatchInlineSnapshot(`"114k"`)
|
||||
expect.soft(roundToKilobytes(clientStatsInlined.totalBytes)).toMatchInlineSnapshot(`"114k"`)
|
||||
expect.soft(roundToKilobytes(clientStats!.totalBytes)).toMatchInlineSnapshot(`"114k"`)
|
||||
expect.soft(roundToKilobytes(clientStatsInlined!.totalBytes)).toMatchInlineSnapshot(`"114k"`)
|
||||
|
||||
const files = new Set([...clientStats.files, ...clientStatsInlined.files].map(f => f.replace(/\..*\.js/, '.js')))
|
||||
const files = new Set([...clientStats!.files, ...clientStatsInlined!.files].map(f => f.replace(/\..*\.js/, '.js')))
|
||||
|
||||
expect([...files]).toMatchInlineSnapshot(`
|
||||
[
|
||||
|
@ -12,8 +12,7 @@
|
||||
"verbatimModuleSyntax": true,
|
||||
/* Strictness */
|
||||
"strict": true,
|
||||
// TODO: enable noUncheckedIndexedAccess
|
||||
// "noUncheckedIndexedAccess": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noUncheckedSideEffectImports": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitOverride": true,
|
||||
|
Loading…
Reference in New Issue
Block a user