vike 0.4.163 → 0.4.164

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/cjs/__internal/index.js +1 -1
  2. package/dist/cjs/node/plugin/index.js +1 -19
  3. package/dist/cjs/node/plugin/plugins/autoFullBuild.js +7 -4
  4. package/dist/cjs/node/plugin/plugins/buildConfig/fixServerAssets.js +8 -4
  5. package/dist/cjs/node/plugin/plugins/buildConfig.js +7 -8
  6. package/dist/cjs/node/plugin/plugins/distFileNames.js +4 -9
  7. package/dist/cjs/node/plugin/shared/getAssetsDir.js +11 -0
  8. package/dist/cjs/node/prerender/runPrerender.js +1 -8
  9. package/dist/cjs/node/runtime/globalContext.js +1 -13
  10. package/dist/cjs/node/runtime/renderPage/logErrorHint.js +2 -13
  11. package/dist/cjs/utils/assertNodeEnv.js +65 -10
  12. package/dist/cjs/utils/projectInfo.js +1 -1
  13. package/dist/esm/__internal/index.js +2 -2
  14. package/dist/esm/node/plugin/index.js +2 -20
  15. package/dist/esm/node/plugin/plugins/autoFullBuild.js +7 -4
  16. package/dist/esm/node/plugin/plugins/buildConfig/fixServerAssets.d.ts +2 -2
  17. package/dist/esm/node/plugin/plugins/buildConfig/fixServerAssets.js +9 -5
  18. package/dist/esm/node/plugin/plugins/buildConfig.js +8 -9
  19. package/dist/esm/node/plugin/plugins/distFileNames.js +1 -6
  20. package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +3 -3
  21. package/dist/esm/node/plugin/shared/getAssetsDir.d.ts +3 -0
  22. package/dist/esm/node/plugin/shared/getAssetsDir.js +8 -0
  23. package/dist/esm/node/prerender/runPrerender.js +2 -9
  24. package/dist/esm/node/runtime/globalContext.js +2 -14
  25. package/dist/esm/node/runtime/renderPage/logErrorHint.js +3 -11
  26. package/dist/esm/utils/assertNodeEnv.d.ts +10 -10
  27. package/dist/esm/utils/assertNodeEnv.js +67 -10
  28. package/dist/esm/utils/projectInfo.d.ts +2 -2
  29. package/dist/esm/utils/projectInfo.js +1 -1
  30. package/package.json +2 -1
