vike 0.4.163-commit-407cb5c → 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.
@@ -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
- }
@@ -45,9 +45,9 @@ async function copyAssets(filesToCopy, config) {
45
45
  const { outDirClient, outDirServer } = (0, utils_js_1.getOutDirs)(config);
46
46
  const assetsDir = (0, getAssetsDir_js_1.getAssetsDir)(config);
47
47
  const assetsDirServer = path_1.default.posix.join(outDirServer, assetsDir);
48
- (0, utils_js_1.assert)((0, fs_1.existsSync)(assetsDirServer));
49
48
  if (!filesToCopy.length)
50
49
  return;
50
+ (0, utils_js_1.assert)((0, fs_1.existsSync)(assetsDirServer));
51
51
  const concurrencyLimit = (0, utils_js_1.pLimit)(10);
52
52
  await Promise.all(filesToCopy.map((file) => concurrencyLimit(() => promises_1.default.cp(path_1.default.posix.join(outDirServer, file), path_1.default.posix.join(outDirClient, file), {
53
53
  recursive: true
@@ -36,7 +36,7 @@ function buildConfig() {
36
36
  order: 'post',
37
37
  async handler(config_) {
38
38
  config = config_;
39
- assertNodeEnv();
39
+ (0, utils_js_1.assertNodeEnv_build)();
40
40
  assertRollupInput(config);
41
41
  const entries = await getEntries(config);
42
42
  (0, utils_js_1.assert)(Object.keys(entries).length > 0);
@@ -55,7 +55,7 @@ function buildConfig() {
55
55
  }
56
56
  },
57
57
  config(config) {
58
- assertNodeEnv();
58
+ (0, utils_js_1.assertNodeEnv_build)();
59
59
  isSsrBuild = (0, utils_js_1.viteIsSSR)(config);
60
60
  return {
61
61
  build: {
@@ -66,7 +66,7 @@ function buildConfig() {
66
66
  };
67
67
  },
68
68
  buildStart() {
69
- assertNodeEnv();
69
+ (0, utils_js_1.assertNodeEnv_build)();
70
70
  },
71
71
  writeBundle: {
72
72
  order: 'post',
@@ -260,6 +260,3 @@ function assertRollupInput(config) {
260
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`);
261
261
  }
262
262
  exports.assertRollupInput = assertRollupInput;
263
- function assertNodeEnv() {
264
- (0, utils_js_1.assertNodeEnvIsNotDev)('building');
265
- }
@@ -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,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-commit-407cb5c';
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
- }
@@ -39,9 +39,9 @@ async function copyAssets(filesToCopy, config) {
39
39
  const { outDirClient, outDirServer } = getOutDirs(config);
40
40
  const assetsDir = getAssetsDir(config);
41
41
  const assetsDirServer = path.posix.join(outDirServer, assetsDir);
42
- assert(existsSync(assetsDirServer));
43
42
  if (!filesToCopy.length)
44
43
  return;
44
+ assert(existsSync(assetsDirServer));
45
45
  const concurrencyLimit = pLimit(10);
46
46
  await Promise.all(filesToCopy.map((file) => concurrencyLimit(() => fs.cp(path.posix.join(outDirServer, file), path.posix.join(outDirClient, file), {
47
47
  recursive: true
@@ -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';
@@ -33,7 +33,7 @@ function buildConfig() {
33
33
  order: 'post',
34
34
  async handler(config_) {
35
35
  config = config_;
36
- assertNodeEnv();
36
+ assertNodeEnv_build();
37
37
  assertRollupInput(config);
38
38
  const entries = await getEntries(config);
39
39
  assert(Object.keys(entries).length > 0);
@@ -52,7 +52,7 @@ function buildConfig() {
52
52
  }
53
53
  },
54
54
  config(config) {
55
- assertNodeEnv();
55
+ assertNodeEnv_build();
56
56
  isSsrBuild = viteIsSSR(config);
57
57
  return {
58
58
  build: {
@@ -63,7 +63,7 @@ function buildConfig() {
63
63
  };
64
64
  },
65
65
  buildStart() {
66
- assertNodeEnv();
66
+ assertNodeEnv_build();
67
67
  },
68
68
  writeBundle: {
69
69
  order: 'post',
@@ -254,6 +254,3 @@ function assertRollupInput(config) {
254
254
  const htmlInput = htmlInputs[0];
255
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`);
256
256
  }
257
- function assertNodeEnv() {
258
- assertNodeEnvIsNotDev('building');
259
- }
@@ -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
- }
@@ -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-commit-407cb5c";
3
+ declare const PROJECT_VERSION: "0.4.164";
4
4
  declare const projectInfo: {
5
5
  projectName: "Vike";
6
- projectVersion: "0.4.163-commit-407cb5c";
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-commit-407cb5c';
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-commit-407cb5c",
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",