vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
171 lines (170 loc) • 8.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertIsNotProductionRuntime = assertIsNotProductionRuntime;
exports.onSetupRuntime = onSetupRuntime;
exports.onSetupBuild = onSetupBuild;
exports.onSetupPrerender = onSetupPrerender;
exports.onSetupPreview = onSetupPreview;
exports.setNodeEnvProduction = setNodeEnvProduction;
exports.markSetup_viteDevServer = markSetup_viteDevServer;
exports.markSetup_vitePreviewServer = markSetup_vitePreviewServer;
exports.markSetup_vikeVitePlugin = markSetup_vikeVitePlugin;
exports.markSetup_isViteDev = markSetup_isViteDev;
const assert_js_1 = require("./assert.js");
const assertIsNotBrowser_js_1 = require("./assertIsNotBrowser.js");
const debug_js_1 = require("./debug.js");
const getGlobalObject_js_1 = require("./getGlobalObject.js");
const isVitest_js_1 = require("./isVitest.js");
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
(0, assertIsNotBrowser_js_1.assertIsNotBrowser)();
const debug = (0, debug_js_1.createDebugger)('vike:setup');
const setup = (0, getGlobalObject_js_1.getGlobalObject)('utils/assertSetup.ts', {});
// Called by Vike modules that want to ensure that they aren't loaded by the server runtime in production
function assertIsNotProductionRuntime() {
if (debug.isActivated)
debug('assertIsNotProductionRuntime()', new Error().stack);
setup.shouldNotBeProduction = true;
}
function onSetupRuntime() {
if (debug.isActivated)
debug('assertSetup()', new Error().stack);
if (isTest())
return;
assertNodeEnvIsNotUndefinedString();
if (!setup.viteDevServer && setup.isViteDev === undefined) {
// TODO: make it assertUsage() again once https://github.com/vikejs/vike/issues/1528 is implemented.
(0, assert_js_1.assertWarning)(!isNodeEnvDev(), `The ${getEnvDescription()}, which is contradictory because the environment seems to be a production environment (Vite isn't loaded), see https://vike.dev/NODE_ENV`, { onlyOnce: true });
(0, assert_js_1.assertUsage)(!setup.vikeVitePlugin, `Loading Vike's Vite plugin (the ${picocolors_1.default.cyan('vike/plugin')} module) is prohibited in production.`);
// This assert() one of the main goal of this file: it ensures assertIsNotProductionRuntime()
(0, assert_js_1.assert)(!setup.shouldNotBeProduction);
}
else {
if (!setup.isPreview && !setup.vitePreviewServer && !setup.isPrerendering) {
// TODO: make it assertUsage() again once https://github.com/vikejs/vike/issues/1528 is implemented.
(0, assert_js_1.assertWarning)(isNodeEnvDev(), `The ${getEnvDescription()}, but Vite is loaded which is prohibited in production, see https://vike.dev/NODE_ENV`, { onlyOnce: true });
}
(0, assert_js_1.assert)(setup.vikeVitePlugin);
(0, assert_js_1.assert)(setup.shouldNotBeProduction);
}
}
// Ensure NODE_ENV is 'production' when building.
// - Used by both Vue and React for bundling minified version:
// - Vue: https://github.com/vuejs/core/blob/f66a75ea75c8aece065b61e2126b4c5b2338aa6e/packages/vue/index.js
// - React: https://github.com/facebook/react/blob/01ab35a9a731dec69995fbd28f3ac7eaad11e183/packages/react/npm/index.js
// - Required for React: setting NODE_ENV to a value other than 'production' triggers an error: https://github.com/vikejs/vike/issues/1469#issuecomment-1969301797
// - Not required for Vue: when building the app, NODE_ENV can be set to a value other than 'production', e.g. 'development'.
function onSetupBuild() {
assertUsageNodeEnvIsNotDev('building');
/* Not needed: Vite already sets `process.env.NODE_ENV = 'production'`
setNodeEnvProduction()
*/
}
// Called by ../node/prerender/runPrerender.ts
function onSetupPrerender() {
markSetup_isPrerendering();
if (getNodeEnv())
assertUsageNodeEnvIsNotDev('pre-rendering');
setNodeEnvProduction();
}
// Called by ../node/api/preview.ts
function onSetupPreview() {
markSetup_isPreview();
}
function isTest() {
return (0, isVitest_js_1.isVitest)() || isNodeEnv('test');
}
// Called by Vite hook configureServer()
function markSetup_viteDevServer() {
if (debug.isActivated)
debug('markSetup_viteDevServer()', new Error().stack);
setup.viteDevServer = true;
}
// Called by Vite hook configurePreviewServer()
function markSetup_vitePreviewServer() {
if (debug.isActivated)
debug('markSetup_vitePreviewServer()', new Error().stack);
setup.vitePreviewServer = true;
}
// Called by ../node/vite/index.ts
function markSetup_vikeVitePlugin() {
if (debug.isActivated)
debug('markSetup_vikeVitePlugin()', new Error().stack);
setup.vikeVitePlugin = true;
}
// Whether Vite is loaded and whether it's in dev mode (the value returned by `isDevCheck()`)
function markSetup_isViteDev(isViteDev) {
if (debug.isActivated)
debug('markSetup_isViteDev()', new Error().stack);
setup.isViteDev = isViteDev;
}
function markSetup_isPrerendering() {
if (debug.isActivated)
debug('markSetup_isPrerendering()', new Error().stack);
setup.isPrerendering = true;
}
function markSetup_isPreview() {
if (debug.isActivated)
debug('markSetup_isPreview()', new Error().stack);
setup.isPreview = true;
}
function assertUsageNodeEnvIsNotDev(operation) {
if (!isNodeEnvDev())
return;
// TODO: make it assertUsage() again once https://github.com/vikejs/vike/issues/1528 is implemented.
(0, assert_js_1.assertWarning)(false, `The ${getEnvDescription()} which is forbidden upon ${operation}, see https://vike.dev/NODE_ENV`, { onlyOnce: true });
}
function getEnvDescription() {
const envType = `${(isNodeEnvDev() ? 'development' : 'production')} environment`;
const nodeEnvDesc = `environment is set to be a ${picocolors_1.default.bold(envType)} by ${picocolors_1.default.cyan(`process.env.NODE_ENV===${JSON.stringify(getNodeEnv())}`)}`;
return nodeEnvDesc;
}
// For example, Wrangler bug replaces `process.env.NODE_ENV` with `"undefined"`
// https://github.com/cloudflare/workers-sdk/issues/7886
function assertNodeEnvIsNotUndefinedString() {
const nodeEnv = getNodeEnv();
(0, assert_js_1.assertWarning)(nodeEnv !== 'undefined', `${picocolors_1.default.cyan('process.env.NODE_ENV==="undefined"')} which is unexpected: ${picocolors_1.default.cyan('process.env.NODE_ENV')} is allowed to be the *value* ${picocolors_1.default.cyan('undefined')} (i.e. ${picocolors_1.default.cyan('process.env.NODE_ENV===undefined')}) but it shouldn't be the *string* ${picocolors_1.default.cyan('"undefined"')} ${picocolors_1.default.underline('https://vike.dev/NODE_ENV')}`, { onlyOnce: true });
}
function isNodeEnvDev() {
const nodeEnv = getNodeEnv();
// That's quite strict, let's see if some user complains
return nodeEnv === undefined || isNodeEnv(['development', 'dev', '']);
}
function isNodeEnv(value) {
const values = Array.isArray(value) ? value : [value];
const nodeEnv = getNodeEnv();
return nodeEnv !== undefined && values.includes(nodeEnv.toLowerCase());
}
function getNodeEnv() {
let val;
try {
val = process.env.NODE_ENV;
}
catch {
return undefined;
}
/*
// Should we show the following warning? So far I don't think so because of the following. Maybe we can show it once we enable users to disable warnings.
// - The warning isn't always actionable, e.g. if it's a tool that dynamically sets `process.env.NODE_ENV`.
// - We assume that tools use `process.env.NODE_ENV` and not something like `const { env } = process; env.NODE_ENV`. Thus, in practice, `val` overrides `val2` so having `val!==val2` isn't an issue.
{
const val2 = process.env['NODE' + '_ENV']
if (val2)
assertWarning(
val === val2,
`Dynamically setting process.env.NODE_ENV to ${val2} hasn't any effect because process.env.NODE_ENV is being statically replaced to ${val}.`,
{ onlyOnce: true }
)
}
//*/
return val;
}
function setNodeEnvProduction() {
// The statement `process.env['NODE_ENV'] = 'production'` chokes webpack v4
const proc = process;
const { env } = proc;
env.NODE_ENV = 'production';
(0, assert_js_1.assert)(isNodeEnv('production'));
}