perf(nuxt): mark define functions as side-effect free at source (#21434)

This commit is contained in:
Anthony Fu 2023-06-07 12:11:24 +02:00 committed by GitHub
parent 2fccfa2d5c
commit 6d59a027c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 3 deletions

View File

@ -94,7 +94,10 @@
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
],
"jsdoc/check-tag-names": ["error", {
"definedTags": ["__NO_SIDE_EFFECTS__"]
}]
},
"settings": {
"jsdoc": {

View File

@ -22,6 +22,7 @@ export default defineComponent({
const cache = new WeakMap()
/*! @__NO_SIDE_EFFECTS__ */
export function createClientOnly<T extends ComponentOptions> (component: T) {
if (cache.has(component)) {
return cache.get(component)

View File

@ -46,6 +46,7 @@ export type NuxtLinkProps = {
ariaCurrentValue?: string
}
/*! @__NO_SIDE_EFFECTS__ */
export function defineNuxtLink (options: NuxtLinkOptions) {
const componentName = options.componentName || 'NuxtLink'

View File

@ -27,6 +27,7 @@ async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<str
}
}
/*! @__NO_SIDE_EFFECTS__ */
export const defineNuxtComponent: typeof defineComponent =
function defineNuxtComponent (...args: any[]): any {
const [options, key] = args

View File

@ -42,7 +42,10 @@ export interface RouteMiddleware {
(to: RouteLocationNormalized, from: RouteLocationNormalized): ReturnType<NavigationGuard>
}
export const defineNuxtRouteMiddleware = (middleware: RouteMiddleware) => middleware
/*! @__NO_SIDE_EFFECTS__ */
export function defineNuxtRouteMiddleware (middleware: RouteMiddleware) {
return middleware
}
export interface AddRouteMiddlewareOptions {
global?: boolean

View File

@ -381,10 +381,12 @@ const orderMap: Record<NonNullable<ObjectPluginInput['enforce']>, number> = {
post: 20
}
/*! @__NO_SIDE_EFFECTS__ */
export function definePayloadPlugin<T extends Record<string, unknown>> (plugin: Plugin<T> | ObjectPluginInput<T>) {
return defineNuxtPlugin(plugin, { order: -40 })
}
/*! @__NO_SIDE_EFFECTS__ */
export function defineNuxtPlugin<T extends Record<string, unknown>> (plugin: Plugin<T> | ObjectPluginInput<T>, meta?: PluginMeta): Plugin<T> {
if (typeof plugin === 'function') { return defineNuxtPlugin({ setup: plugin }, meta) }
@ -433,6 +435,7 @@ export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp |
}
}
/*! @__NO_SIDE_EFFECTS__ */
/**
* Returns the current Nuxt instance.
*/
@ -455,6 +458,7 @@ export function useNuxtApp (): NuxtApp {
return nuxtAppInstance
}
/*! @__NO_SIDE_EFFECTS__ */
export function useRuntimeConfig (): RuntimeConfig {
return useNuxtApp().$config
}

View File

@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.5k"')
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.6k"')
const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2284k"')