Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): optimise layer dependencies with vite #25752

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'node:fs'
import * as vite from 'vite'
import { dirname, join, resolve } from 'pathe'
import { dirname, join, normalize, resolve } from 'pathe'
import type { Nuxt, NuxtBuilder, ViteConfig } from '@nuxt/schema'
import { addVitePlugin, isIgnored, logger, resolvePath } from '@nuxt/kit'
import replace from '@rollup/plugin-replace'
Expand Down Expand Up @@ -138,6 +138,29 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
ctx.config.build!.watch = undefined
}

// Identify which layers will need to have an extra resolve step.
const layerDirs: string[] = []
const delimitedRootDir = nuxt.options.rootDir + '/'
for (const layer of nuxt.options._layers) {
if (layer.config.srcDir !== nuxt.options.srcDir && !layer.config.srcDir.startsWith(delimitedRootDir)) {
layerDirs.push(layer.config.srcDir + '/')
}
}
if (layerDirs.length > 0) {
ctx.config.plugins!.push({
name: 'nuxt:optimize-layer-deps',
enforce: 'pre',
async resolveId (source, _importer) {
if (!_importer) { return }
const importer = normalize(_importer)
if (layerDirs.some(dir => importer.startsWith(dir))) {
// Trigger vite to optimize dependencies imported within a layer, just as if they were imported in final project
await this.resolve(source, join(nuxt.options.srcDir, 'index.html'), { skipSelf: true }).catch(() => null)
}
},
})
}

// Add type-checking
if (!ctx.nuxt.options.test && (ctx.nuxt.options.typescript.typeCheck === true || (ctx.nuxt.options.typescript.typeCheck === 'build' && !ctx.nuxt.options.dev))) {
const checker = await import('vite-plugin-checker').then(r => r.default)
Expand Down