diff --git a/packages/vuepress/bin/vuepress-vite.js b/packages/vuepress/bin/vuepress-vite.js index e53ca2947c..5cd6866dc0 100755 --- a/packages/vuepress/bin/vuepress-vite.js +++ b/packages/vuepress/bin/vuepress-vite.js @@ -1,6 +1,19 @@ #!/usr/bin/env node -import { viteBundler } from '@vuepress/bundler-vite' -import { cli } from '@vuepress/cli' +import process from 'node:process' + +// Set `NODE_ENV` before loading the bundler, because some of its dependencies +// (e.g. `vue`) select their development or production builds according to +// `NODE_ENV` when they are loaded, which is earlier than the `dev` / `build` +// command sets it. Keep the defaults in sync with the commands. +const command = process.argv[2] +if (command === 'dev') { + process.env.NODE_ENV ??= 'development' +} else if (command === 'build') { + process.env.NODE_ENV ??= 'production' +} + +const { viteBundler } = await import('@vuepress/bundler-vite') +const { cli } = await import('@vuepress/cli') cli({ bundler: viteBundler() }) diff --git a/packages/vuepress/bin/vuepress-webpack.js b/packages/vuepress/bin/vuepress-webpack.js index 0985d7decf..5f26818831 100755 --- a/packages/vuepress/bin/vuepress-webpack.js +++ b/packages/vuepress/bin/vuepress-webpack.js @@ -1,6 +1,19 @@ #!/usr/bin/env node -import { webpackBundler } from '@vuepress/bundler-webpack' -import { cli } from '@vuepress/cli' +import process from 'node:process' + +// Set `NODE_ENV` before loading the bundler, because some of its dependencies +// (e.g. `vue`) select their development or production builds according to +// `NODE_ENV` when they are loaded, which is earlier than the `dev` / `build` +// command sets it. Keep the defaults in sync with the commands. +const command = process.argv[2] +if (command === 'dev') { + process.env.NODE_ENV ??= 'development' +} else if (command === 'build') { + process.env.NODE_ENV ??= 'production' +} + +const { webpackBundler } = await import('@vuepress/bundler-webpack') +const { cli } = await import('@vuepress/cli') cli({ bundler: webpackBundler() })