vike 0.4.158 → 0.4.159-commit-71760c7
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.
- package/dist/cjs/node/plugin/index.js +20 -1
- package/dist/cjs/node/plugin/plugins/devConfig/index.js +1 -1
- package/dist/cjs/node/plugin/plugins/importBuild/index.js +1 -1
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +18 -0
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +14 -15
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → transformImportStatements.js} +23 -8
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/transpileAndExecuteFile.js +44 -34
- package/dist/cjs/node/plugin/plugins/previewConfig.js +1 -1
- package/dist/cjs/node/plugin/shared/loggerNotProd/log.js +3 -3
- package/dist/cjs/node/runtime/globalContext.js +4 -3
- package/dist/cjs/shared/page-configs/assertPlusFileExport.js +44 -0
- package/dist/cjs/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
- package/dist/cjs/shared/route/abort.js +1 -1
- package/dist/cjs/utils/assert.js +5 -6
- package/dist/cjs/utils/assertIsNotProductionRuntime.js +35 -17
- package/dist/cjs/utils/nodeEnv.js +11 -1
- package/dist/cjs/utils/projectInfo.js +2 -4
- package/dist/esm/node/plugin/index.js +21 -2
- package/dist/esm/node/plugin/plugins/devConfig/index.js +2 -2
- package/dist/esm/node/plugin/plugins/importBuild/index.js +2 -2
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.d.ts +2 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getConfigFileExport.js +12 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/configDefinitionsBuiltIn.d.ts +6 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +12 -13
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → transformImportStatements.d.ts} +7 -5
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → transformImportStatements.js} +23 -7
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/transpileAndExecuteFile.js +43 -33
- package/dist/esm/node/plugin/plugins/previewConfig.js +2 -2
- package/dist/esm/node/plugin/shared/loggerNotProd/log.js +3 -3
- package/dist/esm/node/runtime/globalContext.js +5 -4
- package/dist/esm/shared/page-configs/PageConfig.d.ts +4 -0
- package/dist/esm/shared/page-configs/assertPlusFileExport.d.ts +2 -0
- package/dist/esm/shared/page-configs/assertPlusFileExport.js +38 -0
- package/dist/esm/shared/page-configs/serialize/parseConfigValuesImported.js +2 -2
- package/dist/esm/shared/route/abort.js +2 -2
- package/dist/esm/shared/types.d.ts +55 -28
- package/dist/esm/utils/assert.js +5 -6
- package/dist/esm/utils/assertIsNotProductionRuntime.d.ts +8 -6
- package/dist/esm/utils/assertIsNotProductionRuntime.js +35 -17
- package/dist/esm/utils/debug.d.ts +1 -1
- package/dist/esm/utils/nodeEnv.d.ts +2 -0
- package/dist/esm/utils/nodeEnv.js +10 -0
- package/dist/esm/utils/projectInfo.d.ts +2 -8
- package/dist/esm/utils/projectInfo.js +2 -4
- package/package.json +1 -1
- package/dist/cjs/shared/page-configs/assertExports.js +0 -67
- package/dist/esm/shared/page-configs/assertExports.d.ts +0 -6
- package/dist/esm/shared/page-configs/assertExports.js +0 -61
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setNodeEnvToProduction = exports.getNodeEnv = void 0;
|
|
3
|
+
exports.isNodeEnvDev = exports.setNodeEnvToProduction = exports.getNodeEnv = void 0;
|
|
4
4
|
function getNodeEnv() {
|
|
5
5
|
if (typeof process === 'undefined')
|
|
6
6
|
return null;
|
|
@@ -14,3 +14,13 @@ function setNodeEnvToProduction() {
|
|
|
14
14
|
env.NODE_ENV = 'production';
|
|
15
15
|
}
|
|
16
16
|
exports.setNodeEnvToProduction = setNodeEnvToProduction;
|
|
17
|
+
function isNodeEnvDev() {
|
|
18
|
+
const nodeEnv = getNodeEnv();
|
|
19
|
+
if (!nodeEnv)
|
|
20
|
+
return true;
|
|
21
|
+
if (['development', 'dev'].includes(nodeEnv))
|
|
22
|
+
return true;
|
|
23
|
+
// That's quite aggressive, let's see if some user complains
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
exports.isNodeEnvDev = isNodeEnvDev;
|
|
@@ -2,13 +2,11 @@
|
|
|
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.
|
|
5
|
+
const PROJECT_VERSION = '0.4.159-commit-71760c7';
|
|
6
6
|
exports.PROJECT_VERSION = PROJECT_VERSION;
|
|
7
7
|
const projectInfo = {
|
|
8
8
|
projectName: 'Vike',
|
|
9
|
-
projectVersion: PROJECT_VERSION
|
|
10
|
-
npmPackageName: 'vike',
|
|
11
|
-
githubRepository: 'https://github.com/vikejs/vike'
|
|
9
|
+
projectVersion: PROJECT_VERSION
|
|
12
10
|
};
|
|
13
11
|
exports.projectInfo = projectInfo;
|
|
14
12
|
// Trick: since `utils/asserts.ts` depends on this file (`utils/projectInfo.ts`), we can have confidence that this file is always instantiated. So that we don't have to initialize this code snippet at every possible entry. (There are a *lot* of entries: `client/router/`, `client/`, `node/`, `node/plugin/`, `node/cli`, etc.)
|
|
@@ -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,
|
|
6
|
+
import { assertUsage, getNodeEnv, isNodeEnvDev, markEnvAsVikePluginLoaded, vikeVitePluginLoadedInProductionError } 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';
|
|
@@ -24,7 +24,8 @@ import { baseUrls } from './plugins/baseUrls.js';
|
|
|
24
24
|
import { envVarsPlugin } from './plugins/envVars.js';
|
|
25
25
|
import pc from '@brillout/picocolors';
|
|
26
26
|
import { fileEnv } from './plugins/fileEnv.js';
|
|
27
|
-
|
|
27
|
+
assertNodeEnv();
|
|
28
|
+
markEnvAsVikePluginLoaded();
|
|
28
29
|
// Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed
|
|
29
30
|
function plugin(vikeConfig) {
|
|
30
31
|
const plugins = [
|
|
@@ -64,3 +65,21 @@ Object.defineProperty(plugin, 'apply', {
|
|
|
64
65
|
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 });
|
|
65
66
|
}
|
|
66
67
|
});
|
|
68
|
+
function assertNodeEnv() {
|
|
69
|
+
const nodeEnv = getNodeEnv();
|
|
70
|
+
if (nodeEnv === 'test')
|
|
71
|
+
return;
|
|
72
|
+
// 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).
|
|
73
|
+
assertUsage(
|
|
74
|
+
/* 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`).
|
|
75
|
+
isNodeEnvDev() || isVikeCliBuild(),
|
|
76
|
+
/*/
|
|
77
|
+
isNodeEnvDev() || true,
|
|
78
|
+
///*/
|
|
79
|
+
[
|
|
80
|
+
pc.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
|
|
81
|
+
'(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
|
|
82
|
+
'while the vike/plugin module is loaded.',
|
|
83
|
+
vikeVitePluginLoadedInProductionError
|
|
84
|
+
].join(' '));
|
|
85
|
+
}
|
|
@@ -3,7 +3,7 @@ import { determineOptimizeDeps } from './determineOptimizeDeps.js';
|
|
|
3
3
|
import { determineFsAllowList } from './determineFsAllowList.js';
|
|
4
4
|
import { getConfigVike } from '../../../shared/getConfigVike.js';
|
|
5
5
|
import { addSsrMiddleware } from '../../shared/addSsrMiddleware.js';
|
|
6
|
-
import {
|
|
6
|
+
import { markEnvAsViteDev } from '../../utils.js';
|
|
7
7
|
import { improveViteLogs } from '../../shared/loggerVite.js';
|
|
8
8
|
import { isErrorDebug } from '../../shared/isErrorDebug.js';
|
|
9
9
|
import { installHttpRequestAsyncStore } from '../../shared/getHttpRequestAsyncStore.js';
|
|
@@ -64,7 +64,7 @@ function devConfig() {
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
configureServer() {
|
|
67
|
-
|
|
67
|
+
markEnvAsViteDev();
|
|
68
68
|
}
|
|
69
69
|
},
|
|
70
70
|
{
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { importBuild };
|
|
2
2
|
import { serverEntryPlugin, findServerEntry } from '@brillout/vite-plugin-server-entry/plugin.js';
|
|
3
|
-
import { assert, getOutDirs,
|
|
3
|
+
import { assert, getOutDirs, toPosixPath, viteIsSSR } from '../../utils.js';
|
|
4
4
|
import path from 'path';
|
|
5
5
|
import { createRequire } from 'module';
|
|
6
6
|
import { getConfigVike } from '../../../shared/getConfigVike.js';
|
|
@@ -32,7 +32,7 @@ function importBuild() {
|
|
|
32
32
|
getImporterCode: () => {
|
|
33
33
|
return getEntryCode(config, configVike);
|
|
34
34
|
},
|
|
35
|
-
libraryName:
|
|
35
|
+
libraryName: 'Vike'
|
|
36
36
|
})
|
|
37
37
|
];
|
|
38
38
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { getConfigFileExport };
|
|
2
|
+
import pc from '@brillout/picocolors';
|
|
3
|
+
import { assertPlusFileExport } from '../../../../../shared/page-configs/assertPlusFileExport.js';
|
|
4
|
+
import { assert, assertUsage, isObject } from '../../../utils.js';
|
|
5
|
+
function getConfigFileExport(fileExports, filePathToShowToUser) {
|
|
6
|
+
assertPlusFileExport(fileExports, filePathToShowToUser, 'config');
|
|
7
|
+
const fileExport = fileExports.default || fileExports.config;
|
|
8
|
+
assert('default' in fileExports !== 'config' in fileExports);
|
|
9
|
+
const exportName = pc.cyan('default' in fileExports ? 'export default' : 'export { config }');
|
|
10
|
+
assertUsage(isObject(fileExport), `The ${exportName} of ${filePathToShowToUser} should be an object (but it's ${pc.cyan(`typeof exportedValue === ${JSON.stringify(typeof fileExport)}`)} instead)`);
|
|
11
|
+
return fileExport;
|
|
12
|
+
}
|
|
@@ -24,12 +24,17 @@ type ConfigDefinition = {
|
|
|
24
24
|
*/
|
|
25
25
|
cumulative?: boolean;
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* Function called when the config value is defined.
|
|
28
28
|
*
|
|
29
29
|
* https://vike.dev/meta
|
|
30
30
|
*/
|
|
31
31
|
effect?: ConfigEffect;
|
|
32
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* Function called when the config value is defined.
|
|
35
|
+
*
|
|
36
|
+
* https://vike.dev/meta
|
|
37
|
+
*/
|
|
33
38
|
type ConfigEffect = (config: {
|
|
34
39
|
/** The config value.
|
|
35
40
|
*
|
|
@@ -7,17 +7,18 @@ import path from 'path';
|
|
|
7
7
|
import { configDefinitionsBuiltIn, configDefinitionsBuiltInGlobal } from './getVikeConfig/configDefinitionsBuiltIn.js';
|
|
8
8
|
import { getLocationId, getFilesystemRouteString, getFilesystemRouteDefinedBy, isInherited, sortAfterInheritanceOrder, isGlobalLocation, applyFilesystemRoutingRootEffect } from './getVikeConfig/filesystemRouting.js';
|
|
9
9
|
import { isTmpFile, transpileAndExecuteFile } from './transpileAndExecuteFile.js';
|
|
10
|
-
import { parseImportData } from './
|
|
10
|
+
import { parseImportData } from './transformImportStatements.js';
|
|
11
11
|
import { isConfigInvalid, isConfigInvalid_set } from '../../../../runtime/renderPage/isConfigInvalid.js';
|
|
12
12
|
import { getViteDevServer } from '../../../../runtime/globalContext.js';
|
|
13
13
|
import { logConfigError, logConfigErrorRecover } from '../../../shared/loggerNotProd.js';
|
|
14
14
|
import { removeSuperfluousViteLog_enable, removeSuperfluousViteLog_disable } from '../../../shared/loggerVite/removeSuperfluousViteLog.js';
|
|
15
15
|
import pc from '@brillout/picocolors';
|
|
16
16
|
import { getConfigDefinedAtString } from '../../../../../shared/page-configs/helpers.js';
|
|
17
|
-
import {
|
|
17
|
+
import { assertPlusFileExport } from '../../../../../shared/page-configs/assertPlusFileExport.js';
|
|
18
18
|
import { getConfigVike } from '../../../../shared/getConfigVike.js';
|
|
19
19
|
import { assertConfigValueIsSerializable } from './getConfigValuesSerialized.js';
|
|
20
20
|
import { crawlPlusFiles } from './getVikeConfig/crawlPlusFiles.js';
|
|
21
|
+
import { getConfigFileExport } from './getConfigFileExport.js';
|
|
21
22
|
assertIsNotProductionRuntime();
|
|
22
23
|
let devServerIsCorrupt = false;
|
|
23
24
|
let wasConfigInvalid = null;
|
|
@@ -137,7 +138,7 @@ function getConfigDefinitionOptional(configDefinitions, configName) {
|
|
|
137
138
|
async function loadValueFile(interfaceValueFile, configName, userRootDir) {
|
|
138
139
|
const { fileExports } = await transpileAndExecuteFile(interfaceValueFile.filePath, true, userRootDir);
|
|
139
140
|
const { filePathToShowToUser } = interfaceValueFile.filePath;
|
|
140
|
-
|
|
141
|
+
assertPlusFileExport(fileExports, filePathToShowToUser, configName);
|
|
141
142
|
Object.entries(fileExports).forEach(([exportName, configValue]) => {
|
|
142
143
|
const configName_ = exportName === 'default' ? configName : exportName;
|
|
143
144
|
interfaceValueFile.configMap[configName_] = { configValue };
|
|
@@ -169,9 +170,8 @@ function getInterfaceFileFromConfigFile(configFile, isConfigExtend) {
|
|
|
169
170
|
isConfigExtend,
|
|
170
171
|
extendsFilePaths
|
|
171
172
|
};
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
Object.entries(fileExports.default).forEach(([configName, configValue]) => {
|
|
173
|
+
const fileExport = getConfigFileExport(fileExports, filePath.filePathToShowToUser);
|
|
174
|
+
Object.entries(fileExport).forEach(([configName, configValue]) => {
|
|
175
175
|
interfaceFile.configMap[configName] = { configValue };
|
|
176
176
|
});
|
|
177
177
|
return interfaceFile;
|
|
@@ -920,18 +920,17 @@ function assertExtendsImportPath(importPath, filePath, configFilePath) {
|
|
|
920
920
|
}
|
|
921
921
|
function getExtendsImportData(configFileExports, configFilePath) {
|
|
922
922
|
const { filePathToShowToUser } = configFilePath;
|
|
923
|
-
|
|
924
|
-
const defaultExports = configFileExports.default;
|
|
923
|
+
const configFileExport = getConfigFileExport(configFileExports, filePathToShowToUser);
|
|
925
924
|
const wrongUsage = `${filePathToShowToUser} sets the config ${pc.cyan('extends')} to an invalid value, see https://vike.dev/extends`;
|
|
926
925
|
let extendList;
|
|
927
|
-
if (!('extends' in
|
|
926
|
+
if (!('extends' in configFileExport)) {
|
|
928
927
|
return [];
|
|
929
928
|
}
|
|
930
|
-
else if (hasProp(
|
|
931
|
-
extendList = [
|
|
929
|
+
else if (hasProp(configFileExport, 'extends', 'string')) {
|
|
930
|
+
extendList = [configFileExport.extends];
|
|
932
931
|
}
|
|
933
|
-
else if (hasProp(
|
|
934
|
-
extendList =
|
|
932
|
+
else if (hasProp(configFileExport, 'extends', 'string[]')) {
|
|
933
|
+
extendList = configFileExport.extends;
|
|
935
934
|
}
|
|
936
935
|
else {
|
|
937
936
|
assertUsage(false, wrongUsage);
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { transformImportStatements };
|
|
2
2
|
export { parseImportData };
|
|
3
3
|
export { isImportData };
|
|
4
4
|
export type { FileImport };
|
|
5
5
|
export type { ImportData };
|
|
6
|
+
export { isVikeRealImport };
|
|
6
7
|
type FileImport = {
|
|
7
8
|
importStatementCode: string;
|
|
8
9
|
importString: string;
|
|
9
10
|
importLocalName: string;
|
|
10
11
|
};
|
|
11
|
-
declare function
|
|
12
|
-
|
|
12
|
+
declare function transformImportStatements(code: string, filePathToShowToUser2: string): {
|
|
13
|
+
noTransformation: true;
|
|
13
14
|
} | {
|
|
14
|
-
|
|
15
|
+
noTransformation: false;
|
|
15
16
|
code: string;
|
|
16
|
-
|
|
17
|
+
fileImportsTransformed: FileImport[];
|
|
17
18
|
};
|
|
18
19
|
/**
|
|
19
20
|
* Data Structure holding info about import statement:
|
|
@@ -41,3 +42,4 @@ declare module 'estree' {
|
|
|
41
42
|
end: number;
|
|
42
43
|
}
|
|
43
44
|
}
|
|
45
|
+
declare function isVikeRealImport(code: string, posStart: number): boolean;
|
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { transformImportStatements };
|
|
2
2
|
export { parseImportData };
|
|
3
3
|
export { isImportData };
|
|
4
|
+
// For ./transformImportStatements.spec.ts
|
|
5
|
+
export { isVikeRealImport };
|
|
4
6
|
// Playground: https://github.com/brillout/acorn-playground
|
|
7
|
+
// Import attributes support: https://github.com/acornjs/acorn/issues/983
|
|
8
|
+
// - Isn't stage 4 yet: https://github.com/tc39/proposal-import-attributes
|
|
5
9
|
import { parse } from 'acorn';
|
|
6
10
|
import { assert, assertUsage, assertWarning, styleFileRE } from '../../../utils.js';
|
|
7
11
|
import pc from '@brillout/picocolors';
|
|
8
|
-
function
|
|
12
|
+
function transformImportStatements(code, filePathToShowToUser2) {
|
|
9
13
|
const spliceOperations = [];
|
|
10
|
-
const
|
|
14
|
+
const fileImportsTransformed = [];
|
|
15
|
+
// Performance trick
|
|
16
|
+
if (!code.includes('import'))
|
|
17
|
+
return { noTransformation: true };
|
|
11
18
|
const imports = getImports(code);
|
|
12
19
|
if (imports.length === 0)
|
|
13
|
-
return {
|
|
20
|
+
return { noTransformation: true };
|
|
14
21
|
imports.forEach((node) => {
|
|
15
22
|
if (node.type !== 'ImportDeclaration')
|
|
16
23
|
return;
|
|
17
24
|
const importPath = node.source.value;
|
|
18
25
|
assert(typeof importPath === 'string');
|
|
19
26
|
const { start, end } = node;
|
|
27
|
+
if (isVikeRealImport(code, end))
|
|
28
|
+
return;
|
|
20
29
|
const importStatementCode = code.slice(start, end);
|
|
21
30
|
// No variable imported
|
|
22
31
|
if (node.specifiers.length === 0) {
|
|
@@ -29,7 +38,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
29
38
|
quote = pc.bold(pc.red(quote));
|
|
30
39
|
}
|
|
31
40
|
const errMsg = [
|
|
32
|
-
`As explained in https://vike.dev/header-file the following import in ${
|
|
41
|
+
`As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
|
|
33
42
|
quote
|
|
34
43
|
].join('\n');
|
|
35
44
|
if (!isWarning) {
|
|
@@ -57,7 +66,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
57
66
|
})();
|
|
58
67
|
const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
|
|
59
68
|
replacement += `const ${importLocalName} = '${importString}';`;
|
|
60
|
-
|
|
69
|
+
fileImportsTransformed.push({
|
|
61
70
|
importStatementCode,
|
|
62
71
|
importString,
|
|
63
72
|
importLocalName
|
|
@@ -70,7 +79,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
70
79
|
});
|
|
71
80
|
});
|
|
72
81
|
const codeMod = spliceMany(code, spliceOperations);
|
|
73
|
-
return { code: codeMod,
|
|
82
|
+
return { code: codeMod, fileImportsTransformed, noTransformation: false };
|
|
74
83
|
}
|
|
75
84
|
function getImports(code) {
|
|
76
85
|
const { body } = parse(code, {
|
|
@@ -146,3 +155,10 @@ function indent(str) {
|
|
|
146
155
|
.map((s) => ` ${s}`)
|
|
147
156
|
.join('\n');
|
|
148
157
|
}
|
|
158
|
+
function isVikeRealImport(code, posStart) {
|
|
159
|
+
let posEnd = code.indexOf('\n', posStart);
|
|
160
|
+
if (posEnd === -1)
|
|
161
|
+
posEnd = code.length;
|
|
162
|
+
const lineEnd = code.slice(posStart, posEnd);
|
|
163
|
+
return lineEnd.includes('@vike-real-import');
|
|
164
|
+
}
|
|
@@ -8,14 +8,14 @@ import path from 'path';
|
|
|
8
8
|
import pc from '@brillout/picocolors';
|
|
9
9
|
import { import_ } from '@brillout/import';
|
|
10
10
|
import { assertPosixPath, getRandomId, assertIsNotProductionRuntime, assert, unique, assertWarning, isObject, toPosixPath } from '../../../utils.js';
|
|
11
|
-
import { isImportData,
|
|
11
|
+
import { isImportData, transformImportStatements } from './transformImportStatements.js';
|
|
12
12
|
import { vikeConfigDependencies } from './getVikeConfig.js';
|
|
13
13
|
import 'source-map-support/register.js';
|
|
14
|
-
import {
|
|
14
|
+
import { getConfigFileExport } from './getConfigFileExport.js';
|
|
15
15
|
assertIsNotProductionRuntime();
|
|
16
16
|
async function transpileAndExecuteFile(filePath, isValueFile, userRootDir) {
|
|
17
|
-
const { code,
|
|
18
|
-
const { fileExports } = await executeFile(filePath, code,
|
|
17
|
+
const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
|
|
18
|
+
const { fileExports } = await executeFile(filePath, code, fileImportsTransformed, isValueFile);
|
|
19
19
|
return { fileExports };
|
|
20
20
|
}
|
|
21
21
|
async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
@@ -23,39 +23,40 @@ async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
|
23
23
|
assertPosixPath(filePathAbsoluteFilesystem);
|
|
24
24
|
vikeConfigDependencies.add(filePathAbsoluteFilesystem);
|
|
25
25
|
let code = await transpileWithEsbuild(filePath, isValueFile, userRootDir);
|
|
26
|
-
let
|
|
26
|
+
let fileImportsTransformed = null;
|
|
27
27
|
{
|
|
28
|
-
const res =
|
|
28
|
+
const res = transformImports(code, filePath, isValueFile);
|
|
29
29
|
if (res) {
|
|
30
30
|
code = res.code;
|
|
31
|
-
|
|
31
|
+
fileImportsTransformed = res.fileImportsTransformed;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
return { code,
|
|
34
|
+
return { code, fileImportsTransformed };
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function transformImports(codeOriginal, filePath, isValueFile) {
|
|
37
37
|
// Do we need to remove the imports?
|
|
38
|
-
const { filePathAbsoluteFilesystem
|
|
38
|
+
const { filePathAbsoluteFilesystem } = filePath;
|
|
39
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
39
40
|
assertPosixPath(filePathAbsoluteFilesystem);
|
|
40
41
|
const isHeader = isHeaderFile(filePathAbsoluteFilesystem);
|
|
41
42
|
const isPageConfigFile = !isValueFile;
|
|
42
43
|
if (!isHeader && !isPageConfigFile) {
|
|
43
44
|
return null;
|
|
44
45
|
}
|
|
45
|
-
assertWarning(isPageConfigFile, `${
|
|
46
|
+
assertWarning(isPageConfigFile, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files should only be used for +config.h.js, see https://vike.dev/header-file`, { onlyOnce: true });
|
|
46
47
|
// Remove the imports
|
|
47
|
-
const res =
|
|
48
|
-
if (res.
|
|
48
|
+
const res = transformImportStatements(codeOriginal, filePathToShowToUser2);
|
|
49
|
+
if (res.noTransformation) {
|
|
49
50
|
return null;
|
|
50
51
|
}
|
|
51
|
-
const { code,
|
|
52
|
+
const { code, fileImportsTransformed } = res;
|
|
52
53
|
if (!isHeader) {
|
|
53
|
-
const filePathCorrect = appendHeaderFileExtension(
|
|
54
|
-
assertWarning(false, `Rename ${
|
|
54
|
+
const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
|
|
55
|
+
assertWarning(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
|
|
55
56
|
onlyOnce: true
|
|
56
57
|
});
|
|
57
58
|
}
|
|
58
|
-
return { code,
|
|
59
|
+
return { code, fileImportsTransformed };
|
|
59
60
|
}
|
|
60
61
|
async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
61
62
|
const entryFilePath = filePath.filePathAbsoluteFilesystem;
|
|
@@ -131,7 +132,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
131
132
|
assert(typeof code === 'string');
|
|
132
133
|
return code;
|
|
133
134
|
}
|
|
134
|
-
async function executeFile(filePath, code,
|
|
135
|
+
async function executeFile(filePath, code, fileImportsTransformed, isValueFile) {
|
|
135
136
|
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
136
137
|
// Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
|
|
137
138
|
// - But seems to break source maps, so I don't think it's worth it
|
|
@@ -156,10 +157,10 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
|
|
|
156
157
|
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
157
158
|
// - We don't need this special object
|
|
158
159
|
fileExports = { ...fileExports };
|
|
159
|
-
if (
|
|
160
|
+
if (fileImportsTransformed && !isValueFile) {
|
|
160
161
|
assert(filePathRelativeToUserRootDir !== undefined);
|
|
161
|
-
const
|
|
162
|
-
assertImportsAreReExported(
|
|
162
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
163
|
+
assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
|
|
163
164
|
}
|
|
164
165
|
return { fileExports };
|
|
165
166
|
}
|
|
@@ -205,29 +206,29 @@ function isTmpFile(filePath) {
|
|
|
205
206
|
const fileName = path.posix.basename(filePath);
|
|
206
207
|
return fileName.startsWith(tmpPrefix);
|
|
207
208
|
}
|
|
208
|
-
function assertImportsAreReExported(
|
|
209
|
-
|
|
210
|
-
const exportedStrings = getExportedStrings(
|
|
209
|
+
function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
|
|
210
|
+
const fileExport = getConfigFileExport(fileExports, filePathToShowToUser2);
|
|
211
|
+
const exportedStrings = getExportedStrings(fileExport);
|
|
211
212
|
Object.values(exportedStrings).forEach((exportVal) => {
|
|
212
213
|
if (typeof exportVal !== 'string')
|
|
213
214
|
return;
|
|
214
215
|
if (!isImportData(exportVal))
|
|
215
216
|
return;
|
|
216
217
|
const importString = exportVal;
|
|
217
|
-
|
|
218
|
+
fileImportsTransformed.forEach((fileImport) => {
|
|
218
219
|
if (fileImport.importString === importString) {
|
|
219
220
|
fileImport.isReExported = true;
|
|
220
221
|
}
|
|
221
222
|
});
|
|
222
223
|
});
|
|
223
|
-
const
|
|
224
|
-
if (
|
|
224
|
+
const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
|
|
225
|
+
if (fileImportsTransformedUnused.length === 0)
|
|
225
226
|
return;
|
|
226
|
-
const importStatements = unique(
|
|
227
|
-
const importNamesUnused =
|
|
228
|
-
const singular =
|
|
229
|
-
assertWarning(
|
|
230
|
-
`${
|
|
227
|
+
const importStatements = unique(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
|
|
228
|
+
const importNamesUnused = fileImportsTransformedUnused.map((fi) => pc.cyan(fi.importLocalName)).join(', ');
|
|
229
|
+
const singular = fileImportsTransformedUnused.length === 1;
|
|
230
|
+
assertWarning(fileImportsTransformedUnused.length === 0, [
|
|
231
|
+
`${filePathToShowToUser2} imports the following:`,
|
|
231
232
|
...importStatements.map((s) => pc.cyan(` ${s}`)),
|
|
232
233
|
`But the import${singular ? '' : 's'} ${importNamesUnused} ${singular ? "isn't" : "aren't"} re-exported at ${pc.cyan('export default { ... }')} and therefore ${singular ? 'has' : 'have'} no effect, see explanation at https://vike.dev/header-file`
|
|
233
234
|
].join('\n'), { onlyOnce: true });
|
|
@@ -269,10 +270,19 @@ function triggerPrepareStackTrace(err) {
|
|
|
269
270
|
}
|
|
270
271
|
}
|
|
271
272
|
function getErrIntroMsg(operation, filePath) {
|
|
273
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
272
274
|
const msg = [
|
|
275
|
+
// prettier ignore
|
|
273
276
|
pc.red(`Failed to ${operation}`),
|
|
274
|
-
pc.bold(pc.red(
|
|
277
|
+
pc.bold(pc.red(filePathToShowToUser2)),
|
|
275
278
|
pc.red(`because:`)
|
|
276
279
|
].join(' ');
|
|
277
280
|
return msg;
|
|
278
281
|
}
|
|
282
|
+
/** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
|
|
283
|
+
function getFilePathToShowToUser2(filePath) {
|
|
284
|
+
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
285
|
+
const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
|
|
286
|
+
assert(filePathToShowToUser2);
|
|
287
|
+
return filePathToShowToUser2;
|
|
288
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { previewConfig };
|
|
2
|
-
import { assertUsage, getOutDirs, resolveOutDir,
|
|
2
|
+
import { assertUsage, getOutDirs, resolveOutDir, markEnvAsVitePreview } from '../utils.js';
|
|
3
3
|
import { getConfigVike } from '../../shared/getConfigVike.js';
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
import path from 'path';
|
|
@@ -28,7 +28,7 @@ function previewConfig() {
|
|
|
28
28
|
- This ugly hack to set appType for preview won't be need once https://github.com/vitejs/vite/pull/14855 is merged.
|
|
29
29
|
config.appType = 'mpa'
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
markEnvAsVitePreview();
|
|
32
32
|
return () => {
|
|
33
33
|
assertDist();
|
|
34
34
|
/* We don't use this condition (we wrongfully always use the SSR middleware) because of the regression introduced by https://github.com/vitejs/vite/pull/14756 which stops servering .html files when `appType: 'custom'`.
|
|
@@ -20,10 +20,10 @@ function logWithVikeTag(msg, logType, category, showVikeVersion = false) {
|
|
|
20
20
|
function getProjectTag(showVikeVersion) {
|
|
21
21
|
let projectTag;
|
|
22
22
|
if (showVikeVersion) {
|
|
23
|
-
projectTag = `[
|
|
23
|
+
projectTag = `[vike@${projectInfo.projectVersion}]`;
|
|
24
24
|
}
|
|
25
25
|
else {
|
|
26
|
-
projectTag = `[
|
|
26
|
+
projectTag = `[vike]`;
|
|
27
27
|
}
|
|
28
28
|
return projectTag;
|
|
29
29
|
}
|
|
@@ -83,7 +83,7 @@ function prependTags(msg, projectTag, category, logType) {
|
|
|
83
83
|
return pc.yellow(s);
|
|
84
84
|
if (projectTag === '[vite]')
|
|
85
85
|
return pc.bold(pc.cyan(s));
|
|
86
|
-
if (projectTag.startsWith(`[
|
|
86
|
+
if (projectTag.startsWith(`[vike`))
|
|
87
87
|
return pc.bold(pc.cyan(s));
|
|
88
88
|
assert(false);
|
|
89
89
|
};
|
|
@@ -6,7 +6,7 @@ export { setGlobalContext_viteDevServer };
|
|
|
6
6
|
export { setGlobalContext_vitePreviewServer };
|
|
7
7
|
export { setGlobalContext_viteConfig };
|
|
8
8
|
export { getRuntimeManifest };
|
|
9
|
-
import { assert, assertUsage,
|
|
9
|
+
import { assert, assertUsage, getGlobalObject, getNodeEnv, isNodeEnvDev, isPlainObject, objectAssign } from './utils.js';
|
|
10
10
|
import { loadImportBuild } from './globalContext/loadImportBuild.js';
|
|
11
11
|
import { setPageFiles } from '../../shared/getPageFiles.js';
|
|
12
12
|
import { assertPluginManifest } from '../shared/assertPluginManifest.js';
|
|
@@ -151,7 +151,8 @@ function assertNodeEnv(hasViteDevServer) {
|
|
|
151
151
|
const nodeEnv = getNodeEnv();
|
|
152
152
|
if (nodeEnv === null || nodeEnv === 'test')
|
|
153
153
|
return;
|
|
154
|
-
const isDevNodeEnv =
|
|
155
|
-
//
|
|
156
|
-
|
|
154
|
+
const isDevNodeEnv = isNodeEnvDev();
|
|
155
|
+
// - 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
|
|
156
|
+
// - 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).
|
|
157
|
+
assertUsage(hasViteDevServer === isDevNodeEnv, `Vite's development server was${hasViteDevServer ? '' : "n't"} instantiated while the environment is set to be a ${isDevNodeEnv ? 'development' : 'production'} environment by ${pc.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`)} which is contradictory, see https://vike.dev/NODE_ENV`);
|
|
157
158
|
}
|
|
@@ -53,6 +53,10 @@ type PageConfigGlobalRuntime = {
|
|
|
53
53
|
type PageConfigGlobalBuildTime = {
|
|
54
54
|
configValueSources: ConfigValueSources;
|
|
55
55
|
};
|
|
56
|
+
/** In what environment(s) the config value is loaded.
|
|
57
|
+
*
|
|
58
|
+
* https://vike.dev/meta
|
|
59
|
+
*/
|
|
56
60
|
type ConfigEnv = {
|
|
57
61
|
client?: boolean;
|
|
58
62
|
server?: boolean;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export { assertPlusFileExport };
|
|
2
|
+
import { assert, assertUsage, assertWarning } from '../utils.js';
|
|
3
|
+
import pc from '@brillout/picocolors';
|
|
4
|
+
const EXPORTS_IGNORE = [
|
|
5
|
+
// vite-plugin-solid adds `export { $$registrations }`
|
|
6
|
+
'$$registrations',
|
|
7
|
+
// @vitejs/plugin-vue adds `export { _rerender_only }`
|
|
8
|
+
'_rerender_only'
|
|
9
|
+
];
|
|
10
|
+
// Tolerate `export { frontmatter }` in .mdx files
|
|
11
|
+
const TOLERATE_SIDE_EXPORTS = ['.md', '.mdx'];
|
|
12
|
+
function assertPlusFileExport(fileExports, filePathToShowToUser, configName) {
|
|
13
|
+
const exportsAll = Object.keys(fileExports).filter((exportName) => !EXPORTS_IGNORE.includes(exportName));
|
|
14
|
+
const exportsInvalid = exportsAll.filter((e) => e !== 'default' && e !== configName);
|
|
15
|
+
if (exportsInvalid.length === 0) {
|
|
16
|
+
if (exportsAll.length === 1) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const exportDefault = pc.cyan('export default');
|
|
20
|
+
const exportNamed = pc.cyan(`export { ${configName} }`);
|
|
21
|
+
if (exportsAll.length === 0) {
|
|
22
|
+
assertUsage(false, `${filePathToShowToUser} doesn't export any value, but it should have a ${exportNamed} or ${exportDefault}`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
assert(exportsAll.length === 2); // because `exportsInvalid.length === 0`
|
|
26
|
+
assertWarning(false, `${filePathToShowToUser} remove ${exportNamed} or ${exportDefault}`, {
|
|
27
|
+
onlyOnce: true
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (TOLERATE_SIDE_EXPORTS.some((ext) => filePathToShowToUser.endsWith(ext)))
|
|
33
|
+
return;
|
|
34
|
+
exportsInvalid.forEach((exportInvalid) => {
|
|
35
|
+
assertWarning(false, `${filePathToShowToUser} should have only one export: move ${pc.cyan(`export { ${exportInvalid} }`)} to its own +${exportsInvalid}.js file`, { onlyOnce: true });
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { parseConfigValuesImported };
|
|
2
2
|
import { assert } from '../../utils.js';
|
|
3
|
-
import {
|
|
3
|
+
import { assertPlusFileExport } from '../assertPlusFileExport.js';
|
|
4
4
|
function parseConfigValuesImported(configValuesImported) {
|
|
5
5
|
const configValues = {};
|
|
6
6
|
const addConfigValue = (configName, value, importPath, exportName) => {
|
|
@@ -21,7 +21,7 @@ function parseConfigValuesImported(configValuesImported) {
|
|
|
21
21
|
if (configValueLoaded.isValueFile) {
|
|
22
22
|
const { exportValues, importPath, configName } = configValueLoaded;
|
|
23
23
|
if (configName !== 'client') {
|
|
24
|
-
|
|
24
|
+
assertPlusFileExport(exportValues, importPath, configName);
|
|
25
25
|
}
|
|
26
26
|
Object.entries(exportValues).forEach(([exportName, exportValue]) => {
|
|
27
27
|
const isSideExport = exportName !== 'default'; // .md files may have "side-exports" such as `export { frontmatter }`
|
|
@@ -7,7 +7,7 @@ export { logAbortErrorHandled };
|
|
|
7
7
|
export { getPageContextFromAllRewrites };
|
|
8
8
|
export { AbortRender };
|
|
9
9
|
export { assertNoInfiniteAbortLoop };
|
|
10
|
-
import { assert, assertInfo, assertUsage, assertWarning, checkType, hasProp, isUriWithProtocol, isUserHookError, joinEnglish, objectAssign,
|
|
10
|
+
import { assert, assertInfo, assertUsage, assertWarning, checkType, hasProp, isUriWithProtocol, isUserHookError, joinEnglish, objectAssign, truncateString } from './utils.js';
|
|
11
11
|
import pc from '@brillout/picocolors';
|
|
12
12
|
/**
|
|
13
13
|
* Abort the rendering of the current page, and redirect the user to another URL instead.
|
|
@@ -132,7 +132,7 @@ function logAbortErrorHandled(err, isProduction, pageContext) {
|
|
|
132
132
|
}
|
|
133
133
|
function assertStatusCode(statusCode, expected, caller) {
|
|
134
134
|
const expectedEnglish = joinEnglish(expected.map((s) => s.toString()), 'or');
|
|
135
|
-
assertWarning(expected.includes(statusCode), `Unepexected status code ${statusCode} passed to ${caller}(), we recommend ${expectedEnglish} instead. (Or reach out at
|
|
135
|
+
assertWarning(expected.includes(statusCode), `Unepexected status code ${statusCode} passed to ${caller}(), we recommend ${expectedEnglish} instead. (Or reach out at https://github.com/vikejs/vike/issues/1008 if you believe ${statusCode} should be added.)`, { onlyOnce: true });
|
|
136
136
|
}
|
|
137
137
|
function getPageContextFromAllRewrites(pageContextsFromRewrite) {
|
|
138
138
|
assertNoInfiniteLoop(pageContextsFromRewrite);
|