Skip to content

Commit

Permalink
perf(nuxt): mark define functions as side-effect free at source (#21434)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 7, 2023
1 parent 2fccfa2 commit 6d59a02
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Expand Up @@ -94,7 +94,10 @@
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
],
"jsdoc/check-tag-names": ["error", {
"definedTags": ["__NO_SIDE_EFFECTS__"]
}]
},
"settings": {
"jsdoc": {
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/app/components/client-only.ts
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/app/components/nuxt-link.ts
Expand Up @@ -46,6 +46,7 @@ export type NuxtLinkProps = {
ariaCurrentValue?: string
}

/*! @__NO_SIDE_EFFECTS__ */
export function defineNuxtLink (options: NuxtLinkOptions) {
const componentName = options.componentName || 'NuxtLink'

Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/app/composables/component.ts
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/nuxt/src/app/composables/router.ts
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxt/src/app/nuxt.ts
Expand Up @@ -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) }

Expand Down Expand Up @@ -433,6 +435,7 @@ export function callWithNuxt<T extends (...args: any[]) => any> (nuxt: NuxtApp |
}
}

/*! @__NO_SIDE_EFFECTS__ */
/**
* Returns the current Nuxt instance.
*/
Expand All @@ -455,6 +458,7 @@ export function useNuxtApp (): NuxtApp {
return nuxtAppInstance
}

/*! @__NO_SIDE_EFFECTS__ */
export function useRuntimeConfig (): RuntimeConfig {
return useNuxtApp().$config
}
Expand Down
2 changes: 1 addition & 1 deletion test/bundle.test.ts
Expand Up @@ -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"')
Expand Down

0 comments on commit 6d59a02

Please sign in to comment.