vike 0.4.159 → 0.4.160-commit-30d535e
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/autoFullBuild.js +2 -2
- package/dist/cjs/node/plugin/plugins/buildConfig.js +9 -0
- package/dist/cjs/node/plugin/plugins/commonConfig.js +1 -1
- package/dist/cjs/node/plugin/plugins/config/stemUtils.js +1 -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/crawlPlusFiles.js +1 -5
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/filesystemRouting.js +28 -46
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.js +129 -0
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveFilePath.js +33 -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/transformFileImports.js} +44 -13
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{transpileAndExecuteFile.js → getVikeConfig/transpileAndExecuteFile.js} +105 -78
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +201 -382
- 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/plugin/utils.js +1 -2
- package/dist/cjs/node/prerender/runPrerender.js +12 -5
- package/dist/cjs/node/runtime/globalContext.js +7 -3
- package/dist/cjs/node/runtime/html/injectAssets/getHtmlTags.js +2 -2
- package/dist/cjs/node/runtime/html/renderHtml.js +1 -1
- package/dist/cjs/node/runtime/html/stream.js +2 -2
- package/dist/cjs/node/runtime/renderPage/executeOnRenderHtmlHook.js +2 -2
- package/dist/cjs/node/runtime/renderPage/getHttpResponseBody.js +1 -1
- package/dist/cjs/node/runtime/renderPage/loadUserFilesServerSide.js +1 -1
- package/dist/cjs/node/runtime/renderPage/{logHintForCjsEsmError.js → logErrorHint.js} +81 -43
- package/dist/cjs/node/runtime/renderPage/loggerProd.js +3 -3
- package/dist/cjs/node/runtime/utils.js +1 -1
- package/dist/cjs/shared/page-configs/assertPlusFileExport.js +44 -0
- package/dist/cjs/shared/page-configs/serialize/parseConfigValuesImported.js +54 -27
- package/dist/cjs/shared/route/abort.js +1 -1
- package/dist/cjs/shared/route/executeGuardHook.js +3 -2
- package/dist/cjs/shared/utils.js +0 -1
- package/dist/cjs/utils/assert.js +5 -6
- package/dist/cjs/utils/assertIsNotProductionRuntime.js +35 -17
- package/dist/cjs/utils/{findUserPackageJsonPath.js → findFile.js} +11 -8
- package/dist/cjs/utils/nodeEnv.js +33 -1
- package/dist/cjs/utils/objectKeys.js +19 -3
- package/dist/cjs/utils/projectInfo.js +2 -4
- package/dist/cjs/utils/sorter.js +62 -1
- package/dist/esm/node/plugin/index.js +21 -2
- package/dist/esm/node/plugin/plugins/autoFullBuild.js +2 -2
- package/dist/esm/node/plugin/plugins/buildConfig.js +10 -1
- package/dist/esm/node/plugin/plugins/commonConfig.js +2 -2
- package/dist/esm/node/plugin/plugins/config/stemUtils.js +2 -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 +8 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles.d.ts +0 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/crawlPlusFiles.js +1 -5
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/filesystemRouting.d.ts +39 -12
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/filesystemRouting.js +29 -47
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.d.ts +21 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/loadFileAtConfigTime.js +123 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveFilePath.d.ts +5 -0
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig/resolveFilePath.js +27 -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/transformFileImports.d.ts} +5 -5
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → getVikeConfig/transformFileImports.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} +105 -78
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.d.ts +16 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +199 -380
- 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/plugin/utils.d.ts +1 -2
- package/dist/esm/node/plugin/utils.js +1 -2
- package/dist/esm/node/prerender/runPrerender.js +13 -6
- package/dist/esm/node/runtime/globalContext.js +8 -4
- package/dist/esm/node/runtime/html/injectAssets/getHtmlTags.js +2 -2
- package/dist/esm/node/runtime/html/renderHtml.js +1 -1
- package/dist/esm/node/runtime/html/stream.js +2 -2
- package/dist/esm/node/runtime/renderPage/executeOnRenderHtmlHook.js +2 -2
- package/dist/esm/node/runtime/renderPage/getHttpResponseBody.js +1 -1
- package/dist/esm/node/runtime/renderPage/loadUserFilesServerSide.js +1 -1
- package/dist/esm/node/runtime/renderPage/logErrorHint.d.ts +8 -0
- package/dist/esm/node/runtime/renderPage/{logHintForCjsEsmError.js → logErrorHint.js} +80 -42
- package/dist/esm/node/runtime/renderPage/loggerProd.js +3 -3
- package/dist/esm/node/runtime/utils.d.ts +1 -1
- package/dist/esm/node/runtime/utils.js +1 -1
- package/dist/esm/shared/page-configs/Config.d.ts +1 -1
- package/dist/esm/shared/page-configs/PageConfig.d.ts +6 -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 +54 -27
- package/dist/esm/shared/route/abort.js +2 -2
- package/dist/esm/shared/route/executeGuardHook.js +3 -2
- package/dist/esm/shared/utils.d.ts +0 -1
- package/dist/esm/shared/utils.js +0 -1
- 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/findFile.d.ts +3 -0
- package/dist/esm/utils/findFile.js +21 -0
- package/dist/esm/utils/nodeEnv.d.ts +6 -0
- package/dist/esm/utils/nodeEnv.js +29 -0
- package/dist/esm/utils/objectKeys.d.ts +10 -1
- package/dist/esm/utils/objectKeys.js +20 -3
- package/dist/esm/utils/projectInfo.d.ts +2 -8
- package/dist/esm/utils/projectInfo.js +2 -4
- package/dist/esm/utils/sorter.d.ts +59 -0
- package/dist/esm/utils/sorter.js +61 -0
- package/package.json +1 -1
- package/dist/cjs/shared/page-configs/assertExports.js +0 -67
- package/dist/cjs/utils/objectEntries.js +0 -8
- package/dist/esm/node/runtime/renderPage/logHintForCjsEsmError.d.ts +0 -13
- package/dist/esm/shared/page-configs/assertExports.d.ts +0 -6
- package/dist/esm/shared/page-configs/assertExports.js +0 -61
- package/dist/esm/utils/findUserPackageJsonPath.d.ts +0 -2
- package/dist/esm/utils/findUserPackageJsonPath.js +0 -18
- package/dist/esm/utils/objectEntries.d.ts +0 -4
- package/dist/esm/utils/objectEntries.js +0 -5
- /package/dist/cjs/node/runtime/renderPage/{logHintForCjsEsmError → logErrorHint}/errors.js +0 -0
- /package/dist/esm/node/runtime/renderPage/{logHintForCjsEsmError → logErrorHint}/errors.d.ts +0 -0
- /package/dist/esm/node/runtime/renderPage/{logHintForCjsEsmError → logErrorHint}/errors.js +0 -0
|
@@ -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.transformFileImports = 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 transformFileImports(code, filePathToShowToUser2,
|
|
14
|
+
// For ./transformFileImports.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 => transformFileImports() => esbuild for bundling.
|
|
30
|
+
// - Or we use an esbuild plugin to apply transformFileImports(). 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 transformFileImports()?
|
|
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.transformFileImports = transformFileImports;
|
|
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) {
|
|
@@ -9,58 +9,68 @@ 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("
|
|
13
|
-
const
|
|
14
|
-
const getVikeConfig_js_1 = require("
|
|
12
|
+
const utils_js_1 = require("../../../../utils.js");
|
|
13
|
+
const transformFileImports_js_1 = require("./transformFileImports.js");
|
|
14
|
+
const getVikeConfig_js_1 = require("../getVikeConfig.js");
|
|
15
15
|
require("source-map-support/register.js");
|
|
16
|
-
const
|
|
16
|
+
const getConfigFileExport_js_1 = require("../getConfigFileExport.js");
|
|
17
17
|
(0, utils_js_1.assertIsNotProductionRuntime)();
|
|
18
|
-
async function transpileAndExecuteFile(filePath,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
async function transpileAndExecuteFile(filePath, transformImports, userRootDir, doNotTranspile = false) {
|
|
19
|
+
if (doNotTranspile) {
|
|
20
|
+
(0, utils_js_1.assert)(!transformImports);
|
|
21
|
+
const fileExports = await executeFile(filePath.filePathAbsoluteFilesystem, filePath);
|
|
22
|
+
if (isHeaderFile(filePath.filePathAbsoluteFilesystem)) {
|
|
23
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
24
|
+
(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 });
|
|
25
|
+
}
|
|
26
|
+
return { fileExports };
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const { code, fileImportsTransformed } = await transpileFile(filePath, transformImports, userRootDir);
|
|
30
|
+
const fileExports = await executeTranspiledFile(filePath, code, fileImportsTransformed);
|
|
31
|
+
return { fileExports };
|
|
32
|
+
}
|
|
22
33
|
}
|
|
23
34
|
exports.transpileAndExecuteFile = transpileAndExecuteFile;
|
|
24
|
-
async function transpileFile(filePath,
|
|
35
|
+
async function transpileFile(filePath, transformImports, userRootDir) {
|
|
25
36
|
const { filePathAbsoluteFilesystem } = filePath;
|
|
37
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
26
38
|
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
27
39
|
getVikeConfig_js_1.vikeConfigDependencies.add(filePathAbsoluteFilesystem);
|
|
28
|
-
let code = await transpileWithEsbuild(filePath,
|
|
29
|
-
let
|
|
30
|
-
{
|
|
31
|
-
const res =
|
|
40
|
+
let code = await transpileWithEsbuild(filePath, userRootDir, transformImports);
|
|
41
|
+
let fileImportsTransformed = null;
|
|
42
|
+
if (transformImports) {
|
|
43
|
+
const res = transformFileImports_(code, filePath);
|
|
32
44
|
if (res) {
|
|
33
45
|
code = res.code;
|
|
34
|
-
|
|
46
|
+
fileImportsTransformed = res.fileImportsTransformed;
|
|
35
47
|
}
|
|
36
48
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const { filePathAbsoluteFilesystem, filePathToShowToUser } = filePath;
|
|
42
|
-
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
43
|
-
const isHeader = isHeaderFile(filePathAbsoluteFilesystem);
|
|
44
|
-
const isPageConfigFile = !isValueFile;
|
|
45
|
-
if (!isHeader && !isPageConfigFile) {
|
|
46
|
-
return null;
|
|
49
|
+
else {
|
|
50
|
+
if (isHeaderFile(filePathAbsoluteFilesystem)) {
|
|
51
|
+
(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 });
|
|
52
|
+
}
|
|
47
53
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
return { code, fileImportsTransformed };
|
|
55
|
+
}
|
|
56
|
+
function transformFileImports_(codeOriginal, filePath) {
|
|
57
|
+
const { filePathAbsoluteFilesystem } = filePath;
|
|
58
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
59
|
+
// Replace import statements with import strings
|
|
60
|
+
const res = (0, transformFileImports_js_1.transformFileImports)(codeOriginal, filePathToShowToUser2);
|
|
61
|
+
if (res.noTransformation) {
|
|
52
62
|
return null;
|
|
53
63
|
}
|
|
54
|
-
const { code,
|
|
55
|
-
if (!
|
|
56
|
-
const filePathCorrect = appendHeaderFileExtension(
|
|
57
|
-
(0, utils_js_1.assertWarning)(false, `Rename ${
|
|
64
|
+
const { code, fileImportsTransformed } = res;
|
|
65
|
+
if (!isHeaderFile(filePathAbsoluteFilesystem)) {
|
|
66
|
+
const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
|
|
67
|
+
(0, utils_js_1.assertWarning)(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
|
|
58
68
|
onlyOnce: true
|
|
59
69
|
});
|
|
60
70
|
}
|
|
61
|
-
return { code,
|
|
71
|
+
return { code, fileImportsTransformed };
|
|
62
72
|
}
|
|
63
|
-
async function transpileWithEsbuild(filePath,
|
|
73
|
+
async function transpileWithEsbuild(filePath, userRootDir, transformImports) {
|
|
64
74
|
const entryFilePath = filePath.filePathAbsoluteFilesystem;
|
|
65
75
|
const entryFileDir = path_1.default.posix.dirname(entryFilePath);
|
|
66
76
|
const options = {
|
|
@@ -76,22 +86,26 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
76
86
|
'NEVER_EMITTED.js'),
|
|
77
87
|
logLevel: 'silent',
|
|
78
88
|
format: 'esm',
|
|
79
|
-
|
|
89
|
+
absWorkingDir: userRootDir,
|
|
90
|
+
// Disable tree-shaking to avoid dead-code elimination, so that unused imports aren't removed.
|
|
91
|
+
// Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
|
|
92
|
+
treeShaking: false,
|
|
80
93
|
minify: false,
|
|
81
|
-
metafile:
|
|
82
|
-
|
|
94
|
+
metafile: !transformImports,
|
|
95
|
+
// We cannot bundle imports that are meant to be transformed
|
|
96
|
+
bundle: !transformImports
|
|
83
97
|
};
|
|
84
|
-
|
|
85
|
-
|
|
98
|
+
// Track dependencies
|
|
99
|
+
if (!transformImports) {
|
|
86
100
|
options.packages = 'external';
|
|
87
101
|
options.plugins = [
|
|
88
102
|
{
|
|
89
|
-
name: 'vike:
|
|
103
|
+
name: 'vike:dependency-tracker',
|
|
90
104
|
setup(b) {
|
|
91
105
|
b.onLoad({ filter: /./ }, (args) => {
|
|
106
|
+
// We collect the dependency `args.path` in case the bulid fails (upon build error => error is thrown => no metafile)
|
|
92
107
|
let { path } = args;
|
|
93
108
|
path = (0, utils_js_1.toPosixPath)(path);
|
|
94
|
-
// We collect the dependency args.path in case it fails to build (upon build error => error is thrown => no metafile)
|
|
95
109
|
getVikeConfig_js_1.vikeConfigDependencies.add(path);
|
|
96
110
|
return undefined;
|
|
97
111
|
});
|
|
@@ -108,11 +122,6 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
108
122
|
}
|
|
109
123
|
];
|
|
110
124
|
}
|
|
111
|
-
else {
|
|
112
|
-
// Avoid dead-code elimination to ensure unused imports aren't removed.
|
|
113
|
-
// Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
|
|
114
|
-
options.treeShaking = false;
|
|
115
|
-
}
|
|
116
125
|
let result;
|
|
117
126
|
try {
|
|
118
127
|
result = await (0, esbuild_1.build)(options);
|
|
@@ -121,7 +130,8 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
121
130
|
await formatBuildErr(err, filePath);
|
|
122
131
|
throw err;
|
|
123
132
|
}
|
|
124
|
-
|
|
133
|
+
// Track dependencies
|
|
134
|
+
if (!transformImports) {
|
|
125
135
|
(0, utils_js_1.assert)(result.metafile);
|
|
126
136
|
Object.keys(result.metafile.inputs).forEach((filePathRelative) => {
|
|
127
137
|
filePathRelative = (0, utils_js_1.toPosixPath)(filePathRelative);
|
|
@@ -134,7 +144,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
134
144
|
(0, utils_js_1.assert)(typeof code === 'string');
|
|
135
145
|
return code;
|
|
136
146
|
}
|
|
137
|
-
async function
|
|
147
|
+
async function executeTranspiledFile(filePath, code, fileImportsTransformed) {
|
|
138
148
|
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
139
149
|
// Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
|
|
140
150
|
// - But seems to break source maps, so I don't think it's worth it
|
|
@@ -143,28 +153,35 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
|
|
|
143
153
|
const clean = () => fs_1.default.unlinkSync(filePathTmp);
|
|
144
154
|
let fileExports = {};
|
|
145
155
|
try {
|
|
146
|
-
fileExports = await (
|
|
156
|
+
fileExports = await executeFile(filePathTmp, filePath);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
clean();
|
|
160
|
+
}
|
|
161
|
+
if (fileImportsTransformed) {
|
|
162
|
+
(0, utils_js_1.assert)(filePathRelativeToUserRootDir !== undefined);
|
|
163
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
164
|
+
assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
|
|
165
|
+
}
|
|
166
|
+
return fileExports;
|
|
167
|
+
}
|
|
168
|
+
async function executeFile(filePathToExecuteAbsoluteFilesystem, filePathSourceFile) {
|
|
169
|
+
let fileExports = {};
|
|
170
|
+
try {
|
|
171
|
+
fileExports = await (0, import_1.import_)(filePathToExecuteAbsoluteFilesystem);
|
|
147
172
|
}
|
|
148
173
|
catch (err) {
|
|
149
174
|
triggerPrepareStackTrace(err);
|
|
150
|
-
const errIntroMsg = getErrIntroMsg('execute',
|
|
175
|
+
const errIntroMsg = getErrIntroMsg('execute', filePathSourceFile);
|
|
151
176
|
(0, utils_js_1.assert)((0, utils_js_1.isObject)(err));
|
|
152
177
|
execErrIntroMsg.set(err, errIntroMsg);
|
|
153
178
|
throw err;
|
|
154
179
|
}
|
|
155
|
-
|
|
156
|
-
clean();
|
|
157
|
-
}
|
|
158
|
-
// Return a plain JavaScript object
|
|
180
|
+
// Return a plain JavaScript object:
|
|
159
181
|
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
160
|
-
// - We don't need this special object
|
|
182
|
+
// - We don't need this special object.
|
|
161
183
|
fileExports = { ...fileExports };
|
|
162
|
-
|
|
163
|
-
(0, utils_js_1.assert)(filePathRelativeToUserRootDir !== undefined);
|
|
164
|
-
const filePathToShowToUser = filePathRelativeToUserRootDir ?? filePathAbsoluteFilesystem;
|
|
165
|
-
assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser);
|
|
166
|
-
}
|
|
167
|
-
return { fileExports };
|
|
184
|
+
return fileExports;
|
|
168
185
|
}
|
|
169
186
|
const formatted = '_formatted';
|
|
170
187
|
function getConfigBuildErrorFormatted(err) {
|
|
@@ -196,10 +213,10 @@ function getConfigExecutionErrorIntroMsg(err) {
|
|
|
196
213
|
}
|
|
197
214
|
exports.getConfigExecutionErrorIntroMsg = getConfigExecutionErrorIntroMsg;
|
|
198
215
|
const tmpPrefix = `[build-`;
|
|
199
|
-
function getFilePathTmp(
|
|
200
|
-
(0, utils_js_1.assertPosixPath)(
|
|
201
|
-
const dirname = path_1.default.posix.dirname(
|
|
202
|
-
const filename = path_1.default.posix.basename(
|
|
216
|
+
function getFilePathTmp(filePathAbsoluteFilesystem) {
|
|
217
|
+
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
218
|
+
const dirname = path_1.default.posix.dirname(filePathAbsoluteFilesystem);
|
|
219
|
+
const filename = path_1.default.posix.basename(filePathAbsoluteFilesystem);
|
|
203
220
|
// Syntax with semicolon `[build:${/*...*/}]` doesn't work on Windows: https://github.com/vikejs/vike/issues/800#issuecomment-1517329455
|
|
204
221
|
const tag = `${tmpPrefix}${(0, utils_js_1.getRandomId)(12)}]`;
|
|
205
222
|
const filePathTmp = path_1.default.posix.join(dirname, `${tag}${filename}.mjs`);
|
|
@@ -211,29 +228,29 @@ function isTmpFile(filePath) {
|
|
|
211
228
|
return fileName.startsWith(tmpPrefix);
|
|
212
229
|
}
|
|
213
230
|
exports.isTmpFile = isTmpFile;
|
|
214
|
-
function assertImportsAreReExported(
|
|
215
|
-
(0,
|
|
216
|
-
const exportedStrings = getExportedStrings(
|
|
231
|
+
function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
|
|
232
|
+
const fileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(fileExports, filePathToShowToUser2);
|
|
233
|
+
const exportedStrings = getExportedStrings(fileExport);
|
|
217
234
|
Object.values(exportedStrings).forEach((exportVal) => {
|
|
218
235
|
if (typeof exportVal !== 'string')
|
|
219
236
|
return;
|
|
220
|
-
if (!(0,
|
|
237
|
+
if (!(0, transformFileImports_js_1.isImportData)(exportVal))
|
|
221
238
|
return;
|
|
222
239
|
const importString = exportVal;
|
|
223
|
-
|
|
240
|
+
fileImportsTransformed.forEach((fileImport) => {
|
|
224
241
|
if (fileImport.importString === importString) {
|
|
225
242
|
fileImport.isReExported = true;
|
|
226
243
|
}
|
|
227
244
|
});
|
|
228
245
|
});
|
|
229
|
-
const
|
|
230
|
-
if (
|
|
246
|
+
const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
|
|
247
|
+
if (fileImportsTransformedUnused.length === 0)
|
|
231
248
|
return;
|
|
232
|
-
const importStatements = (0, utils_js_1.unique)(
|
|
233
|
-
const importNamesUnused =
|
|
234
|
-
const singular =
|
|
235
|
-
(0, utils_js_1.assertWarning)(
|
|
236
|
-
`${
|
|
249
|
+
const importStatements = (0, utils_js_1.unique)(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
|
|
250
|
+
const importNamesUnused = fileImportsTransformedUnused.map((fi) => picocolors_1.default.cyan(fi.importLocalName)).join(', ');
|
|
251
|
+
const singular = fileImportsTransformedUnused.length === 1;
|
|
252
|
+
(0, utils_js_1.assertWarning)(fileImportsTransformedUnused.length === 0, [
|
|
253
|
+
`${filePathToShowToUser2} imports the following:`,
|
|
237
254
|
...importStatements.map((s) => picocolors_1.default.cyan(` ${s}`)),
|
|
238
255
|
`But the import${singular ? '' : 's'} ${importNamesUnused} ${singular ? "isn't" : "aren't"} re-exported at ${picocolors_1.default.cyan('export default { ... }')} and therefore ${singular ? 'has' : 'have'} no effect, see explanation at https://vike.dev/header-file`
|
|
239
256
|
].join('\n'), { onlyOnce: true });
|
|
@@ -255,6 +272,7 @@ function getExportedStrings(obj) {
|
|
|
255
272
|
return exportedStrings;
|
|
256
273
|
}
|
|
257
274
|
function isHeaderFile(filePath) {
|
|
275
|
+
(0, utils_js_1.assertPosixPath)(filePath);
|
|
258
276
|
const basenameParts = path_1.default.posix.basename(filePath).split('.');
|
|
259
277
|
return basenameParts.includes('h');
|
|
260
278
|
}
|
|
@@ -275,10 +293,19 @@ function triggerPrepareStackTrace(err) {
|
|
|
275
293
|
}
|
|
276
294
|
}
|
|
277
295
|
function getErrIntroMsg(operation, filePath) {
|
|
296
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
278
297
|
const msg = [
|
|
298
|
+
// prettier ignore
|
|
279
299
|
picocolors_1.default.red(`Failed to ${operation}`),
|
|
280
|
-
picocolors_1.default.bold(picocolors_1.default.red(
|
|
300
|
+
picocolors_1.default.bold(picocolors_1.default.red(filePathToShowToUser2)),
|
|
281
301
|
picocolors_1.default.red(`because:`)
|
|
282
302
|
].join(' ');
|
|
283
303
|
return msg;
|
|
284
304
|
}
|
|
305
|
+
/** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
|
|
306
|
+
function getFilePathToShowToUser2(filePath) {
|
|
307
|
+
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
308
|
+
const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
|
|
309
|
+
(0, utils_js_1.assert)(filePathToShowToUser2);
|
|
310
|
+
return filePathToShowToUser2;
|
|
311
|
+
}
|