Skip to content

Commit

Permalink
fix(vite): optimise layer dependencies with vite (#25752)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 13, 2024
1 parent 795050e commit f0a7ccd
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 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,31 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
ctx.config.build!.watch = undefined
}

if (nuxt.options.dev) {
// 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

0 comments on commit f0a7ccd

Please sign in to comment.