vike 0.4.159-commit-0424983 → 0.4.159-commit-d73533a
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/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/{transformImports.js → getVikeConfig/transformImports.js} +3 -3
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +43 -29
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +54 -274
- package/dist/cjs/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
- package/dist/cjs/node/plugin/shared/loggerNotProd.js +1 -1
- package/dist/cjs/utils/projectInfo.js +1 -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/{transformImports.js → getVikeConfig/transformImports.js} +3 -3
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.d.ts → getVikeConfig/transpileAndExecuteFile.d.ts} +2 -2
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +44 -30
- 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 +50 -270
- package/dist/esm/node/plugin/shared/getHttpRequestAsyncStore.js +1 -1
- package/dist/esm/node/plugin/shared/loggerNotProd.js +1 -1
- package/dist/esm/utils/projectInfo.d.ts +2 -2
- package/dist/esm/utils/projectInfo.js +1 -1
- package/package.json +1 -1
- /package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{transformImports.d.ts → getVikeConfig/transformImports.d.ts} +0 -0
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
|
+
}
|
|
@@ -8,7 +8,7 @@ exports.isImportData = exports.parseImportData = exports.transformImports = void
|
|
|
8
8
|
// Import attributes support: https://github.com/acornjs/acorn/issues/983
|
|
9
9
|
// - Isn't stage 4 yet: https://github.com/tc39/proposal-import-attributes
|
|
10
10
|
const acorn_1 = require("acorn");
|
|
11
|
-
const utils_js_1 = require("
|
|
11
|
+
const utils_js_1 = require("../../../../utils.js");
|
|
12
12
|
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
13
13
|
function transformImports(code, filePathToShowToUser2,
|
|
14
14
|
// For ./transformImports.spec.ts
|
|
@@ -26,13 +26,13 @@ skipWarnings) {
|
|
|
26
26
|
return;
|
|
27
27
|
const importPath = node.source.value;
|
|
28
28
|
(0, utils_js_1.assert)(typeof importPath === 'string');
|
|
29
|
-
// - This doesn't work
|
|
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
30
|
// - Or we use an esbuild plugin to apply transformImports(). Maybe we can completely skip the need for acorn?
|
|
31
31
|
// - ?real breaks TypeScript, and TypeScript isn't working on supporting query params: https://github.com/microsoft/TypeScript/issues/10988#issuecomment-867135453
|
|
32
32
|
// - Import attributes would be the best.
|
|
33
33
|
// - But it only works with Node.js >=21: https://nodejs.org/api/esm.html#import-attributes
|
|
34
34
|
// - But it's probably ok to tell users "to use real imports you need Node.js 21 or above".
|
|
35
|
-
// - It
|
|
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
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
37
|
// - acorn supports it over an acorn plugin: https://github.com/acornjs/acorn/issues/983
|
|
38
38
|
// - Maybe we can use an esbuild plugin instead of acorn to apply transformImports()?
|
|
@@ -9,24 +9,34 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
11
11
|
const import_1 = require("@brillout/import");
|
|
12
|
-
const utils_js_1 = require("
|
|
12
|
+
const utils_js_1 = require("../../../../utils.js");
|
|
13
13
|
const transformImports_js_1 = require("./transformImports.js");
|
|
14
|
-
const getVikeConfig_js_1 = require("
|
|
14
|
+
const getVikeConfig_js_1 = require("../getVikeConfig.js");
|
|
15
15
|
require("source-map-support/register.js");
|
|
16
|
-
const getConfigFileExport_js_1 = require("
|
|
16
|
+
const getConfigFileExport_js_1 = require("../getConfigFileExport.js");
|
|
17
17
|
(0, utils_js_1.assertIsNotProductionRuntime)();
|
|
18
18
|
async function transpileAndExecuteFile(filePath, isValueFile, userRootDir, isConfigOfExtension = false) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
if (isConfigOfExtension) {
|
|
20
|
+
const fileExports = await executeFile(filePath.filePathAbsoluteFilesystem, filePath);
|
|
21
|
+
if (isHeaderFile(filePath.filePathAbsoluteFilesystem)) {
|
|
22
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
23
|
+
(0, utils_js_1.assertWarning)(false, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files don't apply to the config files of extensions`, { onlyOnce: true });
|
|
24
|
+
}
|
|
25
|
+
return { fileExports };
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
|
|
29
|
+
const fileExports = await executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile);
|
|
30
|
+
return { fileExports };
|
|
31
|
+
}
|
|
22
32
|
}
|
|
23
33
|
exports.transpileAndExecuteFile = transpileAndExecuteFile;
|
|
24
|
-
async function transpileFile(filePath, isValueFile, userRootDir
|
|
34
|
+
async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
25
35
|
const { filePathAbsoluteFilesystem } = filePath;
|
|
26
36
|
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
27
37
|
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
28
38
|
getVikeConfig_js_1.vikeConfigDependencies.add(filePathAbsoluteFilesystem);
|
|
29
|
-
const importsAreTransformed = !
|
|
39
|
+
const importsAreTransformed = !isValueFile;
|
|
30
40
|
let code = await transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile);
|
|
31
41
|
let fileImportsTransformed = null;
|
|
32
42
|
if (importsAreTransformed) {
|
|
@@ -41,9 +51,6 @@ async function transpileFile(filePath, isValueFile, userRootDir, isConfigOfExten
|
|
|
41
51
|
if (isValueFile) {
|
|
42
52
|
(0, utils_js_1.assertWarning)(false, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files only apply to +config.h.js, see https://vike.dev/header-file`, { onlyOnce: true });
|
|
43
53
|
}
|
|
44
|
-
else if (isConfigOfExtension) {
|
|
45
|
-
(0, utils_js_1.assertWarning)(false, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files don't apply to the config files of extensions`, { onlyOnce: true });
|
|
46
|
-
}
|
|
47
54
|
else {
|
|
48
55
|
(0, utils_js_1.assert)(false);
|
|
49
56
|
}
|
|
@@ -143,7 +150,7 @@ async function transpileWithEsbuild(filePath, userRootDir, importsAreTransformed
|
|
|
143
150
|
(0, utils_js_1.assert)(typeof code === 'string');
|
|
144
151
|
return code;
|
|
145
152
|
}
|
|
146
|
-
async function
|
|
153
|
+
async function executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile) {
|
|
147
154
|
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
148
155
|
// Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
|
|
149
156
|
// - But seems to break source maps, so I don't think it's worth it
|
|
@@ -152,28 +159,35 @@ async function executeFile(filePath, code, fileImportsTransformed, isValueFile)
|
|
|
152
159
|
const clean = () => fs_1.default.unlinkSync(filePathTmp);
|
|
153
160
|
let fileExports = {};
|
|
154
161
|
try {
|
|
155
|
-
fileExports = await (
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
triggerPrepareStackTrace(err);
|
|
159
|
-
const errIntroMsg = getErrIntroMsg('execute', filePath);
|
|
160
|
-
(0, utils_js_1.assert)((0, utils_js_1.isObject)(err));
|
|
161
|
-
execErrIntroMsg.set(err, errIntroMsg);
|
|
162
|
-
throw err;
|
|
162
|
+
fileExports = await executeFile(filePathTmp, filePath);
|
|
163
163
|
}
|
|
164
164
|
finally {
|
|
165
165
|
clean();
|
|
166
166
|
}
|
|
167
|
-
// Return a plain JavaScript object
|
|
168
|
-
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
169
|
-
// - We don't need this special object
|
|
170
|
-
fileExports = { ...fileExports };
|
|
171
167
|
if (fileImportsTransformed && !isValueFile) {
|
|
172
168
|
(0, utils_js_1.assert)(filePathRelativeToUserRootDir !== undefined);
|
|
173
169
|
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
174
170
|
assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
|
|
175
171
|
}
|
|
176
|
-
return
|
|
172
|
+
return fileExports;
|
|
173
|
+
}
|
|
174
|
+
async function executeFile(filePathToExecuteAbsoluteFilesystem, filePathSourceFile) {
|
|
175
|
+
let fileExports = {};
|
|
176
|
+
try {
|
|
177
|
+
fileExports = await (0, import_1.import_)(filePathToExecuteAbsoluteFilesystem);
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
triggerPrepareStackTrace(err);
|
|
181
|
+
const errIntroMsg = getErrIntroMsg('execute', filePathSourceFile);
|
|
182
|
+
(0, utils_js_1.assert)((0, utils_js_1.isObject)(err));
|
|
183
|
+
execErrIntroMsg.set(err, errIntroMsg);
|
|
184
|
+
throw err;
|
|
185
|
+
}
|
|
186
|
+
// Return a plain JavaScript object:
|
|
187
|
+
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
188
|
+
// - We don't need this special object.
|
|
189
|
+
fileExports = { ...fileExports };
|
|
190
|
+
return fileExports;
|
|
177
191
|
}
|
|
178
192
|
const formatted = '_formatted';
|
|
179
193
|
function getConfigBuildErrorFormatted(err) {
|
|
@@ -205,10 +219,10 @@ function getConfigExecutionErrorIntroMsg(err) {
|
|
|
205
219
|
}
|
|
206
220
|
exports.getConfigExecutionErrorIntroMsg = getConfigExecutionErrorIntroMsg;
|
|
207
221
|
const tmpPrefix = `[build-`;
|
|
208
|
-
function getFilePathTmp(
|
|
209
|
-
(0, utils_js_1.assertPosixPath)(
|
|
210
|
-
const dirname = path_1.default.posix.dirname(
|
|
211
|
-
const filename = path_1.default.posix.basename(
|
|
222
|
+
function getFilePathTmp(filePathAbsoluteFilesystem) {
|
|
223
|
+
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
224
|
+
const dirname = path_1.default.posix.dirname(filePathAbsoluteFilesystem);
|
|
225
|
+
const filename = path_1.default.posix.basename(filePathAbsoluteFilesystem);
|
|
212
226
|
// Syntax with semicolon `[build:${/*...*/}]` doesn't work on Windows: https://github.com/vikejs/vike/issues/800#issuecomment-1517329455
|
|
213
227
|
const tag = `${tmpPrefix}${(0, utils_js_1.getRandomId)(12)}]`;
|
|
214
228
|
const filePathTmp = path_1.default.posix.join(dirname, `${tag}${filename}.mjs`);
|