From 5113d768972c113091a271722c92e2905710e959 Mon Sep 17 00:00:00 2001 From: maoger Date: Sun, 27 Sep 2026 09:18:52 +0800 Subject: [PATCH] fix(vuepress): set NODE_ENV before loading bundler in bin shorthands `vuepress-vite` and `vuepress-webpack` imported the bundler statically, so it was evaluated before the `dev` / `build` command set the default `NODE_ENV`. The bundlers load `vue` at module top level (e.g. `@vuepress/bundlerutils` imports `ssrContextKey` from `vue`, and `@vitejs/plugin-vue` / `vue-loader` load `vue` / `vue/compiler-sfc`), and the CJS entries of `vue` and `@vue/*` pick their development or production builds by `NODE_ENV` when first required. As a result, `vuepress-vite build` and `vuepress-webpack build` ran with the development builds of the Vue runtime and template compiler, while `vue/server-renderer`, imported later for SSR, used its production build, mixing both in one process. SSR was noticeably slower, and the output differed from `vuepress build` with the bundler set in the config file, e.g. the dev compiler kept HTML comments of Markdown pages. Set the default `NODE_ENV` according to the command in these bins, and import the bundler and the cli dynamically afterwards. An explicitly provided `NODE_ENV` is still respected, and other commands and options are left to the cli as before. --- packages/vuepress/bin/vuepress-vite.js | 17 +++++++++++++++-- packages/vuepress/bin/vuepress-webpack.js | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) 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() })