8000 perf(nuxt,vite): use filters to avoid running hooks unnecessarily · nuxt/nuxt@e762a1e · GitHub
[go: up one dir, main page]

Skip to content

Commit e762a1e

Browse files
committed
perf(nuxt,vite): use filters to avoid running hooks unnecessarily
1 parent 5bd8e81 commit e762a1e

File tree

5 files changed

+69
-44
lines changed

5 files changed

+69
-44
lines changed

packages/nuxt/src/core/plugins/layer-aliasing.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface LayerAliasingOptions {
1313

1414
const ALIAS_RE = /(?<=['"])[~@]{1,2}(?=\/)/g
1515
const ALIAS_RE_SINGLE = /(?<=['"])[~@]{1,2}(?=\/)/
16+
const ALIAS_ID_RE = /^[~@]{1,2}\//
1617

1718
export const LayerAliasingPlugin = (options: LayerAliasingOptions) => createUnplugin((_options, meta) => {
1819
const aliases: Record<string, Record<string, string>> = {}
@@ -60,6 +61,9 @@ export const LayerAliasingPlugin = (options: LayerAliasingOptions) => createUnpl
6061
vite: {
6162
resolveId: {
6263
order: 'pre',
64+
filter: {
65+
id: ALIAS_ID_RE,
66+
},
6367
async handler (id, importer) {
6468
if (!importer) { return }
6569

packages/nuxt/src/core/plugins/resolve-deep-imports.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { resolveModulePath } from 'exsolve'
33
import { isAbsolute, normalize, resolve } from 'pathe'
44
import type { Environment, Plugin } from 'vite'
55
import { directoryToURL, resolveAlias } from '@nuxt/kit'
6+
import escapeRE from 'escape-string-regexp'
67
import type { Nuxt } from '@nuxt/schema'
78

89
import { pkgDir } from '../../dirs'
@@ -33,47 +34,58 @@ export function ResolveDeepImportsPlugin (nuxt: Nuxt): Plugin {
3334
return {
3435
name: 'nuxt:resolve-bare-imports',
3536
enforce: 'post',
36-
async resolveId (id, importer) {
37-
if (!importer || isAbsolute(id) || (!isAbsolute(importer) && !VIRTUAL_RE.test(importer)) || exclude.some(e => id.startsWith(e))) {
38-
return
39-
}
37+
resolveId: {
38+
filter: {
39+
id: {
40+
exclude: [
41+
// absolute path
42+
/^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Z]:[/\\]/i,
43+
...exclude.map(e => new RegExp('^' + escapeRE(e))),
44+
],
45+
},
46+
},
47+
async handler (id, importer) {
48+
if (!importer || (!isAbsolute(importer) && !VIRTUAL_RE.test(importer))) {
49+
return
50+
}
4051

41-
const normalisedId = resolveAlias(normalize(id), nuxt.options.alias)
42-
const isNuxtTemplate = importer.startsWith('virtual:nuxt')
43-
const normalisedImporter = (isNuxtTemplate ? decodeURIComponent(importer) : importer).replace(VIRTUAL_RE, '')
52+
const normalisedId = resolveAlias(normalize(id), nuxt.options.alias)
53+
const isNuxtTemplate = importer.startsWith('virtual:nuxt')
54+
const normalisedImporter = (isNuxtTemplate ? decodeURIComponent(importer) : importer).replace(VIRTUAL_RE, '')
4455

45-
if (nuxt.options.experimental.templateImportResolution !== false && isNuxtTemplate) {
46-
const template = nuxt.options.build.templates.find(t => resolve(nuxt.options.buildDir, t.filename!) === normalisedImporter)
47-
if (template?._path) {
48-
const res = await this.resolve?.(normalisedId, template._path, { skipSelf: true })
49-
if (res !== undefined && res !== null) {
50-
return res
56+
if (nuxt.options.experimental.templateImportResolution !== false && isNuxtTemplate) {
57+
const template = nuxt.options.build.templates.find(t => resolve(nuxt.options.buildDir, t.filename!) === normalisedImporter)
58+
if (template?._path) {
59+
const res = await this.resolve?.(normalisedId, template._path, { skipSelf: true })
60+
if (res !== undefined && res !== null) {
61+
return res
62+
}
5163
}
5264
}
53-
}
5465

55-
const dir = parseNodeModulePath(normalisedImporter).dir || pkgDir
66+
const dir = parseNodeModulePath(normalisedImporter).dir || pkgDir
5667

57-
const res = await this.resolve?.(normalisedId, dir, { skipSelf: true })
58-
if (res !== undefined && res !== null) {
59-
return res
60-
}
68+
const res = await this.resolve?.(normalisedId, dir, { skipSelf: true })
69+
if (res !== undefined && res !== null) {
70+
return res
71+
}
6172

62-
const environmentConditions = conditions[this.environment.name] ||= resolveConditions(this.environment)
73+
const environmentConditions = conditions[this.environment.name] ||= resolveConditions(this.environment)
6374

64-
const path = resolveModulePath(id, {
65-
from: [dir, ...nuxt.options.modulesDir].map(d => directoryToURL(d)),
66-
suffixes: ['', 'index'],
67-
conditions: environmentConditions,
68-
try: true,
69-
})
75+
const path = resolveModulePath(id, {
76+
from: [dir, ...nuxt.options.modulesDir].map(d => directoryToURL(d)),
77+
suffixes: ['', 'index'],
78+
conditions: environmentConditions,
79+
try: true,
80+
})
7081

71-
if (!path) {
72-
logger.debug('Could not resolve id', id, importer)
73-
return null
74-
}
82+
if (!path) {
83+
logger.debug('Could not resolve id', id, importer)
84+
return null
85+
}
7586

76-
return normalize(path)
87+
return normalize(path)
88+
},
7789
},
7890
}
7991
}

packages/vite/src/plugins/environments.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { dirname, isAbsolute, join, relative, resolve } from 'pathe'
66
import { useNitro } from '@nuxt/kit'
77
import { resolveModulePath } from 'exsolve'
88
import { defineEnv } from 'unenv'
9+
import escapeStringRegexp from 'escape-string-regexp'
910

1011
export function EnvironmentsPlugin (nuxt: Nuxt): Plugin {
1112
const fileNames = withoutLeadingSlash(join(nuxt.options.app.buildAssetsDir, '[hash].js'))
@@ -80,7 +81,12 @@ export function EnvironmentsPlugin (nuxt: Nuxt): Plugin {
8081
{
8182
name: 'nuxt:client:aliases',
8283
enforce: 'post',
83-
resolveId: source => clientAliases[source],
84+
resolveId: {
85+
filter: {
86+
id: Object.keys(clientAliases).map(id => new RegExp('^' + escapeStringRegexp(id) + '$')),
87+
},
88+
handler: source => clientAliases[source],
89+
},
8490
},
8591
]
8692
} else if (environment.name === 'ssr') {

packages/vite/src/plugins/public-dirs.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isCSSRequest } from 'vite'
77
import type { Plugin } from 'vite'
88

99
const PREFIX = 'virtual:public?'
10+
const PREFIX_RE = /^virtual:public\?/
1011
const CSS_URL_RE = /url\((\/[^)]+)\)/g
1112
const CSS_URL_SINGLE_RE = /url\(\/[^)]+\)/
1213
const RENDER_CHUNK_RE = /(?<= = )['"`]/
@@ -51,17 +52,21 @@ export const PublicDirsPlugin = (options: VitePublicDirsPluginOptions): Plugin[]
5152
},
5253
load: {
5354
order: 'pre',
55+
filter: {
56+
id: PREFIX_RE,
57+
},
5458
handler (id) {
55-
if (id.startsWith(PREFIX)) {
56-
return `import { publicAssetsURL } from '#internal/nuxt/paths';export default publicAssetsURL(${JSON.stringify(decodeURIComponent(id.slice(PREFIX.length)))})`
57-
}
59+
return `import { publicAssetsURL } from '#internal/nuxt/paths';export default publicAssetsURL(${JSON.stringify(decodeURIComponent(id.slice(PREFIX.length)))})`
5860
},
5961
},
6062
resolveId: {
6163
order: 'post',
64+
filter: {
65+
id: {
66+
exclude: [/^\/__skip_vite$/, /^[^/]/, /^\/@fs/],
67+
},
68+
},
6269
handler (id) {
63-
if (id === '/__skip_vite' || id[0] !== '/' || id.startsWith('/@fs')) { return }
64-
6570
if (resolveFromPublicAssets(id)) {
6671
return PREFIX + encodeURIComponent(id)
6772
}

packages/vite/src/plugins/ssr-styles.ts

+5Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ export function SSRStylesPlugin (nuxt: Nuxt): Plugin | undefined {
9898
return
9999
}
100100

101-
if (id === '#build/css' || id.endsWith('.vue') || isCSS(id)) {
102-
const res = await this.resolve(id, importer, { ..._options, skipSelf: true })
103-
if (res) {
104-
return {
105-
...res,
106-
moduleSideEffects: false,
107-
}
101+
const res = await this.resolve(id, importer, { ..._options, skipSelf: true })
102+
if (res) {
103+
return {
104+
...res,
105+
moduleSideEffects: false,
108106
}
109107
}
110108
},

0 commit comments

Comments
 (0)
0