Skip to content

Commit

Permalink
feat: optimize deps option to turn off auto discovery (#13000)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 26, 2023
1 parent 5731ac9 commit bd86375
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 8 deletions.
8 changes: 8 additions & 0 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -125,6 +125,14 @@ export interface DepOptimizationConfig {
* @experimental
*/
disabled?: boolean | 'build' | 'dev'
/**
* Automatic dependency discovery. When `noDiscovery` is true, only dependencies
* listed in `include` will be optimized. The scanner isn't run for cold start
* in this case. CJS-only dependencies must be present in `include` during dev.
* @default false
* @experimental
*/
noDiscovery?: boolean
}

export type DepOptimizationOptions = DepOptimizationConfig & {
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/optimizer/optimizer.ts
Expand Up @@ -217,7 +217,11 @@ async function createDepsOptimizer(
newDepsDiscovered = true
}

if (!isBuild) {
if (config.optimizeDeps.noDiscovery) {
// We don't need to scan for dependencies or wait for the static crawl to end
// Run the first optimization run immediately
runOptimizer()
} else if (!isBuild) {
// Important, the scanner is dev only
depsOptimizer.scanProcessing = new Promise((resolve) => {
// Runs in the background in case blocking high priority tasks
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/preAlias.ts
Expand Up @@ -49,7 +49,9 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
if (optimizedId) {
return optimizedId // aliased dep already optimized
}

if (depsOptimizer.options.noDiscovery) {
return
}
const resolved = await this.resolve(id, importer, {
...options,
custom: { ...options.custom, 'vite:pre-alias': true },
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -816,6 +816,7 @@ export function tryNodeResolve(
}

const skipOptimization =
depsOptimizer?.options.noDiscovery ||
!isJsType ||
(importer && isInNodeModules(importer)) ||
exclude?.includes(pkgId) ||
Expand Down
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest'
import { isBuild, page, readFile } from '~utils'

test('optimized dep', async () => {
expect(await page.textContent('.optimized-dep')).toBe('[success]')
})

test('vue + vuex', async () => {
expect(await page.textContent('.vue')).toMatch(`[success]`)
})

test.runIf(!isBuild)('metadata', async () => {
const meta = await readFile('node_modules/.vite/deps/_metadata.json')
expect(meta).toMatch(`"@vitejs/test-dep-no-discovery"`)
expect(meta).not.toMatch(`"vue"`)
expect(meta).not.toMatch(`"vuex"`)
})
@@ -0,0 +1,2 @@
// written in cjs, optimization should convert this to esm
module.exports = '[success]'
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-dep-no-discovery",
"private": true,
"version": "1.0.0",
"main": "index.js"
}
24 changes: 24 additions & 0 deletions playground/optimize-deps-no-discovery/index.html
@@ -0,0 +1,24 @@
<h1>Optimize Deps</h1>

<h2>Optimized Dep</h2>
<div class="optimized-dep"></div>

<h2>Vue & Vuex</h2>
<div class="vue"></div>

<script>
function text(el, text) {
document.querySelector(el).textContent = text
}
</script>

<script type="module">
import msg from '@vitejs/test-dep-no-discovery'
text('.optimized-dep', msg)

import { createApp } from 'vue'
import { createStore } from 'vuex'
if (typeof createApp === 'function' && typeof createStore === 'function') {
text('.vue', '[success]')
}
</script>
16 changes: 16 additions & 0 deletions playground/optimize-deps-no-discovery/package.json
@@ -0,0 +1,16 @@
{
"name": "@vitejs/test-optimize-deps-no-discovery",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"@vitejs/test-dep-no-discovery": "file:./dep-no-discovery",
"vue": "^3.2.47",
"vuex": "^4.1.0"
}
}
21 changes: 21 additions & 0 deletions playground/optimize-deps-no-discovery/vite.config.js
@@ -0,0 +1,21 @@
import { defineConfig } from 'vite'

// Overriding the NODE_ENV set by vitest
process.env.NODE_ENV = ''

export default defineConfig({
optimizeDeps: {
disabled: false,
noDiscovery: true,
include: ['@vitejs/test-dep-no-discovery'],
},

build: {
// to make tests faster
minify: false,
// Avoid @rollup/plugin-commonjs
commonjsOptions: {
include: [],
},
},
})
27 changes: 21 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd86375

Please sign in to comment.