vike 0.4.159-commit-6bcb2fd → 0.4.159-commit-71760c7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +3 -3
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → transformImportStatements.js} +23 -8
- package/dist/cjs/node/plugin/plugins/importUserCode/v1-design/transpileAndExecuteFile.js +42 -32
- package/dist/cjs/utils/projectInfo.js +1 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/getVikeConfig.js +1 -1
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.d.ts → transformImportStatements.d.ts} +7 -5
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/{replaceImportStatements.js → transformImportStatements.js} +23 -7
- package/dist/esm/node/plugin/plugins/importUserCode/v1-design/transpileAndExecuteFile.js +41 -31
- package/dist/esm/utils/projectInfo.d.ts +2 -2
- package/dist/esm/utils/projectInfo.js +1 -1
- package/package.json +1 -1
|
@@ -9,7 +9,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
9
9
|
const configDefinitionsBuiltIn_js_1 = require("./getVikeConfig/configDefinitionsBuiltIn.js");
|
|
10
10
|
const filesystemRouting_js_1 = require("./getVikeConfig/filesystemRouting.js");
|
|
11
11
|
const transpileAndExecuteFile_js_1 = require("./transpileAndExecuteFile.js");
|
|
12
|
-
const
|
|
12
|
+
const transformImportStatements_js_1 = require("./transformImportStatements.js");
|
|
13
13
|
const isConfigInvalid_js_1 = require("../../../../runtime/renderPage/isConfigInvalid.js");
|
|
14
14
|
const globalContext_js_1 = require("../../../../runtime/globalContext.js");
|
|
15
15
|
const loggerNotProd_js_1 = require("../../../shared/loggerNotProd.js");
|
|
@@ -582,7 +582,7 @@ function isDefiningPageConfig(configName) {
|
|
|
582
582
|
function resolveImport(configValue, importerFilePath, userRootDir, configEnv, configName) {
|
|
583
583
|
if (typeof configValue !== 'string')
|
|
584
584
|
return null;
|
|
585
|
-
const importData = (0,
|
|
585
|
+
const importData = (0, transformImportStatements_js_1.parseImportData)(configValue);
|
|
586
586
|
if (!importData)
|
|
587
587
|
return null;
|
|
588
588
|
const { importPath, exportName } = importData;
|
|
@@ -941,7 +941,7 @@ function getExtendsImportData(configFileExports, configFilePath) {
|
|
|
941
941
|
(0, utils_js_1.assertUsage)(false, wrongUsage);
|
|
942
942
|
}
|
|
943
943
|
const extendsImportData = extendList.map((importDataSerialized) => {
|
|
944
|
-
const importData = (0,
|
|
944
|
+
const importData = (0, transformImportStatements_js_1.parseImportData)(importDataSerialized);
|
|
945
945
|
(0, utils_js_1.assertUsage)(importData, wrongUsage);
|
|
946
946
|
return importData;
|
|
947
947
|
});
|
|
@@ -3,23 +3,30 @@ 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.isVikeRealImport = exports.isImportData = exports.parseImportData = exports.transformImportStatements = 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
11
|
const utils_js_1 = require("../../../utils.js");
|
|
10
12
|
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
11
|
-
function
|
|
13
|
+
function transformImportStatements(code, filePathToShowToUser2) {
|
|
12
14
|
const spliceOperations = [];
|
|
13
|
-
const
|
|
15
|
+
const fileImportsTransformed = [];
|
|
16
|
+
// Performance trick
|
|
17
|
+
if (!code.includes('import'))
|
|
18
|
+
return { noTransformation: true };
|
|
14
19
|
const imports = getImports(code);
|
|
15
20
|
if (imports.length === 0)
|
|
16
|
-
return {
|
|
21
|
+
return { noTransformation: true };
|
|
17
22
|
imports.forEach((node) => {
|
|
18
23
|
if (node.type !== 'ImportDeclaration')
|
|
19
24
|
return;
|
|
20
25
|
const importPath = node.source.value;
|
|
21
26
|
(0, utils_js_1.assert)(typeof importPath === 'string');
|
|
22
27
|
const { start, end } = node;
|
|
28
|
+
if (isVikeRealImport(code, end))
|
|
29
|
+
return;
|
|
23
30
|
const importStatementCode = code.slice(start, end);
|
|
24
31
|
// No variable imported
|
|
25
32
|
if (node.specifiers.length === 0) {
|
|
@@ -32,7 +39,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
32
39
|
quote = picocolors_1.default.bold(picocolors_1.default.red(quote));
|
|
33
40
|
}
|
|
34
41
|
const errMsg = [
|
|
35
|
-
`As explained in https://vike.dev/header-file the following import in ${
|
|
42
|
+
`As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
|
|
36
43
|
quote
|
|
37
44
|
].join('\n');
|
|
38
45
|
if (!isWarning) {
|
|
@@ -60,7 +67,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
60
67
|
})();
|
|
61
68
|
const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
|
|
62
69
|
replacement += `const ${importLocalName} = '${importString}';`;
|
|
63
|
-
|
|
70
|
+
fileImportsTransformed.push({
|
|
64
71
|
importStatementCode,
|
|
65
72
|
importString,
|
|
66
73
|
importLocalName
|
|
@@ -73,9 +80,9 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
73
80
|
});
|
|
74
81
|
});
|
|
75
82
|
const codeMod = spliceMany(code, spliceOperations);
|
|
76
|
-
return { code: codeMod,
|
|
83
|
+
return { code: codeMod, fileImportsTransformed, noTransformation: false };
|
|
77
84
|
}
|
|
78
|
-
exports.
|
|
85
|
+
exports.transformImportStatements = transformImportStatements;
|
|
79
86
|
function getImports(code) {
|
|
80
87
|
const { body } = (0, acorn_1.parse)(code, {
|
|
81
88
|
ecmaVersion: 'latest',
|
|
@@ -152,3 +159,11 @@ function indent(str) {
|
|
|
152
159
|
.map((s) => ` ${s}`)
|
|
153
160
|
.join('\n');
|
|
154
161
|
}
|
|
162
|
+
function isVikeRealImport(code, posStart) {
|
|
163
|
+
let posEnd = code.indexOf('\n', posStart);
|
|
164
|
+
if (posEnd === -1)
|
|
165
|
+
posEnd = code.length;
|
|
166
|
+
const lineEnd = code.slice(posStart, posEnd);
|
|
167
|
+
return lineEnd.includes('@vike-real-import');
|
|
168
|
+
}
|
|
169
|
+
exports.isVikeRealImport = isVikeRealImport;
|
|
@@ -10,14 +10,14 @@ const path_1 = __importDefault(require("path"));
|
|
|
10
10
|
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
|
|
11
11
|
const import_1 = require("@brillout/import");
|
|
12
12
|
const utils_js_1 = require("../../../utils.js");
|
|
13
|
-
const
|
|
13
|
+
const transformImportStatements_js_1 = require("./transformImportStatements.js");
|
|
14
14
|
const getVikeConfig_js_1 = require("./getVikeConfig.js");
|
|
15
15
|
require("source-map-support/register.js");
|
|
16
16
|
const getConfigFileExport_js_1 = require("./getConfigFileExport.js");
|
|
17
17
|
(0, utils_js_1.assertIsNotProductionRuntime)();
|
|
18
18
|
async function transpileAndExecuteFile(filePath, isValueFile, userRootDir) {
|
|
19
|
-
const { code,
|
|
20
|
-
const { fileExports } = await executeFile(filePath, code,
|
|
19
|
+
const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
|
|
20
|
+
const { fileExports } = await executeFile(filePath, code, fileImportsTransformed, isValueFile);
|
|
21
21
|
return { fileExports };
|
|
22
22
|
}
|
|
23
23
|
exports.transpileAndExecuteFile = transpileAndExecuteFile;
|
|
@@ -26,39 +26,40 @@ async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
|
26
26
|
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
27
27
|
getVikeConfig_js_1.vikeConfigDependencies.add(filePathAbsoluteFilesystem);
|
|
28
28
|
let code = await transpileWithEsbuild(filePath, isValueFile, userRootDir);
|
|
29
|
-
let
|
|
29
|
+
let fileImportsTransformed = null;
|
|
30
30
|
{
|
|
31
|
-
const res =
|
|
31
|
+
const res = transformImports(code, filePath, isValueFile);
|
|
32
32
|
if (res) {
|
|
33
33
|
code = res.code;
|
|
34
|
-
|
|
34
|
+
fileImportsTransformed = res.fileImportsTransformed;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
return { code,
|
|
37
|
+
return { code, fileImportsTransformed };
|
|
38
38
|
}
|
|
39
|
-
function
|
|
39
|
+
function transformImports(codeOriginal, filePath, isValueFile) {
|
|
40
40
|
// Do we need to remove the imports?
|
|
41
|
-
const { filePathAbsoluteFilesystem
|
|
41
|
+
const { filePathAbsoluteFilesystem } = filePath;
|
|
42
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
42
43
|
(0, utils_js_1.assertPosixPath)(filePathAbsoluteFilesystem);
|
|
43
44
|
const isHeader = isHeaderFile(filePathAbsoluteFilesystem);
|
|
44
45
|
const isPageConfigFile = !isValueFile;
|
|
45
46
|
if (!isHeader && !isPageConfigFile) {
|
|
46
47
|
return null;
|
|
47
48
|
}
|
|
48
|
-
(0, utils_js_1.assertWarning)(isPageConfigFile, `${
|
|
49
|
+
(0, utils_js_1.assertWarning)(isPageConfigFile, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files should only be used for +config.h.js, see https://vike.dev/header-file`, { onlyOnce: true });
|
|
49
50
|
// Remove the imports
|
|
50
|
-
const res = (0,
|
|
51
|
-
if (res.
|
|
51
|
+
const res = (0, transformImportStatements_js_1.transformImportStatements)(codeOriginal, filePathToShowToUser2);
|
|
52
|
+
if (res.noTransformation) {
|
|
52
53
|
return null;
|
|
53
54
|
}
|
|
54
|
-
const { code,
|
|
55
|
+
const { code, fileImportsTransformed } = res;
|
|
55
56
|
if (!isHeader) {
|
|
56
|
-
const filePathCorrect = appendHeaderFileExtension(
|
|
57
|
-
(0, utils_js_1.assertWarning)(false, `Rename ${
|
|
57
|
+
const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
|
|
58
|
+
(0, utils_js_1.assertWarning)(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
|
|
58
59
|
onlyOnce: true
|
|
59
60
|
});
|
|
60
61
|
}
|
|
61
|
-
return { code,
|
|
62
|
+
return { code, fileImportsTransformed };
|
|
62
63
|
}
|
|
63
64
|
async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
64
65
|
const entryFilePath = filePath.filePathAbsoluteFilesystem;
|
|
@@ -134,7 +135,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
134
135
|
(0, utils_js_1.assert)(typeof code === 'string');
|
|
135
136
|
return code;
|
|
136
137
|
}
|
|
137
|
-
async function executeFile(filePath, code,
|
|
138
|
+
async function executeFile(filePath, code, fileImportsTransformed, isValueFile) {
|
|
138
139
|
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
139
140
|
// Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
|
|
140
141
|
// - But seems to break source maps, so I don't think it's worth it
|
|
@@ -159,10 +160,10 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
|
|
|
159
160
|
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
160
161
|
// - We don't need this special object
|
|
161
162
|
fileExports = { ...fileExports };
|
|
162
|
-
if (
|
|
163
|
+
if (fileImportsTransformed && !isValueFile) {
|
|
163
164
|
(0, utils_js_1.assert)(filePathRelativeToUserRootDir !== undefined);
|
|
164
|
-
const
|
|
165
|
-
assertImportsAreReExported(
|
|
165
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
166
|
+
assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
|
|
166
167
|
}
|
|
167
168
|
return { fileExports };
|
|
168
169
|
}
|
|
@@ -211,29 +212,29 @@ function isTmpFile(filePath) {
|
|
|
211
212
|
return fileName.startsWith(tmpPrefix);
|
|
212
213
|
}
|
|
213
214
|
exports.isTmpFile = isTmpFile;
|
|
214
|
-
function assertImportsAreReExported(
|
|
215
|
-
const fileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(fileExports,
|
|
215
|
+
function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
|
|
216
|
+
const fileExport = (0, getConfigFileExport_js_1.getConfigFileExport)(fileExports, filePathToShowToUser2);
|
|
216
217
|
const exportedStrings = getExportedStrings(fileExport);
|
|
217
218
|
Object.values(exportedStrings).forEach((exportVal) => {
|
|
218
219
|
if (typeof exportVal !== 'string')
|
|
219
220
|
return;
|
|
220
|
-
if (!(0,
|
|
221
|
+
if (!(0, transformImportStatements_js_1.isImportData)(exportVal))
|
|
221
222
|
return;
|
|
222
223
|
const importString = exportVal;
|
|
223
|
-
|
|
224
|
+
fileImportsTransformed.forEach((fileImport) => {
|
|
224
225
|
if (fileImport.importString === importString) {
|
|
225
226
|
fileImport.isReExported = true;
|
|
226
227
|
}
|
|
227
228
|
});
|
|
228
229
|
});
|
|
229
|
-
const
|
|
230
|
-
if (
|
|
230
|
+
const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
|
|
231
|
+
if (fileImportsTransformedUnused.length === 0)
|
|
231
232
|
return;
|
|
232
|
-
const importStatements = (0, utils_js_1.unique)(
|
|
233
|
-
const importNamesUnused =
|
|
234
|
-
const singular =
|
|
235
|
-
(0, utils_js_1.assertWarning)(
|
|
236
|
-
`${
|
|
233
|
+
const importStatements = (0, utils_js_1.unique)(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
|
|
234
|
+
const importNamesUnused = fileImportsTransformedUnused.map((fi) => picocolors_1.default.cyan(fi.importLocalName)).join(', ');
|
|
235
|
+
const singular = fileImportsTransformedUnused.length === 1;
|
|
236
|
+
(0, utils_js_1.assertWarning)(fileImportsTransformedUnused.length === 0, [
|
|
237
|
+
`${filePathToShowToUser2} imports the following:`,
|
|
237
238
|
...importStatements.map((s) => picocolors_1.default.cyan(` ${s}`)),
|
|
238
239
|
`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
240
|
].join('\n'), { onlyOnce: true });
|
|
@@ -275,10 +276,19 @@ function triggerPrepareStackTrace(err) {
|
|
|
275
276
|
}
|
|
276
277
|
}
|
|
277
278
|
function getErrIntroMsg(operation, filePath) {
|
|
279
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
278
280
|
const msg = [
|
|
281
|
+
// prettier ignore
|
|
279
282
|
picocolors_1.default.red(`Failed to ${operation}`),
|
|
280
|
-
picocolors_1.default.bold(picocolors_1.default.red(
|
|
283
|
+
picocolors_1.default.bold(picocolors_1.default.red(filePathToShowToUser2)),
|
|
281
284
|
picocolors_1.default.red(`because:`)
|
|
282
285
|
].join(' ');
|
|
283
286
|
return msg;
|
|
284
287
|
}
|
|
288
|
+
/** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
|
|
289
|
+
function getFilePathToShowToUser2(filePath) {
|
|
290
|
+
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
291
|
+
const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
|
|
292
|
+
(0, utils_js_1.assert)(filePathToShowToUser2);
|
|
293
|
+
return filePathToShowToUser2;
|
|
294
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PROJECT_VERSION = exports.projectInfo = void 0;
|
|
4
4
|
const assertSingleInstance_js_1 = require("./assertSingleInstance.js");
|
|
5
|
-
const PROJECT_VERSION = '0.4.159-commit-
|
|
5
|
+
const PROJECT_VERSION = '0.4.159-commit-71760c7';
|
|
6
6
|
exports.PROJECT_VERSION = PROJECT_VERSION;
|
|
7
7
|
const projectInfo = {
|
|
8
8
|
projectName: 'Vike',
|
|
@@ -7,7 +7,7 @@ import path from 'path';
|
|
|
7
7
|
import { configDefinitionsBuiltIn, configDefinitionsBuiltInGlobal } from './getVikeConfig/configDefinitionsBuiltIn.js';
|
|
8
8
|
import { getLocationId, getFilesystemRouteString, getFilesystemRouteDefinedBy, isInherited, sortAfterInheritanceOrder, isGlobalLocation, applyFilesystemRoutingRootEffect } from './getVikeConfig/filesystemRouting.js';
|
|
9
9
|
import { isTmpFile, transpileAndExecuteFile } from './transpileAndExecuteFile.js';
|
|
10
|
-
import { parseImportData } from './
|
|
10
|
+
import { parseImportData } from './transformImportStatements.js';
|
|
11
11
|
import { isConfigInvalid, isConfigInvalid_set } from '../../../../runtime/renderPage/isConfigInvalid.js';
|
|
12
12
|
import { getViteDevServer } from '../../../../runtime/globalContext.js';
|
|
13
13
|
import { logConfigError, logConfigErrorRecover } from '../../../shared/loggerNotProd.js';
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { transformImportStatements };
|
|
2
2
|
export { parseImportData };
|
|
3
3
|
export { isImportData };
|
|
4
4
|
export type { FileImport };
|
|
5
5
|
export type { ImportData };
|
|
6
|
+
export { isVikeRealImport };
|
|
6
7
|
type FileImport = {
|
|
7
8
|
importStatementCode: string;
|
|
8
9
|
importString: string;
|
|
9
10
|
importLocalName: string;
|
|
10
11
|
};
|
|
11
|
-
declare function
|
|
12
|
-
|
|
12
|
+
declare function transformImportStatements(code: string, filePathToShowToUser2: string): {
|
|
13
|
+
noTransformation: true;
|
|
13
14
|
} | {
|
|
14
|
-
|
|
15
|
+
noTransformation: false;
|
|
15
16
|
code: string;
|
|
16
|
-
|
|
17
|
+
fileImportsTransformed: FileImport[];
|
|
17
18
|
};
|
|
18
19
|
/**
|
|
19
20
|
* Data Structure holding info about import statement:
|
|
@@ -41,3 +42,4 @@ declare module 'estree' {
|
|
|
41
42
|
end: number;
|
|
42
43
|
}
|
|
43
44
|
}
|
|
45
|
+
declare function isVikeRealImport(code: string, posStart: number): boolean;
|
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { transformImportStatements };
|
|
2
2
|
export { parseImportData };
|
|
3
3
|
export { isImportData };
|
|
4
|
+
// For ./transformImportStatements.spec.ts
|
|
5
|
+
export { isVikeRealImport };
|
|
4
6
|
// Playground: https://github.com/brillout/acorn-playground
|
|
7
|
+
// Import attributes support: https://github.com/acornjs/acorn/issues/983
|
|
8
|
+
// - Isn't stage 4 yet: https://github.com/tc39/proposal-import-attributes
|
|
5
9
|
import { parse } from 'acorn';
|
|
6
10
|
import { assert, assertUsage, assertWarning, styleFileRE } from '../../../utils.js';
|
|
7
11
|
import pc from '@brillout/picocolors';
|
|
8
|
-
function
|
|
12
|
+
function transformImportStatements(code, filePathToShowToUser2) {
|
|
9
13
|
const spliceOperations = [];
|
|
10
|
-
const
|
|
14
|
+
const fileImportsTransformed = [];
|
|
15
|
+
// Performance trick
|
|
16
|
+
if (!code.includes('import'))
|
|
17
|
+
return { noTransformation: true };
|
|
11
18
|
const imports = getImports(code);
|
|
12
19
|
if (imports.length === 0)
|
|
13
|
-
return {
|
|
20
|
+
return { noTransformation: true };
|
|
14
21
|
imports.forEach((node) => {
|
|
15
22
|
if (node.type !== 'ImportDeclaration')
|
|
16
23
|
return;
|
|
17
24
|
const importPath = node.source.value;
|
|
18
25
|
assert(typeof importPath === 'string');
|
|
19
26
|
const { start, end } = node;
|
|
27
|
+
if (isVikeRealImport(code, end))
|
|
28
|
+
return;
|
|
20
29
|
const importStatementCode = code.slice(start, end);
|
|
21
30
|
// No variable imported
|
|
22
31
|
if (node.specifiers.length === 0) {
|
|
@@ -29,7 +38,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
29
38
|
quote = pc.bold(pc.red(quote));
|
|
30
39
|
}
|
|
31
40
|
const errMsg = [
|
|
32
|
-
`As explained in https://vike.dev/header-file the following import in ${
|
|
41
|
+
`As explained in https://vike.dev/header-file the following import in ${filePathToShowToUser2} has no effect:`,
|
|
33
42
|
quote
|
|
34
43
|
].join('\n');
|
|
35
44
|
if (!isWarning) {
|
|
@@ -57,7 +66,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
57
66
|
})();
|
|
58
67
|
const importString = serializeImportData({ importPath, exportName, importStringWasGenerated: true });
|
|
59
68
|
replacement += `const ${importLocalName} = '${importString}';`;
|
|
60
|
-
|
|
69
|
+
fileImportsTransformed.push({
|
|
61
70
|
importStatementCode,
|
|
62
71
|
importString,
|
|
63
72
|
importLocalName
|
|
@@ -70,7 +79,7 @@ function replaceImportStatements(code, filePathToShowToUser) {
|
|
|
70
79
|
});
|
|
71
80
|
});
|
|
72
81
|
const codeMod = spliceMany(code, spliceOperations);
|
|
73
|
-
return { code: codeMod,
|
|
82
|
+
return { code: codeMod, fileImportsTransformed, noTransformation: false };
|
|
74
83
|
}
|
|
75
84
|
function getImports(code) {
|
|
76
85
|
const { body } = parse(code, {
|
|
@@ -146,3 +155,10 @@ function indent(str) {
|
|
|
146
155
|
.map((s) => ` ${s}`)
|
|
147
156
|
.join('\n');
|
|
148
157
|
}
|
|
158
|
+
function isVikeRealImport(code, posStart) {
|
|
159
|
+
let posEnd = code.indexOf('\n', posStart);
|
|
160
|
+
if (posEnd === -1)
|
|
161
|
+
posEnd = code.length;
|
|
162
|
+
const lineEnd = code.slice(posStart, posEnd);
|
|
163
|
+
return lineEnd.includes('@vike-real-import');
|
|
164
|
+
}
|
|
@@ -8,14 +8,14 @@ import path from 'path';
|
|
|
8
8
|
import pc from '@brillout/picocolors';
|
|
9
9
|
import { import_ } from '@brillout/import';
|
|
10
10
|
import { assertPosixPath, getRandomId, assertIsNotProductionRuntime, assert, unique, assertWarning, isObject, toPosixPath } from '../../../utils.js';
|
|
11
|
-
import { isImportData,
|
|
11
|
+
import { isImportData, transformImportStatements } from './transformImportStatements.js';
|
|
12
12
|
import { vikeConfigDependencies } from './getVikeConfig.js';
|
|
13
13
|
import 'source-map-support/register.js';
|
|
14
14
|
import { getConfigFileExport } from './getConfigFileExport.js';
|
|
15
15
|
assertIsNotProductionRuntime();
|
|
16
16
|
async function transpileAndExecuteFile(filePath, isValueFile, userRootDir) {
|
|
17
|
-
const { code,
|
|
18
|
-
const { fileExports } = await executeFile(filePath, code,
|
|
17
|
+
const { code, fileImportsTransformed } = await transpileFile(filePath, isValueFile, userRootDir);
|
|
18
|
+
const { fileExports } = await executeFile(filePath, code, fileImportsTransformed, isValueFile);
|
|
19
19
|
return { fileExports };
|
|
20
20
|
}
|
|
21
21
|
async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
@@ -23,39 +23,40 @@ async function transpileFile(filePath, isValueFile, userRootDir) {
|
|
|
23
23
|
assertPosixPath(filePathAbsoluteFilesystem);
|
|
24
24
|
vikeConfigDependencies.add(filePathAbsoluteFilesystem);
|
|
25
25
|
let code = await transpileWithEsbuild(filePath, isValueFile, userRootDir);
|
|
26
|
-
let
|
|
26
|
+
let fileImportsTransformed = null;
|
|
27
27
|
{
|
|
28
|
-
const res =
|
|
28
|
+
const res = transformImports(code, filePath, isValueFile);
|
|
29
29
|
if (res) {
|
|
30
30
|
code = res.code;
|
|
31
|
-
|
|
31
|
+
fileImportsTransformed = res.fileImportsTransformed;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
return { code,
|
|
34
|
+
return { code, fileImportsTransformed };
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function transformImports(codeOriginal, filePath, isValueFile) {
|
|
37
37
|
// Do we need to remove the imports?
|
|
38
|
-
const { filePathAbsoluteFilesystem
|
|
38
|
+
const { filePathAbsoluteFilesystem } = filePath;
|
|
39
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
39
40
|
assertPosixPath(filePathAbsoluteFilesystem);
|
|
40
41
|
const isHeader = isHeaderFile(filePathAbsoluteFilesystem);
|
|
41
42
|
const isPageConfigFile = !isValueFile;
|
|
42
43
|
if (!isHeader && !isPageConfigFile) {
|
|
43
44
|
return null;
|
|
44
45
|
}
|
|
45
|
-
assertWarning(isPageConfigFile, `${
|
|
46
|
+
assertWarning(isPageConfigFile, `${filePathToShowToUser2} is a JavaScript header file (.h.js), but JavaScript header files should only be used for +config.h.js, see https://vike.dev/header-file`, { onlyOnce: true });
|
|
46
47
|
// Remove the imports
|
|
47
|
-
const res =
|
|
48
|
-
if (res.
|
|
48
|
+
const res = transformImportStatements(codeOriginal, filePathToShowToUser2);
|
|
49
|
+
if (res.noTransformation) {
|
|
49
50
|
return null;
|
|
50
51
|
}
|
|
51
|
-
const { code,
|
|
52
|
+
const { code, fileImportsTransformed } = res;
|
|
52
53
|
if (!isHeader) {
|
|
53
|
-
const filePathCorrect = appendHeaderFileExtension(
|
|
54
|
-
assertWarning(false, `Rename ${
|
|
54
|
+
const filePathCorrect = appendHeaderFileExtension(filePathToShowToUser2);
|
|
55
|
+
assertWarning(false, `Rename ${filePathToShowToUser2} to ${filePathCorrect}, see https://vike.dev/header-file`, {
|
|
55
56
|
onlyOnce: true
|
|
56
57
|
});
|
|
57
58
|
}
|
|
58
|
-
return { code,
|
|
59
|
+
return { code, fileImportsTransformed };
|
|
59
60
|
}
|
|
60
61
|
async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
61
62
|
const entryFilePath = filePath.filePathAbsoluteFilesystem;
|
|
@@ -131,7 +132,7 @@ async function transpileWithEsbuild(filePath, bundle, userRootDir) {
|
|
|
131
132
|
assert(typeof code === 'string');
|
|
132
133
|
return code;
|
|
133
134
|
}
|
|
134
|
-
async function executeFile(filePath, code,
|
|
135
|
+
async function executeFile(filePath, code, fileImportsTransformed, isValueFile) {
|
|
135
136
|
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
136
137
|
// Alternative to using a temporary file: https://github.com/vitejs/vite/pull/13269
|
|
137
138
|
// - But seems to break source maps, so I don't think it's worth it
|
|
@@ -156,10 +157,10 @@ async function executeFile(filePath, code, fileImports, isValueFile) {
|
|
|
156
157
|
// - import() returns `[Module: null prototype] { default: { onRenderClient: '...' }}`
|
|
157
158
|
// - We don't need this special object
|
|
158
159
|
fileExports = { ...fileExports };
|
|
159
|
-
if (
|
|
160
|
+
if (fileImportsTransformed && !isValueFile) {
|
|
160
161
|
assert(filePathRelativeToUserRootDir !== undefined);
|
|
161
|
-
const
|
|
162
|
-
assertImportsAreReExported(
|
|
162
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
163
|
+
assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2);
|
|
163
164
|
}
|
|
164
165
|
return { fileExports };
|
|
165
166
|
}
|
|
@@ -205,8 +206,8 @@ function isTmpFile(filePath) {
|
|
|
205
206
|
const fileName = path.posix.basename(filePath);
|
|
206
207
|
return fileName.startsWith(tmpPrefix);
|
|
207
208
|
}
|
|
208
|
-
function assertImportsAreReExported(
|
|
209
|
-
const fileExport = getConfigFileExport(fileExports,
|
|
209
|
+
function assertImportsAreReExported(fileImportsTransformed, fileExports, filePathToShowToUser2) {
|
|
210
|
+
const fileExport = getConfigFileExport(fileExports, filePathToShowToUser2);
|
|
210
211
|
const exportedStrings = getExportedStrings(fileExport);
|
|
211
212
|
Object.values(exportedStrings).forEach((exportVal) => {
|
|
212
213
|
if (typeof exportVal !== 'string')
|
|
@@ -214,20 +215,20 @@ function assertImportsAreReExported(fileImports, fileExports, filePathToShowToUs
|
|
|
214
215
|
if (!isImportData(exportVal))
|
|
215
216
|
return;
|
|
216
217
|
const importString = exportVal;
|
|
217
|
-
|
|
218
|
+
fileImportsTransformed.forEach((fileImport) => {
|
|
218
219
|
if (fileImport.importString === importString) {
|
|
219
220
|
fileImport.isReExported = true;
|
|
220
221
|
}
|
|
221
222
|
});
|
|
222
223
|
});
|
|
223
|
-
const
|
|
224
|
-
if (
|
|
224
|
+
const fileImportsTransformedUnused = fileImportsTransformed.filter((fi) => !fi.isReExported);
|
|
225
|
+
if (fileImportsTransformedUnused.length === 0)
|
|
225
226
|
return;
|
|
226
|
-
const importStatements = unique(
|
|
227
|
-
const importNamesUnused =
|
|
228
|
-
const singular =
|
|
229
|
-
assertWarning(
|
|
230
|
-
`${
|
|
227
|
+
const importStatements = unique(fileImportsTransformedUnused.map((fi) => fi.importStatementCode));
|
|
228
|
+
const importNamesUnused = fileImportsTransformedUnused.map((fi) => pc.cyan(fi.importLocalName)).join(', ');
|
|
229
|
+
const singular = fileImportsTransformedUnused.length === 1;
|
|
230
|
+
assertWarning(fileImportsTransformedUnused.length === 0, [
|
|
231
|
+
`${filePathToShowToUser2} imports the following:`,
|
|
231
232
|
...importStatements.map((s) => pc.cyan(` ${s}`)),
|
|
232
233
|
`But the import${singular ? '' : 's'} ${importNamesUnused} ${singular ? "isn't" : "aren't"} re-exported at ${pc.cyan('export default { ... }')} and therefore ${singular ? 'has' : 'have'} no effect, see explanation at https://vike.dev/header-file`
|
|
233
234
|
].join('\n'), { onlyOnce: true });
|
|
@@ -269,10 +270,19 @@ function triggerPrepareStackTrace(err) {
|
|
|
269
270
|
}
|
|
270
271
|
}
|
|
271
272
|
function getErrIntroMsg(operation, filePath) {
|
|
273
|
+
const filePathToShowToUser2 = getFilePathToShowToUser2(filePath);
|
|
272
274
|
const msg = [
|
|
275
|
+
// prettier ignore
|
|
273
276
|
pc.red(`Failed to ${operation}`),
|
|
274
|
-
pc.bold(pc.red(
|
|
277
|
+
pc.bold(pc.red(filePathToShowToUser2)),
|
|
275
278
|
pc.red(`because:`)
|
|
276
279
|
].join(' ');
|
|
277
280
|
return msg;
|
|
278
281
|
}
|
|
282
|
+
/** `filePath.filePathToShowToUser` may show the import path of a package, use `filePathToShowToUser2` instead always show a file path instead. */
|
|
283
|
+
function getFilePathToShowToUser2(filePath) {
|
|
284
|
+
const { filePathAbsoluteFilesystem, filePathRelativeToUserRootDir } = filePath;
|
|
285
|
+
const filePathToShowToUser2 = filePathRelativeToUserRootDir || filePathAbsoluteFilesystem;
|
|
286
|
+
assert(filePathToShowToUser2);
|
|
287
|
+
return filePathToShowToUser2;
|
|
288
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { projectInfo };
|
|
2
2
|
export { PROJECT_VERSION };
|
|
3
|
-
declare const PROJECT_VERSION: "0.4.159-commit-
|
|
3
|
+
declare const PROJECT_VERSION: "0.4.159-commit-71760c7";
|
|
4
4
|
declare const projectInfo: {
|
|
5
5
|
projectName: "Vike";
|
|
6
|
-
projectVersion: "0.4.159-commit-
|
|
6
|
+
projectVersion: "0.4.159-commit-71760c7";
|
|
7
7
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { projectInfo };
|
|
2
2
|
export { PROJECT_VERSION };
|
|
3
3
|
import { onProjectInfo } from './assertSingleInstance.js';
|
|
4
|
-
const PROJECT_VERSION = '0.4.159-commit-
|
|
4
|
+
const PROJECT_VERSION = '0.4.159-commit-71760c7';
|
|
5
5
|
const projectInfo = {
|
|
6
6
|
projectName: 'Vike',
|
|
7
7
|
projectVersion: PROJECT_VERSION
|