From fc8a5a056e5b71ca4117cc5e1aeee50cab198e6d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 29 Jul 2022 16:12:35 +0800 Subject: [PATCH 1/2] fix(vite): improve code spliting close #6202 --- packages/vite/src/client.ts | 8 ++++---- packages/vite/src/css.ts | 2 +- packages/vite/src/dirs.ts | 5 ----- packages/vite/src/utils/dirs.ts | 5 +++++ packages/vite/src/vite-node.ts | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) delete mode 100644 packages/vite/src/dirs.ts create mode 100644 packages/vite/src/utils/dirs.ts diff --git a/packages/vite/src/client.ts b/packages/vite/src/client.ts index beb13de0c30..783ebf70b10 100644 --- a/packages/vite/src/client.ts +++ b/packages/vite/src/client.ts @@ -9,12 +9,10 @@ import { joinURL, withLeadingSlash, withoutLeadingSlash, withTrailingSlash } fro import escapeRE from 'escape-string-regexp' import defu from 'defu' import { cacheDirPlugin } from './plugins/cache-dir' -import { analyzePlugin } from './plugins/analyze' import { wpfs } from './utils/wpfs' import type { ViteBuildContext, ViteOptions } from './vite' import { writeManifest } from './manifest' import { devStyleSSRPlugin } from './plugins/dev-ssr-css' -import { viteNodePlugin } from './vite-node' export async function buildClient (ctx: ViteBuildContext) { const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, { @@ -58,7 +56,9 @@ export async function buildClient (ctx: ViteBuildContext) { rootDir: ctx.nuxt.options.rootDir, buildAssetsURL: joinURL(ctx.nuxt.options.app.baseURL, ctx.nuxt.options.app.buildAssetsDir) }), - viteNodePlugin(ctx) + ctx.nuxt.options.experimental.viteNode + ? await import('./vite-node').then(r => r.viteNodePlugin(ctx)) + : undefined ], appType: 'custom', server: { @@ -87,7 +87,7 @@ export async function buildClient (ctx: ViteBuildContext) { // Add analyze plugin if needed if (ctx.nuxt.options.build.analyze) { - clientConfig.plugins.push(...analyzePlugin(ctx)) + clientConfig.plugins.push(...await import('./plugins/analyze').then(r => r.analyzePlugin(ctx))) } await ctx.nuxt.callHook('vite:extendConfig', clientConfig, { isClient: true, isServer: false }) diff --git a/packages/vite/src/css.ts b/packages/vite/src/css.ts index 71b05746d48..0914a94b8cf 100644 --- a/packages/vite/src/css.ts +++ b/packages/vite/src/css.ts @@ -1,7 +1,7 @@ import { requireModule } from '@nuxt/kit' import type { Nuxt } from '@nuxt/schema' import type { ViteOptions } from './vite' -import { distDir } from './dirs' +import { distDir } from './utils/dirs' export function resolveCSSOptions (nuxt: Nuxt): ViteOptions['css'] { const css: ViteOptions['css'] & { postcss: Exclude } = { diff --git a/packages/vite/src/dirs.ts b/packages/vite/src/dirs.ts deleted file mode 100644 index 6eacc10603d..00000000000 --- a/packages/vite/src/dirs.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { fileURLToPath } from 'node:url' -import { dirname, resolve } from 'pathe' - -export const distDir = dirname(fileURLToPath(import.meta.url)) -export const pkgDir = resolve(distDir, '..') diff --git a/packages/vite/src/utils/dirs.ts b/packages/vite/src/utils/dirs.ts new file mode 100644 index 00000000000..b31a9bebfc1 --- /dev/null +++ b/packages/vite/src/utils/dirs.ts @@ -0,0 +1,5 @@ +import { fileURLToPath } from 'node:url' +import { resolve } from 'pathe' + +export const distDir = resolve(fileURLToPath(import.meta.url), '../..') +export const pkgDir = resolve(distDir, '..') diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index 7efb545f615..66e745af7d1 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -6,7 +6,7 @@ import { resolve } from 'pathe' import { addServerMiddleware } from '@nuxt/kit' import type { Plugin as VitePlugin, ViteDevServer } from 'vite' import { resolve as resolveModule } from 'mlly' -import { distDir } from './dirs' +import { distDir } from './utils/dirs' import type { ViteBuildContext } from './vite' import { isCSS } from './utils' import { createIsExternal } from './utils/external' From daca6b6fda13da75dcae5fc861eb81ec75480039 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 29 Jul 2022 20:25:10 +0800 Subject: [PATCH 2/2] chore: fix --- packages/vite/src/css.ts | 2 +- packages/vite/src/dirs.ts | 7 +++++++ packages/vite/src/utils/dirs.ts | 5 ----- packages/vite/src/vite-node.ts | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) create mode 100644 packages/vite/src/dirs.ts delete mode 100644 packages/vite/src/utils/dirs.ts diff --git a/packages/vite/src/css.ts b/packages/vite/src/css.ts index 0914a94b8cf..71b05746d48 100644 --- a/packages/vite/src/css.ts +++ b/packages/vite/src/css.ts @@ -1,7 +1,7 @@ import { requireModule } from '@nuxt/kit' import type { Nuxt } from '@nuxt/schema' import type { ViteOptions } from './vite' -import { distDir } from './utils/dirs' +import { distDir } from './dirs' export function resolveCSSOptions (nuxt: Nuxt): ViteOptions['css'] { const css: ViteOptions['css'] & { postcss: Exclude } = { diff --git a/packages/vite/src/dirs.ts b/packages/vite/src/dirs.ts new file mode 100644 index 00000000000..aac9cab1ccd --- /dev/null +++ b/packages/vite/src/dirs.ts @@ -0,0 +1,7 @@ +import { fileURLToPath } from 'node:url' +import { dirname, resolve } from 'pathe' + +let _distDir = dirname(fileURLToPath(import.meta.url)) +if (_distDir.endsWith('chunks')) { _distDir = dirname(_distDir) } +export const distDir = _distDir +export const pkgDir = resolve(distDir, '..') diff --git a/packages/vite/src/utils/dirs.ts b/packages/vite/src/utils/dirs.ts deleted file mode 100644 index b31a9bebfc1..00000000000 --- a/packages/vite/src/utils/dirs.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { fileURLToPath } from 'node:url' -import { resolve } from 'pathe' - -export const distDir = resolve(fileURLToPath(import.meta.url), '../..') -export const pkgDir = resolve(distDir, '..') diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index 66e745af7d1..7efb545f615 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -6,7 +6,7 @@ import { resolve } from 'pathe' import { addServerMiddleware } from '@nuxt/kit' import type { Plugin as VitePlugin, ViteDevServer } from 'vite' import { resolve as resolveModule } from 'mlly' -import { distDir } from './utils/dirs' +import { distDir } from './dirs' import type { ViteBuildContext } from './vite' import { isCSS } from './utils' import { createIsExternal } from './utils/external'