@@ -13,7 +13,7 @@ const renderPageAlreadyRouted_js_1 = require("../node/runtime/renderPage/renderP
13
13
  * @param config
14
14
  */
15
15
  async function getPagesAndRoutes() {
16
- (0, assertNodeEnv_js_1.setNodeEnvToProduction)();
16
+ (0, assertNodeEnv_js_1.handleNodeEnv_vitePluginVercel)();
17
17
  await (0, globalContext_js_1.initGlobalContext)(true);
18
18
  const globalContext = (0, globalContext_js_1.getGlobalContext)();
19
19
  (0, assert_js_1.assert)(globalContext.isProduction === true);
@@ -27,7 +27,7 @@ const baseUrls_js_1 = require("./plugins/baseUrls.js");
27
27
  const envVars_js_1 = require("./plugins/envVars.js");
28
28
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
29
29
  const fileEnv_js_1 = require("./plugins/fileEnv.js");
30
- assertNodeEnv();
30
+ (0, utils_js_2.assertNodeEnv_onVikePluginLoad)();
31
31
  (0, utils_js_2.markEnvAsVikePluginLoaded)();
32
32
  // Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed
33
33
  function plugin(vikeConfig) {
@@ -69,21 +69,3 @@ Object.defineProperty(plugin, 'apply', {
69
69
  (0, utils_js_2.assertUsage)(false, `Add ${picocolors_1.default.cyan('vike()')} instead of ${picocolors_1.default.cyan('vike')} to vite.config.js#plugins (i.e. call the function and add the return value instead of adding the function itself)`, { showStackTrace: true });
70
70
  }
71
71
  });
72
- function assertNodeEnv() {
73
- const nodeEnv = (0, utils_js_2.getNodeEnv)();
74
- if (nodeEnv === 'test')
75
- return;
76
- // We should change this to be a warning if it blocks users (e.g. if a bad-citizen tool sets a wrong process.env.NODE_ENV value).
77
- (0, utils_js_2.assertUsage)(
78
- /* We can enable this assertion after Vike's CLI is implemented and using Vite's CLI is deprecated (we can then check whether the context is a `$ vike build`).
79
- isNodeEnvDev() || isVikeCliBuild(),
80
- /*/
81
- (0, utils_js_2.isNodeEnvDev)() || true,
82
- ///*/
83
- [
84
- picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
85
- '(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
86
- 'while the vike/plugin module is loaded.',
87
- utils_js_2.vikeVitePluginLoadedInProductionError
88
- ].join(' '));
89
- }
@@ -11,6 +11,7 @@ const getConfigVike_js_1 = require("../../shared/getConfigVike.js");
11
11
  const isViteCliCall_js_1 = require("../shared/isViteCliCall.js");
12
12
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
13
13
  const logErrorHint_js_1 = require("../../runtime/renderPage/logErrorHint.js");
14
+ const buildConfig_js_1 = require("./buildConfig.js");
14
15
  let forceExit = false;
15
16
  function autoFullBuild() {
16
17
  let config;
@@ -64,10 +65,12 @@ async function triggerFullBuild(config, configVike, bundle) {
64
65
  return; // already triggered
65
66
  if (isDisabled(configVike))
66
67
  return;
67
- /* Is this @vitejs/plugin-legacy workaround still needed? Should we re-implement it?
68
- // vike.json missing => it isn't a `$ vite build` call (e.g. @vitejs/plugin-legacy calls Vite's build() API) => skip
69
- if (!bundle['vike.json']) return
70
- */
68
+ // Workaround for @vitejs/plugin-legacy
69
+ // - The legacy plugin triggers its own Rollup build for the client-side.
70
+ // - The legacy plugin doesn't generate a manifest => we can use that to detect the legacy plugin build.
71
+ // - Issue & reproduction: https://github.com/vikejs/vike/issues/1154#issuecomment-1965954636
72
+ if (!bundle[buildConfig_js_1.manifestTempFile])
73
+ return;
71
74
  const configFromCli = !(0, isViteCliCall_js_1.isViteCliCall)() ? null : (0, isViteCliCall_js_1.getViteConfigFromCli)();
72
75
  const configInline = {
73
76
  ...configFromCli,
@@ -10,6 +10,7 @@ const fs_1 = require("fs");
10
10
  const utils_js_1 = require("../../utils.js");
11
11
  const virtualFilePageConfigValuesAll_js_1 = require("../../../shared/virtual-files/virtualFilePageConfigValuesAll.js");
12
12
  const buildConfig_js_1 = require("../buildConfig.js");
13
+ const getAssetsDir_js_1 = require("../../shared/getAssetsDir.js");
13
14
  /**
14
15
  * true => use workaround config.build.ssrEmitAssets
15
16
  * false => use workaround extractAssets plugin
@@ -23,11 +24,12 @@ function fixServerAssets_isEnabled() {
23
24
  }
24
25
  exports.fixServerAssets_isEnabled = fixServerAssets_isEnabled;
25
26
  /** https://github.com/vikejs/vike/issues/1339 */
26
- async function fixServerAssets(outDirs) {
27
+ async function fixServerAssets(config) {
28
+ const outDirs = (0, utils_js_1.getOutDirs)(config);
27
29
  const clientManifest = await loadManifest(outDirs.outDirClient);
28
30
  const serverManifest = await loadManifest(outDirs.outDirServer);
29
31
  const { clientManifestMod, filesToCopy } = addServerAssets(clientManifest, serverManifest);
30
- await copyAssets(filesToCopy, outDirs);
32
+ await copyAssets(filesToCopy, config);
31
33
  return clientManifestMod;
32
34
  }
33
35
  exports.fixServerAssets = fixServerAssets;
@@ -39,8 +41,10 @@ async function loadManifest(outDir) {
39
41
  (0, utils_js_1.assert)(manifest);
40
42
  return manifest;
41
43
  }
42
- async function copyAssets(filesToCopy, { outDirClient, outDirServer }) {
43
- const assetsDirServer = path_1.default.posix.join(outDirServer, 'assets');
44
+ async function copyAssets(filesToCopy, config) {
45
+ const { outDirClient, outDirServer } = (0, utils_js_1.getOutDirs)(config);
46
+ const assetsDir = (0, getAssetsDir_js_1.getAssetsDir)(config);
47
+ const assetsDirServer = path_1.default.posix.join(outDirServer, assetsDir);
44
48
  if (!filesToCopy.length)
45
49
  return;
46
50
  (0, utils_js_1.assert)((0, fs_1.existsSync)(assetsDirServer));
@@ -27,14 +27,16 @@ function buildConfig() {
27
27
  let isServerAssetsFixEnabled;
28
28
  let isSsrBuild;
29
29
  let outDirs;
30
+ let config;
30
31
  return {
31
32
  name: 'vike:buildConfig',
32
33
  apply: 'build',
33
34
  enforce: 'post',
34
35
  configResolved: {
35
36
  order: 'post',
36
- async handler(config) {
37
- assertNodeEnv();
37
+ async handler(config_) {
38
+ config = config_;
39
+ (0, utils_js_1.assertNodeEnv_build)();
38
40
  assertRollupInput(config);
39
41
  const entries = await getEntries(config);
40
42
  (0, utils_js_1.assert)(Object.keys(entries).length > 0);
@@ -53,7 +55,7 @@ function buildConfig() {
53
55
  }
54
56
  },
55
57
  config(config) {
56
- assertNodeEnv();
58
+ (0, utils_js_1.assertNodeEnv_build)();
57
59
  isSsrBuild = (0, utils_js_1.viteIsSSR)(config);
58
60
  return {
59
61
  build: {
@@ -64,7 +66,7 @@ function buildConfig() {
64
66
  };
65
67
  },
66
68
  buildStart() {
67
- assertNodeEnv();
69
+ (0, utils_js_1.assertNodeEnv_build)();
68
70
  },
69
71
  writeBundle: {
70
72
  order: 'post',
@@ -81,7 +83,7 @@ function buildConfig() {
81
83
  await promises_1.default.copyFile(clientManifestFilePath, assetsJsonFilePath);
82
84
  }
83
85
  else {
84
- const clientManifestMod = await (0, fixServerAssets_js_1.fixServerAssets)(outDirs);
86
+ const clientManifestMod = await (0, fixServerAssets_js_1.fixServerAssets)(config);
85
87
  await promises_1.default.writeFile(assetsJsonFilePath, JSON.stringify(clientManifestMod, null, 2), 'utf-8');
86
88
  }
87
89
  await promises_1.default.rm(clientManifestFilePath);
@@ -258,6 +260,3 @@ function assertRollupInput(config) {
258
260
  (0, utils_js_1.assertUsage)(htmlInput === undefined, `The entry ${htmlInput} of config build.rollupOptions.input is an HTML entry which is forbidden when using Vike, instead follow https://vike.dev/add`);
259
261
  }
260
262
  exports.assertRollupInput = assertRollupInput;
261
- function assertNodeEnv() {
262
- (0, utils_js_1.assertNodeEnvIsNotDev)('building');
263
- }
@@ -9,6 +9,7 @@ exports.distFileNames = void 0;
9
9
  // - Blocker: https://github.com/rollup/rollup/issues/4724
10
10
  const utils_js_1 = require("../utils.js");
11
11
  const path_1 = __importDefault(require("path"));
12
+ const getAssetsDir_js_1 = require("../shared/getAssetsDir.js");
12
13
  function distFileNames() {
13
14
  return {
14
15
  name: 'vike:distFileNames',
@@ -41,7 +42,7 @@ function distFileNames() {
41
42
  }
42
43
  exports.distFileNames = distFileNames;
43
44
  function getAssetFileName(assetInfo, config) {
44
- const assetsDir = getAssetsDir(config);
45
+ const assetsDir = (0, getAssetsDir_js_1.getAssetsDir)(config);
45
46
  const dir = assetsDir + '/static';
46
47
  let { name } = assetInfo;
47
48
  if (!name) {
@@ -69,13 +70,13 @@ function getChunkFileName(_chunkInfo, config) {
69
70
  const isForClientSide = !config.build.ssr;
70
71
  let name = 'chunks/chunk-[hash].js';
71
72
  if (isForClientSide) {
72
- const assetsDir = getAssetsDir(config);
73
+ const assetsDir = (0, getAssetsDir_js_1.getAssetsDir)(config);
73
74
  name = `${assetsDir}/${name}`;
74
75
  }
75
76
  return name;
76
77
  }
77
78
  function getEntryFileName(chunkInfo, config, isEntry) {
78
- const assetsDir = getAssetsDir(config);
79
+ const assetsDir = (0, getAssetsDir_js_1.getAssetsDir)(config);
79
80
  const isForClientSide = !config.build.ssr;
80
81
  let { name } = chunkInfo;
81
82
  (0, utils_js_1.assertPosixPath)(name);
@@ -169,9 +170,3 @@ function getRollupOutputs(config) {
169
170
  }
170
171
  return output;
171
172
  }
172
- function getAssetsDir(config) {
173
- let { assetsDir } = config.build;
174
- (0, utils_js_1.assertUsage)(assetsDir, `${assetsDir} cannot be an empty string`);
175
- assetsDir = assetsDir.split(/\/|\\/).filter(Boolean).join('/');
176
- return assetsDir;
177
- }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAssetsDir = void 0;
4
+ const utils_js_1 = require("../utils.js");
5
+ function getAssetsDir(config) {
6
+ let { assetsDir } = config.build;
7
+ (0, utils_js_1.assertUsage)(assetsDir, `${assetsDir} cannot be an empty string`);
8
+ assetsDir = assetsDir.split(/\/|\\/).filter(Boolean).join('/');
9
+ return assetsDir;
10
+ }
11
+ exports.getAssetsDir = getAssetsDir;
@@ -87,7 +87,7 @@ async function runPrerender(options, manuallyTriggered) {
87
87
  if (logLevel === 'info') {
88
88
  console.log(`${picocolors_1.default.cyan(`vike v${utils_js_1.projectInfo.projectVersion}`)} ${picocolors_1.default.green('pre-rendering HTML...')}`);
89
89
  }
90
- handleNodeEnv();
90
+ (0, utils_js_1.handleNodeEnv_prerender)();
91
91
  await disableReactStreaming();
92
92
  const viteConfig = await (0, vite_1.resolveConfig)(options.viteConfig || {}, 'vike pre-rendering', 'production');
93
93
  (0, globalContext_js_1.setGlobalContext_prerender)(viteConfig);
@@ -743,10 +743,3 @@ function assertIsNotAbort(err, urlOr404) {
743
743
  (0, utils_js_1.assert)(abortCall);
744
744
  (0, utils_js_1.assertUsage)(false, `${picocolors_1.default.cyan(abortCall)} thrown${thrownBy} while pre-rendering ${urlOr404} but ${picocolors_1.default.cyan(abortCaller)} isn't supported for pre-rendered pages`);
745
745
  }
746
- function handleNodeEnv() {
747
- const assertNodeEnv = () => (0, utils_js_1.assertNodeEnvIsNotDev)('pre-rendering');
748
- if ((0, utils_js_1.getNodeEnv)())
749
- assertNodeEnv();
750
- (0, utils_js_1.setNodeEnvToProduction)();
751
- assertNodeEnv();
752
- }
@@ -85,7 +85,7 @@ async function initGlobalContext(isPrerendering = false, outDir) {
85
85
  if (globalObject.globalContext)
86
86
  return;
87
87
  const { viteDevServer, viteConfig } = globalObject;
88
- assertNodeEnv(!!viteDevServer);
88
+ (0, utils_js_1.assertNodeEnv_runtime)(!!viteDevServer);
89
89
  const isProduction = !viteDevServer;
90
90
  if (!isProduction) {
91
91
  (0, utils_js_1.assert)(viteConfig);
@@ -183,15 +183,3 @@ function assertViteManifest(manifest) {
183
183
  })
184
184
  */
185
185
  }
186
- function assertNodeEnv(hasViteDevServer) {
187
- const nodeEnv = (0, utils_js_1.getNodeEnv)();
188
- if (nodeEnv === null || nodeEnv === 'test')
189
- return;
190
- const isDev = (0, utils_js_1.isNodeEnvDev)();
191
- // Calling Vite's createServer() is enough for hasViteDevServer to be true, even without actually adding Vite's development middleware to the server: https://github.com/vikejs/vike/issues/792#issuecomment-1516830759
192
- if (hasViteDevServer === isDev)
193
- return;
194
- const nodeEnvDesc = (0, utils_js_1.getNodeEnvDesc)();
195
- // We should change this to be a warning if it blocks users (e.g. if a bad-citizen tool sets a wrong process.env.NODE_ENV value)
196
- (0, utils_js_1.assertUsage)(false, `Vite's development server was${hasViteDevServer ? '' : "n't"} instantiated while the ${nodeEnvDesc} which is contradictory, see https://vike.dev/NODE_ENV`);
197
- }
@@ -1,10 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.getHint = exports.isKnownError = exports.isCjsEsmError = exports.logErrorHint = void 0;
7
- const picocolors_1 = __importDefault(require("@brillout/picocolors"));
8
4
  const utils_js_1 = require("../utils.js");
9
5
  const knownErrors = [
10
6
  {
@@ -40,15 +36,7 @@ function getHint(error) {
40
36
  }
41
37
  const res = isCjsEsmError(error);
42
38
  if (res) {
43
- const packageNames = res === true ? null : res;
44
- const hint = [
45
- 'Error could be a CJS/ESM issue, consider ',
46
- !packageNames || packageNames.length === 0
47
- ? 'using'
48
- : `adding ${(0, utils_js_1.joinEnglish)(packageNames.map((p) => picocolors_1.default.cyan(p)), 'or')} to`,
49
- ` ${picocolors_1.default.cyan('ssr.noExternal')}`,
50
- ', see https://vike.dev/broken-npm-package'
51
- ].join('');
39
+ const hint = 'The error seems to be a CJS/ESM issue, see https://vike.dev/broken-npm-package';
52
40
  return hint;
53
41
  }
54
42
  return null;
@@ -81,6 +69,7 @@ function isCjsEsmError(error) {
81
69
  packageNames.forEach((packageName) => {
82
70
  (0, utils_js_1.assert)(!['vite', 'vike'].includes(packageName));
83
71
  });
72
+ // We don't use this anymore: we could return `true` instead. Shall we remove returning a list of npm packages?
84
73
  return packageNames;
85
74
  }
86
75
  exports.isCjsEsmError = isCjsEsmError;
@@ -1,29 +1,84 @@
1
1
  "use strict";
2
- // Utils to manage process.env.NODE_ENV and, most notably, to assert correct usage, which is crucial:
3
- // - https://github.com/vikejs/vike/issues/1469#issuecomment-1919518096
4
- // - https://github.com/vitejs/vite/blob/76f30ae23b92f9af910ec02d98e2baaefa12141f/packages/vite/src/node/config.ts#L567
2
+ // This file serves the following three purposes:
3
+ // 1. Upon building the app, we ensure the right NODE_ENV value is set.
4
+ // - Both Vue and React use NODE_ENV for enabling production-specific features:
5
+ // - Vue: https://github.com/vuejs/core/blob/f66a75ea75c8aece065b61e2126b4c5b2338aa6e/packages/vue/index.js
6
+ // - React: https://github.com/facebook/react/blob/01ab35a9a731dec69995fbd28f3ac7eaad11e183/packages/react/npm/index.js
7
+ // - Setting NODE_ENV to 'production' doesn't seem to make any sense in development.
8
+ // - With React upon building the app, setting NODE_ENV to a value other than 'production' triggers an error: https://github.com/vikejs/vike/issues/1469#issuecomment-1969301797
9
+ // - With Vue upon building the app, NODE_ENV can be set to a value other than 'production', e.g. 'development'.
10
+ // 2. Ensure Vite isn't loaded in production.
11
+ // - We currently only check whether Vite's development middleware is instantiated (i.e. whether Vite's `createServer()` was called). Is there a way to detect whether Vite's code is loaded?
12
+ // 3. Ensure NODE_ENV isn't mistakenly set to a wrong value.
5
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
6
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
7
15
  };
8
16
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.assertNodeEnvIsNotDev = exports.getNodeEnvDesc = exports.isNodeEnvDev = exports.setNodeEnvToProduction = exports.getNodeEnv = void 0;
17
+ exports.handleNodeEnv_vitePluginVercel = exports.handleNodeEnv_prerender = exports.assertNodeEnv_onVikePluginLoad = exports.assertNodeEnv_runtime = exports.assertNodeEnv_build = void 0;
10
18
  const picocolors_1 = __importDefault(require("@brillout/picocolors"));
11
19
  const assertIsNotBrowser_js_1 = require("./assertIsNotBrowser.js");
12
20
  const assert_js_1 = require("./assert.js");
21
+ const assertIsNotProductionRuntime_js_1 = require("./assertIsNotProductionRuntime.js");
13
22
  (0, assertIsNotBrowser_js_1.assertIsNotBrowser)();
23
+ function assertNodeEnv_build() {
24
+ assertNodeEnvIsNotDev('building');
25
+ }
26
+ exports.assertNodeEnv_build = assertNodeEnv_build;
27
+ function assertNodeEnv_runtime(viteDevServerExists) {
28
+ const nodeEnv = getNodeEnv();
29
+ if (nodeEnv === null || nodeEnv === 'test')
30
+ return;
31
+ const isDev = isNodeEnvDev();
32
+ // Calling Vite's createServer() is enough for hasViteDevServer to be true, even without actually adding Vite's development middleware to the server: https://github.com/vikejs/vike/issues/792#issuecomment-1516830759
33
+ if (viteDevServerExists === isDev)
34
+ return;
35
+ const nodeEnvDesc = getNodeEnvDesc();
36
+ // TODO: make it assertUsage() again once #1528 is implemented.
37
+ (0, assert_js_1.assertWarning)(false, `Vite's development server was${viteDevServerExists ? '' : "n't"} instantiated while the ${nodeEnvDesc} which is contradictory, see https://vike.dev/NODE_ENV`, { onlyOnce: true });
38
+ }
39
+ exports.assertNodeEnv_runtime = assertNodeEnv_runtime;
40
+ function assertNodeEnv_onVikePluginLoad() {
41
+ const nodeEnv = getNodeEnv();
42
+ if (nodeEnv === 'test')
43
+ return;
44
+ // TODO: make it assertUsage() again once #1528 is implemented.
45
+ (0, assert_js_1.assertWarning)(
46
+ /* We can enable this assertion after Vike's CLI is implemented and using Vite's CLI is deprecated (we can then check whether the context is a `$ vike build`).
47
+ isNodeEnvDev() || isVikeCliBuild(),
48
+ /*/
49
+ isNodeEnvDev() || true,
50
+ ///*/
51
+ [
52
+ picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
53
+ '(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
54
+ 'while the vike/plugin module is loaded.',
55
+ assertIsNotProductionRuntime_js_1.vikeVitePluginLoadedInProductionError
56
+ ].join(' '), { onlyOnce: true });
57
+ }
58
+ exports.assertNodeEnv_onVikePluginLoad = assertNodeEnv_onVikePluginLoad;
59
+ function handleNodeEnv_prerender() {
60
+ const assertNodeEnv = () => assertNodeEnvIsNotDev('pre-rendering');
61
+ if (getNodeEnv())
62
+ assertNodeEnv();
63
+ setNodeEnvToProduction();
64
+ assertNodeEnv();
65
+ }
66
+ exports.handleNodeEnv_prerender = handleNodeEnv_prerender;
67
+ function handleNodeEnv_vitePluginVercel() {
68
+ setNodeEnvToProduction();
69
+ }
70
+ exports.handleNodeEnv_vitePluginVercel = handleNodeEnv_vitePluginVercel;
14
71
  function getNodeEnv() {
15
72
  if (typeof process === 'undefined')
16
73
  return null;
17
74
  return process.env.NODE_ENV;
18
75
  }
19
- exports.getNodeEnv = getNodeEnv;
20
76
  function setNodeEnvToProduction() {
21
77
  // The statement `process.env['NODE_ENV'] = 'production'` chokes webpack v4
22
78
  const proc = process;
23
79
  const { env } = proc;
24
80
  env.NODE_ENV = 'production';
25
81
  }
26
- exports.setNodeEnvToProduction = setNodeEnvToProduction;
27
82
  function isNodeEnvDev() {
28
83
  const nodeEnv = getNodeEnv();
29
84
  if (!nodeEnv)
@@ -33,19 +88,19 @@ function isNodeEnvDev() {
33
88
  // That's quite aggressive, let's see if some user complains
34
89
  return false;
35
90
  }
36
- exports.isNodeEnvDev = isNodeEnvDev;
37
91
  function getNodeEnvDesc() {
38
92
  const nodeEnv = getNodeEnv();
39
93
  const isDev = isNodeEnvDev();
40
94
  const nodeEnvDesc = `environment is set to be a ${(isDev ? 'development' : 'production')} environment by ${picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`)}`;
41
95
  return nodeEnvDesc;
42
96
  }
43
- exports.getNodeEnvDesc = getNodeEnvDesc;
44
97
  function assertNodeEnvIsNotDev(operation) {
45
98
  const isDev = isNodeEnvDev();
46
99
  if (!isDev)
47
100
  return;
48
101
  const nodeEnvDesc = getNodeEnvDesc();
49
- (0, assert_js_1.assertUsage)(false, `The ${nodeEnvDesc} which is forbidden upon ${operation}, see https://vike.dev/NODE_ENV`);
102
+ // TODO: make it assertUsage() again once #1528 is implemented.
103
+ (0, assert_js_1.assertWarning)(false, `The ${nodeEnvDesc} which is forbidden upon ${operation}, see https://vike.dev/NODE_ENV`, {
104
+ onlyOnce: true
105
+ });
50
106
  }
51
- exports.assertNodeEnvIsNotDev = assertNodeEnvIsNotDev;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PROJECT_VERSION = exports.projectInfo = void 0;
4
4
  const assertSingleInstance_js_1 = require("./assertSingleInstance.js");
5
- const PROJECT_VERSION = '0.4.163';
5
+ const PROJECT_VERSION = '0.4.164';
6
6
  exports.PROJECT_VERSION = PROJECT_VERSION;
7
7
  const projectInfo = {
8
8
  projectName: 'Vike',
@@ -2,7 +2,7 @@
2
2
  export { route, getPagesAndRoutes };
3
3
  import { route as routeInternal } from '../shared/route/index.js';
4
4
  import { getGlobalContext, initGlobalContext } from '../node/runtime/globalContext.js';
5
- import { setNodeEnvToProduction } from '../utils/assertNodeEnv.js';
5
+ import { handleNodeEnv_vitePluginVercel } from '../utils/assertNodeEnv.js';
6
6
  import { assert } from '../utils/assert.js';
7
7
  import { getRenderContext } from '../node/runtime/renderPage/renderPageAlreadyRouted.js';
8
8
  /**
@@ -12,7 +12,7 @@ import { getRenderContext } from '../node/runtime/renderPage/renderPageAlreadyRo
12
12
  * @param config
13
13
  */
14
14
  async function getPagesAndRoutes() {
15
- setNodeEnvToProduction();
15
+ handleNodeEnv_vitePluginVercel();
16
16
  await initGlobalContext(true);
17
17
  const globalContext = getGlobalContext();
18
18
  assert(globalContext.isProduction === true);
@@ -3,7 +3,7 @@ export { plugin };
3
3
  // TODO/v1-release: remove
4
4
  export { plugin as ssr };
5
5
  export { PROJECT_VERSION as version } from './utils.js';
6
- import { assertUsage, getNodeEnv, isNodeEnvDev, markEnvAsVikePluginLoaded, vikeVitePluginLoadedInProductionError } from './utils.js';
6
+ import { assertNodeEnv_onVikePluginLoad, assertUsage, markEnvAsVikePluginLoaded } from './utils.js';
7
7
  import { buildConfig } from './plugins/buildConfig.js';
8
8
  import { previewConfig } from './plugins/previewConfig.js';
9
9
  import { autoFullBuild } from './plugins/autoFullBuild.js';
@@ -23,7 +23,7 @@ import { baseUrls } from './plugins/baseUrls.js';
23
23
  import { envVarsPlugin } from './plugins/envVars.js';
24
24
  import pc from '@brillout/picocolors';
25
25
  import { fileEnv } from './plugins/fileEnv.js';
26
- assertNodeEnv();
26
+ assertNodeEnv_onVikePluginLoad();
27
27
  markEnvAsVikePluginLoaded();
28
28
  // Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed
29
29
  function plugin(vikeConfig) {
@@ -63,21 +63,3 @@ Object.defineProperty(plugin, 'apply', {
63
63
  assertUsage(false, `Add ${pc.cyan('vike()')} instead of ${pc.cyan('vike')} to vite.config.js#plugins (i.e. call the function and add the return value instead of adding the function itself)`, { showStackTrace: true });
64
64
  }
65
65
  });
66
- function assertNodeEnv() {
67
- const nodeEnv = getNodeEnv();
68
- if (nodeEnv === 'test')
69
- return;
70
- // We should change this to be a warning if it blocks users (e.g. if a bad-citizen tool sets a wrong process.env.NODE_ENV value).
71
- assertUsage(
72
- /* We can enable this assertion after Vike's CLI is implemented and using Vite's CLI is deprecated (we can then check whether the context is a `$ vike build`).
73
- isNodeEnvDev() || isVikeCliBuild(),
74
- /*/
75
- isNodeEnvDev() || true,
76
- ///*/
77
- [
78
- pc.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
79
- '(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
80
- 'while the vike/plugin module is loaded.',
81
- vikeVitePluginLoadedInProductionError
82
- ].join(' '));
83
- }
@@ -6,6 +6,7 @@ import { getConfigVike } from '../../shared/getConfigVike.js';
6
6
  import { isViteCliCall, getViteConfigFromCli } from '../shared/isViteCliCall.js';
7
7
  import pc from '@brillout/picocolors';
8
8
  import { logErrorHint } from '../../runtime/renderPage/logErrorHint.js';
9
+ import { manifestTempFile } from './buildConfig.js';
9
10
  let forceExit = false;
10
11
  function autoFullBuild() {
11
12
  let config;
@@ -58,10 +59,12 @@ async function triggerFullBuild(config, configVike, bundle) {
58
59
  return; // already triggered
59
60
  if (isDisabled(configVike))
60
61
  return;
61
- /* Is this @vitejs/plugin-legacy workaround still needed? Should we re-implement it?
62
- // vike.json missing => it isn't a `$ vite build` call (e.g. @vitejs/plugin-legacy calls Vite's build() API) => skip
63
- if (!bundle['vike.json']) return
64
- */
62
+ // Workaround for @vitejs/plugin-legacy
63
+ // - The legacy plugin triggers its own Rollup build for the client-side.
64
+ // - The legacy plugin doesn't generate a manifest => we can use that to detect the legacy plugin build.
65
+ // - Issue & reproduction: https://github.com/vikejs/vike/issues/1154#issuecomment-1965954636
66
+ if (!bundle[manifestTempFile])
67
+ return;
65
68
  const configFromCli = !isViteCliCall() ? null : getViteConfigFromCli();
66
69
  const configInline = {
67
70
  ...configFromCli,
@@ -1,7 +1,7 @@
1
1
  export { fixServerAssets };
2
2
  export { fixServerAssets_isEnabled };
3
3
  import { ViteManifest } from '../../../shared/ViteManifest.js';
4
- import { OutDirs } from '../../utils.js';
4
+ import { ResolvedConfig } from 'vite';
5
5
  /**
6
6
  * true => use workaround config.build.ssrEmitAssets
7
7
  * false => use workaround extractAssets plugin
@@ -10,4 +10,4 @@ import { OutDirs } from '../../utils.js';
10
10
  */
11
11
  declare function fixServerAssets_isEnabled(): boolean;
12
12
  /** https://github.com/vikejs/vike/issues/1339 */
13
- declare function fixServerAssets(outDirs: OutDirs): Promise<ViteManifest>;
13
+ declare function fixServerAssets(config: ResolvedConfig): Promise<ViteManifest>;
@@ -3,9 +3,10 @@ export { fixServerAssets_isEnabled };
3
3
  import fs from 'fs/promises';
4
4
  import path from 'path';
5
5
  import { existsSync } from 'fs';
6
- import { assert, pLimit, unique } from '../../utils.js';
6
+ import { assert, getOutDirs, pLimit, unique } from '../../utils.js';
7
7
  import { isVirtualFileIdPageConfigValuesAll } from '../../../shared/virtual-files/virtualFilePageConfigValuesAll.js';
8
8
  import { manifestTempFile } from '../buildConfig.js';
9
+ import { getAssetsDir } from '../../shared/getAssetsDir.js';
9
10
  /**
10
11
  * true => use workaround config.build.ssrEmitAssets
11
12
  * false => use workaround extractAssets plugin
@@ -18,11 +19,12 @@ function fixServerAssets_isEnabled() {
18
19
  return true;
19
20
  }
20
21
  /** https://github.com/vikejs/vike/issues/1339 */
21
- async function fixServerAssets(outDirs) {
22
+ async function fixServerAssets(config) {
23
+ const outDirs = getOutDirs(config);
22
24
  const clientManifest = await loadManifest(outDirs.outDirClient);
23
25
  const serverManifest = await loadManifest(outDirs.outDirServer);
24
26
  const { clientManifestMod, filesToCopy } = addServerAssets(clientManifest, serverManifest);
25
- await copyAssets(filesToCopy, outDirs);
27
+ await copyAssets(filesToCopy, config);
26
28
  return clientManifestMod;
27
29
  }
28
30
  async function loadManifest(outDir) {
@@ -33,8 +35,10 @@ async function loadManifest(outDir) {
33
35
  assert(manifest);
34
36
  return manifest;
35
37
  }
36
- async function copyAssets(filesToCopy, { outDirClient, outDirServer }) {
37
- const assetsDirServer = path.posix.join(outDirServer, 'assets');
38
+ async function copyAssets(filesToCopy, config) {
39
+ const { outDirClient, outDirServer } = getOutDirs(config);
40
+ const assetsDir = getAssetsDir(config);
41
+ const assetsDirServer = path.posix.join(outDirServer, assetsDir);
38
42
  if (!filesToCopy.length)
39
43
  return;
40
44
  assert(existsSync(assetsDirServer));
@@ -2,7 +2,7 @@ export { buildConfig };
2
2
  export { assertRollupInput };
3
3
  export { analyzeClientEntries };
4
4
  export { manifestTempFile };
5
- import { assert, resolveOutDir, viteIsSSR, getFilePathAbsolute, addOnBeforeLogHook, removeFileExtention, unique, assertPosixPath, assertUsage, injectRollupInputs, normalizeRollupInput, assertNodeEnvIsNotDev, getOutDirs, isNpmPackageImport } from '../utils.js';
5
+ import { assert, resolveOutDir, viteIsSSR, getFilePathAbsolute, addOnBeforeLogHook, removeFileExtention, unique, assertPosixPath, assertUsage, injectRollupInputs, normalizeRollupInput, getOutDirs, isNpmPackageImport, assertNodeEnv_build } from '../utils.js';
6
6
  import { getVikeConfig, isV1Design } from './importUserCode/v1-design/getVikeConfig.js';
7
7
  import { getConfigValue } from '../../../shared/page-configs/helpers.js';
8
8
  import { findPageFiles } from '../shared/findPageFiles.js';
@@ -24,14 +24,16 @@ function buildConfig() {
24
24
  let isServerAssetsFixEnabled;
25
25
  let isSsrBuild;
26
26
  let outDirs;
27
+ let config;
27
28
  return {
28
29
  name: 'vike:buildConfig',
29
30
  apply: 'build',
30
31
  enforce: 'post',
31
32
  configResolved: {
32
33
  order: 'post',
33
- async handler(config) {
34
- assertNodeEnv();
34
+ async handler(config_) {
35
+ config = config_;
36
+ assertNodeEnv_build();
35
37
  assertRollupInput(config);
36
38
  const entries = await getEntries(config);
37
39
  assert(Object.keys(entries).length > 0);
@@ -50,7 +52,7 @@ function buildConfig() {
50
52
  }
51
53
  },
52
54
  config(config) {
53
- assertNodeEnv();
55
+ assertNodeEnv_build();
54
56
  isSsrBuild = viteIsSSR(config);
55
57
  return {
56
58
  build: {
@@ -61,7 +63,7 @@ function buildConfig() {
61
63
  };
62
64
  },
63
65
  buildStart() {
64
- assertNodeEnv();
66
+ assertNodeEnv_build();
65
67
  },
66
68
  writeBundle: {
67
69
  order: 'post',
@@ -78,7 +80,7 @@ function buildConfig() {
78
80
  await fs.copyFile(clientManifestFilePath, assetsJsonFilePath);
79
81
  }
80
82
  else {
81
- const clientManifestMod = await fixServerAssets(outDirs);
83
+ const clientManifestMod = await fixServerAssets(config);
82
84
  await fs.writeFile(assetsJsonFilePath, JSON.stringify(clientManifestMod, null, 2), 'utf-8');
83
85
  }
84
86
  await fs.rm(clientManifestFilePath);
@@ -252,6 +254,3 @@ function assertRollupInput(config) {
252
254
  const htmlInput = htmlInputs[0];
253
255
  assertUsage(htmlInput === undefined, `The entry ${htmlInput} of config build.rollupOptions.input is an HTML entry which is forbidden when using Vike, instead follow https://vike.dev/add`);
254
256
  }
255
- function assertNodeEnv() {
256
- assertNodeEnvIsNotDev('building');
257
- }
@@ -4,6 +4,7 @@ export { distFileNames };
4
4
  // - Blocker: https://github.com/rollup/rollup/issues/4724
5
5
  import { assertPosixPath, assert, assertUsage } from '../utils.js';
6
6
  import path from 'path';
7
+ import { getAssetsDir } from '../shared/getAssetsDir.js';
7
8
  function distFileNames() {
8
9
  return {
9
10
  name: 'vike:distFileNames',
@@ -163,9 +164,3 @@ function getRollupOutputs(config) {
163
164
  }
164
165
  return output;
165
166
  }
166
- function getAssetsDir(config) {
167
- let { assetsDir } = config.build;
168
- assertUsage(assetsDir, `${assetsDir} cannot be an empty string`);
169
- assetsDir = assetsDir.split(/\/|\\/).filter(Boolean).join('/');
170
- return assetsDir;
171
- }
@@ -3,7 +3,7 @@ export { reloadVikeConfig };
3
3
  export { vikeConfigDependencies };
4
4
  export { isVikeConfigFile };
5
5
  export { isV1Design };
6
- export type { VikeConfig };
6
+ export type { VikeConfigObject };
7
7
  export type { InterfaceValueFile };
8
8
  import type { PageConfigGlobalBuildTime, PageConfigBuildTime, FilePathResolved } from '../../../../../shared/page-configs/PageConfig.js';
9
9
  import { type LocationId } from './getVikeConfig/filesystemRouting.js';
@@ -21,13 +21,13 @@ type InterfaceValueFile = InterfaceFileCommons & {
21
21
  configName: string;
22
22
  };
23
23
  type ConfigName = string;
24
- type VikeConfig = {
24
+ type VikeConfigObject = {
25
25
  pageConfigs: PageConfigBuildTime[];
26
26
  pageConfigGlobal: PageConfigGlobalBuildTime;
27
27
  globalVikeConfig: Record<string, unknown>;
28
28
  };
29
29
  declare const vikeConfigDependencies: Set<string>;
30
30
  declare function reloadVikeConfig(userRootDir: string, outDirRoot: string): void;
31
- declare function getVikeConfig(config: ResolvedConfig, isDev: boolean, tolerateInvalidConfig?: boolean): Promise<VikeConfig>;
31
+ declare function getVikeConfig(config: ResolvedConfig, isDev: boolean, tolerateInvalidConfig?: boolean): Promise<VikeConfigObject>;
32
32
  declare function isV1Design(config: ResolvedConfig, isDev: boolean): Promise<boolean>;
33
33
  declare function isVikeConfigFile(filePath: string): boolean;
@@ -0,0 +1,3 @@
1
+ export { getAssetsDir };
2
+ import type { ResolvedConfig } from 'vite';
3
+ declare function getAssetsDir(config: ResolvedConfig): string;
@@ -0,0 +1,8 @@
1
+ export { getAssetsDir };
2
+ import { assertUsage } from '../utils.js';
3
+ function getAssetsDir(config) {
4
+ let { assetsDir } = config.build;
5
+ assertUsage(assetsDir, `${assetsDir} cannot be an empty string`);
6
+ assetsDir = assetsDir.split(/\/|\\/).filter(Boolean).join('/');
7
+ return assetsDir;
8
+ }
@@ -5,7 +5,7 @@ export { runPrerender_forceExit };
5
5
  import '../runtime/page-files/setup.js';
6
6
  import path from 'path';
7
7
  import { route } from '../../shared/route/index.js';
8
- import { assert, assertUsage, assertWarning, hasProp, projectInfo, objectAssign, isObjectWithKeys, isCallable, getOutDirs, isPropertyGetter, assertPosixPath, urlToFile, executeHook, isPlainObject, setNodeEnvToProduction, isUserHookError, assertNodeEnvIsNotDev, getNodeEnv } from './utils.js';
8
+ import { assert, assertUsage, assertWarning, hasProp, projectInfo, objectAssign, isObjectWithKeys, isCallable, getOutDirs, isPropertyGetter, assertPosixPath, urlToFile, executeHook, isPlainObject, isUserHookError, handleNodeEnv_prerender } from './utils.js';
9
9
  import { pLimit } from '../../utils/pLimit.js';
10
10
  import { prerenderPage, prerender404Page, getRenderContext, getPageContextInitEnhanced } from '../runtime/renderPage/renderPageAlreadyRouted.js';
11
11
  import pc from '@brillout/picocolors';
@@ -59,7 +59,7 @@ async function runPrerender(options, manuallyTriggered) {
59
59
  if (logLevel === 'info') {
60
60
  console.log(`${pc.cyan(`vike v${projectInfo.projectVersion}`)} ${pc.green('pre-rendering HTML...')}`);
61
61
  }
62
- handleNodeEnv();
62
+ handleNodeEnv_prerender();
63
63
  await disableReactStreaming();
64
64
  const viteConfig = await resolveConfig(options.viteConfig || {}, 'vike pre-rendering', 'production');
65
65
  setGlobalContext_prerender(viteConfig);
@@ -714,10 +714,3 @@ function assertIsNotAbort(err, urlOr404) {
714
714
  assert(abortCall);
715
715
  assertUsage(false, `${pc.cyan(abortCall)} thrown${thrownBy} while pre-rendering ${urlOr404} but ${pc.cyan(abortCaller)} isn't supported for pre-rendered pages`);
716
716
  }
717
- function handleNodeEnv() {
718
- const assertNodeEnv = () => assertNodeEnvIsNotDev('pre-rendering');
719
- if (getNodeEnv())
720
- assertNodeEnv();
721
- setNodeEnvToProduction();
722
- assertNodeEnv();
723
- }
@@ -9,7 +9,7 @@ export { getViteConfig };
9
9
  export { setGlobalContext_viteDevServer };
10
10
  export { setGlobalContext_prerender };
11
11
  export { getRuntimeManifest };
12
- import { assert, assertUsage, assertWarning, getGlobalObject, getNodeEnv, getNodeEnvDesc, isNodeEnvDev, isPlainObject, objectAssign, objectKeys } from './utils.js';
12
+ import { assert, assertNodeEnv_runtime, assertUsage, assertWarning, getGlobalObject, isPlainObject, objectAssign, objectKeys } from './utils.js';
13
13
  import { loadImportBuild } from './globalContext/loadImportBuild.js';
14
14
  import { setPageFiles } from '../../shared/getPageFiles.js';
15
15
  import { assertPluginManifest } from '../shared/assertPluginManifest.js';
@@ -83,7 +83,7 @@ async function initGlobalContext(isPrerendering = false, outDir) {
83
83
  if (globalObject.globalContext)
84
84
  return;
85
85
  const { viteDevServer, viteConfig } = globalObject;
86
- assertNodeEnv(!!viteDevServer);
86
+ assertNodeEnv_runtime(!!viteDevServer);
87
87
  const isProduction = !viteDevServer;
88
88
  if (!isProduction) {
89
89
  assert(viteConfig);
@@ -179,15 +179,3 @@ function assertViteManifest(manifest) {
179
179
  })
180
180
  */
181
181
  }
182
- function assertNodeEnv(hasViteDevServer) {
183
- const nodeEnv = getNodeEnv();
184
- if (nodeEnv === null || nodeEnv === 'test')
185
- return;
186
- const isDev = isNodeEnvDev();
187
- // Calling Vite's createServer() is enough for hasViteDevServer to be true, even without actually adding Vite's development middleware to the server: https://github.com/vikejs/vike/issues/792#issuecomment-1516830759
188
- if (hasViteDevServer === isDev)
189
- return;
190
- const nodeEnvDesc = getNodeEnvDesc();
191
- // We should change this to be a warning if it blocks users (e.g. if a bad-citizen tool sets a wrong process.env.NODE_ENV value)
192
- assertUsage(false, `Vite's development server was${hasViteDevServer ? '' : "n't"} instantiated while the ${nodeEnvDesc} which is contradictory, see https://vike.dev/NODE_ENV`);
193
- }
@@ -3,8 +3,7 @@ export { logErrorHint };
3
3
  export { isCjsEsmError };
4
4
  export { isKnownError };
5
5
  export { getHint };
6
- import pc from '@brillout/picocolors';
7
- import { assert, formatHintLog, isNotNullish, isObject, unique, joinEnglish } from '../utils.js';
6
+ import { assert, formatHintLog, isNotNullish, isObject, unique } from '../utils.js';
8
7
  const knownErrors = [
9
8
  {
10
9
  errMsg: 'jsxDEV is not a function',
@@ -38,15 +37,7 @@ function getHint(error) {
38
37
  }
39
38
  const res = isCjsEsmError(error);
40
39
  if (res) {
41
- const packageNames = res === true ? null : res;
42
- const hint = [
43
- 'Error could be a CJS/ESM issue, consider ',
44
- !packageNames || packageNames.length === 0
45
- ? 'using'
46
- : `adding ${joinEnglish(packageNames.map((p) => pc.cyan(p)), 'or')} to`,
47
- ` ${pc.cyan('ssr.noExternal')}`,
48
- ', see https://vike.dev/broken-npm-package'
49
- ].join('');
40
+ const hint = 'The error seems to be a CJS/ESM issue, see https://vike.dev/broken-npm-package';
50
41
  return hint;
51
42
  }
52
43
  return null;
@@ -77,6 +68,7 @@ function isCjsEsmError(error) {
77
68
  packageNames.forEach((packageName) => {
78
69
  assert(!['vite', 'vike'].includes(packageName));
79
70
  });
71
+ // We don't use this anymore: we could return `true` instead. Shall we remove returning a list of npm packages?
80
72
  return packageNames;
81
73
  }
82
74
  function normalizeRes(res) {
@@ -1,10 +1,10 @@
1
- export { getNodeEnv };
2
- export { setNodeEnvToProduction };
3
- export { isNodeEnvDev };
4
- export { getNodeEnvDesc };
5
- export { assertNodeEnvIsNotDev };
6
- declare function getNodeEnv(): null | undefined | string;
7
- declare function setNodeEnvToProduction(): void | undefined;
8
- declare function isNodeEnvDev(): boolean;
9
- declare function getNodeEnvDesc(): `environment is set to be a ${string} environment by process.env.NODE_ENV === ${string}`;
10
- declare function assertNodeEnvIsNotDev(operation: 'building' | 'pre-rendering'): void;
1
+ export { assertNodeEnv_build };
2
+ export { assertNodeEnv_runtime };
3
+ export { assertNodeEnv_onVikePluginLoad };
4
+ export { handleNodeEnv_prerender };
5
+ export { handleNodeEnv_vitePluginVercel };
6
+ declare function assertNodeEnv_build(): void;
7
+ declare function assertNodeEnv_runtime(viteDevServerExists: boolean): void;
8
+ declare function assertNodeEnv_onVikePluginLoad(): void;
9
+ declare function handleNodeEnv_prerender(): void;
10
+ declare function handleNodeEnv_vitePluginVercel(): void;
@@ -1,15 +1,69 @@
1
- // Utils to manage process.env.NODE_ENV and, most notably, to assert correct usage, which is crucial:
2
- // - https://github.com/vikejs/vike/issues/1469#issuecomment-1919518096
3
- // - https://github.com/vitejs/vite/blob/76f30ae23b92f9af910ec02d98e2baaefa12141f/packages/vite/src/node/config.ts#L567
4
- export { getNodeEnv };
5
- export { setNodeEnvToProduction };
6
- export { isNodeEnvDev };
7
- export { getNodeEnvDesc };
8
- export { assertNodeEnvIsNotDev };
1
+ // This file serves the following three purposes:
2
+ // 1. Upon building the app, we ensure the right NODE_ENV value is set.
3
+ // - Both Vue and React use NODE_ENV for enabling production-specific features:
4
+ // - Vue: https://github.com/vuejs/core/blob/f66a75ea75c8aece065b61e2126b4c5b2338aa6e/packages/vue/index.js
5
+ // - React: https://github.com/facebook/react/blob/01ab35a9a731dec69995fbd28f3ac7eaad11e183/packages/react/npm/index.js
6
+ // - Setting NODE_ENV to 'production' doesn't seem to make any sense in development.
7
+ // - With React upon building the app, setting NODE_ENV to a value other than 'production' triggers an error: https://github.com/vikejs/vike/issues/1469#issuecomment-1969301797
8
+ // - With Vue upon building the app, NODE_ENV can be set to a value other than 'production', e.g. 'development'.
9
+ // 2. Ensure Vite isn't loaded in production.
10
+ // - We currently only check whether Vite's development middleware is instantiated (i.e. whether Vite's `createServer()` was called). Is there a way to detect whether Vite's code is loaded?
11
+ // 3. Ensure NODE_ENV isn't mistakenly set to a wrong value.
12
+ // TODO: to fully implement the aforementioned points we need to implement a [new setting `allowNodeEnv`](https://github.com/vikejs/vike/issues/1528)
13
+ export { assertNodeEnv_build };
14
+ export { assertNodeEnv_runtime };
15
+ export { assertNodeEnv_onVikePluginLoad };
16
+ // Vite already handles build
17
+ export { handleNodeEnv_prerender };
18
+ export { handleNodeEnv_vitePluginVercel };
9
19
  import pc from '@brillout/picocolors';
10
20
  import { assertIsNotBrowser } from './assertIsNotBrowser.js';
11
- import { assertUsage } from './assert.js';
21
+ import { assertWarning } from './assert.js';
22
+ import { vikeVitePluginLoadedInProductionError } from './assertIsNotProductionRuntime.js';
12
23
  assertIsNotBrowser();
24
+ function assertNodeEnv_build() {
25
+ assertNodeEnvIsNotDev('building');
26
+ }
27
+ function assertNodeEnv_runtime(viteDevServerExists) {
28
+ const nodeEnv = getNodeEnv();
29
+ if (nodeEnv === null || nodeEnv === 'test')
30
+ return;
31
+ const isDev = isNodeEnvDev();
32
+ // Calling Vite's createServer() is enough for hasViteDevServer to be true, even without actually adding Vite's development middleware to the server: https://github.com/vikejs/vike/issues/792#issuecomment-1516830759
33
+ if (viteDevServerExists === isDev)
34
+ return;
35
+ const nodeEnvDesc = getNodeEnvDesc();
36
+ // TODO: make it assertUsage() again once #1528 is implemented.
37
+ assertWarning(false, `Vite's development server was${viteDevServerExists ? '' : "n't"} instantiated while the ${nodeEnvDesc} which is contradictory, see https://vike.dev/NODE_ENV`, { onlyOnce: true });
38
+ }
39
+ function assertNodeEnv_onVikePluginLoad() {
40
+ const nodeEnv = getNodeEnv();
41
+ if (nodeEnv === 'test')
42
+ return;
43
+ // TODO: make it assertUsage() again once #1528 is implemented.
44
+ assertWarning(
45
+ /* We can enable this assertion after Vike's CLI is implemented and using Vite's CLI is deprecated (we can then check whether the context is a `$ vike build`).
46
+ isNodeEnvDev() || isVikeCliBuild(),
47
+ /*/
48
+ isNodeEnvDev() || true,
49
+ ///*/
50
+ [
51
+ pc.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
52
+ '(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
53
+ 'while the vike/plugin module is loaded.',
54
+ vikeVitePluginLoadedInProductionError
55
+ ].join(' '), { onlyOnce: true });
56
+ }
57
+ function handleNodeEnv_prerender() {
58
+ const assertNodeEnv = () => assertNodeEnvIsNotDev('pre-rendering');
59
+ if (getNodeEnv())
60
+ assertNodeEnv();
61
+ setNodeEnvToProduction();
62
+ assertNodeEnv();
63
+ }
64
+ function handleNodeEnv_vitePluginVercel() {
65
+ setNodeEnvToProduction();
66
+ }
13
67
  function getNodeEnv() {
14
68
  if (typeof process === 'undefined')
15
69
  return null;
@@ -41,5 +95,8 @@ function assertNodeEnvIsNotDev(operation) {
41
95
  if (!isDev)
42
96
  return;
43
97
  const nodeEnvDesc = getNodeEnvDesc();
44
- assertUsage(false, `The ${nodeEnvDesc} which is forbidden upon ${operation}, see https://vike.dev/NODE_ENV`);
98
+ // TODO: make it assertUsage() again once #1528 is implemented.
99
+ assertWarning(false, `The ${nodeEnvDesc} which is forbidden upon ${operation}, see https://vike.dev/NODE_ENV`, {
100
+ onlyOnce: true
101
+ });
45
102
  }
@@ -1,7 +1,7 @@
1
1
  export { projectInfo };
2
2
  export { PROJECT_VERSION };
3
- declare const PROJECT_VERSION: "0.4.163";
3
+ declare const PROJECT_VERSION: "0.4.164";
4
4
  declare const projectInfo: {
5
5
  projectName: "Vike";
6
- projectVersion: "0.4.163";
6
+ projectVersion: "0.4.164";
7
7
  };
@@ -1,7 +1,7 @@
1
1
  export { projectInfo };
2
2
  export { PROJECT_VERSION };
3
3
  import { onProjectInfo } from './assertSingleInstance.js';
4
- const PROJECT_VERSION = '0.4.163';
4
+ const PROJECT_VERSION = '0.4.164';
5
5
  const projectInfo = {
6
6
  projectName: 'Vike',
7
7
  projectVersion: PROJECT_VERSION
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.163",
3
+ "version": "0.4.164",
4
4
  "scripts": {
5
5
  "dev": "tsc --watch",
6
6
  "build": "rimraf dist/ && pnpm run build:esm && pnpm run build:cjs",
@@ -169,6 +169,7 @@
169
169
  "vike": "./node/cli/bin-entry.js"
170
170
  },
171
171
  "devDependencies": {
172
+ "@biomejs/biome": "^1.5.3",
172
173
  "@brillout/import": "^0.2.3",
173
174
  "@brillout/json-serializer": "^0.5.8",
174
175
  "@brillout/picocolors": "^1.0.10",