vike 0.4.159 → 0.4.160-commit-a1101c1
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 +58 -279
- 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/node/runtime/html/injectAssets/getHtmlTags.js +1 -1
- 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/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 +55 -276
- 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/node/runtime/html/injectAssets/getHtmlTags.js +1 -1
- 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/shared/page-configs/Config.d.ts +1 -1
- 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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { transformImports };
|
|
2
2
|
export { parseImportData };
|
|
3
3
|
export { isImportData };
|
|
4
4
|
export type { FileImport };
|
|
@@ -8,12 +8,12 @@ type FileImport = {
|
|
|
8
8
|
importString: string;
|
|
9
9
|
importLocalName: string;
|
|
10
10
|
};
|
|
11
|
-
declare function
|
|
12
|
-
|
|
11
|
+
declare function transformImports(code: string, filePathToShowToUser2: string, skipWarnings?: true): {
|
|
12
|
+
noTransformation: true;
|
|
13
13
|
} | {
|
|
14
|
-
|
|
14
|
+
noTransformation: false;
|
|
15
15
|
code: string;
|
|
16
|
-
|
|
16
|
+
fileImportsTransformed: FileImport[];
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* Data Structure holding info about import statement:
|
|
@@ -1,21 +1,50 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { transformImports };
|
|
2
2
|
export { parseImportData };
|
|
3
3
|
export { isImportData };
|
|
4
4
|
// Playground: https://github.com/brillout/acorn-playground
|
|
5
|
+
// Import attributes support: https://github.com/acornjs/acorn/issues/983
|
|
6
|
+
// - Isn't stage 4 yet: https://github.com/tc39/proposal-import-attributes
|
|
5
7
|
import { parse } from 'acorn';
|
|
6
|
-
import { assert, assertUsage, assertWarning, styleFileRE } from '
|
|
8
|
+
import { assert, assertUsage, assertWarning, styleFileRE } from '../../../../utils.js';
|
|
7
9
|
import pc from '@brillout/picocolors';
|
|
8
|
-
function
|
|
10
|
+
function transformImports(code, filePathToShowToUser2,
|
|
11
|
+
// For ./transformImports.spec.ts
|
|
12
|
+
skipWarnings) {
|
|
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');
|
|
26
|
+
// - This doesn't work. To make it work we would need to run esbuild twice: esbuild for TypeScript to JavaScript => transformImports() => esbuild for bundling.
|
|
27
|
+
// - Or we use an esbuild plugin to apply transformImports(). Maybe we can completely skip the need for acorn?
|
|
28
|
+
// - ?real breaks TypeScript, and TypeScript isn't working on supporting query params: https://github.com/microsoft/TypeScript/issues/10988#issuecomment-867135453
|
|
29
|
+
// - Import attributes would be the best.
|
|
30
|
+
// - But it only works with Node.js >=21: https://nodejs.org/api/esm.html#import-attributes
|
|
31
|
+
// - But it's probably ok to tell users "to use real imports you need Node.js 21 or above".
|
|
32
|
+
// - 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
|
|
33
|
+
// - Esbuid seems to support it: https://esbuild.github.io/plugins/#on-load-arguments:~:text=This%20contains%20a%20map%20of%20the%20import%20attributes%20that
|
|
34
|
+
// - acorn supports it over an acorn plugin: https://github.com/acornjs/acorn/issues/983
|
|
35
|
+
// - Maybe we can use an esbuild plugin instead of acorn to apply transformImports()?
|
|
36
|
+
// - Using a magic comment `// @vike-real-import` is tricky:
|
|
37
|
+
// - Esbuild removes comments: https://github.com/evanw/esbuild/issues/1439#issuecomment-877656182
|
|
38
|
+
// - Using source maps to track these magic comments is brittle (source maps can easily break)
|
|
39
|
+
if (importPath.endsWith('?real')) {
|
|
40
|
+
const { start, end } = node.source;
|
|
41
|
+
spliceOperations.push({
|
|
42
|
+
start,
|
|
43
|
+
end,
|
|
44
|
+
replacement: importPath.slice(0, -1 * '?real'.length)
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
19
48
|
const { start, end } = node;
|
|
20
49
|
const importStatementCode = code.slice(start, end);
|
|
21
50
|
// No variable imported
|
|
@@ -29,13 +58,15 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
29
58
|
quote = pc.bold(pc.red(quote));
|
|
30
59
|
}
|
|
31
60
|
const errMsg = [
|
|
32
|
-
`As explained in https://vike.dev/header-file the following import in ${
|
|
61
|
+
`As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
|
|
33
62
|
quote
|
|
34
63
|
].join('\n');
|
|
35
|
-
if (!
|
|
36
|
-
|
|
64
|
+
if (!skipWarnings) {
|
|
65
|
+
if (!isWarning) {
|
|
66
|
+
assertUsage(false, errMsg);
|
|
67
|
+
}
|
|
68
|
+
assertWarning(false, errMsg, { onlyOnce: true });
|
|
37
69
|
}
|
|
38
|
-
assertWarning(false, errMsg, { onlyOnce: true });
|
|
39
70
|
}
|
|
40
71
|
let replacement = '';
|
|
41
72
|
node.specifiers.forEach((specifier) => {
|
|
@@ -57,7 +88,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
57
88
|
})();
|
|
58
89
|
const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
|
|
59
90
|
replacement += `const ${importLocalName} = '${importString}';`;
|
|
60
|
-
|
|
91
|
+
fileImportsTransformed.push({
|
|
61
92
|
importStatementCode,
|
|
62
93
|
importString,
|
|
63
94
|
importLocalName
|
|
@@ -70,7 +101,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
70
101
|
});
|
|
71
102
|
});
|
|
72
103
|
const codeMod = spliceMany(code, spliceOperations);
|
|
73
|
-
return { code: codeMod,
|
|
104
|
+
return { code: codeMod, fileImportsTransformed, noTransformation: false };
|
|
74
105
|
}
|
|
75
106
|
function getImports(code) {
|
|
76
107
|
const { body } = parse(code, {
|
|
@@ -137,7 +168,7 @@ function spliceMany(str, operations) {
|
|
|
137
168
|
.join('');
|
|
138
169
|
endPrev = end;
|
|
139
170
|
});
|
|
140
|
-
strMod += str.slice(endPrev, str.length
|
|
171
|
+
strMod += str.slice(endPrev, str.length);
|
|
141
172
|
return strMod;
|
|
142
173
|
}
|
|
143
174
|
function indent(str) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { transpileAndExecuteFile };
|
|
2
2
|
export { getConfigBuildErrorFormatted };
|
|
3
|
-
export { getConfigExecutionErrorIntroMsg
|
|
3
|
+
export { getConfigExecutionErrorIntroMsg };
|
|
4
4
|
export { isTmpFile };
|
|
5
5
|
import 'source-map-support/register.js';
|
|
6
|
-
import type { FilePathResolved } from '
|
|
7
|
-
declare function transpileAndExecuteFile(filePath: FilePathResolved, isValueFile: boolean, userRootDir: string): Promise<{
|
|
6
|
+
import type { FilePathResolved } from '../../../../../../shared/page-configs/PageConfig.js';
|
|
7
|
+
declare function transpileAndExecuteFile(filePath: FilePathResolved, isValueFile: boolean, userRootDir: string, isConfigOfExtension?: boolean): Promise<{
|
|
8
8
|
fileExports: Record<string, unknown>;
|
|
9
9
|
}>;
|
|
10
10
|
declare function getConfigBuildErrorFormatted(err: unknown): null | string;
|
|
@@ -1,63 +1,79 @@
|
|
|
1
1
|
export { transpileAndExecuteFile };
|
|
2
2
|
export { getConfigBuildErrorFormatted };
|
|
3
|
-
export { getConfigExecutionErrorIntroMsg
|
|
3
|
+
export { getConfigExecutionErrorIntroMsg };
|
|
4
4
|
export { isTmpFile };
|
|
5
5
|
import { build, formatMessages } from 'esbuild';
|
|
6
6
|
import fs from 'fs';
|
|
7
7
|
import path from 'path';
|
|
8
8
|
import pc from '@brillout/picocolors';
|
|
9
9
|
import { import_ } from '@brillout/import';
|
|
10
|
-
import { assertPosixPath, getRandomId, assertIsNotProductionRuntime, assert, unique, assertWarning, isObject, toPosixPath } from '
|
|
11
|
-
import { isImportData,
|
|
12
|
-
import { vikeConfigDependencies } from '
|
|
10
|
+
import { assertPosixPath, getRandomId, assertIsNotProductionRuntime, assert, unique, assertWarning, isObject, toPosixPath } from '../../../../utils.js';
|
|
11
|
+
import { isImportData, transformImports } from './transformImports.js';
|
|
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
|
-
async function transpileAndExecuteFile(filePath, isValueFile, userRootDir) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
async function transpileAndExecuteFile(filePath, isValueFile, userRootDir, isConfigOfExtension = false) {
|
|
17
|
+
if (isConfigOfExtension) {
|
|
18
|
+
const fileExports = await executeFile(filePath.filePathAbsoluteFilesystem, filePath);
|
|
19
|
+
if (isHeaderFile(filePath.filePathAbsoluteFilesystem)) {
|
|
20
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
21
|
+
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 });
|
|
22
|
+
}
|
|
23
|
+
return { fileExports };
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
|
|
27
|
+
const fileExports = await executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile);
|
|
28
|
+
return { fileExports };
|
|
29
|
+
}
|
|
20
30
|
}
|
|
21
31
|
async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
22
32
|
const { filePathAbsoluteFilesystem } = filePath;
|
|
33
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
23
34
|
assertPosixPath(filePathAbsoluteFilesystem);
|
|
24
35
|
vikeConfigDependencies.add(filePathAbsoluteFilesystem);
|
|
25
|
-
|
|
26
|
-
let
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
const importsAreTransformed = !isValueFile;
|
|
37
|
+
let code = await transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile);
|
|
38
|
+
let fileImportsTransformed = null;
|
|
39
|
+
if (importsAreTransformed) {
|
|
40
|
+
const res = transformImports_(code, filePath);
|
|
29
41
|
if (res) {
|
|
30
42
|
code = res.code;
|
|
31
|
-
|
|
43
|
+
fileImportsTransformed = res.fileImportsTransformed;
|
|
32
44
|
}
|
|
33
45
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return null;
|
|
46
|
+
else {
|
|
47
|
+
if (isHeaderFile(filePathAbsoluteFilesystem)) {
|
|
48
|
+
if (isValueFile) {
|
|
49
|
+
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 });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
assert(false);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
44
55
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
56
|
+
return { code, fileImportsTransformed };
|
|
57
|
+
}
|
|
58
|
+
function transformImports_(codeOriginal, filePath) {
|
|
59
|
+
const { filePathAbsoluteFilesystem } = filePath;
|
|
60
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
61
|
+
// Replace import statements with import strings
|
|
62
|
+
const res = transformImports(codeOriginal, filePathToShowToUser2);
|
|
63
|
+
if (res.noTransformation) {
|
|
49
64
|
return null;
|
|
50
65
|
}
|
|
51
|
-
const { code,
|
|
52
|
-
if (!
|
|
53
|
-
const filePathCorrect = appendHeaderFileExtension(
|
|
54
|
-
assertWarning(false, `Rename ${
|
|
66
|
+
const { code, fileImportsTransformed } = res;
|
|
67
|
+
if (!isHeaderFile(filePathAbsoluteFilesystem)) {
|
|
68
|
+
const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
|
|
69
|
+
assertWarning(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
|
|
55
70
|
onlyOnce: true
|
|
56
71
|
});
|
|
57
72
|
}
|
|
58
|
-
return { code,
|
|
73
|
+
return { code, fileImportsTransformed };
|
|
59
74
|
}
|
|
60
|
-
async function transpileWithEsbuild(filePath,
|
|
75
|
+
async function transpileWithEsbuild(filePath, userRootDir, importsAreTransformed, isValueFile) {
|
|
76
|
+
const isConfigFile = !isValueFile;
|
|
61
77
|
const entryFilePath = filePath.filePathAbsoluteFilesystem;
|
|
62
78
|
const entryFileDir = path.posix.dirname(entryFilePath);
|
|
63
79
|
const options = {
|
|
@@ -73,22 +89,26 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
73
89
|
'NEVER_EMITTED.js'),
|
|
74
90
|
logLevel: 'silent',
|
|
75
91
|
format: 'esm',
|
|
76
|
-
|
|
92
|
+
absWorkingDir: userRootDir,
|
|
93
|
+
// Disable tree-shaking to avoid dead-code elimination, so that unused imports aren't removed.
|
|
94
|
+
// Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
|
|
95
|
+
treeShaking: false,
|
|
77
96
|
minify: false,
|
|
78
|
-
metafile:
|
|
79
|
-
|
|
97
|
+
metafile: isConfigFile,
|
|
98
|
+
// We cannot bundle imports that are meant to be transformed
|
|
99
|
+
bundle: !importsAreTransformed
|
|
80
100
|
};
|
|
81
|
-
|
|
82
|
-
|
|
101
|
+
// Track dependencies
|
|
102
|
+
if (isConfigFile) {
|
|
83
103
|
options.packages = 'external';
|
|
84
104
|
options.plugins = [
|
|
85
105
|
{
|
|
86
|
-
name: 'vike:
|
|
106
|
+
name: 'vike:dependency-tracker',
|
|
87
107
|
setup(b) {
|
|
88
108
|
b.onLoad({ filter: /./ }, (args) => {
|
|
109
|
+
// We collect the dependency `args.path` in case the bulid fails (upon build error => error is thrown => no metafile)
|
|
89
110
|
let { path } = args;
|
|
90
111
|
path = toPosixPath(path);
|
|
91
|
-
// We collect the dependency args.path in case it fails to build (upon build error => error is thrown => no metafile)
|
|
92
112
|
vikeConfigDependencies.add(path);
|
|
93
113
|
return undefined;
|
|
94
114
|
});
|
|
@@ -105,11 +125,6 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
105
125
|
}
|
|
106
126
|
];
|
|
107
127
|
}
|
|
108
|
-
else {
|
|
109
|
-
// Avoid dead-code elimination to ensure unused imports aren't removed.
|
|
110
|
-
// Esbuild still sometimes removes unused imports because of TypeScript: https://github.com/evanw/esbuild/issues/3034
|
|
111
|
-
options.treeShaking = false;
|
|
112
|
-
}
|
|
113
128
|
let result;
|
|
114
129
|
try {
|
|
115
130
|
result = await build(options);
|
|
@@ -118,7 +133,8 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
118
133
|
await formatBuildErr(err, filePath);
|
|
119
134
|
throw err;
|
|
120
135
|
}
|
|
121
|
-
|
|
136
|
+
// Track dependencies
|
|
137
|
+
if (isConfigFile) {
|
|
122
138
|
assert(result.metafile);
|
|
123
139
|
Object.keys(result.metafile.inputs).forEach((filePathRelative) => {
|
|
124
140
|
filePathRelative = toPosixPath(filePathRelative);
|
|
@@ -131,7 +147,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
131
147
|
assert(typeof code === 'string');
|
|
132
148
|
return code;
|
|
133
149
|
}
|
|
134
|
-
async function
|
|
150
|
+
async function executeTranspiledFile(filePath, code, fileImportsTransformed, isValueFile) {
|
|
135
151
|
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
136
152
|
// Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
|
|
137
153
|
// - But seems to break source maps, so I don't think it's worth it
|
|
@@ -140,28 +156,35 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
|
|
|
140
156
|
const clean = () => fs.unlinkSync(filePathTmp);
|
|
141
157
|
let fileExports = {};
|
|
142
158
|
try {
|
|
143
|
-
fileExports = await
|
|
159
|
+
fileExports = await executeFile(filePathTmp, filePath);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
clean();
|
|
163
|
+
}
|
|
164
|
+
if (fileImportsTransformed && !isValueFile) {
|
|
165
|
+
assert(filePathRelativeToUserRootDir !== undefined);
|
|
166
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
167
|
+
assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
|
|
168
|
+
}
|
|
169
|
+
return fileExports;
|
|
170
|
+
}
|
|
171
|
+
async function executeFile(filePathToExecuteAbsoluteFilesystem, filePathSourceFile) {
|
|
172
|
+
let fileExports = {};
|
|
173
|
+
try {
|
|
174
|
+
fileExports = await import_(filePathToExecuteAbsoluteFilesystem);
|
|
144
175
|
}
|
|
145
176
|
catch (err) {
|
|
146
177
|
triggerPrepareStackTrace(err);
|
|
147
|
-
const errIntroMsg = getErrIntroMsg('execute',
|
|
178
|
+
const errIntroMsg = getErrIntroMsg('execute', filePathSourceFile);
|
|
148
179
|
assert(isObject(err));
|
|
149
180
|
execErrIntroMsg.set(err, errIntroMsg);
|
|
150
181
|
throw err;
|
|
151
182
|
}
|
|
152
|
-
|
|
153
|
-
clean();
|
|
154
|
-
}
|
|
155
|
-
// Return a plain JavaScript object
|
|
183
|
+
// Return a plain JavaScript object:
|
|
156
184
|
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
157
|
-
// - We don't need this special object
|
|
185
|
+
// - We don't need this special object.
|
|
158
186
|
fileExports = { ...fileExports };
|
|
159
|
-
|
|
160
|
-
assert(filePathRelativeToUserRootDir !== undefined);
|
|
161
|
-
const filePathToShowToUser = filePathRelativeToUserRootDir ?? filePathAbsoluteFilesystem;
|
|
162
|
-
assertImportsAreReExported(fileImports, fileExports, filePathToShowToUser);
|
|
163
|
-
}
|
|
164
|
-
return { fileExports };
|
|
187
|
+
return fileExports;
|
|
165
188
|
}
|
|
166
189
|
const formatted = '_formatted';
|
|
167
190
|
function getConfigBuildErrorFormatted(err) {
|
|
@@ -191,10 +214,10 @@ function getConfigExecutionErrorIntroMsg(err) {
|
|
|
191
214
|
return errIntroMsg ?? null;
|
|
192
215
|
}
|
|
193
216
|
const tmpPrefix = `[build-`;
|
|
194
|
-
function getFilePathTmp(
|
|
195
|
-
assertPosixPath(
|
|
196
|
-
const dirname = path.posix.dirname(
|
|
197
|
-
const filename = path.posix.basename(
|
|
217
|
+
function getFilePathTmp(filePathAbsoluteFilesystem) {
|
|
218
|
+
assertPosixPath(filePathAbsoluteFilesystem);
|
|
219
|
+
const dirname = path.posix.dirname(filePathAbsoluteFilesystem);
|
|
220
|
+
const filename = path.posix.basename(filePathAbsoluteFilesystem);
|
|
198
221
|
// Syntax with semicolon `[build:${/*...*/}]` doesn't work on Windows: https://github.com/vikejs/vike/issues/800#issuecomment-1517329455
|
|
199
222
|
const tag = `${tmpPrefix}${getRandomId(12)}]`;
|
|
200
223
|
const filePathTmp = path.posix.join(dirname, `${tag}${filename}.mjs`);
|
|
@@ -205,29 +228,29 @@ function isTmpFile(filePath) {
|
|
|
205
228
|
const fileName = path.posix.basename(filePath);
|
|
206
229
|
return fileName.startsWith(tmpPrefix);
|
|
207
230
|
}
|
|
208
|
-
function assertImportsAreReExported(
|
|
209
|
-
|
|
210
|
-
const exportedStrings = getExportedStrings(
|
|
231
|
+
function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
|
|
232
|
+
const fileExport = getConfigFileExport(fileExports, filePathToShowToUser2);
|
|
233
|
+
const exportedStrings = getExportedStrings(fileExport);
|
|
211
234
|
Object.values(exportedStrings).forEach((exportVal) => {
|
|
212
235
|
if (typeof exportVal !== 'string')
|
|
213
236
|
return;
|
|
214
237
|
if (!isImportData(exportVal))
|
|
215
238
|
return;
|
|
216
239
|
const importString = exportVal;
|
|
217
|
-
|
|
240
|
+
fileImportsTransformed.forEach((fileImport) => {
|
|
218
241
|
if (fileImport.importString === importString) {
|
|
219
242
|
fileImport.isReExported = true;
|
|
220
243
|
}
|
|
221
244
|
});
|
|
222
245
|
});
|
|
223
|
-
const
|
|
224
|
-
if (
|
|
246
|
+
const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
|
|
247
|
+
if (fileImportsTransformedUnused.length === 0)
|
|
225
248
|
return;
|
|
226
|
-
const importStatements = unique(
|
|
227
|
-
const importNamesUnused =
|
|
228
|
-
const singular =
|
|
229
|
-
assertWarning(
|
|
230
|
-
`${
|
|
249
|
+
const importStatements = unique(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
|
|
250
|
+
const importNamesUnused = fileImportsTransformedUnused.map((fi) => pc.cyan(fi.importLocalName)).join(', ');
|
|
251
|
+
const singular = fileImportsTransformedUnused.length === 1;
|
|
252
|
+
assertWarning(fileImportsTransformedUnused.length === 0, [
|
|
253
|
+
`${filePathToShowToUser2} imports the following:`,
|
|
231
254
|
...importStatements.map((s) => pc.cyan(` ${s}`)),
|
|
232
255
|
`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
256
|
].join('\n'), { onlyOnce: true });
|
|
@@ -249,6 +272,7 @@ function getExportedStrings(obj) {
|
|
|
249
272
|
return exportedStrings;
|
|
250
273
|
}
|
|
251
274
|
function isHeaderFile(filePath) {
|
|
275
|
+
assertPosixPath(filePath);
|
|
252
276
|
const basenameParts = path.posix.basename(filePath).split('.');
|
|
253
277
|
return basenameParts.includes('h');
|
|
254
278
|
}
|
|
@@ -269,10 +293,19 @@ function triggerPrepareStackTrace(err) {
|
|
|
269
293
|
}
|
|
270
294
|
}
|
|
271
295
|
function getErrIntroMsg(operation, filePath) {
|
|
296
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
272
297
|
const msg = [
|
|
298
|
+
// prettier ignore
|
|
273
299
|
pc.red(`Failed to ${operation}`),
|
|
274
|
-
pc.bold(pc.red(
|
|
300
|
+
pc.bold(pc.red(filePathToShowToUser2)),
|
|
275
301
|
pc.red(`because:`)
|
|
276
302
|
].join(' ');
|
|
277
303
|
return msg;
|
|
278
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
|
+
assert(filePathToShowToUser2);
|
|
310
|
+
return filePathToShowToUser2;
|
|
311
|
+
}
|
|
@@ -2,9 +2,22 @@ export { getVikeConfig };
|
|
|
2
2
|
export { reloadVikeConfig };
|
|
3
3
|
export { vikeConfigDependencies };
|
|
4
4
|
export { isVikeConfigFile };
|
|
5
|
-
|
|
5
|
+
export type { InterfaceValueFile };
|
|
6
|
+
import type { PageConfigGlobalBuildTime, PageConfigBuildTime, FilePathResolved } from '../../../../../shared/page-configs/PageConfig.js';
|
|
6
7
|
import type { ExtensionResolved } from '../../../../../shared/ConfigVike.js';
|
|
7
8
|
import type { ResolvedConfig } from 'vite';
|
|
9
|
+
type InterfaceFileCommons = {
|
|
10
|
+
filePath: FilePathResolved;
|
|
11
|
+
fileExportsByConfigName: Record<ConfigName, {
|
|
12
|
+
configValue?: unknown;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
type InterfaceValueFile = InterfaceFileCommons & {
|
|
16
|
+
isConfigFile: false;
|
|
17
|
+
isValueFile: true;
|
|
18
|
+
configName: string;
|
|
19
|
+
};
|
|
20
|
+
type ConfigName = string;
|
|
8
21
|
type VikeConfig = {
|
|
9
22
|
pageConfigs: PageConfigBuildTime[];
|
|
10
23
|
pageConfigGlobal: PageConfigGlobalBuildTime;
|