vike 0.4.159 → 0.4.160
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/loadFileConfigEnv.js +129 -0
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +139 -0
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformImports.js} +44 -13
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +110 -77
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +57 -278
- package/dist/cjs/node/plugin/plugins/previewConfig.js +1 -1
- package/dist/cjs/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
- package/dist/cjs/node/plugin/shared/loggerNotProd/log.js +3 -3
- package/dist/cjs/node/plugin/shared/loggerNotProd.js +1 -1
- 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/loadFileConfigEnv.d.ts +21 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.js +123 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.d.ts +12 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js +133 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → getVikeConfig/transformImports.d.ts} +5 -5
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformImports.js} +43 -12
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.d.ts → getVikeConfig/transpileAndExecuteFile.d.ts} +3 -3
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +110 -77
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +14 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +54 -275
- package/dist/esm/node/plugin/plugins/previewConfig.js +2 -2
- package/dist/esm/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
- package/dist/esm/node/plugin/shared/loggerNotProd/log.js +3 -3
- package/dist/esm/node/plugin/shared/loggerNotProd.js +1 -1
- 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/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
|
@@ -28,7 +28,8 @@ const baseUrls_js_1 = require("./plugins/baseUrls.js");
|
|
|
28
28
|
const envVars_js_1 = require("./plugins/envVars.js");
|
|
29
29
|
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
30
30
|
const fileEnv_js_1 = require("./plugins/fileEnv.js");
|
|
31
|
-
(
|
|
31
|
+
assertNodeEnv();
|
|
32
|
+
(0, utils_js_2.markEnvAsVikePluginLoaded)();
|
|
32
33
|
// Return as `any` to avoid Plugin type mismatches when there are multiple Vite versions installed
|
|
33
34
|
function plugin(vikeConfig) {
|
|
34
35
|
const plugins = [
|
|
@@ -70,3 +71,21 @@ Object.defineProperty(plugin, 'apply', {
|
|
|
70
71
|
(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 });
|
|
71
72
|
}
|
|
72
73
|
});
|
|
74
|
+
function assertNodeEnv() {
|
|
75
|
+
const nodeEnv = (0, utils_js_2.getNodeEnv)();
|
|
76
|
+
if (nodeEnv === 'test')
|
|
77
|
+
return;
|
|
78
|
+
// 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).
|
|
79
|
+
(0, utils_js_2.assertUsage)(
|
|
80
|
+
/* 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`).
|
|
81
|
+
isNodeEnvDev() || isVikeCliBuild(),
|
|
82
|
+
/*/
|
|
83
|
+
(0, utils_js_2.isNodeEnvDev)() || true,
|
|
84
|
+
///*/
|
|
85
|
+
[
|
|
86
|
+
picocolors_1.default.cyan(`process.env.NODE_ENV === ${JSON.stringify(nodeEnv)}`),
|
|
87
|
+
'(which Vike interprets as a non-development environment https://vike.dev/NODE_ENV)',
|
|
88
|
+
'while the vike/plugin module is loaded.',
|
|
89
|
+
utils_js_2.vikeVitePluginLoadedInProductionError
|
|
90
|
+
].join(' '));
|
|
91
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getConfigFileExport = void 0;
|
|
7
|
+
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
8
|
+
const assertPlusFileExport_js_1 = require("../../../../../shared/page-configs/assertPlusFileExport.js");
|
|
9
|
+
const utils_js_1 = require("../../../utils.js");
|
|
10
|
+
function getConfigFileExport(fileExports, filePathToShowToUser) {
|
|
11
|
+
(0, assertPlusFileExport_js_1.assertPlusFileExport)(fileExports, filePathToShowToUser, 'config');
|
|
12
|
+
const fileExport = fileExports.default || fileExports.config;
|
|
13
|
+
(0, utils_js_1.assert)('default' in fileExports !== 'config' in fileExports);
|
|
14
|
+
const exportName = picocolors_1.default.cyan('default' in fileExports ? 'export default' : 'export { config }');
|
|
15
|
+
(0, utils_js_1.assertUsage)((0, utils_js_1.isObject)(fileExport), `The ${exportName} of ${filePathToShowToUser} should be an object (but it's ${picocolors_1.default.cyan(`typeof exportedValue === ${JSON.stringify(typeof fileExport)}`)} instead)`);
|
|
16
|
+
return fileExport;
|
|
17
|
+
}
|
|
18
|
+
exports.getConfigFileExport = getConfigFileExport;
|
package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileConfigEnv.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Files loadded at config time:
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.loadConfigFile = exports.loadValueFile = exports.loadImportedFile = void 0;
|
|
8
|
+
const utils_js_1 = require("../../../../utils.js");
|
|
9
|
+
const transpileAndExecuteFile_js_1 = require("./transpileAndExecuteFile.js");
|
|
10
|
+
const assertPlusFileExport_js_1 = require("../../../../../../shared/page-configs/assertPlusFileExport.js");
|
|
11
|
+
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
12
|
+
const transformImports_js_1 = require("./transformImports.js");
|
|
13
|
+
const getConfigFileExport_js_1 = require("../getConfigFileExport.js");
|
|
14
|
+
const resolveImportPath_js_1 = require("./resolveImportPath.js");
|
|
15
|
+
(0, utils_js_1.assertIsNotProductionRuntime)();
|
|
16
|
+
// Load fake import
|
|
17
|
+
async function loadImportedFile(import_, userRootDir, importedFilesLoaded) {
|
|
18
|
+
const f = import_.filePathAbsoluteFilesystem;
|
|
19
|
+
if (!importedFilesLoaded[f]) {
|
|
20
|
+
importedFilesLoaded[f] = (0, transpileAndExecuteFile_js_1.transpileAndExecuteFile)(import_, true, userRootDir).then((r) => r.fileExports);
|
|
21
|
+
}
|
|
22
|
+
const fileExports = await importedFilesLoaded[f];
|
|
23
|
+
const fileExport = fileExports[import_.fileExportName];
|
|
24
|
+
return fileExport;
|
|
25
|
+
}
|
|
26
|
+
exports.loadImportedFile = loadImportedFile;
|
|
27
|
+
// Load +{configName}.js
|
|
28
|
+
async function loadValueFile(interfaceValueFile, configName, userRootDir) {
|
|
29
|
+
const { fileExports } = await (0, transpileAndExecuteFile_js_1.transpileAndExecuteFile)(interfaceValueFile.filePath, true, userRootDir);
|
|
30
|
+
const { filePathToShowToUser } = interfaceValueFile.filePath;
|
|
31
|
+
(0, assertPlusFileExport_js_1.assertPlusFileExport)(fileExports, filePathToShowToUser, configName);
|
|
32
|
+
Object.entries(fileExports).forEach(([exportName, configValue]) => {
|
|
33
|
+
const configName_ = exportName === 'default' ? configName : exportName;
|
|
34
|
+
interfaceValueFile.fileExportsByConfigName[configName_] = { configValue };
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
exports.loadValueFile = loadValueFile;
|
|
38
|
+
// Load +config.js, including all its extends fake imports
|
|
39
|
+
async function loadConfigFile(configFilePath, userRootDir, visited, isConfigOfExtension) {
|
|
40
|
+
const { filePathAbsoluteFilesystem } = configFilePath;
|
|
41
|
+
assertNoInfiniteLoop(visited, filePathAbsoluteFilesystem);
|
|
42
|
+
const { fileExports } = await (0, transpileAndExecuteFile_js_1.transpileAndExecuteFile)(configFilePath, false, userRootDir, isConfigOfExtension);
|
|
43
|
+
const { extendsConfigs, extendsFilePaths } = await loadExtendsConfigs(fileExports, configFilePath, userRootDir, [
|
|
44
|
+
...visited,
|
|
45
|
+
filePathAbsoluteFilesystem
|
|
46
|
+
]);
|
|
47
|
+
const configFile = {
|
|
48
|
+
fileExports,
|
|
49
|
+
filePath: configFilePath,
|
|
50
|
+
extendsFilePaths
|
|
51
|
+
};
|
|
52
|
+
return { configFile, extendsConfigs };
|
|
53
|
+
}
|
|
54
|
+
exports.loadConfigFile = loadConfigFile;
|
|
55
|
+
function assertNoInfiniteLoop(visited, filePathAbsoluteFilesystem) {
|
|
56
|
+
const idx = visited.indexOf(filePathAbsoluteFilesystem);
|
|
57
|
+
if (idx === -1)
|
|
58
|
+
return;
|
|
59
|
+
const loop = visited.slice(idx);
|
|
60
|
+
(0, utils_js_1.assert)(loop[0] === filePathAbsoluteFilesystem);
|
|
61
|
+
(0, utils_js_1.assertUsage)(idx === -1, `Infinite extends loop ${[...loop, filePathAbsoluteFilesystem].join('>')}`);
|
|
62
|
+
}
|
|
63
|
+
async function loadExtendsConfigs(configFileExports, configFilePath, userRootDir, visited) {
|
|
64
|
+
const extendsImportData = getExtendsImportData(configFileExports, configFilePath);
|
|
65
|
+
const extendsConfigFiles = [];
|
|
66
|
+
extendsImportData.map((importData) => {
|
|
67
|
+
const { importPath: importPath } = importData;
|
|
68
|
+
const filePathAbsoluteFilesystem = (0, resolveImportPath_js_1.resolveImportPath)(importData, configFilePath);
|
|
69
|
+
(0, resolveImportPath_js_1.assertImportPath)(filePathAbsoluteFilesystem, importData, configFilePath);
|
|
70
|
+
warnUserLandExtension(importPath, configFilePath);
|
|
71
|
+
// - filePathRelativeToUserRootDir has no functionality beyond nicer error messages for user
|
|
72
|
+
// - Using importPath would be visually nicer but it's ambigous => we rather pick filePathAbsoluteFilesystem for added clarity
|
|
73
|
+
const filePathRelativeToUserRootDir = determineFilePathRelativeToUserDir(filePathAbsoluteFilesystem, userRootDir);
|
|
74
|
+
const filePathAbsoluteVite = filePathRelativeToUserRootDir ?? importPath;
|
|
75
|
+
extendsConfigFiles.push({
|
|
76
|
+
filePathAbsoluteFilesystem,
|
|
77
|
+
filePathAbsoluteVite,
|
|
78
|
+
filePathRelativeToUserRootDir,
|
|
79
|
+
filePathToShowToUser: filePathAbsoluteVite,
|
|
80
|
+
importPathAbsolute: importPath
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
const extendsConfigs = [];
|
|
84
|
+
await Promise.all(extendsConfigFiles.map(async (configFilePath) => {
|
|
85
|
+
const result = await loadConfigFile(configFilePath, userRootDir, visited, true);
|
|
86
|
+
extendsConfigs.push(result.configFile);
|
|
87
|
+
extendsConfigs.push(...result.extendsConfigs);
|
|
88
|
+
}));
|
|
89
|
+
const extendsFilePaths = extendsConfigFiles.map((f) => f.filePathAbsoluteFilesystem);
|
|
90
|
+
return { extendsConfigs, extendsFilePaths };
|
|
91
|
+
}
|
|
92
|
+
function determineFilePathRelativeToUserDir(filePathAbsoluteFilesystem, userRootDir) {
|
|
93
|
+
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
94
|
+
(0, utils_js_1.assertPosixPath)(userRootDir);
|
|
95
|
+
if (!filePathAbsoluteFilesystem.startsWith(userRootDir)) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
let filePathRelativeToUserRootDir = filePathAbsoluteFilesystem.slice(userRootDir.length);
|
|
99
|
+
if (!filePathRelativeToUserRootDir.startsWith('/'))
|
|
100
|
+
filePathRelativeToUserRootDir = '/' + filePathRelativeToUserRootDir;
|
|
101
|
+
return filePathRelativeToUserRootDir;
|
|
102
|
+
}
|
|
103
|
+
function warnUserLandExtension(importPath, configFilePath) {
|
|
104
|
+
(0, utils_js_1.assertWarning)((0, utils_js_1.isNpmPackageImport)(importPath), `${configFilePath.filePathToShowToUser} uses ${picocolors_1.default.cyan('extends')} to inherit from ${picocolors_1.default.cyan(importPath)} which is a user-land file: this is experimental and may be remove at any time. Reach out to a maintainer if you need this.`, { onlyOnce: true });
|
|
105
|
+
}
|
|
106
|
+
function getExtendsImportData(configFileExports, configFilePath) {
|
|
107
|
+
const { filePathToShowToUser } = configFilePath;
|
|
108
|
+
const configFileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(configFileExports, filePathToShowToUser);
|
|
109
|
+
const wrongUsage = `${filePathToShowToUser} sets the config ${picocolors_1.default.cyan('extends')} to an invalid value, see https://vike.dev/extends`;
|
|
110
|
+
let extendList;
|
|
111
|
+
if (!('extends' in configFileExport)) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
else if ((0, utils_js_1.hasProp)(configFileExport, 'extends', 'string')) {
|
|
115
|
+
extendList = [configFileExport.extends];
|
|
116
|
+
}
|
|
117
|
+
else if ((0, utils_js_1.hasProp)(configFileExport, 'extends', 'string[]')) {
|
|
118
|
+
extendList = configFileExport.extends;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
(0, utils_js_1.assertUsage)(false, wrongUsage);
|
|
122
|
+
}
|
|
123
|
+
const extendsImportData = extendList.map((importDataSerialized) => {
|
|
124
|
+
const importData = (0, transformImports_js_1.parseImportData)(importDataSerialized);
|
|
125
|
+
(0, utils_js_1.assertUsage)(importData, wrongUsage);
|
|
126
|
+
return importData;
|
|
127
|
+
});
|
|
128
|
+
return extendsImportData;
|
|
129
|
+
}
|
package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveImportPath.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.clearFilesEnvMap = exports.assertImportPath = exports.resolveImportPath = exports.resolveImport = void 0;
|
|
7
|
+
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
8
|
+
const utils_js_1 = require("../../../../utils.js");
|
|
9
|
+
const transformImports_js_1 = require("./transformImports.js");
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const filesEnvMap = new Map();
|
|
12
|
+
function resolveImport(configValue, importerFilePath, userRootDir, configEnv, configName) {
|
|
13
|
+
if (typeof configValue !== 'string')
|
|
14
|
+
return null;
|
|
15
|
+
const importData = (0, transformImports_js_1.parseImportData)(configValue);
|
|
16
|
+
if (!importData)
|
|
17
|
+
return null;
|
|
18
|
+
const { importPath, exportName } = importData;
|
|
19
|
+
const filePathAbsoluteFilesystem = resolveImportPath(importData, importerFilePath);
|
|
20
|
+
assertFileEnv(filePathAbsoluteFilesystem ?? importPath, configEnv, configName);
|
|
21
|
+
const fileExportPathToShowToUser = exportName === 'default' || exportName === configName ? [] : [exportName];
|
|
22
|
+
if (importPath.startsWith('.')) {
|
|
23
|
+
// We need to resolve relative paths into absolute paths. Because the import paths are included in virtual files:
|
|
24
|
+
// ```
|
|
25
|
+
// [vite] Internal server error: Failed to resolve import "./onPageTransitionHooks" from "virtual:vike:pageConfigValuesAll:client:/pages/index". Does the file exist?
|
|
26
|
+
// ```
|
|
27
|
+
assertImportPath(filePathAbsoluteFilesystem, importData, importerFilePath);
|
|
28
|
+
const filePathRelativeToUserRootDir = resolveImportPath_relativeToUserRootDir(filePathAbsoluteFilesystem, importData, importerFilePath, userRootDir);
|
|
29
|
+
const filePath = {
|
|
30
|
+
filePathAbsoluteFilesystem,
|
|
31
|
+
filePathRelativeToUserRootDir,
|
|
32
|
+
filePathAbsoluteVite: filePathRelativeToUserRootDir,
|
|
33
|
+
filePathToShowToUser: filePathRelativeToUserRootDir,
|
|
34
|
+
importPathAbsolute: null
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
...filePath,
|
|
38
|
+
fileExportName: exportName,
|
|
39
|
+
fileExportPathToShowToUser
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
// importPath can be:
|
|
44
|
+
// - an npm package import
|
|
45
|
+
// - a path alias
|
|
46
|
+
const filePath = {
|
|
47
|
+
filePathAbsoluteFilesystem,
|
|
48
|
+
filePathRelativeToUserRootDir: null,
|
|
49
|
+
filePathAbsoluteVite: importPath,
|
|
50
|
+
filePathToShowToUser: importPath,
|
|
51
|
+
importPathAbsolute: importPath
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
...filePath,
|
|
55
|
+
fileExportName: exportName,
|
|
56
|
+
fileExportPathToShowToUser
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.resolveImport = resolveImport;
|
|
61
|
+
function resolveImportPath_relativeToUserRootDir(filePathAbsoluteFilesystem, importData, configFilePath, userRootDir) {
|
|
62
|
+
(0, utils_js_1.assertPosixPath)(userRootDir);
|
|
63
|
+
let filePathRelativeToUserRootDir;
|
|
64
|
+
if (filePathAbsoluteFilesystem.startsWith(userRootDir)) {
|
|
65
|
+
filePathRelativeToUserRootDir = getVitePathFromAbsolutePath(filePathAbsoluteFilesystem, userRootDir);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
(0, utils_js_1.assertUsage)(false, `${configFilePath.filePathToShowToUser} imports from a relative path ${picocolors_1.default.cyan(importData.importPath)} outside of ${userRootDir} which is forbidden: import from a relative path inside ${userRootDir}, or import from a dependency's package.json#exports entry instead`);
|
|
69
|
+
// None of the following works. Seems to be a Vite bug?
|
|
70
|
+
// /*
|
|
71
|
+
// assert(filePathAbsoluteFilesystem.startsWith('/'))
|
|
72
|
+
// filePath = `/@fs${filePathAbsoluteFilesystem}`
|
|
73
|
+
// /*/
|
|
74
|
+
// filePathRelativeToUserRootDir = path.posix.relative(userRootDir, filePathAbsoluteFilesystem)
|
|
75
|
+
// assert(filePathRelativeToUserRootDir.startsWith('../'))
|
|
76
|
+
// filePathRelativeToUserRootDir = '/' + filePathRelativeToUserRootDir
|
|
77
|
+
// //*/
|
|
78
|
+
}
|
|
79
|
+
(0, utils_js_1.assertPosixPath)(filePathRelativeToUserRootDir);
|
|
80
|
+
(0, utils_js_1.assert)(filePathRelativeToUserRootDir.startsWith('/'));
|
|
81
|
+
return filePathRelativeToUserRootDir;
|
|
82
|
+
}
|
|
83
|
+
function resolveImportPath(importData, importerFilePath) {
|
|
84
|
+
const importerFilePathAbsolute = importerFilePath.filePathAbsoluteFilesystem;
|
|
85
|
+
(0, utils_js_1.assertPosixPath)(importerFilePathAbsolute);
|
|
86
|
+
const cwd = path_1.default.posix.dirname(importerFilePathAbsolute);
|
|
87
|
+
// We can't use import.meta.resolve() as of Junary 2023 (and probably for a lot longer): https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules#comment137174954_62272600:~:text=But%20the%20argument%20parent%20(aka%20cwd)%20still%20requires%20a%20flag
|
|
88
|
+
// filePathAbsoluteFilesystem is expected to be null when importData.importPath is a Vite path alias
|
|
89
|
+
const filePathAbsoluteFilesystem = (0, utils_js_1.requireResolve)(importData.importPath, cwd);
|
|
90
|
+
return filePathAbsoluteFilesystem;
|
|
91
|
+
}
|
|
92
|
+
exports.resolveImportPath = resolveImportPath;
|
|
93
|
+
function assertImportPath(filePathAbsoluteFilesystem, importData, importerFilePath) {
|
|
94
|
+
const { importPath: importPath, importStringWasGenerated, importString } = importData;
|
|
95
|
+
const { filePathToShowToUser } = importerFilePath;
|
|
96
|
+
if (!filePathAbsoluteFilesystem) {
|
|
97
|
+
const importPathString = picocolors_1.default.cyan(`'${importPath}'`);
|
|
98
|
+
const errIntro = importStringWasGenerated
|
|
99
|
+
? `The import path ${importPathString} in ${filePathToShowToUser}`
|
|
100
|
+
: `The import ${picocolors_1.default.cyan(importString)} defined in ${filePathToShowToUser}`;
|
|
101
|
+
const errIntro2 = `${errIntro} couldn't be resolved: does ${importPathString}`;
|
|
102
|
+
if (importPath.startsWith('.')) {
|
|
103
|
+
(0, utils_js_1.assertUsage)(false, `${errIntro2} point to an existing file?`);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
(0, utils_js_1.assertUsage)(false, `${errIntro2} exist?`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.assertImportPath = assertImportPath;
|
|
111
|
+
function assertFileEnv(filePathForEnvCheck, configEnv, configName) {
|
|
112
|
+
(0, utils_js_1.assertPosixPath)(filePathForEnvCheck);
|
|
113
|
+
if (!filesEnvMap.has(filePathForEnvCheck)) {
|
|
114
|
+
filesEnvMap.set(filePathForEnvCheck, []);
|
|
115
|
+
}
|
|
116
|
+
const fileEnv = filesEnvMap.get(filePathForEnvCheck);
|
|
117
|
+
fileEnv.push({ configEnv, configName });
|
|
118
|
+
const configDifferentEnv = fileEnv.filter((c) => !(0, utils_js_1.deepEqual)(c.configEnv, configEnv))[0];
|
|
119
|
+
if (configDifferentEnv) {
|
|
120
|
+
(0, utils_js_1.assertUsage)(false, [
|
|
121
|
+
`${filePathForEnvCheck} defines the value of configs living in different environments:`,
|
|
122
|
+
...[configDifferentEnv, { configName, configEnv }].map((c) => ` - config ${picocolors_1.default.cyan(c.configName)} which value lives in environment ${picocolors_1.default.cyan(JSON.stringify(c.configEnv))}`),
|
|
123
|
+
'Defining config values in the same file is allowed only if they live in the same environment, see https://vike.dev/header-file'
|
|
124
|
+
].join('\n'));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function clearFilesEnvMap() {
|
|
128
|
+
filesEnvMap.clear();
|
|
129
|
+
}
|
|
130
|
+
exports.clearFilesEnvMap = clearFilesEnvMap;
|
|
131
|
+
function getVitePathFromAbsolutePath(filePathAbsoluteFilesystem, root) {
|
|
132
|
+
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
133
|
+
(0, utils_js_1.assertPosixPath)(root);
|
|
134
|
+
(0, utils_js_1.assert)(filePathAbsoluteFilesystem.startsWith(root));
|
|
135
|
+
let vitePath = path_1.default.posix.relative(root, filePathAbsoluteFilesystem);
|
|
136
|
+
(0, utils_js_1.assert)(!vitePath.startsWith('/') && !vitePath.startsWith('.'));
|
|
137
|
+
vitePath = '/' + vitePath;
|
|
138
|
+
return vitePath;
|
|
139
|
+
}
|
|
@@ -3,22 +3,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isImportData = exports.parseImportData = exports.
|
|
6
|
+
exports.isImportData = exports.parseImportData = exports.transformImports = void 0;
|
|
7
7
|
// Playground: https://github.com/brillout/acorn-playground
|
|
8
|
+
// Import attributes support: https://github.com/acornjs/acorn/issues/983
|
|
9
|
+
// - Isn't stage 4 yet: https://github.com/tc39/proposal-import-attributes
|
|
8
10
|
const acorn_1 = require("acorn");
|
|
9
|
-
const utils_js_1 = require("
|
|
11
|
+
const utils_js_1 = require("../../../../utils.js");
|
|
10
12
|
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
11
|
-
function
|
|
13
|
+
function transformImports(code, filePathToShowToUser2,
|
|
14
|
+
// For ./transformImports.spec.ts
|
|
15
|
+
skipWarnings) {
|
|
12
16
|
const spliceOperations = [];
|
|
13
|
-
const
|
|
17
|
+
const fileImportsTransformed = [];
|
|
18
|
+
// Performance trick
|
|
19
|
+
if (!code.includes('import'))
|
|
20
|
+
return { noTransformation: true };
|
|
14
21
|
const imports = getImports(code);
|
|
15
22
|
if (imports.length === 0)
|
|
16
|
-
return {
|
|
23
|
+
return { noTransformation: true };
|
|
17
24
|
imports.forEach((node) => {
|
|
18
25
|
if (node.type !== 'ImportDeclaration')
|
|
19
26
|
return;
|
|
20
27
|
const importPath = node.source.value;
|
|
21
28
|
(0, utils_js_1.assert)(typeof importPath === 'string');
|
|
29
|
+
// - This doesn't work. To make it work we would need to run esbuild twice: esbuild for TypeScript to JavaScript => transformImports() => esbuild for bundling.
|
|
30
|
+
// - Or we use an esbuild plugin to apply transformImports(). Maybe we can completely skip the need for acorn?
|
|
31
|
+
// - ?real breaks TypeScript, and TypeScript isn't working on supporting query params: https://github.com/microsoft/TypeScript/issues/10988#issuecomment-867135453
|
|
32
|
+
// - Import attributes would be the best.
|
|
33
|
+
// - But it only works with Node.js >=21: https://nodejs.org/api/esm.html#import-attributes
|
|
34
|
+
// - But it's probably ok to tell users "to use real imports you need Node.js 21 or above".
|
|
35
|
+
// - It works well with TypeScript: it doesn't complain upon `with { type: 'unknown-to-typescript' }` and go-to-definition & types are preserved: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html#import-attributes
|
|
36
|
+
// - Esbuid seems to support it: https://esbuild.github.io/plugins/#on-load-arguments:~:text=This%20contains%20a%20map%20of%20the%20import%20attributes%20that
|
|
37
|
+
// - acorn supports it over an acorn plugin: https://github.com/acornjs/acorn/issues/983
|
|
38
|
+
// - Maybe we can use an esbuild plugin instead of acorn to apply transformImports()?
|
|
39
|
+
// - Using a magic comment `// @vike-real-import` is tricky:
|
|
40
|
+
// - Esbuild removes comments: https://github.com/evanw/esbuild/issues/1439#issuecomment-877656182
|
|
41
|
+
// - Using source maps to track these magic comments is brittle (source maps can easily break)
|
|
42
|
+
if (importPath.endsWith('?real')) {
|
|
43
|
+
const { start, end } = node.source;
|
|
44
|
+
spliceOperations.push({
|
|
45
|
+
start,
|
|
46
|
+
end,
|
|
47
|
+
replacement: importPath.slice(0, -1 * '?real'.length)
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
22
51
|
const { start, end } = node;
|
|
23
52
|
const importStatementCode = code.slice(start, end);
|
|
24
53
|
// No variable imported
|
|
@@ -32,13 +61,15 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
32
61
|
quote = picocolors_1.default.bold(picocolors_1.default.red(quote));
|
|
33
62
|
}
|
|
34
63
|
const errMsg = [
|
|
35
|
-
`As explained in https://vike.dev/header-file the following import in ${
|
|
64
|
+
`As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
|
|
36
65
|
quote
|
|
37
66
|
].join('\n');
|
|
38
|
-
if (!
|
|
39
|
-
(
|
|
67
|
+
if (!skipWarnings) {
|
|
68
|
+
if (!isWarning) {
|
|
69
|
+
(0, utils_js_1.assertUsage)(false, errMsg);
|
|
70
|
+
}
|
|
71
|
+
(0, utils_js_1.assertWarning)(false, errMsg, { onlyOnce: true });
|
|
40
72
|
}
|
|
41
|
-
(0, utils_js_1.assertWarning)(false, errMsg, { onlyOnce: true });
|
|
42
73
|
}
|
|
43
74
|
let replacement = '';
|
|
44
75
|
node.specifiers.forEach((specifier) => {
|
|
@@ -60,7 +91,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
60
91
|
})();
|
|
61
92
|
const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
|
|
62
93
|
replacement += `const ${importLocalName} = '${importString}';`;
|
|
63
|
-
|
|
94
|
+
fileImportsTransformed.push({
|
|
64
95
|
importStatementCode,
|
|
65
96
|
importString,
|
|
66
97
|
importLocalName
|
|
@@ -73,9 +104,9 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
73
104
|
});
|
|
74
105
|
});
|
|
75
106
|
const codeMod = spliceMany(code, spliceOperations);
|
|
76
|
-
return { code: codeMod,
|
|
107
|
+
return { code: codeMod, fileImportsTransformed, noTransformation: false };
|
|
77
108
|
}
|
|
78
|
-
exports.
|
|
109
|
+
exports.transformImports = transformImports;
|
|
79
110
|
function getImports(code) {
|
|
80
111
|
const { body } = (0, acorn_1.parse)(code, {
|
|
81
112
|
ecmaVersion: 'latest',
|
|
@@ -143,7 +174,7 @@ function spliceMany(str, operations) {
|
|
|
143
174
|
.join('');
|
|
144
175
|
endPrev = end;
|
|
145
176
|
});
|
|
146
|
-
strMod += str.slice(endPrev, str.length
|
|
177
|
+
strMod += str.slice(endPrev, str.length);
|
|
147
178
|
return strMod;
|
|
148
179
|
}
|
|
149
180
|
function indent(str) {
|