perf: early return chained functions with falsy values (#25647)

This commit is contained in:
Michael Brevard 2024-02-26 18:08:45 +02:00 committed by GitHub
parent be85a72f99
commit ff1bb56e3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 52 additions and 44 deletions

View File

@ -89,9 +89,9 @@ export function isNuxt3 (nuxt: Nuxt = useNuxt()) {
* Get nuxt version
*/
export function getNuxtVersion (nuxt: Nuxt | any = useNuxt() /* TODO: LegacyNuxt */) {
const version = (nuxt?._version || nuxt?.version || nuxt?.constructor?.version || '').replace(/^v/g, '')
if (!version) {
const rawVersion = nuxt?._version || nuxt?.version || nuxt?.constructor?.version
if (!rawVersion) {
throw new Error('Cannot determine nuxt version! Is current instance passed?')
}
return version
return rawVersion.replace(/^v/g, '')
}

View File

@ -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 = parseInt((pkg.version || '').split('.')[0])
const majorVersion = pkg.version ? parseInt(pkg.version.split('.')[0]) : ''
const rootDir = pathToFileURL(opts.cwd || process.cwd()).href

View File

@ -13,21 +13,23 @@ const props = defineProps({
const _error = props.error
// TODO: extract to a separate utility
const stacktrace = (_error.stack || '')
.split('\n')
.splice(1)
.map((line) => {
const text = line
.replace('webpack:/', '')
.replace('.vue', '.js') // TODO: Support sourcemap
.trim()
return {
text,
internal: (line.includes('node_modules') && !line.includes('.cache')) ||
line.includes('internal') ||
line.includes('new Promise')
}
}).map(i => `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`).join('\n')
const stacktrace = _error.stack
? _error.stack
.split('\n')
.splice(1)
.map((line) => {
const text = line
.replace('webpack:/', '')
.replace('.vue', '.js') // TODO: Support sourcemap
.trim()
return {
text,
internal: (line.includes('node_modules') && !line.includes('.cache')) ||
line.includes('internal') ||
line.includes('new Promise')
}
}).map(i => `<span class="stack${i.internal ? ' internal' : ''}">${i.text}</span>`).join('\n')
: ''
// Error page props
const statusCode = Number(_error.statusCode || 500)

View File

@ -233,7 +233,7 @@ export function checkForCircularDependencies (_plugins: Array<NuxtPlugin & Omit<
return []
}
visited.push(name)
return (deps[name] || []).flatMap(dep => checkDeps(dep, [...visited]))
return deps[name]?.length ? deps[name].flatMap(dep => checkDeps(dep, [...visited])) : []
}
for (const name in deps) {
checkDeps(name)

View File

@ -287,13 +287,15 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
const routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: routeRules })
)
const payloadSuffix = nuxt.options.experimental.renderJsonPayloads ? '/_payload.json' : '/_payload.js'
for (const route of nitro._prerenderedRoutes || []) {
if (!route.error && route.route.endsWith(payloadSuffix)) {
const url = route.route.slice(0, -payloadSuffix.length) || '/'
const rules = defu({}, ...routeRulesMatcher.matchAll(url).reverse()) as Record<string, any>
if (!rules.prerender) {
prerenderedRoutes.add(url)
if (nitro._prerenderedRoutes?.length) {
const payloadSuffix = nuxt.options.experimental.renderJsonPayloads ? '/_payload.json' : '/_payload.js'
for (const route of nitro._prerenderedRoutes) {
if (!route.error && route.route.endsWith(payloadSuffix)) {
const url = route.route.slice(0, -payloadSuffix.length) || '/'
const rules = defu({}, ...routeRulesMatcher.matchAll(url).reverse()) as Record<string, any>
if (!rules.prerender) {
prerenderedRoutes.add(url)
}
}
}
}

View File

@ -21,11 +21,10 @@ export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => {
if (!DEVONLY_COMP_SINGLE_RE.test(code)) { return }
const s = new MagicString(code)
for (const match of code.matchAll(DEVONLY_COMP_RE) || []) {
for (const match of code.matchAll(DEVONLY_COMP_RE)) {
const ast: Node = parse(match[0]).children[0]
const fallback: Node | undefined = ast.children?.find((n: Node) => n.name === 'template' && Object.values(n.attributes).includes('#fallback'))
const replacement = fallback ? match[0].slice(fallback.loc[0].end, fallback.loc[fallback.loc.length - 1].start) : ''
s.overwrite(match.index!, match.index! + match[0].length, replacement)
}

View File

@ -31,7 +31,7 @@ export const TreeShakeComposablesPlugin = createUnplugin((options: TreeShakeComp
const s = new MagicString(code)
const strippedCode = stripLiteral(code)
for (const match of strippedCode.matchAll(COMPOSABLE_RE_GLOBAL) || []) {
for (const match of strippedCode.matchAll(COMPOSABLE_RE_GLOBAL)) {
s.overwrite(match.index!, match.index! + match[0].length, `${match[1]} false && /*@__PURE__*/ ${match[2]}`)
}

View File

@ -28,7 +28,7 @@ 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()))
matchedSuffix.unshift(...splitByCase(prefixParts[index]).map(p => p.toLowerCase()))
const matchedSuffixContent = matchedSuffix.join('/')
if ((fileNamePartsContent === matchedSuffixContent || fileNamePartsContent.startsWith(matchedSuffixContent + '/')) ||
// e.g Item/Item/Item.vue -> Item

View File

@ -86,10 +86,10 @@ export const NoScript = defineComponent({
},
setup: setupForUseMeta((props, { slots }) => {
const noscript = { ...props }
const textContent = (slots.default?.() || [])
.filter(({ children }) => children)
.map(({ children }) => children)
.join('')
const slotVnodes = slots.default?.()
const textContent = slotVnodes
? slotVnodes.filter(({ children }) => children).map(({ children }) => children).join('')
: ''
if (textContent) {
noscript.children = textContent
}

View File

@ -271,13 +271,15 @@ export default defineNuxtModule({
processPages(pages)
})
nuxt.hook('nitro:build:before', (nitro) => {
for (const route of nitro.options.prerender.routes || []) {
// Skip default route value as we only generate it if it is already
// in the detected routes from `~/pages`.
if (route === '/') { continue }
prerenderRoutes.add(route)
if (nitro.options.prerender.routes.length) {
for (const route of nitro.options.prerender.routes) {
// Skip default route value as we only generate it if it is already
// in the detected routes from `~/pages`.
if (route === '/') { continue }
prerenderRoutes.add(route)
}
nitro.options.prerender.routes = Array.from(prerenderRoutes)
}
nitro.options.prerender.routes = Array.from(prerenderRoutes)
})
})
@ -389,13 +391,13 @@ export default defineNuxtModule({
const getSources = (pages: NuxtPage[]): string[] => pages
.filter(p => Boolean(p.file))
.flatMap(p =>
[relative(nuxt.options.srcDir, p.file as string), ...getSources(p.children || [])]
[relative(nuxt.options.srcDir, p.file as string), ...(p.children?.length ? getSources(p.children) : [])]
)
// Do not prefetch page chunks
nuxt.hook('build:manifest', (manifest) => {
if (nuxt.options.dev) { return }
const sourceFiles = getSources(nuxt.apps.default.pages || [])
const sourceFiles = nuxt.apps.default?.pages?.length ? getSources(nuxt.apps.default.pages) : []
for (const key in manifest) {
if (manifest[key].isEntry) {

View File

@ -144,6 +144,8 @@ export function ssrStylesPlugin (options: SSRStylePluginOptions): Plugin {
if (id === options.entry && (options.shouldInline === true || (typeof options.shouldInline === 'function' && options.shouldInline(id)))) {
const s = new MagicString(code)
options.clientCSSMap[id] ||= new Set()
if (!options.globalCSS.length) { return }
for (const file of options.globalCSS) {
const resolved = await this.resolve(file) ?? await this.resolve(file, id)
const res = await this.resolve(file + '?inline&used') ?? await this.resolve(file + '?inline&used', id)

View File

@ -63,7 +63,7 @@ export async function warmupViteServer (
try {
const mod = await server.moduleGraph.getModuleByUrl(url, isServer)
const deps = mod?.ssrTransformResult?.deps /* server */ || Array.from(mod?.importedModules /* client */ || []).map(m => m.url)
const deps = mod?.ssrTransformResult?.deps /* server */ || mod?.importedModules.size ? Array.from(mod?.importedModules /* client */).map(m => m.url) : []
await Promise.all(deps.map(m => warmup(m)))
} catch (e) {
logger.debug('[warmup] tracking dependencies for %s failed with: %s', url, e)

View File

@ -1986,6 +1986,7 @@ describe('component islands', () => {
link.href = link.href.replace(fixtureDir, '/<rootDir>').replaceAll('//', '/')
link.key = link.key.replace(/-[a-zA-Z0-9]+$/, '')
}
result.head.link.sort((a, b) => b.href.localeCompare(a.href))
}
// TODO: fix rendering of styles in webpack