Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(vite): fix distDir resolution #6215

Merged
merged 4 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is going to be the default option, I think we can safely import it normally when removed experimental flag. Difference is small but rollup this way can optimize startup time to add nested dependencies to top level dist/index.mjs

: undefined
],
appType: 'custom',
server: {
Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/css.ts
Original file line number Diff line number Diff line change
@@ -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<ViteOptions['css']['postcss'], string> } = {
Expand Down
5 changes: 0 additions & 5 deletions packages/vite/src/dirs.ts

This file was deleted.

5 changes: 5 additions & 0 deletions packages/vite/src/utils/dirs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { fileURLToPath } from 'node:url'
import { resolve } from 'pathe'

export const distDir = resolve(fileURLToPath(import.meta.url), '../..')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was intentionally in top-level to match source and generated chunk path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but unbuild always extracts it into a chunk even when I have directly imported it in index.ts. Or maybe we need another top-level export dirs to make sure it's always on top-level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a non-problem because we used to have only one file for this package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The only issue is that is approach can be easily broken if chunk generation changes with unbuild>rollup.

Currently, we use this workaround for the main nuxt pkg:

if (_distDir.endsWith('chunks')) { _distDir = dirname(_distDir) }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, updated

export const pkgDir = resolve(distDir, '..')
2 changes: 1 addition & 1 deletion packages/vite/src/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down