@open-pioneer/vite-plugin-pioneer 6.0.3 → 7.0.0
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/CHANGELOG.md +14 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +1388 -1747
- package/dist/index.js.map +1 -7
- package/package.json +20 -14
- package/types.d.ts +0 -72
package/dist/index.js
CHANGED
|
@@ -1,184 +1,202 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { dirname as dirname4 } from "node:path";
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join, posix, relative, resolve } from "node:path";
|
|
4
3
|
import { cwd } from "node:process";
|
|
5
|
-
import { RuntimeSupport
|
|
6
|
-
import { normalizePath as normalizePath5 } from "vite";
|
|
7
|
-
|
|
8
|
-
// src/codegen/generateAppMetadata.ts
|
|
9
|
-
import { RuntimeSupport } from "@open-pioneer/build-common";
|
|
10
|
-
|
|
11
|
-
// src/codegen/shared.ts
|
|
12
|
-
import { existsSync } from "fs";
|
|
13
|
-
import { basename, dirname, posix } from "node:path/posix";
|
|
14
|
-
import { join } from "path/posix";
|
|
4
|
+
import { BUILD_CONFIG_NAME, PackageMetadataV1, RuntimeSupport, createPackageConfigFromBuildConfig, createPackageConfigFromPackageMetadata, loadBuildConfig } from "@open-pioneer/build-common";
|
|
15
5
|
import { normalizePath } from "vite";
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
6
|
+
import { existsSync } from "fs";
|
|
7
|
+
import posix$1, { basename, dirname as dirname$1, posix as posix$2 } from "node:path/posix";
|
|
8
|
+
import { join as join$1 } from "path/posix";
|
|
9
|
+
import generateModule from "@babel/generator";
|
|
10
|
+
import templateModule from "@babel/template";
|
|
11
|
+
import nodesModule from "@babel/types";
|
|
12
|
+
import { readFile as readFile$1, realpath } from "fs/promises";
|
|
13
|
+
import { basename as basename$1, dirname as dirname$2, posix as posix$3 } from "path";
|
|
14
|
+
import { ResolverFactory } from "oxc-resolver";
|
|
15
|
+
import { glob } from "tinyglobby";
|
|
16
|
+
import pkg from "debug";
|
|
17
|
+
import * as fs from "node:fs";
|
|
18
|
+
import { existsSync as existsSync$1 } from "node:fs";
|
|
19
|
+
import { findDepPkgJsonPath } from "vitefu";
|
|
20
|
+
import { load } from "js-yaml";
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
import { createErrorMap, fromZodError } from "zod-validation-error";
|
|
23
|
+
//#region src/codegen/shared.ts
|
|
24
|
+
/**
|
|
25
|
+
* MODULE ID ENCODING
|
|
26
|
+
* ==================
|
|
27
|
+
*
|
|
28
|
+
* There are two encoding schemes for virtual modules in this file:
|
|
29
|
+
*
|
|
30
|
+
* 1. Leading null byte, e.g. `\0open-pioneer:deployment`.
|
|
31
|
+
* This is used for "global" modules that are not scoped to a specific file, app or package.
|
|
32
|
+
* This encoding is the usual practice in the rollup / vite ecosystem, making it very reliable.
|
|
33
|
+
* It has the disadvantage that the generated files show up "somewhere" in the browser debugger.
|
|
34
|
+
* 2. Magic filenames, e.g. /path/to/app/@@open-pioneer-app.
|
|
35
|
+
* These use a different prefix ("@@") for file names so they show up next to the relevant location,
|
|
36
|
+
* for example as if they were developed in a specific package.
|
|
37
|
+
* This was needed for both better debugging experience and to make some imports work well.
|
|
38
|
+
*/
|
|
39
|
+
const VIRTUAL_PREFIX = "\0open-pioneer:";
|
|
40
|
+
const VIRTUAL_PREFIX_RE = /\0open-pioneer:(?<suffix>.*)/;
|
|
41
|
+
const APP_MODULE = "@@open-pioneer-app";
|
|
42
|
+
const APP_META_QUERY = "open-pioneer-app";
|
|
43
|
+
const APP_META_RE = /[?&]open-pioneer-app(?:$|&)/;
|
|
44
|
+
const APP_PACKAGES_QUERY = "open-pioneer-packages";
|
|
45
|
+
const APP_PACKAGES_RE = /[?&]open-pioneer-packages(?:$|&)/;
|
|
46
|
+
const APP_CSS_QUERY = "open-pioneer-styles";
|
|
47
|
+
const APP_CSS_RE = /[?&]open-pioneer-styles(?:$|&)/;
|
|
48
|
+
const APP_I18N_INDEX_QUERY = "open-pioneer-i18n-index";
|
|
49
|
+
const APP_I18N_INDEX_RE = /[?&]open-pioneer-i18n-index(?:$|&)/;
|
|
50
|
+
const APP_I18N_QUERY = "open-pioneer-i18n";
|
|
51
|
+
const APP_I18N_RE = /[?&]open-pioneer-i18n(?:$|&)/;
|
|
52
|
+
const APP_I18N_LOCALE_RE = /[?&]locale=(?<locale>.*?)(?:$|&)/;
|
|
53
|
+
const PACKAGE_HOOKS_MODULE = "@@open-pioneer-react-hooks";
|
|
54
|
+
const SOURCE_INFO_MODULE = "@@open-pioneer-source-info";
|
|
55
|
+
const SOURCE_INFO_MODULE_MODULE_PATH_RE = /@@open-pioneer-source-info\/(?<module_path>.*)&lang=js$/;
|
|
56
|
+
const SOURCE_FILE_RE = /^(.*?)(?:\?|$)/;
|
|
57
|
+
/**
|
|
58
|
+
* Filter for rolldown.
|
|
59
|
+
*
|
|
60
|
+
* These regular expressions should match against our virtual modules (see above)
|
|
61
|
+
* but should filter out as many unrelated module ids as possible for performance.
|
|
62
|
+
*/
|
|
63
|
+
const VIRTUAL_ID_FILTER = [
|
|
64
|
+
/^\0?open-pioneer:/,
|
|
65
|
+
/[?&]open-pioneer/,
|
|
66
|
+
/@@open-pioneer/
|
|
67
|
+
];
|
|
68
|
+
/**
|
|
69
|
+
* Takes a module id as input and parses it.
|
|
70
|
+
* If this plugin is not responsible to load the module id, this function returns `undefined`.
|
|
71
|
+
*
|
|
72
|
+
* Otherwise, the module is parsed into an object and returned.
|
|
73
|
+
*/
|
|
35
74
|
function parseVirtualModuleId(inputModuleId) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const base = basename(sourceFile);
|
|
56
|
-
if (base === PACKAGE_HOOKS_MODULE) {
|
|
57
|
-
return {
|
|
58
|
-
type: "package-hooks",
|
|
59
|
-
packageDirectory: dirname(sourceFile)
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
const sourceInfoMatch = moduleId.match(SOURCE_INFO_MODULE_MODULE_PATH_RE);
|
|
63
|
-
if (sourceInfoMatch) {
|
|
64
|
-
return parseSourceInfoModule(moduleId, sourceInfoMatch);
|
|
65
|
-
}
|
|
66
|
-
if (base === APP_MODULE) {
|
|
67
|
-
return parseAppModuleId(sourceFile, moduleId);
|
|
68
|
-
}
|
|
69
|
-
return void 0;
|
|
75
|
+
const moduleId = normalizePath(inputModuleId);
|
|
76
|
+
if (!moduleId) return;
|
|
77
|
+
if (moduleId.startsWith("\0")) {
|
|
78
|
+
const virtualPrefixMatch = inputModuleId.match(VIRTUAL_PREFIX_RE);
|
|
79
|
+
if (virtualPrefixMatch) {
|
|
80
|
+
if (virtualPrefixMatch.groups?.["suffix"] === "deployment") return { type: "deployment" };
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const sourceFile = getSourceFile(moduleId);
|
|
85
|
+
if (!sourceFile) return;
|
|
86
|
+
const base = basename(sourceFile);
|
|
87
|
+
if (base === PACKAGE_HOOKS_MODULE) return {
|
|
88
|
+
type: "package-hooks",
|
|
89
|
+
packageDirectory: dirname$1(sourceFile)
|
|
90
|
+
};
|
|
91
|
+
const sourceInfoMatch = moduleId.match(SOURCE_INFO_MODULE_MODULE_PATH_RE);
|
|
92
|
+
if (sourceInfoMatch) return parseSourceInfoModule(moduleId, sourceInfoMatch);
|
|
93
|
+
if (base === APP_MODULE) return parseAppModuleId(sourceFile, moduleId);
|
|
70
94
|
}
|
|
71
95
|
function parseSourceInfoModule(moduleId, sourceInfoMatch) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const fullModulePath = join(moduleRoot, relativeModulePath);
|
|
85
|
-
const packageJsonPath = findPackageJson(dirname(fullModulePath), moduleRoot);
|
|
86
|
-
if (!packageJsonPath) {
|
|
87
|
-
throw new Error(`Cannot determine package.json for source info module id: ${moduleId}`);
|
|
88
|
-
}
|
|
89
|
-
const packageDirectory = dirname(packageJsonPath);
|
|
90
|
-
return { type: "source-info", modulePath: relativeModulePath, packageDirectory };
|
|
96
|
+
const encodedModulePath = sourceInfoMatch?.groups?.["module_path"];
|
|
97
|
+
if (!encodedModulePath) throw new Error(`Missing module path in source info module id: ${moduleId}`);
|
|
98
|
+
const relativeModulePath = decodeURIComponent(encodedModulePath);
|
|
99
|
+
const moduleRoot = moduleId.substring(0, moduleId.indexOf(`${SOURCE_INFO_MODULE}`));
|
|
100
|
+
if (!moduleRoot) throw new Error(`Cannot determine module root directory for source info module id: ${moduleId}`);
|
|
101
|
+
const packageJsonPath = findPackageJson(dirname$1(join$1(moduleRoot, relativeModulePath)), moduleRoot);
|
|
102
|
+
if (!packageJsonPath) throw new Error(`Cannot determine package.json for source info module id: ${moduleId}`);
|
|
103
|
+
return {
|
|
104
|
+
type: "source-info",
|
|
105
|
+
modulePath: relativeModulePath,
|
|
106
|
+
packageDirectory: dirname$1(packageJsonPath)
|
|
107
|
+
};
|
|
91
108
|
}
|
|
92
109
|
function parseAppModuleId(sourceFile, moduleId) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
const packageDirectory = dirname$1(sourceFile);
|
|
111
|
+
if (moduleId.match(APP_META_RE)) return {
|
|
112
|
+
type: "app-meta",
|
|
113
|
+
packageDirectory
|
|
114
|
+
};
|
|
115
|
+
else if (moduleId.match(APP_PACKAGES_RE)) return {
|
|
116
|
+
type: "app-packages",
|
|
117
|
+
packageDirectory
|
|
118
|
+
};
|
|
119
|
+
else if (moduleId.match(APP_CSS_RE)) {
|
|
120
|
+
if (moduleId.endsWith(".map")) return;
|
|
121
|
+
return {
|
|
122
|
+
type: "app-css",
|
|
123
|
+
packageDirectory
|
|
124
|
+
};
|
|
125
|
+
} else if (moduleId.match(APP_I18N_INDEX_RE)) return {
|
|
126
|
+
type: "app-i18n-index",
|
|
127
|
+
packageDirectory
|
|
128
|
+
};
|
|
129
|
+
else if (moduleId.match(APP_I18N_RE)) {
|
|
130
|
+
const type = "app-i18n";
|
|
131
|
+
const locale = moduleId.match(APP_I18N_LOCALE_RE)?.groups?.["locale"];
|
|
132
|
+
if (!locale) throw new Error("Missing locale name in i18n module request");
|
|
133
|
+
return {
|
|
134
|
+
type,
|
|
135
|
+
packageDirectory,
|
|
136
|
+
locale
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
throw new Error(`Unexpected module id: ${moduleId}`);
|
|
119
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Serializes the given virtual module descriptor into a module id string.
|
|
143
|
+
*/
|
|
120
144
|
function serializeModuleId(mod) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return `${mod.packageDirectory}/${APP_MODULE}?${APP_CSS_QUERY}`;
|
|
132
|
-
case "app-i18n-index":
|
|
133
|
-
return `${mod.packageDirectory}/${APP_MODULE}?${APP_I18N_INDEX_QUERY}`;
|
|
134
|
-
case "app-i18n":
|
|
135
|
-
return `${mod.packageDirectory}/${APP_MODULE}?${APP_I18N_QUERY}&locale=${mod.locale}`;
|
|
136
|
-
case "deployment":
|
|
137
|
-
return `${VIRTUAL_PREFIX}deployment`;
|
|
138
|
-
}
|
|
145
|
+
switch (mod.type) {
|
|
146
|
+
case "package-hooks": return `${mod.packageDirectory}/${PACKAGE_HOOKS_MODULE}`;
|
|
147
|
+
case "source-info": return serializeSourceInfoModule(mod);
|
|
148
|
+
case "app-meta": return `${mod.packageDirectory}/${APP_MODULE}?${APP_META_QUERY}`;
|
|
149
|
+
case "app-packages": return `${mod.packageDirectory}/${APP_MODULE}?${APP_PACKAGES_QUERY}`;
|
|
150
|
+
case "app-css": return `${mod.packageDirectory}/${APP_MODULE}?${APP_CSS_QUERY}`;
|
|
151
|
+
case "app-i18n-index": return `${mod.packageDirectory}/${APP_MODULE}?${APP_I18N_INDEX_QUERY}`;
|
|
152
|
+
case "app-i18n": return `${mod.packageDirectory}/${APP_MODULE}?${APP_I18N_QUERY}&locale=${mod.locale}`;
|
|
153
|
+
case "deployment": return `${VIRTUAL_PREFIX}deployment`;
|
|
154
|
+
}
|
|
139
155
|
}
|
|
140
156
|
function serializeSourceInfoModule(mod) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
return `${mod.packageDirectory}/${SOURCE_INFO_MODULE}/${encodeURIComponent(relativeModulePath)}&lang=js`;
|
|
157
|
+
const packageDirectory = mod.packageDirectory;
|
|
158
|
+
const modulePath = mod.modulePath;
|
|
159
|
+
const normalizedModulePath = normalizePath(modulePath);
|
|
160
|
+
const relativeModulePath = posix$2.relative(packageDirectory, normalizedModulePath);
|
|
161
|
+
if (relativeModulePath.match(/^\.\.?[\\/]/)) throw new Error("Internal error: unexpected relative path");
|
|
162
|
+
return `${mod.packageDirectory}/${SOURCE_INFO_MODULE}/${encodeURIComponent(relativeModulePath)}&lang=js`;
|
|
149
163
|
}
|
|
150
164
|
function getSourceFile(moduleId) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return void 0;
|
|
155
|
-
}
|
|
156
|
-
return sourceFile;
|
|
165
|
+
const sourceFile = moduleId.match(SOURCE_FILE_RE)?.[1];
|
|
166
|
+
if (!sourceFile || moduleId[0] == "\0") return;
|
|
167
|
+
return sourceFile;
|
|
157
168
|
}
|
|
158
169
|
function findPackageJson(startDir, rootDir) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
const parent = dirname(dir);
|
|
169
|
-
dir = parent === dir || parent === "." ? "" : parent;
|
|
170
|
-
}
|
|
171
|
-
return void 0;
|
|
170
|
+
let dir = startDir;
|
|
171
|
+
while (dir) {
|
|
172
|
+
const candidate = join$1(dir, "package.json");
|
|
173
|
+
if (existsSync(candidate)) return candidate;
|
|
174
|
+
if (normalizePath(dir) == normalizePath(rootDir)) return;
|
|
175
|
+
const parent = dirname$1(dir);
|
|
176
|
+
dir = parent === dir || parent === "." ? "" : parent;
|
|
177
|
+
}
|
|
172
178
|
}
|
|
173
|
-
|
|
174
|
-
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/codegen/generateAppMetadata.ts
|
|
181
|
+
/**
|
|
182
|
+
* Generates the main app metadata module.
|
|
183
|
+
* It delegates the actual metadata generation to auxiliary modules.
|
|
184
|
+
*/
|
|
175
185
|
function generateAppMetadata(packageDirectory, metadataModuleId, runtimeMetadataVersion) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
186
|
+
const packagesModule = serializeModuleId({
|
|
187
|
+
type: "app-packages",
|
|
188
|
+
packageDirectory
|
|
189
|
+
});
|
|
190
|
+
const cssModule = serializeModuleId({
|
|
191
|
+
type: "app-css",
|
|
192
|
+
packageDirectory
|
|
193
|
+
}) + "&inline&lang.scss";
|
|
194
|
+
const i18nModule = serializeModuleId({
|
|
195
|
+
type: "app-i18n-index",
|
|
196
|
+
packageDirectory
|
|
197
|
+
});
|
|
198
|
+
const boxMessages = RuntimeSupport.getRuntimeFeatures(runtimeMetadataVersion).supportsMessageBox;
|
|
199
|
+
return `
|
|
182
200
|
import { createBox } from ${JSON.stringify(metadataModuleId)};
|
|
183
201
|
import packages from ${JSON.stringify(packagesModule)};
|
|
184
202
|
import stylesString from ${JSON.stringify(cssModule)};
|
|
@@ -229,27 +247,26 @@ export {
|
|
|
229
247
|
};
|
|
230
248
|
`.trim();
|
|
231
249
|
}
|
|
232
|
-
|
|
233
|
-
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/codegen/generateCombinedCss.ts
|
|
252
|
+
/**
|
|
253
|
+
* Generates a css file that imports the css file defined by the packages.
|
|
254
|
+
*/
|
|
234
255
|
function generateCombinedCss(packages) {
|
|
235
|
-
|
|
236
|
-
|
|
256
|
+
const cssImport = (file) => `@import ${JSON.stringify(file)};`;
|
|
257
|
+
return packages.filter((pkg) => !!pkg.cssFilePath).map((pkg) => cssImport(pkg.cssFilePath)).join("\n");
|
|
237
258
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
var ReportableError = class extends Error {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
var nodes = nodesModule;
|
|
250
|
-
|
|
251
|
-
// src/codegen/generateI18n.ts
|
|
252
|
-
var INDEX_TEMPLATE = template.program(`
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/ReportableError.ts
|
|
261
|
+
var ReportableError = class extends Error {};
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region src/utils/babelDeps.ts
|
|
264
|
+
const generate = generateModule.default || generateModule;
|
|
265
|
+
const template = templateModule.default || templateModule;
|
|
266
|
+
const nodes = nodesModule;
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/codegen/generateI18n.ts
|
|
269
|
+
const INDEX_TEMPLATE = template.program(`
|
|
253
270
|
export const locales = %%LOCALES_ARRAY%%;
|
|
254
271
|
|
|
255
272
|
export function loadMessages(locale) {
|
|
@@ -257,118 +274,93 @@ var INDEX_TEMPLATE = template.program(`
|
|
|
257
274
|
throw new Error(\`Unsupported locale: '\${locale}'\`);
|
|
258
275
|
}
|
|
259
276
|
`);
|
|
260
|
-
|
|
277
|
+
const IMPORT_TEMPLATE = template.statement(`
|
|
261
278
|
return import(%%MODULE_ID%%).then(mod => mod.default);
|
|
262
279
|
`);
|
|
263
|
-
|
|
280
|
+
const MESSAGES_TEMPLATE = template.program(`
|
|
264
281
|
const messages = JSON.parse(%%SERIALIZED%%);
|
|
265
282
|
export default messages;
|
|
266
283
|
`);
|
|
284
|
+
/**
|
|
285
|
+
* Generates a lookup table for the languages supported by the given application.
|
|
286
|
+
*/
|
|
267
287
|
function generateI18nIndex(packageDirectory, locales) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return nodes.switchCase(nodes.stringLiteral(locale), [importStatement]);
|
|
283
|
-
})
|
|
284
|
-
);
|
|
285
|
-
const program = INDEX_TEMPLATE({
|
|
286
|
-
LOCALES_ARRAY: localesArray,
|
|
287
|
-
LOCALE_SWITCH: switchStmt
|
|
288
|
-
});
|
|
289
|
-
return generate(program).code;
|
|
288
|
+
const localesArray = nodes.arrayExpression(locales.map((locale) => nodes.stringLiteral(locale)));
|
|
289
|
+
const switchStmt = nodes.switchStatement(nodes.identifier("locale"), locales.map((locale) => {
|
|
290
|
+
const localeModuleId = serializeModuleId({
|
|
291
|
+
type: "app-i18n",
|
|
292
|
+
packageDirectory,
|
|
293
|
+
locale
|
|
294
|
+
});
|
|
295
|
+
const importStatement = IMPORT_TEMPLATE({ MODULE_ID: nodes.stringLiteral(localeModuleId) });
|
|
296
|
+
return nodes.switchCase(nodes.stringLiteral(locale), [importStatement]);
|
|
297
|
+
}));
|
|
298
|
+
return generate(INDEX_TEMPLATE({
|
|
299
|
+
LOCALES_ARRAY: localesArray,
|
|
300
|
+
LOCALE_SWITCH: switchStmt
|
|
301
|
+
})).code;
|
|
290
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Generates a messages module containing a single default export: the messages record.
|
|
305
|
+
*
|
|
306
|
+
* Messages are taken from the given packages (for the specified locale).
|
|
307
|
+
*/
|
|
291
308
|
async function generateI18nMessages(options) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
continue;
|
|
320
|
-
}
|
|
321
|
-
const updatedMessages = new Map([...existingMessages, ...messages]);
|
|
322
|
-
packageMessages.set(packageName, updatedMessages);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
for (const packageName of packagesNeedingMessages) {
|
|
326
|
-
const messages = packageMessages.get(packageName);
|
|
327
|
-
if (!messages) {
|
|
328
|
-
throw new ReportableError(
|
|
329
|
-
`Package '${packageName}' does not support locale '${locale}'. Either update the package or add messages from the app via 'overrides'.`
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
const allMessages = {};
|
|
334
|
-
for (const [packageName, messages] of packageMessages.entries()) {
|
|
335
|
-
allMessages[packageName] = Object.fromEntries(messages.entries());
|
|
336
|
-
}
|
|
337
|
-
const serialized = JSON.stringify(allMessages);
|
|
338
|
-
const program = MESSAGES_TEMPLATE({
|
|
339
|
-
SERIALIZED: nodes.stringLiteral(serialized)
|
|
340
|
-
});
|
|
341
|
-
return generate(program).code;
|
|
309
|
+
const { locale, appName, packages, loadI18n } = options;
|
|
310
|
+
const packageMessages = /* @__PURE__ */ new Map();
|
|
311
|
+
const packagesNeedingMessages = /* @__PURE__ */ new Set();
|
|
312
|
+
let packageOverrides;
|
|
313
|
+
for (const pkg of packages) {
|
|
314
|
+
if (pkg.i18nPaths.size !== 0) packagesNeedingMessages.add(pkg.name);
|
|
315
|
+
const filePath = pkg.i18nPaths.get(locale);
|
|
316
|
+
if (!filePath) continue;
|
|
317
|
+
const { messages, overrides } = await loadI18n(pkg, filePath);
|
|
318
|
+
if (pkg.name === appName) packageOverrides = overrides;
|
|
319
|
+
else if (overrides) throw new ReportableError(`Unexpected 'overrides' block in '${filePath}'. Overrides are only supported in the app.`);
|
|
320
|
+
packageMessages.set(pkg.name, messages);
|
|
321
|
+
}
|
|
322
|
+
if (packageOverrides) for (const [packageName, messages] of packageOverrides.entries()) {
|
|
323
|
+
const existingMessages = packageMessages.get(packageName);
|
|
324
|
+
if (!existingMessages) {
|
|
325
|
+
packageMessages.set(packageName, messages);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
const updatedMessages = new Map([...existingMessages, ...messages]);
|
|
329
|
+
packageMessages.set(packageName, updatedMessages);
|
|
330
|
+
}
|
|
331
|
+
for (const packageName of packagesNeedingMessages) if (!packageMessages.get(packageName)) throw new ReportableError(`Package '${packageName}' does not support locale '${locale}'. Either update the package or add messages from the app via 'overrides'.`);
|
|
332
|
+
const allMessages = {};
|
|
333
|
+
for (const [packageName, messages] of packageMessages.entries()) allMessages[packageName] = Object.fromEntries(messages.entries());
|
|
334
|
+
const serialized = JSON.stringify(allMessages);
|
|
335
|
+
return generate(MESSAGES_TEMPLATE({ SERIALIZED: nodes.stringLiteral(serialized) })).code;
|
|
342
336
|
}
|
|
343
|
-
|
|
344
|
-
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/codegen/IdGenerator.ts
|
|
345
339
|
var IdGenerator = class {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
340
|
+
#existing = /* @__PURE__ */ new Map();
|
|
341
|
+
/**
|
|
342
|
+
* Returns a unique identifier (in the context of this instance) that resembles the input value.
|
|
343
|
+
*/
|
|
344
|
+
generate(input) {
|
|
345
|
+
const clean = cleanName(input);
|
|
346
|
+
const existing = this.#existing;
|
|
347
|
+
const count = existing.get(clean) ?? 0;
|
|
348
|
+
existing.set(clean, count + 1);
|
|
349
|
+
return count === 0 ? clean : `${clean}_${count}`;
|
|
350
|
+
}
|
|
357
351
|
};
|
|
358
352
|
function cleanName(input) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
return clean || "var";
|
|
353
|
+
let clean = input.replace(/[^A-Za-z0-9_]+/g, "_");
|
|
354
|
+
clean = clean.replace(/[\d_]+$/, "");
|
|
355
|
+
if (/^\d/.test(clean)) clean = "_" + clean;
|
|
356
|
+
return clean || "var";
|
|
365
357
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/codegen/generatePackagesMetadata.ts
|
|
360
|
+
const SERVICE_IMPORT = template.statement(`
|
|
369
361
|
import { %%SERVICE_NAME%% as %%IMPORT_NAME%% } from %%IMPORT_SOURCE%%;
|
|
370
362
|
`);
|
|
371
|
-
|
|
363
|
+
const PKG_OBJECT = template.expression(`
|
|
372
364
|
{
|
|
373
365
|
name: %%PACKAGE_NAME%%,
|
|
374
366
|
services: %%PACKAGE_SERVICES%%,
|
|
@@ -376,7 +368,7 @@ var PKG_OBJECT = template.expression(`
|
|
|
376
368
|
properties: %%PROPERTIES%%
|
|
377
369
|
}
|
|
378
370
|
`);
|
|
379
|
-
|
|
371
|
+
const SERVICE_OBJECT = template.expression(`
|
|
380
372
|
{
|
|
381
373
|
name: %%SERVICE_NAME%%,
|
|
382
374
|
clazz: %%SERVICE_IMPORT%%,
|
|
@@ -384,1589 +376,1238 @@ var SERVICE_OBJECT = template.expression(`
|
|
|
384
376
|
references: %%SERVICE_REFERENCES%%
|
|
385
377
|
}
|
|
386
378
|
`);
|
|
387
|
-
|
|
379
|
+
const INTERFACE_OBJECT = template.expression(`
|
|
388
380
|
{
|
|
389
381
|
name: %%INTERFACE_NAME%%,
|
|
390
382
|
qualifier: %%QUALIFIER%%
|
|
391
383
|
}
|
|
392
384
|
`);
|
|
393
|
-
|
|
385
|
+
const REFERENCE_OBJECT = template.expression(`
|
|
394
386
|
{
|
|
395
387
|
name: %%INTERFACE_NAME%%,
|
|
396
388
|
qualifier: %%QUALIFIER%%,
|
|
397
389
|
all: %%ALL%%
|
|
398
390
|
}
|
|
399
391
|
`);
|
|
400
|
-
|
|
392
|
+
const UI_OBJECT = template.expression(`
|
|
401
393
|
{
|
|
402
394
|
references: %%UI_REFERENCES%%
|
|
403
395
|
}
|
|
404
396
|
`);
|
|
405
|
-
|
|
397
|
+
const PROPERTY_OBJECT = template.expression(`
|
|
406
398
|
{
|
|
407
399
|
value: %%VALUE%%,
|
|
408
400
|
required: %%REQUIRED%%
|
|
409
401
|
}
|
|
410
402
|
`);
|
|
403
|
+
/**
|
|
404
|
+
* Generates a combined metadata structure that is essentially a Record<string, metadata.PackageMetadata>.
|
|
405
|
+
* The object contents must match the shape required by the runtime (declared in runtime/metadata/index.ts).
|
|
406
|
+
*/
|
|
411
407
|
function generatePackagesMetadata({ appName, packages }) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
});
|
|
439
|
-
imports.push(renderedImporter);
|
|
440
|
-
return id;
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
packagesMetadata.properties.push(
|
|
444
|
-
nodes.objectProperty(nodes.stringLiteral(pkg2.name), packageMetadata)
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
const program = nodes.program([...imports, nodes.exportDefaultDeclaration(packagesMetadata)]);
|
|
448
|
-
return generate(program).code;
|
|
408
|
+
const idGenerator = new IdGenerator();
|
|
409
|
+
const packagesMetadata = nodes.objectExpression([]);
|
|
410
|
+
const imports = [];
|
|
411
|
+
let overrides;
|
|
412
|
+
for (const pkg of packages) if (pkg.name === appName) overrides = pkg.config.overrides;
|
|
413
|
+
else if (pkg.config.overrides) throw new ReportableError(`Unexpected 'overrides' in package '${pkg.name}'. Overrides are only supported in the app.`);
|
|
414
|
+
for (const pkg of packages) {
|
|
415
|
+
const packageOverrides = overrides?.get(pkg.name);
|
|
416
|
+
const packageMetadata = generatePackageMetadata(pkg, {
|
|
417
|
+
enableService(serviceName) {
|
|
418
|
+
return packageOverrides?.services?.get(serviceName)?.enabled ?? true;
|
|
419
|
+
},
|
|
420
|
+
importServiceClass(variableName, className, moduleId) {
|
|
421
|
+
const id = idGenerator.generate(variableName);
|
|
422
|
+
const renderedImporter = SERVICE_IMPORT({
|
|
423
|
+
SERVICE_NAME: nodes.identifier(className),
|
|
424
|
+
IMPORT_NAME: nodes.identifier(id),
|
|
425
|
+
IMPORT_SOURCE: nodes.stringLiteral(moduleId)
|
|
426
|
+
});
|
|
427
|
+
imports.push(renderedImporter);
|
|
428
|
+
return id;
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
packagesMetadata.properties.push(nodes.objectProperty(nodes.stringLiteral(pkg.name), packageMetadata));
|
|
432
|
+
}
|
|
433
|
+
return generate(nodes.program([...imports, nodes.exportDefaultDeclaration(packagesMetadata)])).code;
|
|
449
434
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
referenceObject(referenceConfig)
|
|
483
|
-
)
|
|
484
|
-
)
|
|
485
|
-
)
|
|
486
|
-
});
|
|
487
|
-
servicesObject.properties.push(
|
|
488
|
-
nodes.objectProperty(nodes.stringLiteral(service.serviceName), serviceObject)
|
|
489
|
-
);
|
|
490
|
-
}
|
|
491
|
-
const uiObject = UI_OBJECT({
|
|
492
|
-
UI_REFERENCES: nodes.arrayExpression(pkg2.config.uiReferences.map((r) => referenceObject(r)))
|
|
493
|
-
});
|
|
494
|
-
const propertiesObject = nodes.objectExpression(
|
|
495
|
-
Array.from(pkg2.config.properties.values()).map(
|
|
496
|
-
(prop) => nodes.objectProperty(
|
|
497
|
-
nodes.stringLiteral(prop.propertyName),
|
|
498
|
-
PROPERTY_OBJECT({
|
|
499
|
-
VALUE: jsonToExpression(prop.defaultValue),
|
|
500
|
-
REQUIRED: nodes.booleanLiteral(prop.required)
|
|
501
|
-
})
|
|
502
|
-
)
|
|
503
|
-
)
|
|
504
|
-
);
|
|
505
|
-
const pkgObject = PKG_OBJECT({
|
|
506
|
-
PACKAGE_NAME: nodes.stringLiteral(pkg2.name),
|
|
507
|
-
PACKAGE_SERVICES: servicesObject,
|
|
508
|
-
PACKAGE_UI: uiObject,
|
|
509
|
-
PROPERTIES: propertiesObject
|
|
510
|
-
});
|
|
511
|
-
return pkgObject;
|
|
435
|
+
/**
|
|
436
|
+
* Generates the metadata object for a single package.
|
|
437
|
+
* As a side effect, service imports for required service classes will be emitted through a callback.
|
|
438
|
+
*/
|
|
439
|
+
function generatePackageMetadata(pkg, options) {
|
|
440
|
+
const servicesObject = nodes.objectExpression([]);
|
|
441
|
+
for (const service of pkg.config.services.values()) {
|
|
442
|
+
if (!options.enableService(service.serviceName)) continue;
|
|
443
|
+
if (!pkg.servicesModulePath) throw new ReportableError(`Package '${pkg.name}' must have a valid services module (typically 'services.ts' or 'services.js').\nThe entry point can be configured by setting the 'servicesModule' property in the build.config.mjs.`);
|
|
444
|
+
const importName = options.importServiceClass(pkg.name + "_" + service.serviceName, service.serviceName, pkg.servicesModulePath);
|
|
445
|
+
const serviceObject = SERVICE_OBJECT({
|
|
446
|
+
SERVICE_NAME: nodes.stringLiteral(service.serviceName),
|
|
447
|
+
SERVICE_IMPORT: nodes.identifier(importName),
|
|
448
|
+
SERVICE_INTERFACES: nodes.arrayExpression(service.provides.map((p) => INTERFACE_OBJECT({
|
|
449
|
+
INTERFACE_NAME: nodes.stringLiteral(p.interfaceName),
|
|
450
|
+
QUALIFIER: p.qualifier == null ? undefinedNode() : nodes.stringLiteral(p.qualifier)
|
|
451
|
+
}))),
|
|
452
|
+
SERVICE_REFERENCES: nodes.objectExpression(Array.from(service.references.entries()).map(([referenceName, referenceConfig]) => nodes.objectProperty(nodes.stringLiteral(referenceName), referenceObject(referenceConfig))))
|
|
453
|
+
});
|
|
454
|
+
servicesObject.properties.push(nodes.objectProperty(nodes.stringLiteral(service.serviceName), serviceObject));
|
|
455
|
+
}
|
|
456
|
+
const uiObject = UI_OBJECT({ UI_REFERENCES: nodes.arrayExpression(pkg.config.uiReferences.map((r) => referenceObject(r))) });
|
|
457
|
+
const propertiesObject = nodes.objectExpression(Array.from(pkg.config.properties.values()).map((prop) => nodes.objectProperty(nodes.stringLiteral(prop.propertyName), PROPERTY_OBJECT({
|
|
458
|
+
VALUE: jsonToExpression(prop.defaultValue),
|
|
459
|
+
REQUIRED: nodes.booleanLiteral(prop.required)
|
|
460
|
+
}))));
|
|
461
|
+
return PKG_OBJECT({
|
|
462
|
+
PACKAGE_NAME: nodes.stringLiteral(pkg.name),
|
|
463
|
+
PACKAGE_SERVICES: servicesObject,
|
|
464
|
+
PACKAGE_UI: uiObject,
|
|
465
|
+
PROPERTIES: propertiesObject
|
|
466
|
+
});
|
|
512
467
|
}
|
|
513
468
|
function jsonToExpression(json) {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
return nodes.numericLiteral(json);
|
|
522
|
-
}
|
|
523
|
-
if (typeof json === "boolean") {
|
|
524
|
-
return nodes.booleanLiteral(json);
|
|
525
|
-
}
|
|
526
|
-
if (Array.isArray(json)) {
|
|
527
|
-
return nodes.arrayExpression(json.map((item) => jsonToExpression(item)));
|
|
528
|
-
}
|
|
529
|
-
if (typeof json === "object") {
|
|
530
|
-
return nodes.objectExpression(
|
|
531
|
-
Object.entries(json).map(
|
|
532
|
-
([name, value]) => nodes.objectProperty(nodes.stringLiteral(name), jsonToExpression(value))
|
|
533
|
-
)
|
|
534
|
-
);
|
|
535
|
-
}
|
|
536
|
-
throw new Error(`Unexpected value while serializing JSON: ${json}.`);
|
|
469
|
+
if (json == null) return nodes.nullLiteral();
|
|
470
|
+
if (typeof json === "string") return nodes.stringLiteral(json);
|
|
471
|
+
if (typeof json === "number") return nodes.numericLiteral(json);
|
|
472
|
+
if (typeof json === "boolean") return nodes.booleanLiteral(json);
|
|
473
|
+
if (Array.isArray(json)) return nodes.arrayExpression(json.map((item) => jsonToExpression(item)));
|
|
474
|
+
if (typeof json === "object") return nodes.objectExpression(Object.entries(json).map(([name, value]) => nodes.objectProperty(nodes.stringLiteral(name), jsonToExpression(value))));
|
|
475
|
+
throw new Error(`Unexpected value while serializing JSON: ${json}.`);
|
|
537
476
|
}
|
|
538
477
|
function referenceObject(ref) {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
});
|
|
478
|
+
return REFERENCE_OBJECT({
|
|
479
|
+
INTERFACE_NAME: nodes.stringLiteral(ref.interfaceName),
|
|
480
|
+
QUALIFIER: ref.qualifier ? nodes.stringLiteral(ref.qualifier) : undefinedNode(),
|
|
481
|
+
ALL: nodes.booleanLiteral(ref.type === "all" ? true : false)
|
|
482
|
+
});
|
|
545
483
|
}
|
|
546
484
|
function undefinedNode() {
|
|
547
|
-
|
|
485
|
+
return nodes.unaryExpression("void", nodes.numericLiteral(0));
|
|
548
486
|
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
487
|
+
//#endregion
|
|
488
|
+
//#region src/deployment/index.ts
|
|
489
|
+
const SENTINEL_FILENAME = "__base_url_sentinel__";
|
|
490
|
+
/**
|
|
491
|
+
* Provides a virtual module that returns metadata about the current deployment.
|
|
492
|
+
* This is the implementation of `open-pioneer:deployment`.
|
|
493
|
+
*
|
|
494
|
+
* NOTE: This works by creating a (dummy) asset file and computing the URL to it.
|
|
495
|
+
* There does not seem to be a more elegant way.
|
|
496
|
+
* See also https://github.com/vitejs/vite/discussions/21251
|
|
497
|
+
*/
|
|
552
498
|
var DeploymentModule = class {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (this.#isDev) {
|
|
580
|
-
let baseUrl = this.#configuredBaseUrl;
|
|
581
|
-
if (!baseUrl || baseUrl === "./") {
|
|
582
|
-
baseUrl = "/";
|
|
583
|
-
}
|
|
584
|
-
return `export const baseUrl = new URL(${JSON.stringify(baseUrl)}, window.origin).href;`;
|
|
585
|
-
}
|
|
586
|
-
return `
|
|
499
|
+
#isDev;
|
|
500
|
+
#configuredBaseUrl;
|
|
501
|
+
#assetId;
|
|
502
|
+
constructor(isDev, configuredBaseUrl) {
|
|
503
|
+
this.#isDev = isDev;
|
|
504
|
+
this.#configuredBaseUrl = configuredBaseUrl;
|
|
505
|
+
}
|
|
506
|
+
onStart(ctx) {
|
|
507
|
+
if (this.#isDev) return;
|
|
508
|
+
if (this.#assetId) throw new Error("Illegal state: onStart() was already called");
|
|
509
|
+
this.#assetId = ctx.emitFile({
|
|
510
|
+
type: "asset",
|
|
511
|
+
fileName: SENTINEL_FILENAME,
|
|
512
|
+
source: "This file should be deleted after the build."
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
onGenerateBundle(bundle) {
|
|
516
|
+
if (!this.#isDev) delete bundle[SENTINEL_FILENAME];
|
|
517
|
+
}
|
|
518
|
+
onLoadModule() {
|
|
519
|
+
if (this.#isDev) {
|
|
520
|
+
let baseUrl = this.#configuredBaseUrl;
|
|
521
|
+
if (!baseUrl || baseUrl === "./") baseUrl = "/";
|
|
522
|
+
return `export const baseUrl = new URL(${JSON.stringify(baseUrl)}, window.origin).href;`;
|
|
523
|
+
}
|
|
524
|
+
return `
|
|
587
525
|
const assetUrl = "__VITE_ASSET__${this.#assetId}__";
|
|
588
526
|
export const baseUrl = new URL("./", assetUrl).href;
|
|
589
527
|
`;
|
|
590
|
-
|
|
528
|
+
}
|
|
591
529
|
};
|
|
592
|
-
|
|
593
|
-
|
|
530
|
+
//#endregion
|
|
531
|
+
//#region src/metadata/Context.ts
|
|
594
532
|
function createMetadataContextFromRolldown(ctx) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
ctx.warn(message);
|
|
608
|
-
}
|
|
609
|
-
};
|
|
533
|
+
return {
|
|
534
|
+
addWatchFile: (id) => {
|
|
535
|
+
ctx.addWatchFile(id);
|
|
536
|
+
},
|
|
537
|
+
resolveLocalFile: async (moduleId, _packageName, packageDirectory) => {
|
|
538
|
+
const importer = `${packageDirectory}/package.json`;
|
|
539
|
+
return (await ctx.resolve(moduleId, importer, { skipSelf: true }))?.id;
|
|
540
|
+
},
|
|
541
|
+
warn: (message) => {
|
|
542
|
+
ctx.warn(message);
|
|
543
|
+
}
|
|
544
|
+
};
|
|
610
545
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
import { dirname as dirname2, posix as posix3 } from "path";
|
|
615
|
-
import { ResolverFactory } from "oxc-resolver";
|
|
616
|
-
import { glob } from "tinyglobby";
|
|
617
|
-
import { normalizePath as normalizePath3 } from "vite";
|
|
618
|
-
|
|
619
|
-
// src/utils/debug.ts
|
|
620
|
-
import pkg from "debug";
|
|
621
|
-
var { debug } = pkg;
|
|
546
|
+
//#endregion
|
|
547
|
+
//#region src/utils/debug.ts
|
|
548
|
+
const { debug: debug$5 } = pkg;
|
|
622
549
|
function createDebugger(namespace) {
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
550
|
+
const logger = debug$5(namespace);
|
|
551
|
+
return function debugFunction(...args) {
|
|
552
|
+
logger(...args);
|
|
553
|
+
};
|
|
627
554
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
import posix2 from "node:path/posix";
|
|
634
|
-
import {
|
|
635
|
-
BUILD_CONFIG_NAME,
|
|
636
|
-
PackageMetadataV1,
|
|
637
|
-
createPackageConfigFromBuildConfig,
|
|
638
|
-
createPackageConfigFromPackageMetadata,
|
|
639
|
-
loadBuildConfig
|
|
640
|
-
} from "@open-pioneer/build-common";
|
|
641
|
-
import { normalizePath as normalizePath2 } from "vite";
|
|
642
|
-
|
|
643
|
-
// src/utils/fileUtils.ts
|
|
644
|
-
import * as fs from "node:fs";
|
|
645
|
-
import { isAbsolute, relative } from "node:path";
|
|
555
|
+
//#endregion
|
|
556
|
+
//#region src/utils/fileUtils.ts
|
|
557
|
+
/**
|
|
558
|
+
* Asynchronously checks for the existence of `file`.
|
|
559
|
+
*/
|
|
646
560
|
function fileExists(file) {
|
|
647
|
-
|
|
561
|
+
return fs.promises.access(file, fs.constants.F_OK).then(() => true).catch(() => false);
|
|
648
562
|
}
|
|
563
|
+
/**
|
|
564
|
+
* Returns true if `candidate` is a child of `directory`.
|
|
565
|
+
*/
|
|
649
566
|
function isInDirectory(candidate, directory) {
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
return !!isChild;
|
|
567
|
+
const rel = relative(directory, candidate);
|
|
568
|
+
return !!(rel && !rel.startsWith("..") && !isAbsolute(rel));
|
|
653
569
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
570
|
+
//#endregion
|
|
571
|
+
//#region src/utils/package.ts
|
|
572
|
+
const PACKAGE_NAME = "@open-pioneer/vite-plugin-pioneer";
|
|
573
|
+
//#endregion
|
|
574
|
+
//#region src/metadata/loadPackageMetadata.ts
|
|
575
|
+
const isDebug$4 = !!process.env.DEBUG;
|
|
576
|
+
const debug$4 = createDebugger("open-pioneer:metadata");
|
|
577
|
+
/**
|
|
578
|
+
* This function is called to read a package's metadata during build
|
|
579
|
+
* (for internal and external packages).
|
|
580
|
+
*
|
|
581
|
+
* Depending on the location of the package either reads build config (source package)
|
|
582
|
+
* or serialized metadata from package.json.
|
|
583
|
+
*/
|
|
661
584
|
async function loadPackageMetadata(ctx, packageDir, options) {
|
|
662
|
-
|
|
585
|
+
return await new PackageMetadataReader(ctx, packageDir, options).readPackageMetadata();
|
|
663
586
|
}
|
|
664
587
|
var PackageMetadataReader = class {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
throw new ReportableError(
|
|
826
|
-
`Failed to parse metadata of package '${packageName}' in ${packageDir}: ${metadataResult.message}`,
|
|
827
|
-
{ cause: metadataResult.cause }
|
|
828
|
-
);
|
|
829
|
-
}
|
|
830
|
-
return {
|
|
831
|
-
configPath: packageJsonPath,
|
|
832
|
-
config: createPackageConfigFromPackageMetadata(metadataResult.value)
|
|
833
|
-
};
|
|
834
|
-
}
|
|
835
|
-
async #parsePackageConfigFromBuildConfig(buildConfigPath) {
|
|
836
|
-
const ctx = this.#ctx;
|
|
837
|
-
const packageDir = this.#packageDir;
|
|
838
|
-
let buildConfig;
|
|
839
|
-
ctx.addWatchFile(normalizePath2(buildConfigPath));
|
|
840
|
-
if (await fileExists(buildConfigPath)) {
|
|
841
|
-
ctx.addWatchFile(normalizePath2(buildConfigPath));
|
|
842
|
-
try {
|
|
843
|
-
buildConfig = await loadBuildConfig(buildConfigPath);
|
|
844
|
-
} catch (e) {
|
|
845
|
-
const cause = e instanceof Error ? e.cause : String(e);
|
|
846
|
-
throw new ReportableError(
|
|
847
|
-
`Failed to load build config ${buildConfigPath}. ${cause}`,
|
|
848
|
-
{
|
|
849
|
-
cause: e
|
|
850
|
-
}
|
|
851
|
-
);
|
|
852
|
-
}
|
|
853
|
-
} else if (!this.#allowMissingBuildConfig) {
|
|
854
|
-
throw new ReportableError(`Expected a ${BUILD_CONFIG_NAME} in ${packageDir}`);
|
|
855
|
-
} else {
|
|
856
|
-
return void 0;
|
|
857
|
-
}
|
|
858
|
-
return {
|
|
859
|
-
configPath: buildConfigPath,
|
|
860
|
-
config: createPackageConfigFromBuildConfig(buildConfig)
|
|
861
|
-
};
|
|
862
|
-
}
|
|
588
|
+
#ctx;
|
|
589
|
+
#packageDir;
|
|
590
|
+
#sourceRoot;
|
|
591
|
+
#packageJsonPath;
|
|
592
|
+
#importedFrom;
|
|
593
|
+
#allowMissingBuildConfig;
|
|
594
|
+
constructor(ctx, packageDir, options) {
|
|
595
|
+
this.#ctx = ctx;
|
|
596
|
+
this.#packageDir = packageDir;
|
|
597
|
+
this.#sourceRoot = options.sourceRoot;
|
|
598
|
+
this.#packageJsonPath = join(packageDir, "package.json");
|
|
599
|
+
this.#importedFrom = options.importedFrom;
|
|
600
|
+
this.#allowMissingBuildConfig = options?.allowMissingBuildConfigInLocalPackage ?? false;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Reads package metadata for the configured package.
|
|
604
|
+
*/
|
|
605
|
+
async readPackageMetadata() {
|
|
606
|
+
const ctx = this.#ctx;
|
|
607
|
+
const packageDir = this.#packageDir;
|
|
608
|
+
const packageJsonPath = this.#packageJsonPath;
|
|
609
|
+
const sourceRoot = this.#sourceRoot;
|
|
610
|
+
const mode = isLocalPackage(packageDir, sourceRoot) ? "local" : "external";
|
|
611
|
+
isDebug$4 && debug$4(`Visiting package directory ${packageDir} in mode ${mode}.`);
|
|
612
|
+
ctx.addWatchFile(packageJsonPath);
|
|
613
|
+
const { name: packageName, version: packageVersion, dependencies, frameworkMetadata } = await parsePackageJson(packageJsonPath);
|
|
614
|
+
const configResult = await this.#readConfig(mode, packageName, frameworkMetadata);
|
|
615
|
+
if (!configResult) return {
|
|
616
|
+
type: "plain",
|
|
617
|
+
name: packageName,
|
|
618
|
+
version: packageVersion,
|
|
619
|
+
directory: packageDir
|
|
620
|
+
};
|
|
621
|
+
const { config, configPath } = configResult;
|
|
622
|
+
let servicesModuleId;
|
|
623
|
+
let servicesModulePath;
|
|
624
|
+
if (config.services.size) {
|
|
625
|
+
const localModuleId = config.servicesModule ?? "./services";
|
|
626
|
+
servicesModuleId = posix$1.join(packageName, localModuleId);
|
|
627
|
+
try {
|
|
628
|
+
servicesModulePath = await this.#resolveServicesModule(mode, packageName, localModuleId);
|
|
629
|
+
} catch (e) {
|
|
630
|
+
throw new ReportableError(`Failed to resolve services entry point for package ${packageDir}`, { cause: e });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
let cssFile;
|
|
634
|
+
if (config.styles) {
|
|
635
|
+
try {
|
|
636
|
+
cssFile = await resolveLocalFile(ctx, packageDir, packageName, config.styles);
|
|
637
|
+
} catch (e) {
|
|
638
|
+
throw new ReportableError(`Failed to resolve css file for package ${packageDir}`, { cause: e });
|
|
639
|
+
}
|
|
640
|
+
if (!cssFile) throw new ReportableError(`Failed to find css file '${config.styles}' in ${packageDir}`);
|
|
641
|
+
}
|
|
642
|
+
const i18nPaths = /* @__PURE__ */ new Map();
|
|
643
|
+
for (const locale of config.languages) {
|
|
644
|
+
if (i18nPaths.has(locale)) throw new ReportableError(`Locale '${locale}' was defined twice in ${configPath}`);
|
|
645
|
+
const path = join(packageDir, "i18n", `${locale}.yaml`);
|
|
646
|
+
i18nPaths.set(locale, normalizePath(path));
|
|
647
|
+
}
|
|
648
|
+
return {
|
|
649
|
+
type: "pioneer-package",
|
|
650
|
+
name: packageName,
|
|
651
|
+
version: packageVersion,
|
|
652
|
+
directory: packageDir,
|
|
653
|
+
packageJsonPath,
|
|
654
|
+
servicesModuleId,
|
|
655
|
+
servicesModulePath,
|
|
656
|
+
cssFilePath: cssFile,
|
|
657
|
+
i18nPaths,
|
|
658
|
+
get locales() {
|
|
659
|
+
return Array.from(i18nPaths.keys());
|
|
660
|
+
},
|
|
661
|
+
dependencies,
|
|
662
|
+
config
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Attempts to read the package's metadata/configuration, depending on mode.
|
|
667
|
+
*/
|
|
668
|
+
async #readConfig(mode, packageName, frameworkMetadata) {
|
|
669
|
+
const ctx = this.#ctx;
|
|
670
|
+
const packageDir = this.#packageDir;
|
|
671
|
+
switch (mode) {
|
|
672
|
+
/** External packages must have framework metadata in their package.json (or they are not considered Open Pioneer Trails packages at all). */
|
|
673
|
+
case "external":
|
|
674
|
+
if (!frameworkMetadata) return;
|
|
675
|
+
return this.#parsePackageConfigFromMetadata(packageName, frameworkMetadata);
|
|
676
|
+
/** Local packages may have either a build.config (the common case) or a built package.json for testing, but never both. */
|
|
677
|
+
case "local": {
|
|
678
|
+
const buildConfigPath = join(packageDir, BUILD_CONFIG_NAME);
|
|
679
|
+
if (existsSync$1(buildConfigPath) && frameworkMetadata) throw new Error(`Package '${packageName}' at ${packageDir} contains both framework metadata in its package.json and a ${BUILD_CONFIG_NAME}. Mixing both formats is not supported. Metadata in package.json files is only intended for distributed packages.`);
|
|
680
|
+
if (frameworkMetadata) {
|
|
681
|
+
ctx.warn(`Using framework metadata from package.json instead of ${BUILD_CONFIG_NAME} in ${packageDir}, make sure that this intended.`);
|
|
682
|
+
return this.#parsePackageConfigFromMetadata(packageName, frameworkMetadata);
|
|
683
|
+
}
|
|
684
|
+
return this.#parsePackageConfigFromBuildConfig(buildConfigPath);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Attempts to resolve the package's services entry point (e.g. ./services.ts), depending on mode.
|
|
690
|
+
*/
|
|
691
|
+
async #resolveServicesModule(mode, packageName, moduleId) {
|
|
692
|
+
const importedFrom = this.#importedFrom;
|
|
693
|
+
if (mode === "external" && importedFrom)
|
|
694
|
+
/**
|
|
695
|
+
* FIXME: This is a workaround for a weird interaction with vite's dependency optimizer and virtual modules.
|
|
696
|
+
* We are trying to import a real file here (e.g. ./node_modules/.../@open-pioneer/some-package/services.js)
|
|
697
|
+
* and we would like to do so via a fully resolved path.
|
|
698
|
+
*
|
|
699
|
+
* However, when using a fully resolved path AND using vite's dev mode AND if that code used by that file
|
|
700
|
+
* is already used elsewhere in the app (and thus optimized) we end up with a "working" import that produces duplicate code
|
|
701
|
+
* (one copy from vite's optimizer in node_modules/.vite and one copy via the direct fully resolved path).
|
|
702
|
+
*
|
|
703
|
+
* By using an unresolved path we bypass this issue as it appears that vite's resolve pipeline handles this correctly -
|
|
704
|
+
* there is no duplicate code in manual experiments.
|
|
705
|
+
*
|
|
706
|
+
* Because this is an unresolved import (e.g. @open-pioneer/some-package/services) from the app's virtual packages module
|
|
707
|
+
* this package must be reachable from the app. For this reason we currently must use pnpm's "shamefully-hoist" option :(
|
|
708
|
+
*/
|
|
709
|
+
return posix$1.join(packageName, moduleId);
|
|
710
|
+
else return await resolveLocalFile(this.#ctx, this.#packageDir, packageName, moduleId);
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Attempts to read the package configuration from the package.json's metadata value.
|
|
714
|
+
* Note that the value may not be present at this point if the package does not use Open Pioneer Trails metadata.
|
|
715
|
+
*/
|
|
716
|
+
async #parsePackageConfigFromMetadata(packageName, frameworkMetadata) {
|
|
717
|
+
const packageDir = this.#packageDir;
|
|
718
|
+
const packageJsonPath = this.#packageJsonPath;
|
|
719
|
+
const metadataResult = PackageMetadataV1.parsePackageMetadata(frameworkMetadata);
|
|
720
|
+
if (metadataResult.type === "error") {
|
|
721
|
+
if (metadataResult.code === "unsupported-version") throw new ReportableError(`Package '${packageName}' in ${packageDir} uses an unsupported package metadata version. The latest version supported by this plugin is ${PackageMetadataV1.LATEST_VERSION}. Try updating ${PACKAGE_NAME}.\n\n` + metadataResult.message, { cause: metadataResult.cause });
|
|
722
|
+
throw new ReportableError(`Failed to parse metadata of package '${packageName}' in ${packageDir}: ${metadataResult.message}`, { cause: metadataResult.cause });
|
|
723
|
+
}
|
|
724
|
+
return {
|
|
725
|
+
configPath: packageJsonPath,
|
|
726
|
+
config: createPackageConfigFromPackageMetadata(metadataResult.value)
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
async #parsePackageConfigFromBuildConfig(buildConfigPath) {
|
|
730
|
+
const ctx = this.#ctx;
|
|
731
|
+
const packageDir = this.#packageDir;
|
|
732
|
+
let buildConfig;
|
|
733
|
+
ctx.addWatchFile(normalizePath(buildConfigPath));
|
|
734
|
+
if (await fileExists(buildConfigPath)) {
|
|
735
|
+
ctx.addWatchFile(normalizePath(buildConfigPath));
|
|
736
|
+
try {
|
|
737
|
+
buildConfig = await loadBuildConfig(buildConfigPath);
|
|
738
|
+
} catch (e) {
|
|
739
|
+
throw new ReportableError(`Failed to load build config ${buildConfigPath}. ${e instanceof Error ? e.cause : String(e)}`, { cause: e });
|
|
740
|
+
}
|
|
741
|
+
} else if (!this.#allowMissingBuildConfig) throw new ReportableError(`Expected a ${BUILD_CONFIG_NAME} in ${packageDir}`);
|
|
742
|
+
else return;
|
|
743
|
+
return {
|
|
744
|
+
configPath: buildConfigPath,
|
|
745
|
+
config: createPackageConfigFromBuildConfig(buildConfig)
|
|
746
|
+
};
|
|
747
|
+
}
|
|
863
748
|
};
|
|
864
749
|
async function parsePackageJson(packageJsonPath) {
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
dep = {
|
|
904
|
-
packageName: packageName2,
|
|
905
|
-
optional
|
|
906
|
-
};
|
|
907
|
-
deps.set(packageName2, dep);
|
|
908
|
-
} else {
|
|
909
|
-
dep.optional &&= optional;
|
|
910
|
-
}
|
|
911
|
-
};
|
|
912
|
-
for (const depName of Object.keys(dependencies)) {
|
|
913
|
-
addDep(depName, false);
|
|
914
|
-
}
|
|
915
|
-
for (const depName of Object.keys(peerDependencies)) {
|
|
916
|
-
addDep(depName, ((_b = (_a = packageJsonContent == null ? void 0 : packageJsonContent.peerDependenciesMeta) == null ? void 0 : _a[depName]) == null ? void 0 : _b.optional) ?? false);
|
|
917
|
-
}
|
|
918
|
-
for (const depName of Object.keys(optionalDependencies)) {
|
|
919
|
-
addDep(depName, true);
|
|
920
|
-
}
|
|
921
|
-
const frameworkMetadata = packageJsonContent[PackageMetadataV1.PACKAGE_JSON_KEY] ?? void 0;
|
|
922
|
-
return {
|
|
923
|
-
name: packageName,
|
|
924
|
-
version: version ?? void 0,
|
|
925
|
-
dependencies: Array.from(deps.values()),
|
|
926
|
-
frameworkMetadata
|
|
927
|
-
};
|
|
750
|
+
if (!await fileExists(packageJsonPath)) throw new ReportableError(`Expected a 'package.json' file at ${packageJsonPath}`);
|
|
751
|
+
let packageJsonContent;
|
|
752
|
+
try {
|
|
753
|
+
packageJsonContent = JSON.parse(await readFile(packageJsonPath, "utf-8"));
|
|
754
|
+
} catch (e) {
|
|
755
|
+
throw new ReportableError(`Failed to read ${packageJsonPath}`, { cause: e });
|
|
756
|
+
}
|
|
757
|
+
const packageName = packageJsonContent.name;
|
|
758
|
+
if (typeof packageName !== "string") throw new ReportableError(`Expected 'name' to be a string in ${packageJsonPath}`);
|
|
759
|
+
const version = packageJsonContent.version;
|
|
760
|
+
if (version != null && typeof version !== "string") throw new ReportableError(`Expected 'version' to be a string in ${packageJsonPath}`);
|
|
761
|
+
const dependencies = packageJsonContent.dependencies ?? {};
|
|
762
|
+
if (typeof dependencies !== "object") throw new ReportableError(`Expected a valid 'dependencies' object in ${packageJsonPath}`);
|
|
763
|
+
const peerDependencies = packageJsonContent.peerDependencies ?? {};
|
|
764
|
+
if (typeof peerDependencies !== "object") throw new ReportableError(`Expected a valid 'peerDependencies' object in ${packageJsonPath}`);
|
|
765
|
+
const optionalDependencies = packageJsonContent.optionalDependencies ?? {};
|
|
766
|
+
if (typeof optionalDependencies !== "object") throw new ReportableError(`Expected a valid 'optionalDependencies' object in ${packageJsonPath}`);
|
|
767
|
+
const deps = /* @__PURE__ */ new Map();
|
|
768
|
+
const addDep = (packageName, optional) => {
|
|
769
|
+
let dep = deps.get(packageName);
|
|
770
|
+
if (!dep) {
|
|
771
|
+
dep = {
|
|
772
|
+
packageName,
|
|
773
|
+
optional
|
|
774
|
+
};
|
|
775
|
+
deps.set(packageName, dep);
|
|
776
|
+
} else dep.optional &&= optional;
|
|
777
|
+
};
|
|
778
|
+
for (const depName of Object.keys(dependencies)) addDep(depName, false);
|
|
779
|
+
for (const depName of Object.keys(peerDependencies)) addDep(depName, packageJsonContent?.peerDependenciesMeta?.[depName]?.optional ?? false);
|
|
780
|
+
for (const depName of Object.keys(optionalDependencies)) addDep(depName, true);
|
|
781
|
+
const frameworkMetadata = packageJsonContent[PackageMetadataV1.PACKAGE_JSON_KEY] ?? void 0;
|
|
782
|
+
return {
|
|
783
|
+
name: packageName,
|
|
784
|
+
version: version ?? void 0,
|
|
785
|
+
dependencies: Array.from(deps.values()),
|
|
786
|
+
frameworkMetadata
|
|
787
|
+
};
|
|
928
788
|
}
|
|
929
789
|
async function resolveLocalFile(ctx, packageDir, packageName, localModuleId) {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
}
|
|
933
|
-
return await ctx.resolveLocalFile(localModuleId, packageName, packageDir);
|
|
790
|
+
if (!localModuleId.startsWith("./")) localModuleId = `./${localModuleId}`;
|
|
791
|
+
return await ctx.resolveLocalFile(localModuleId, packageName, packageDir);
|
|
934
792
|
}
|
|
935
|
-
|
|
793
|
+
const NODE_MODULES_RE = /[\\/]node_modules[\\/]/;
|
|
936
794
|
function isLocalPackage(file, sourceDir) {
|
|
937
|
-
|
|
795
|
+
return isInDirectory(file, sourceDir) && !NODE_MODULES_RE.test(file);
|
|
938
796
|
}
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
797
|
+
//#endregion
|
|
798
|
+
//#region src/metadata/findTrailsPackages.ts
|
|
799
|
+
const isDebug$3 = !!process.env.DEBUG;
|
|
800
|
+
const debug$3 = createDebugger("open-pioneer:metadata");
|
|
943
801
|
async function findTrailsPackages(sourceRoot) {
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
const depResult = await packageResolver.async(
|
|
978
|
-
packageDirectory,
|
|
979
|
-
dependency.packageName + "/package.json"
|
|
980
|
-
);
|
|
981
|
-
if (depResult.path) {
|
|
982
|
-
const depPath = await realpath(dirname2(depResult.path));
|
|
983
|
-
visit(depPath);
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
trailsPackages.sort((p1, p2) => p1.name.localeCompare(p2.name, "en"));
|
|
988
|
-
return trailsPackages;
|
|
802
|
+
const ctx = createDummyContext();
|
|
803
|
+
const packageResolver = ResolverFactory.default();
|
|
804
|
+
const localPackageJsonFiles = await glob("**/package.json", {
|
|
805
|
+
cwd: sourceRoot,
|
|
806
|
+
absolute: true,
|
|
807
|
+
ignore: ["**/node_modules/**"]
|
|
808
|
+
});
|
|
809
|
+
const seen = /* @__PURE__ */ new Set();
|
|
810
|
+
const workQueue = [];
|
|
811
|
+
const visit = (packageDirectory) => {
|
|
812
|
+
const normalizedPath = normalizePath(packageDirectory);
|
|
813
|
+
if (seen.has(normalizedPath)) return;
|
|
814
|
+
seen.add(normalizedPath);
|
|
815
|
+
workQueue.push(normalizedPath);
|
|
816
|
+
};
|
|
817
|
+
for (const file of localPackageJsonFiles) visit(dirname$2(file));
|
|
818
|
+
const trailsPackages = [];
|
|
819
|
+
while (workQueue.length) {
|
|
820
|
+
const packageDirectory = workQueue.pop();
|
|
821
|
+
const pkg = await loadPackageMetadata(ctx, packageDirectory, {
|
|
822
|
+
sourceRoot,
|
|
823
|
+
importedFrom: void 0,
|
|
824
|
+
allowMissingBuildConfigInLocalPackage: true
|
|
825
|
+
});
|
|
826
|
+
if (pkg.type === "plain") continue;
|
|
827
|
+
trailsPackages.push(pkg);
|
|
828
|
+
for (const dependency of pkg.dependencies) {
|
|
829
|
+
const depResult = await packageResolver.async(packageDirectory, dependency.packageName + "/package.json");
|
|
830
|
+
if (depResult.path) visit(await realpath(dirname$2(depResult.path)));
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
trailsPackages.sort((p1, p2) => p1.name.localeCompare(p2.name, "en"));
|
|
834
|
+
return trailsPackages;
|
|
989
835
|
}
|
|
990
836
|
function createDummyContext() {
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
};
|
|
837
|
+
return {
|
|
838
|
+
addWatchFile(_file) {},
|
|
839
|
+
async resolveLocalFile(moduleId, _packageDir, packageName) {
|
|
840
|
+
return posix$3.join(packageName, moduleId);
|
|
841
|
+
},
|
|
842
|
+
warn(message) {
|
|
843
|
+
isDebug$3 && debug$3("Problem during trails package discovery: %s", message);
|
|
844
|
+
}
|
|
845
|
+
};
|
|
1001
846
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
847
|
+
//#endregion
|
|
848
|
+
//#region src/utils/Cache.ts
|
|
849
|
+
/**
|
|
850
|
+
* A simple cache implementing read-through semantics.
|
|
851
|
+
*
|
|
852
|
+
* The {@link CacheProvider} passed to the cache's constructor
|
|
853
|
+
* implements the actual computation of values.
|
|
854
|
+
*
|
|
855
|
+
* The cache deduplicates computations and caches their results
|
|
856
|
+
* until they are invalidated.
|
|
857
|
+
*/
|
|
1011
858
|
var Cache = class {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
if (jobs.get(id) === job) {
|
|
1064
|
-
values.set(id, value);
|
|
1065
|
-
}
|
|
1066
|
-
return value;
|
|
1067
|
-
} finally {
|
|
1068
|
-
if (jobs.get(id) === job) {
|
|
1069
|
-
jobs.delete(id);
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
859
|
+
#values = /* @__PURE__ */ new Map();
|
|
860
|
+
#jobs = /* @__PURE__ */ new Map();
|
|
861
|
+
#provider;
|
|
862
|
+
constructor(provider) {
|
|
863
|
+
this.#provider = provider;
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Removes an entry associated with the given key.
|
|
867
|
+
*/
|
|
868
|
+
invalidate(key) {
|
|
869
|
+
const id = this.#provider.getId(key);
|
|
870
|
+
const oldValue = this.#values.get(id);
|
|
871
|
+
this.#values.delete(id);
|
|
872
|
+
this.#jobs.delete(id);
|
|
873
|
+
if (oldValue) this.#provider.onInvalidate?.(key, oldValue);
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Retrieves the value associated with `key`.
|
|
877
|
+
* A cached result will be used if present, otherwise the
|
|
878
|
+
* cache provider will be called.
|
|
879
|
+
*
|
|
880
|
+
* Note: computations may throw errors.
|
|
881
|
+
* Errors will not be cached.
|
|
882
|
+
*
|
|
883
|
+
* @param key the lookup key
|
|
884
|
+
* @param context an arbitrary set of values that will be forwarded to the provider
|
|
885
|
+
* should be provider be called.
|
|
886
|
+
* @returns the value associated with `key`
|
|
887
|
+
*/
|
|
888
|
+
async get(key, ...context) {
|
|
889
|
+
const provider = this.#provider;
|
|
890
|
+
const id = provider.getId(key);
|
|
891
|
+
const values = this.#values;
|
|
892
|
+
const existingValue = values.get(id);
|
|
893
|
+
if (existingValue) {
|
|
894
|
+
provider.onCachedReturn?.(key, existingValue);
|
|
895
|
+
return Promise.resolve(existingValue);
|
|
896
|
+
}
|
|
897
|
+
const jobs = this.#jobs;
|
|
898
|
+
const existingJob = jobs.get(id);
|
|
899
|
+
if (existingJob) return existingJob;
|
|
900
|
+
const job = provider.getValue(key, ...context);
|
|
901
|
+
jobs.set(id, job);
|
|
902
|
+
try {
|
|
903
|
+
const value = await job;
|
|
904
|
+
if (jobs.get(id) === job) values.set(id, value);
|
|
905
|
+
return value;
|
|
906
|
+
} finally {
|
|
907
|
+
if (jobs.get(id) === job) jobs.delete(id);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
1073
910
|
};
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
var MESSAGES_SCHEMA = z.lazy(
|
|
1081
|
-
() => z.record(
|
|
1082
|
-
z.string(),
|
|
1083
|
-
z.union([z.string(), MESSAGES_SCHEMA], {
|
|
1084
|
-
error: "String or a nested record of messages"
|
|
1085
|
-
})
|
|
1086
|
-
).nullish()
|
|
1087
|
-
);
|
|
1088
|
-
var I18N_SCHEMA = z.strictObject({
|
|
1089
|
-
messages: MESSAGES_SCHEMA.nullish().optional(),
|
|
1090
|
-
overrides: z.record(z.string(), MESSAGES_SCHEMA).nullish().optional()
|
|
911
|
+
//#endregion
|
|
912
|
+
//#region src/metadata/parseI18nYaml.ts
|
|
913
|
+
const MESSAGES_SCHEMA = z.lazy(() => z.record(z.string(), z.union([z.string(), MESSAGES_SCHEMA], { error: "String or a nested record of messages" })).nullish());
|
|
914
|
+
const I18N_SCHEMA = z.strictObject({
|
|
915
|
+
messages: MESSAGES_SCHEMA.nullish().optional(),
|
|
916
|
+
overrides: z.record(z.string(), MESSAGES_SCHEMA).nullish().optional()
|
|
1091
917
|
}).nullish();
|
|
918
|
+
/**
|
|
919
|
+
* Loads an i18n.yaml file from the file system and parses it.
|
|
920
|
+
*/
|
|
1092
921
|
async function loadI18nFile(path) {
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
922
|
+
let content;
|
|
923
|
+
try {
|
|
924
|
+
content = await readFile$1(path, "utf-8");
|
|
925
|
+
} catch (e) {
|
|
926
|
+
throw new ReportableError(`Failed to read ${path}`, { cause: e });
|
|
927
|
+
}
|
|
928
|
+
try {
|
|
929
|
+
return parseI18nYaml(content);
|
|
930
|
+
} catch (e) {
|
|
931
|
+
throw new ReportableError(`Failed to parse ${path}`, { cause: e });
|
|
932
|
+
}
|
|
1104
933
|
}
|
|
934
|
+
/**
|
|
935
|
+
* Parses an i18n yaml document.
|
|
936
|
+
*/
|
|
1105
937
|
function parseI18nYaml(yaml) {
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
}
|
|
1110
|
-
return parseI18nFile(data);
|
|
938
|
+
let data = void 0;
|
|
939
|
+
if (yaml.trim()) data = load(yaml);
|
|
940
|
+
return parseI18nFile(data);
|
|
1111
941
|
}
|
|
1112
|
-
|
|
942
|
+
const ERROR_MAP = createErrorMap();
|
|
943
|
+
/**
|
|
944
|
+
* Parses the JavaScript object representing an i18n file.
|
|
945
|
+
*/
|
|
1113
946
|
function parseI18nFile(data) {
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
overrides = gatherOverrides((rawI18n == null ? void 0 : rawI18n.overrides) ?? void 0);
|
|
1125
|
-
}
|
|
1126
|
-
return { messages, overrides };
|
|
947
|
+
const result = I18N_SCHEMA.safeParse(data, { error: ERROR_MAP });
|
|
948
|
+
if (!result.success) throw fromZodError(result.error);
|
|
949
|
+
const rawI18n = result.data;
|
|
950
|
+
const messages = gatherMessages(rawI18n?.messages ?? void 0);
|
|
951
|
+
let overrides;
|
|
952
|
+
if (typeof rawI18n?.overrides === "object") overrides = gatherOverrides(rawI18n?.overrides ?? void 0);
|
|
953
|
+
return {
|
|
954
|
+
messages,
|
|
955
|
+
overrides
|
|
956
|
+
};
|
|
1127
957
|
}
|
|
1128
958
|
function gatherMessages(data) {
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
}
|
|
1147
|
-
messages.set(id, message);
|
|
1148
|
-
};
|
|
1149
|
-
if (data) {
|
|
1150
|
-
visitChildren(data);
|
|
1151
|
-
}
|
|
1152
|
-
return messages;
|
|
959
|
+
const messages = /* @__PURE__ */ new Map();
|
|
960
|
+
const prefix = [];
|
|
961
|
+
const visitChildren = (record) => {
|
|
962
|
+
for (const [key, value] of Object.entries(record)) if (typeof value === "string") visitMessage(key, value);
|
|
963
|
+
else if (value) {
|
|
964
|
+
prefix.push(key);
|
|
965
|
+
visitChildren(value);
|
|
966
|
+
prefix.pop();
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
const visitMessage = (key, message) => {
|
|
970
|
+
const id = [...prefix, key].join(".");
|
|
971
|
+
if (messages.has(id)) throw new Error(`Message '${id}' was already defined.`);
|
|
972
|
+
messages.set(id, message);
|
|
973
|
+
};
|
|
974
|
+
if (data) visitChildren(data);
|
|
975
|
+
return messages;
|
|
1153
976
|
}
|
|
1154
977
|
function gatherOverrides(data) {
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
}
|
|
1162
|
-
return overrides;
|
|
978
|
+
const overrides = /* @__PURE__ */ new Map();
|
|
979
|
+
if (data) for (const [key, value] of Object.entries(data)) {
|
|
980
|
+
const messages = gatherMessages(value ?? void 0);
|
|
981
|
+
overrides.set(key, messages);
|
|
982
|
+
}
|
|
983
|
+
return overrides;
|
|
1163
984
|
}
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
985
|
+
//#endregion
|
|
986
|
+
//#region src/metadata/MetadataRepository.ts
|
|
987
|
+
const isDebug$2 = !!process.env.DEBUG;
|
|
988
|
+
const debug$2 = createDebugger("open-pioneer:metadata");
|
|
989
|
+
/**
|
|
990
|
+
* Provides metadata about apps and packages.
|
|
991
|
+
* Metadata is read from disk on demand and will then be cached
|
|
992
|
+
* until one of the file dependencies has changed.
|
|
993
|
+
*/
|
|
1168
994
|
var MetadataRepository = class {
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
const trackingCtx = {
|
|
1342
|
-
resolveLocalFile: ctx.resolveLocalFile.bind(ctx),
|
|
1343
|
-
addWatchFile(id) {
|
|
1344
|
-
ctx.addWatchFile(id);
|
|
1345
|
-
watchFiles.add(id);
|
|
1346
|
-
},
|
|
1347
|
-
warn: ctx.warn.bind(ctx)
|
|
1348
|
-
};
|
|
1349
|
-
const metadata = await loadPackageMetadata(trackingCtx, directory, {
|
|
1350
|
-
sourceRoot,
|
|
1351
|
-
importedFrom
|
|
1352
|
-
});
|
|
1353
|
-
isDebug3 && debug4(`Metadata for '${metadata.name}': %O`, metadata);
|
|
1354
|
-
if (metadata.type === "pioneer-package") {
|
|
1355
|
-
const existingMetadata = this._byName.get(metadata.name);
|
|
1356
|
-
if (existingMetadata && normalizePath4(existingMetadata.directory) !== normalizePath4(metadata.directory)) {
|
|
1357
|
-
const message = `Encountered the package '${metadata.name}' at two different locations.
|
|
1358
|
-
Trails packages cannot be used more than once in the same application.
|
|
1359
|
-
All packages must use a common version of '${metadata.name}'.
|
|
1360
|
-
|
|
1361
|
-
1. ${formatPackage(existingMetadata)}
|
|
1362
|
-
|
|
1363
|
-
2. ${formatPackage(metadata)}`;
|
|
1364
|
-
throw new ReportableError(message);
|
|
1365
|
-
}
|
|
1366
|
-
this._byName.set(metadata.name, metadata);
|
|
1367
|
-
}
|
|
1368
|
-
return {
|
|
1369
|
-
metadata,
|
|
1370
|
-
watchFiles
|
|
1371
|
-
};
|
|
1372
|
-
},
|
|
1373
|
-
onInvalidate(directory, entry) {
|
|
1374
|
-
isDebug3 && debug4(`Removed cache entry for '${entry.metadata.name}'`);
|
|
1375
|
-
this._byName.delete(entry.metadata.name);
|
|
1376
|
-
},
|
|
1377
|
-
onCachedReturn(directory, entry) {
|
|
1378
|
-
isDebug3 && debug4(`Returning cached metadata for '${entry.metadata.name}'`);
|
|
1379
|
-
}
|
|
1380
|
-
};
|
|
1381
|
-
return new Cache(provider);
|
|
1382
|
-
}
|
|
995
|
+
#sourceRoot;
|
|
996
|
+
#packageMetadataCache;
|
|
997
|
+
#i18nCache;
|
|
998
|
+
/**
|
|
999
|
+
* @param sourceRoot Source folder on disk, needed to detect 'local' packages
|
|
1000
|
+
*/
|
|
1001
|
+
constructor(sourceRoot) {
|
|
1002
|
+
this.#sourceRoot = sourceRoot;
|
|
1003
|
+
this.#packageMetadataCache = this.#createPackageMetadataCache();
|
|
1004
|
+
this.#i18nCache = this.#createI18nCache();
|
|
1005
|
+
}
|
|
1006
|
+
reset() {
|
|
1007
|
+
this.#packageMetadataCache = this.#createPackageMetadataCache();
|
|
1008
|
+
this.#i18nCache = this.#createI18nCache();
|
|
1009
|
+
}
|
|
1010
|
+
/**
|
|
1011
|
+
* Called by the plugin when a file has been changed.
|
|
1012
|
+
* The cached data associated with that file (if any) will be removed.
|
|
1013
|
+
*/
|
|
1014
|
+
onFileChanged(path) {
|
|
1015
|
+
if (isPackageJson(path) || isBuildConfig(path)) this.#packageMetadataCache.invalidate(dirname$2(path));
|
|
1016
|
+
this.#i18nCache.invalidate(path);
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* Returns the combined application metadata of the app in `appDirectory`.
|
|
1020
|
+
* Starting from the app's package.json, dependencies will be visited recursively.
|
|
1021
|
+
*
|
|
1022
|
+
* For packages that use our package extensions (services etc.), metadata will be gathered and
|
|
1023
|
+
* will be returned here.
|
|
1024
|
+
*/
|
|
1025
|
+
async getAppMetadata(ctx, appDirectory) {
|
|
1026
|
+
isDebug$2 && debug$2(`Request for app metadata of ${appDirectory}`);
|
|
1027
|
+
const appPackageMetadata = await this.#getPackageMetadata(ctx, {
|
|
1028
|
+
type: "absolute",
|
|
1029
|
+
directory: appDirectory
|
|
1030
|
+
});
|
|
1031
|
+
if (!appPackageMetadata) throw new ReportableError(`Failed to parse app metadata in ${appDirectory}. Ensure that the app is a valid local package.`);
|
|
1032
|
+
const appLocales = Array.from(appPackageMetadata.i18nPaths.keys());
|
|
1033
|
+
const packageMetadataByName = /* @__PURE__ */ new Map();
|
|
1034
|
+
const packageSeenByDirectory = /* @__PURE__ */ new Set();
|
|
1035
|
+
packageMetadataByName.set(appPackageMetadata.name, appPackageMetadata);
|
|
1036
|
+
packageSeenByDirectory.add(appPackageMetadata.directory);
|
|
1037
|
+
const visitDependencies = async (dependencies, importedFrom) => {
|
|
1038
|
+
const jobs = dependencies.map(async (dependency) => {
|
|
1039
|
+
const packageMetadata = await this.#getPackageMetadata(ctx, {
|
|
1040
|
+
type: "unresolved",
|
|
1041
|
+
dependency,
|
|
1042
|
+
importedFrom
|
|
1043
|
+
});
|
|
1044
|
+
if (!packageMetadata) return;
|
|
1045
|
+
if (!packageMetadataByName.has(packageMetadata.name)) packageMetadataByName.set(packageMetadata.name, packageMetadata);
|
|
1046
|
+
if (!packageSeenByDirectory.has(packageMetadata.directory)) {
|
|
1047
|
+
packageSeenByDirectory.add(packageMetadata.directory);
|
|
1048
|
+
await visitDependencies(packageMetadata.dependencies, packageMetadata.packageJsonPath);
|
|
1049
|
+
} else isDebug$2 && debug$2(`Skipping already visited package at %s`, packageMetadata.directory);
|
|
1050
|
+
});
|
|
1051
|
+
return await Promise.all(jobs);
|
|
1052
|
+
};
|
|
1053
|
+
await visitDependencies(appPackageMetadata.dependencies, appPackageMetadata.packageJsonPath);
|
|
1054
|
+
const packages = Array.from(packageMetadataByName.values());
|
|
1055
|
+
const runtimeMetadataVersion = detectRuntimeMetadataVersion(packages);
|
|
1056
|
+
return {
|
|
1057
|
+
name: appPackageMetadata.name,
|
|
1058
|
+
directory: appPackageMetadata.directory,
|
|
1059
|
+
locales: appLocales,
|
|
1060
|
+
packageJsonPath: appPackageMetadata.packageJsonPath,
|
|
1061
|
+
appPackage: appPackageMetadata,
|
|
1062
|
+
packages,
|
|
1063
|
+
runtimeMetadataVersion
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Returns package metadata associated with the given package.
|
|
1068
|
+
*/
|
|
1069
|
+
async #getPackageMetadata(ctx, loc) {
|
|
1070
|
+
isDebug$2 && debug$2(`Request for package metadata of ${formatPackageLocation(loc)}`);
|
|
1071
|
+
const packageDir = await this.#resolvePackageLocation(loc);
|
|
1072
|
+
if (!packageDir) return;
|
|
1073
|
+
const importedFrom = loc.type === "absolute" ? void 0 : loc.importedFrom;
|
|
1074
|
+
const entry = await this.#packageMetadataCache.get(packageDir, ctx, importedFrom);
|
|
1075
|
+
propagateWatchFiles(entry.watchFiles, ctx);
|
|
1076
|
+
if (entry.metadata.type === "plain") {
|
|
1077
|
+
isDebug$2 && debug$2(`Skipping package '${packageDir}'.`);
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
return entry.metadata;
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Returns the parsed contents of the given i18n file.
|
|
1084
|
+
*/
|
|
1085
|
+
async getI18nFile(ctx, path) {
|
|
1086
|
+
const { i18n, watchFiles } = await this.#i18nCache.get(path, ctx);
|
|
1087
|
+
propagateWatchFiles(watchFiles, ctx);
|
|
1088
|
+
return i18n;
|
|
1089
|
+
}
|
|
1090
|
+
async #resolvePackageLocation(loc) {
|
|
1091
|
+
if (loc.type === "absolute") return await realpath(loc.directory);
|
|
1092
|
+
const { packageName, optional } = loc.dependency;
|
|
1093
|
+
const packagePath = await findDepPkgJsonPath(packageName, loc.importedFrom);
|
|
1094
|
+
if (!packagePath) {
|
|
1095
|
+
if (optional) {
|
|
1096
|
+
isDebug$2 && debug$2(`Optional package '${packageName}' was not found.`);
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
throw new ReportableError(`Failed to find package '${packageName}' (from '${loc.importedFrom}'), is the dependency installed correctly?`);
|
|
1100
|
+
}
|
|
1101
|
+
const packageDir = dirname$2(await realpath(packagePath));
|
|
1102
|
+
isDebug$2 && debug$2(`Found package '${packageName}' at ${packageDir}`);
|
|
1103
|
+
return packageDir;
|
|
1104
|
+
}
|
|
1105
|
+
#createI18nCache() {
|
|
1106
|
+
return new Cache({
|
|
1107
|
+
getId(path) {
|
|
1108
|
+
return normalizePath(path);
|
|
1109
|
+
},
|
|
1110
|
+
async getValue(path, ctx) {
|
|
1111
|
+
isDebug$2 && debug$2(`Loading i18n file ${path}`);
|
|
1112
|
+
ctx.addWatchFile(path);
|
|
1113
|
+
return {
|
|
1114
|
+
i18n: await loadI18nFile(path),
|
|
1115
|
+
watchFiles: /* @__PURE__ */ new Set([path])
|
|
1116
|
+
};
|
|
1117
|
+
},
|
|
1118
|
+
onInvalidate(path) {
|
|
1119
|
+
isDebug$2 && debug$2(`Removed cache entry for i18n file ${path}`);
|
|
1120
|
+
},
|
|
1121
|
+
onCachedReturn(path) {
|
|
1122
|
+
isDebug$2 && debug$2(`Returning cached entry for i18n file ${path}`);
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
#createPackageMetadataCache() {
|
|
1127
|
+
const sourceRoot = this.#sourceRoot;
|
|
1128
|
+
return new Cache({
|
|
1129
|
+
_byName: /* @__PURE__ */ new Map(),
|
|
1130
|
+
getId(directory) {
|
|
1131
|
+
return normalizePath(directory);
|
|
1132
|
+
},
|
|
1133
|
+
async getValue(directory, ctx, importedFrom) {
|
|
1134
|
+
isDebug$2 && debug$2(`Loading metadata for package at ${directory} (imported from ${importedFrom ?? "N/A"})`);
|
|
1135
|
+
const watchFiles = /* @__PURE__ */ new Set();
|
|
1136
|
+
const metadata = await loadPackageMetadata({
|
|
1137
|
+
resolveLocalFile: ctx.resolveLocalFile.bind(ctx),
|
|
1138
|
+
addWatchFile(id) {
|
|
1139
|
+
ctx.addWatchFile(id);
|
|
1140
|
+
watchFiles.add(id);
|
|
1141
|
+
},
|
|
1142
|
+
warn: ctx.warn.bind(ctx)
|
|
1143
|
+
}, directory, {
|
|
1144
|
+
sourceRoot,
|
|
1145
|
+
importedFrom
|
|
1146
|
+
});
|
|
1147
|
+
isDebug$2 && debug$2(`Metadata for '${metadata.name}': %O`, metadata);
|
|
1148
|
+
if (metadata.type === "pioneer-package") {
|
|
1149
|
+
const existingMetadata = this._byName.get(metadata.name);
|
|
1150
|
+
if (existingMetadata && normalizePath(existingMetadata.directory) !== normalizePath(metadata.directory)) throw new ReportableError(`Encountered the package '${metadata.name}' at two different locations.\nTrails packages cannot be used more than once in the same application.\nAll packages must use a common version of '${metadata.name}'.\n\n1. ${formatPackage(existingMetadata)}\n\n2. ${formatPackage(metadata)}`);
|
|
1151
|
+
this._byName.set(metadata.name, metadata);
|
|
1152
|
+
}
|
|
1153
|
+
return {
|
|
1154
|
+
metadata,
|
|
1155
|
+
watchFiles
|
|
1156
|
+
};
|
|
1157
|
+
},
|
|
1158
|
+
onInvalidate(directory, entry) {
|
|
1159
|
+
isDebug$2 && debug$2(`Removed cache entry for '${entry.metadata.name}'`);
|
|
1160
|
+
this._byName.delete(entry.metadata.name);
|
|
1161
|
+
},
|
|
1162
|
+
onCachedReturn(directory, entry) {
|
|
1163
|
+
isDebug$2 && debug$2(`Returning cached metadata for '${entry.metadata.name}'`);
|
|
1164
|
+
}
|
|
1165
|
+
});
|
|
1166
|
+
}
|
|
1383
1167
|
};
|
|
1384
1168
|
function detectRuntimeMetadataVersion(packages) {
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
case "invalid-version":
|
|
1398
|
-
throw new ReportableError(`Invalid metadata version ${metadataVersion}`, {
|
|
1399
|
-
cause: versionResult.error
|
|
1400
|
-
});
|
|
1401
|
-
case "unsupported-version":
|
|
1402
|
-
throw new ReportableError(
|
|
1403
|
-
`Runtime metadata version ${metadataVersion} of ${runtimePackage.name} is not supported.
|
|
1404
|
-
This plugin supports versions ^${RuntimeSupport2.CURRENT_METADATA_MAJOR}.`
|
|
1405
|
-
);
|
|
1406
|
-
}
|
|
1407
|
-
}
|
|
1408
|
-
isDebug3 && debug4("Detected runtime version %s", versionResult);
|
|
1409
|
-
return versionResult;
|
|
1169
|
+
const runtimePackage = packages.find((p) => p.name === RuntimeSupport.RUNTIME_PACKAGE_NAME);
|
|
1170
|
+
if (!runtimePackage) return RuntimeSupport.DEFAULT_METADATA_VERSION;
|
|
1171
|
+
const metadataVersion = runtimePackage.config.runtimeMeta?.metadataVersion;
|
|
1172
|
+
if (!metadataVersion) return RuntimeSupport.DEFAULT_METADATA_VERSION;
|
|
1173
|
+
const versionResult = RuntimeSupport.getSupportedRuntimeMetadataVersion(metadataVersion);
|
|
1174
|
+
if (typeof versionResult != "string" && "code" in versionResult) switch (versionResult.code) {
|
|
1175
|
+
case "invalid-version": throw new ReportableError(`Invalid metadata version ${metadataVersion}`, { cause: versionResult.error });
|
|
1176
|
+
case "unsupported-version": throw new ReportableError(`Runtime metadata version ${metadataVersion} of ${runtimePackage.name} is not supported.
|
|
1177
|
+
This plugin supports versions ^${RuntimeSupport.CURRENT_METADATA_MAJOR}.`);
|
|
1178
|
+
}
|
|
1179
|
+
isDebug$2 && debug$2("Detected runtime version %s", versionResult);
|
|
1180
|
+
return versionResult;
|
|
1410
1181
|
}
|
|
1411
1182
|
function propagateWatchFiles(watchFiles, ctx) {
|
|
1412
|
-
|
|
1413
|
-
ctx.addWatchFile(file);
|
|
1414
|
-
}
|
|
1183
|
+
for (const file of watchFiles) ctx.addWatchFile(file);
|
|
1415
1184
|
}
|
|
1416
1185
|
function formatPackageLocation(loc) {
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
return `${name} required by ${loc.importedFrom}`;
|
|
1426
|
-
}
|
|
1427
|
-
}
|
|
1186
|
+
switch (loc.type) {
|
|
1187
|
+
case "absolute": return `${loc.directory}`;
|
|
1188
|
+
case "unresolved": {
|
|
1189
|
+
let name = `'${loc.dependency.packageName}'`;
|
|
1190
|
+
if (loc.dependency.optional) name += " (optional)";
|
|
1191
|
+
return `${name} required by ${loc.importedFrom}`;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1428
1194
|
}
|
|
1429
|
-
|
|
1195
|
+
const PACKAGE_JSON_RE = /[\\/]package\.json($|\?)/;
|
|
1430
1196
|
function isPackageJson(file) {
|
|
1431
|
-
|
|
1197
|
+
return PACKAGE_JSON_RE.test(file);
|
|
1432
1198
|
}
|
|
1433
1199
|
function isBuildConfig(file) {
|
|
1434
|
-
|
|
1200
|
+
return basename$1(file) === BUILD_CONFIG_NAME;
|
|
1435
1201
|
}
|
|
1436
1202
|
function formatPackage(packageMetadata) {
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
str += ` at ${packageMetadata.directory}`;
|
|
1442
|
-
return str;
|
|
1203
|
+
let str = `${packageMetadata.name}`;
|
|
1204
|
+
if (packageMetadata.version != null) str += `@${packageMetadata.version}`;
|
|
1205
|
+
str += ` at ${packageMetadata.directory}`;
|
|
1206
|
+
return str;
|
|
1443
1207
|
}
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1208
|
+
//#endregion
|
|
1209
|
+
//#region src/metadata/validateI18nConfig.ts
|
|
1210
|
+
const isDebug$1 = !!process.env.DEBUG;
|
|
1211
|
+
const debug$1 = createDebugger("open-pioneer:validateI18nConfig");
|
|
1448
1212
|
async function validateI18nConfig(ctx, repository, appMetadata) {
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1213
|
+
const locales = appMetadata.locales;
|
|
1214
|
+
for (const pkg of appMetadata.packages) {
|
|
1215
|
+
isDebug$1 && debug$1("Checking i18n files of package %s", pkg.name);
|
|
1216
|
+
await checkPackageI18nFiles(ctx, pkg, locales);
|
|
1217
|
+
}
|
|
1218
|
+
await checkAppI18n(ctx, repository, appMetadata);
|
|
1455
1219
|
}
|
|
1456
|
-
async function checkPackageI18nFiles(ctx,
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
);
|
|
1469
|
-
} else {
|
|
1470
|
-
isDebug4 && debug5("i18n file %s exists", filePath);
|
|
1471
|
-
}
|
|
1472
|
-
}
|
|
1220
|
+
async function checkPackageI18nFiles(ctx, pkg, locales) {
|
|
1221
|
+
for (const locale of locales) {
|
|
1222
|
+
const filePath = pkg.i18nPaths.get(locale);
|
|
1223
|
+
if (!filePath) {
|
|
1224
|
+
isDebug$1 && debug$1("Package does not support locale '%s'", locale);
|
|
1225
|
+
continue;
|
|
1226
|
+
}
|
|
1227
|
+
isDebug$1 && debug$1("Checking i18n file %s", filePath);
|
|
1228
|
+
ctx.addWatchFile(filePath);
|
|
1229
|
+
if (!await fileExists(filePath)) throw new ReportableError(`I18n file in package '${pkg.name}' for locale '${locale}' does not exist: '${filePath}'.`);
|
|
1230
|
+
else isDebug$1 && debug$1("i18n file %s exists", filePath);
|
|
1231
|
+
}
|
|
1473
1232
|
}
|
|
1474
1233
|
async function checkAppI18n(ctx, repository, appMetadata) {
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
return;
|
|
1507
|
-
}
|
|
1508
|
-
errors.sort((p1, p2) => p1.name.localeCompare(p2.name, "en"));
|
|
1509
|
-
const getPackageErrors = () => {
|
|
1510
|
-
const MAX = 3;
|
|
1511
|
-
const take = Math.min(errors.length, MAX);
|
|
1512
|
-
const remaining = errors.length - take;
|
|
1513
|
-
let buffer = "";
|
|
1514
|
-
for (let i = 0; i < take; ++i) {
|
|
1515
|
-
if (i > 0) {
|
|
1516
|
-
buffer += ", ";
|
|
1517
|
-
}
|
|
1518
|
-
const pkg2 = errors[i];
|
|
1519
|
-
buffer += `'${pkg2.name}' (${pkg2.locales.toSorted().join(", ")})`;
|
|
1520
|
-
}
|
|
1521
|
-
if (remaining > 0) {
|
|
1522
|
-
buffer += ` (and ${remaining} more)`;
|
|
1523
|
-
}
|
|
1524
|
-
return buffer;
|
|
1525
|
-
};
|
|
1526
|
-
const formattedAppLocales = appMetadata.locales.join(", ") || "none";
|
|
1527
|
-
const formattedPackageErrors = getPackageErrors();
|
|
1528
|
-
throw new ReportableError(
|
|
1529
|
-
`Invalid i18n configuration in application at ${appMetadata.directory}:
|
|
1530
|
-
There is no match between the locales supported by the application (${formattedAppLocales}) and the locales supported by the packages ${formattedPackageErrors}.`
|
|
1531
|
-
);
|
|
1234
|
+
const appPackage = appMetadata.appPackage;
|
|
1235
|
+
const appLocales = new Set(appMetadata.locales);
|
|
1236
|
+
const appI18nFiles = await Promise.all(Array.from(appLocales).map((locale) => {
|
|
1237
|
+
const i18nPath = appPackage.i18nPaths.get(locale);
|
|
1238
|
+
if (i18nPath == null) throw new Error(`App package '${appPackage.name}' does not have an i18n file for locale '${locale}'.`);
|
|
1239
|
+
return repository.getI18nFile(ctx, i18nPath);
|
|
1240
|
+
}));
|
|
1241
|
+
const errors = [];
|
|
1242
|
+
for (const pkg of appMetadata.packages) {
|
|
1243
|
+
if (pkg.locales.length === 0) continue;
|
|
1244
|
+
if (pkg.locales.some((locale) => appLocales.has(locale))) continue;
|
|
1245
|
+
if (appI18nFiles.some((i18nFile) => i18nFile.overrides?.get(pkg.name))) continue;
|
|
1246
|
+
errors.push(pkg);
|
|
1247
|
+
}
|
|
1248
|
+
if (errors.length === 0) return;
|
|
1249
|
+
errors.sort((p1, p2) => p1.name.localeCompare(p2.name, "en"));
|
|
1250
|
+
const getPackageErrors = () => {
|
|
1251
|
+
const take = Math.min(errors.length, 3);
|
|
1252
|
+
const remaining = errors.length - take;
|
|
1253
|
+
let buffer = "";
|
|
1254
|
+
for (let i = 0; i < take; ++i) {
|
|
1255
|
+
if (i > 0) buffer += ", ";
|
|
1256
|
+
const pkg = errors[i];
|
|
1257
|
+
buffer += `'${pkg.name}' (${pkg.locales.toSorted().join(", ")})`;
|
|
1258
|
+
}
|
|
1259
|
+
if (remaining > 0) buffer += ` (and ${remaining} more)`;
|
|
1260
|
+
return buffer;
|
|
1261
|
+
};
|
|
1262
|
+
const formattedAppLocales = appMetadata.locales.join(", ") || "none";
|
|
1263
|
+
const formattedPackageErrors = getPackageErrors();
|
|
1264
|
+
throw new ReportableError(`Invalid i18n configuration in application at ${appMetadata.directory}:\nThere is no match between the locales supported by the application (${formattedAppLocales}) and the locales supported by the packages ${formattedPackageErrors}.`);
|
|
1532
1265
|
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1266
|
+
//#endregion
|
|
1267
|
+
//#region src/codegenPlugin.ts
|
|
1268
|
+
const isDebug = !!process.env.DEBUG;
|
|
1269
|
+
const debug = createDebugger("open-pioneer:codegen");
|
|
1537
1270
|
function codegenPlugin() {
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
const generatedSourceCode = generateCombinedCss(appMetadata.packages);
|
|
1673
|
-
isDebug5 && debug6("Generated app css: %O", generatedSourceCode);
|
|
1674
|
-
return jsModule(generatedSourceCode);
|
|
1675
|
-
}
|
|
1676
|
-
case "app-i18n-index": {
|
|
1677
|
-
await validateI18nConfig(ctx, repository, appMetadata);
|
|
1678
|
-
const generatedSourceCode = generateI18nIndex(
|
|
1679
|
-
mod.packageDirectory,
|
|
1680
|
-
appMetadata.locales
|
|
1681
|
-
);
|
|
1682
|
-
isDebug5 && debug6("Generated i18n lookup: %O", generatedSourceCode);
|
|
1683
|
-
return jsModule(generatedSourceCode);
|
|
1684
|
-
}
|
|
1685
|
-
case "app-i18n": {
|
|
1686
|
-
const generatedSourceCode = await generateI18nMessages({
|
|
1687
|
-
locale: mod.locale,
|
|
1688
|
-
appName: appMetadata.name,
|
|
1689
|
-
packages: appMetadata.packages,
|
|
1690
|
-
loadI18n: async (pkg2, filePath) => {
|
|
1691
|
-
ctx.addWatchFile(filePath);
|
|
1692
|
-
if (!await fileExists(filePath)) {
|
|
1693
|
-
throw new ReportableError(
|
|
1694
|
-
`I18n file at ${filePath} does not exist.`
|
|
1695
|
-
);
|
|
1696
|
-
}
|
|
1697
|
-
return repository.getI18nFile(ctx, filePath);
|
|
1698
|
-
}
|
|
1699
|
-
});
|
|
1700
|
-
isDebug5 && debug6("Generated i18n messages: %O", generatedSourceCode);
|
|
1701
|
-
return jsModule(generatedSourceCode);
|
|
1702
|
-
}
|
|
1703
|
-
default:
|
|
1704
|
-
assertNever(mod.type);
|
|
1705
|
-
}
|
|
1706
|
-
} catch (e) {
|
|
1707
|
-
reportError(this, e, isDev);
|
|
1708
|
-
}
|
|
1709
|
-
}
|
|
1710
|
-
},
|
|
1711
|
-
watchChange(id, _change) {
|
|
1712
|
-
isDebug5 && debug6("File %s changed", id);
|
|
1713
|
-
repository.onFileChanged(id);
|
|
1714
|
-
},
|
|
1715
|
-
generateBundle(_options, bundle) {
|
|
1716
|
-
deploymentModule.onGenerateBundle(bundle);
|
|
1717
|
-
}
|
|
1718
|
-
};
|
|
1271
|
+
let isDev;
|
|
1272
|
+
let rootDir;
|
|
1273
|
+
let repository;
|
|
1274
|
+
let deploymentModule;
|
|
1275
|
+
return {
|
|
1276
|
+
name: "pioneer:codegen",
|
|
1277
|
+
async config(userConfig, env) {
|
|
1278
|
+
isDev = env.command === "serve";
|
|
1279
|
+
rootDir = userConfig.root ?? cwd();
|
|
1280
|
+
repository = new MetadataRepository(rootDir);
|
|
1281
|
+
if (isDev) return await configureDevOptimizer(userConfig, rootDir);
|
|
1282
|
+
},
|
|
1283
|
+
configResolved(config) {
|
|
1284
|
+
deploymentModule = new DeploymentModule(isDev, config.base);
|
|
1285
|
+
},
|
|
1286
|
+
async buildStart() {
|
|
1287
|
+
repository?.reset();
|
|
1288
|
+
deploymentModule.onStart(this);
|
|
1289
|
+
},
|
|
1290
|
+
resolveId: {
|
|
1291
|
+
filter: { id: VIRTUAL_ID_FILTER },
|
|
1292
|
+
async handler(moduleId, importer) {
|
|
1293
|
+
try {
|
|
1294
|
+
if (!importer) return;
|
|
1295
|
+
if (parseVirtualModuleId(moduleId)) return moduleId;
|
|
1296
|
+
let virtualModule;
|
|
1297
|
+
try {
|
|
1298
|
+
virtualModule = RuntimeSupport.parseVirtualModule(moduleId);
|
|
1299
|
+
} catch (e) {
|
|
1300
|
+
this.error({
|
|
1301
|
+
id: moduleId,
|
|
1302
|
+
message: "Invalid virtual module id",
|
|
1303
|
+
cause: e
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
switch (virtualModule) {
|
|
1307
|
+
case "app": return serializeModuleId({
|
|
1308
|
+
type: "app-meta",
|
|
1309
|
+
packageDirectory: getPackageDirectoryFromImporter(importer, rootDir)
|
|
1310
|
+
});
|
|
1311
|
+
case "react-hooks": return serializeModuleId({
|
|
1312
|
+
type: "package-hooks",
|
|
1313
|
+
packageDirectory: getPackageDirectoryFromImporter(importer, rootDir)
|
|
1314
|
+
});
|
|
1315
|
+
case "source-info": return serializeModuleId({
|
|
1316
|
+
type: "source-info",
|
|
1317
|
+
modulePath: importer,
|
|
1318
|
+
packageDirectory: getPackageDirectoryFromImporter(importer, rootDir)
|
|
1319
|
+
});
|
|
1320
|
+
case "deployment": return serializeModuleId({ type: "deployment" });
|
|
1321
|
+
}
|
|
1322
|
+
} catch (e) {
|
|
1323
|
+
reportError(this, e, isDev);
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
},
|
|
1327
|
+
load: {
|
|
1328
|
+
filter: { id: VIRTUAL_ID_FILTER },
|
|
1329
|
+
async handler(moduleId) {
|
|
1330
|
+
try {
|
|
1331
|
+
const mod = parseVirtualModuleId(moduleId);
|
|
1332
|
+
if (!mod) return;
|
|
1333
|
+
isDebug && debug("Loading virtual module %O", mod);
|
|
1334
|
+
if (mod.type === "deployment") return jsModule(deploymentModule.onLoadModule());
|
|
1335
|
+
const packageJsonPath = await resolvePackageJson(this, mod);
|
|
1336
|
+
if (mod.type === "package-hooks") {
|
|
1337
|
+
const directory = mod.packageDirectory;
|
|
1338
|
+
const packageJsonPath = (await this.resolve(directory + "/package.json"))?.id;
|
|
1339
|
+
if (!packageJsonPath) throw new ReportableError(`Failed to resolve package.json in ${directory}`);
|
|
1340
|
+
const packageName = await getPackageName(this, packageJsonPath);
|
|
1341
|
+
const generatedSourceCode = RuntimeSupport.generateReactHooks(packageName, RuntimeSupport.REACT_INTEGRATION_MODULE_ID);
|
|
1342
|
+
isDebug && debug("Generated hooks code: %O", generatedSourceCode);
|
|
1343
|
+
return jsModule(generatedSourceCode);
|
|
1344
|
+
}
|
|
1345
|
+
if (mod.type === "source-info") {
|
|
1346
|
+
const packageName = await getPackageName(this, packageJsonPath);
|
|
1347
|
+
return jsModule(RuntimeSupport.generateSourceInfo(packageName, mod.modulePath));
|
|
1348
|
+
}
|
|
1349
|
+
const ctx = createMetadataContextFromRolldown(this);
|
|
1350
|
+
const appMetadata = await repository.getAppMetadata(ctx, dirname(packageJsonPath));
|
|
1351
|
+
if (mod.type === "app-meta") {
|
|
1352
|
+
const runtimeVersion = appMetadata.runtimeMetadataVersion;
|
|
1353
|
+
isDebug && debug("Generating app metadata for runtime version %s", runtimeVersion);
|
|
1354
|
+
return jsModule(generateAppMetadata(mod.packageDirectory, RuntimeSupport.METADATA_MODULE_ID, runtimeVersion));
|
|
1355
|
+
}
|
|
1356
|
+
switch (mod.type) {
|
|
1357
|
+
case "app-packages": {
|
|
1358
|
+
const generatedSourceCode = generatePackagesMetadata({
|
|
1359
|
+
appName: appMetadata.name,
|
|
1360
|
+
packages: appMetadata.packages
|
|
1361
|
+
});
|
|
1362
|
+
isDebug && debug("Generated app metadata: %O", generatedSourceCode);
|
|
1363
|
+
return jsModule(generatedSourceCode);
|
|
1364
|
+
}
|
|
1365
|
+
case "app-css": {
|
|
1366
|
+
const generatedSourceCode = generateCombinedCss(appMetadata.packages);
|
|
1367
|
+
isDebug && debug("Generated app css: %O", generatedSourceCode);
|
|
1368
|
+
return jsModule(generatedSourceCode);
|
|
1369
|
+
}
|
|
1370
|
+
case "app-i18n-index": {
|
|
1371
|
+
await validateI18nConfig(ctx, repository, appMetadata);
|
|
1372
|
+
const generatedSourceCode = generateI18nIndex(mod.packageDirectory, appMetadata.locales);
|
|
1373
|
+
isDebug && debug("Generated i18n lookup: %O", generatedSourceCode);
|
|
1374
|
+
return jsModule(generatedSourceCode);
|
|
1375
|
+
}
|
|
1376
|
+
case "app-i18n": {
|
|
1377
|
+
const generatedSourceCode = await generateI18nMessages({
|
|
1378
|
+
locale: mod.locale,
|
|
1379
|
+
appName: appMetadata.name,
|
|
1380
|
+
packages: appMetadata.packages,
|
|
1381
|
+
loadI18n: async (pkg, filePath) => {
|
|
1382
|
+
ctx.addWatchFile(filePath);
|
|
1383
|
+
if (!await fileExists(filePath)) throw new ReportableError(`I18n file at ${filePath} does not exist.`);
|
|
1384
|
+
return repository.getI18nFile(ctx, filePath);
|
|
1385
|
+
}
|
|
1386
|
+
});
|
|
1387
|
+
isDebug && debug("Generated i18n messages: %O", generatedSourceCode);
|
|
1388
|
+
return jsModule(generatedSourceCode);
|
|
1389
|
+
}
|
|
1390
|
+
default: assertNever(mod.type);
|
|
1391
|
+
}
|
|
1392
|
+
} catch (e) {
|
|
1393
|
+
reportError(this, e, isDev);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
},
|
|
1397
|
+
watchChange(id, _change) {
|
|
1398
|
+
isDebug && debug("File %s changed", id);
|
|
1399
|
+
repository.onFileChanged(id);
|
|
1400
|
+
},
|
|
1401
|
+
generateBundle(_options, bundle) {
|
|
1402
|
+
deploymentModule.onGenerateBundle(bundle);
|
|
1403
|
+
}
|
|
1404
|
+
};
|
|
1719
1405
|
}
|
|
1720
1406
|
async function resolvePackageJson(ctx, mod) {
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
throw new ReportableError(`Failed to resolve package.json in ${mod.packageDirectory}`);
|
|
1725
|
-
}
|
|
1726
|
-
return path;
|
|
1407
|
+
const path = (await ctx.resolve(mod.packageDirectory + "/package.json"))?.id;
|
|
1408
|
+
if (!path) throw new ReportableError(`Failed to resolve package.json in ${mod.packageDirectory}`);
|
|
1409
|
+
return path;
|
|
1727
1410
|
}
|
|
1728
1411
|
async function configureDevOptimizer(userConfig, rootDir) {
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
rolldownOptions: {
|
|
1734
|
-
plugins: [createOptimizeDepsRolldownPlugin()]
|
|
1735
|
-
}
|
|
1736
|
-
}
|
|
1737
|
-
};
|
|
1412
|
+
return { optimizeDeps: {
|
|
1413
|
+
include: await getOptimizeDepsIncludes(rootDir),
|
|
1414
|
+
rolldownOptions: { plugins: [createOptimizeDepsRolldownPlugin()] }
|
|
1415
|
+
} };
|
|
1738
1416
|
}
|
|
1417
|
+
/**
|
|
1418
|
+
* Find external trails packages and add their services modules (if any)
|
|
1419
|
+
* to vite's `optimizeDeps.include`.
|
|
1420
|
+
*
|
|
1421
|
+
* This should reduce the number of `✨ optimized dependencies changed. reloading` events during development.
|
|
1422
|
+
*
|
|
1423
|
+
* NOTE: A better solution would be to teach vite's dependency optimizer how to interpret "open-pioneer:*",
|
|
1424
|
+
* so that it accurately understands the actual additional imports from trails into node modules (i.e. all services etc.).
|
|
1425
|
+
*
|
|
1426
|
+
* I couldn't get the esbuild plugin working, however.
|
|
1427
|
+
* This could be reattempted when vite has migrated to rolldown.
|
|
1428
|
+
*/
|
|
1739
1429
|
async function getOptimizeDepsIncludes(rootDir) {
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
trailsModules.push(`${RuntimeSupport3.RUNTIME_PACKAGE_NAME}/**`);
|
|
1749
|
-
isDebug5 && debug6("Optimizing additional modules %O", trailsModules);
|
|
1750
|
-
return trailsModules;
|
|
1430
|
+
const trailsModules = (await findTrailsPackages(rootDir)).flatMap((pkg) => {
|
|
1431
|
+
if (!/[/\\]node_modules[/\\]/.test(pkg.directory)) return [];
|
|
1432
|
+
const serviceModule = pkg.servicesModuleId;
|
|
1433
|
+
return serviceModule ? [serviceModule] : [];
|
|
1434
|
+
});
|
|
1435
|
+
trailsModules.push(`${RuntimeSupport.RUNTIME_PACKAGE_NAME}/**`);
|
|
1436
|
+
isDebug && debug("Optimizing additional modules %O", trailsModules);
|
|
1437
|
+
return trailsModules;
|
|
1751
1438
|
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Creates a rolldown plugin for use during Vite's dependency optimization phase.
|
|
1441
|
+
* It marks all `open-pioneer:*` virtual module imports as external so that they
|
|
1442
|
+
* are left untouched by rolldown and can later be handled by the normal Vite plugin.
|
|
1443
|
+
*/
|
|
1752
1444
|
function createOptimizeDepsRolldownPlugin() {
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1445
|
+
return {
|
|
1446
|
+
name: "pioneer:optimize-deps",
|
|
1447
|
+
resolveId(id) {
|
|
1448
|
+
if (/^open-pioneer:/.test(id)) return {
|
|
1449
|
+
id,
|
|
1450
|
+
external: true
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
};
|
|
1761
1454
|
}
|
|
1762
1455
|
function reportError(ctx, error, isDev) {
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
ctx.error({
|
|
1776
|
-
message,
|
|
1777
|
-
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1778
|
-
cause: error.cause,
|
|
1779
|
-
// oxlint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1780
|
-
stack: error.stack
|
|
1781
|
-
});
|
|
1456
|
+
let message;
|
|
1457
|
+
if (error instanceof ReportableError) message = error.message;
|
|
1458
|
+
else message = "Internal error: " + (String(error?.message) || "Unknown error");
|
|
1459
|
+
if (isDev) for (const cause of getCauseMessages(error)) {
|
|
1460
|
+
message += "\n\n";
|
|
1461
|
+
message += `Caused by: ${cause}`;
|
|
1462
|
+
}
|
|
1463
|
+
ctx.error({
|
|
1464
|
+
message,
|
|
1465
|
+
cause: error.cause,
|
|
1466
|
+
stack: error.stack
|
|
1467
|
+
});
|
|
1782
1468
|
}
|
|
1783
1469
|
function getPackageDirectoryFromDirectory(directory, rootDir) {
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
}
|
|
1788
|
-
const packageDir = dirname4(packageJsonPath);
|
|
1789
|
-
return normalizePath5(packageDir);
|
|
1470
|
+
const packageJsonPath = findPackageJson(directory, rootDir);
|
|
1471
|
+
if (!packageJsonPath) throw new ReportableError(`Failed to find package.json for package from '${directory}'.`);
|
|
1472
|
+
return normalizePath(dirname(packageJsonPath));
|
|
1790
1473
|
}
|
|
1791
1474
|
function getPackageDirectoryFromImporter(importer, rootDir) {
|
|
1792
|
-
|
|
1475
|
+
return getPackageDirectoryFromDirectory(dirname(importer), rootDir);
|
|
1793
1476
|
}
|
|
1794
1477
|
async function getPackageName(ctx, packageJsonPath) {
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
}
|
|
1805
|
-
return name;
|
|
1478
|
+
let name;
|
|
1479
|
+
try {
|
|
1480
|
+
const content = await readFile(packageJsonPath, "utf-8");
|
|
1481
|
+
name = JSON.parse(content).name;
|
|
1482
|
+
} catch (e) {
|
|
1483
|
+
throw new ReportableError(`Failed to read package.json file`, { cause: e });
|
|
1484
|
+
}
|
|
1485
|
+
if (!name) throw new ReportableError(`Failed to read package name from ${packageJsonPath}.`);
|
|
1486
|
+
return name;
|
|
1806
1487
|
}
|
|
1807
1488
|
function jsModule(code) {
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1489
|
+
return {
|
|
1490
|
+
code,
|
|
1491
|
+
moduleType: "js"
|
|
1492
|
+
};
|
|
1812
1493
|
}
|
|
1813
1494
|
function getCauseMessages(error) {
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
causes.push(message);
|
|
1825
|
-
error = cause;
|
|
1826
|
-
}
|
|
1827
|
-
return causes;
|
|
1495
|
+
const causes = [];
|
|
1496
|
+
while (1) {
|
|
1497
|
+
const cause = error?.cause;
|
|
1498
|
+
if (!cause) break;
|
|
1499
|
+
const message = cause.message;
|
|
1500
|
+
if (!message || typeof message !== "string") break;
|
|
1501
|
+
causes.push(message);
|
|
1502
|
+
error = cause;
|
|
1503
|
+
}
|
|
1504
|
+
return causes;
|
|
1828
1505
|
}
|
|
1829
1506
|
function assertNever(x) {
|
|
1830
|
-
|
|
1507
|
+
throw new Error("Should not be reached: " + x);
|
|
1831
1508
|
}
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
import { existsSync as existsSync3 } from "node:fs";
|
|
1835
|
-
import { posix as posix4, resolve } from "node:path";
|
|
1836
|
-
import { cwd as cwd2 } from "node:process";
|
|
1509
|
+
//#endregion
|
|
1510
|
+
//#region src/mpaPlugin.ts
|
|
1837
1511
|
function mpaPlugin(options) {
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
};
|
|
1871
|
-
return {
|
|
1872
|
-
build: {
|
|
1873
|
-
rolldownOptions
|
|
1874
|
-
}
|
|
1875
|
-
};
|
|
1876
|
-
},
|
|
1877
|
-
configResolved(config) {
|
|
1878
|
-
resolvedConfig = config;
|
|
1879
|
-
}
|
|
1880
|
-
};
|
|
1512
|
+
const rootSite = options?.rootSite ?? false;
|
|
1513
|
+
const apps = options?.apps || [];
|
|
1514
|
+
const sites = options?.sites || [];
|
|
1515
|
+
let resolvedConfig;
|
|
1516
|
+
return {
|
|
1517
|
+
name: "pioneer:mpa",
|
|
1518
|
+
config(config) {
|
|
1519
|
+
const sourceRoot = config.root ?? cwd();
|
|
1520
|
+
const { entryPoints, appNames } = gatherEntryPoints({
|
|
1521
|
+
apps,
|
|
1522
|
+
sites,
|
|
1523
|
+
rootSite,
|
|
1524
|
+
sourceRoot
|
|
1525
|
+
});
|
|
1526
|
+
return { build: { rolldownOptions: {
|
|
1527
|
+
input: entryPoints,
|
|
1528
|
+
preserveEntrySignatures: "allow-extension",
|
|
1529
|
+
output: {
|
|
1530
|
+
entryFileNames(chunk) {
|
|
1531
|
+
if (appNames.includes(chunk.name)) return "[name].js";
|
|
1532
|
+
return posix.join(resolvedConfig.build.assetsDir, "[hash:12].js");
|
|
1533
|
+
},
|
|
1534
|
+
chunkFileNames(chunk) {
|
|
1535
|
+
return posix.join(resolvedConfig.build.assetsDir, "[hash:12].js");
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
} } };
|
|
1539
|
+
},
|
|
1540
|
+
configResolved(config) {
|
|
1541
|
+
resolvedConfig = config;
|
|
1542
|
+
}
|
|
1543
|
+
};
|
|
1881
1544
|
}
|
|
1882
1545
|
function gatherEntryPoints(options) {
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
}
|
|
1928
|
-
const entryPoints = {};
|
|
1929
|
-
let entryPointCount = 0;
|
|
1930
|
-
for (const { name, path } of [apps, sites].flat()) {
|
|
1931
|
-
const existingPath = entryPoints[name];
|
|
1932
|
-
if (existingPath) {
|
|
1933
|
-
throw new Error(
|
|
1934
|
-
`Entry point '${name}' was defined twice (paths ${existingPath} and ${path}). Remove the duplicated entry.`
|
|
1935
|
-
);
|
|
1936
|
-
}
|
|
1937
|
-
entryPoints[name] = path;
|
|
1938
|
-
++entryPointCount;
|
|
1939
|
-
}
|
|
1940
|
-
if (entryPointCount === 0) {
|
|
1941
|
-
throw new Error(
|
|
1942
|
-
"You must configure at least one site or one app in the pioneer plugin options."
|
|
1943
|
-
);
|
|
1944
|
-
}
|
|
1945
|
-
const appNames = apps.map(({ name }) => name);
|
|
1946
|
-
return {
|
|
1947
|
-
entryPoints,
|
|
1948
|
-
appNames
|
|
1949
|
-
};
|
|
1546
|
+
let apps;
|
|
1547
|
+
if (Array.isArray(options.apps)) apps = options.apps.map((appName) => {
|
|
1548
|
+
return {
|
|
1549
|
+
name: appName,
|
|
1550
|
+
path: findMatchingEntryPointFile(appName, resolve(options.sourceRoot, "apps", appName, "app"))
|
|
1551
|
+
};
|
|
1552
|
+
});
|
|
1553
|
+
else apps = Object.entries(options.apps).map(([name, appPath]) => {
|
|
1554
|
+
const path = resolve(options.sourceRoot, appPath);
|
|
1555
|
+
if (!existsSync$1(path)) throw new Error(`Failed to find app '${name}' at ${path}. Ensure that the path is spelled correctly.`);
|
|
1556
|
+
return {
|
|
1557
|
+
name,
|
|
1558
|
+
path
|
|
1559
|
+
};
|
|
1560
|
+
});
|
|
1561
|
+
const sites = options.sites.map((sitePath) => {
|
|
1562
|
+
const path = resolve(options.sourceRoot, sitePath, "index.html");
|
|
1563
|
+
if (!existsSync$1(path)) throw new Error(`Failed to find site at ${path}. Ensure that the site name in your vite configuration is spelled correctly.`);
|
|
1564
|
+
return {
|
|
1565
|
+
name: sitePath,
|
|
1566
|
+
path
|
|
1567
|
+
};
|
|
1568
|
+
});
|
|
1569
|
+
if (options.rootSite) {
|
|
1570
|
+
const path = resolve(options.sourceRoot, "index.html");
|
|
1571
|
+
if (!existsSync$1(path)) throw new Error(`Failed to find root site at ${path}. Create the missing file or set 'rootSite' to false.`);
|
|
1572
|
+
sites.push({
|
|
1573
|
+
name: "index",
|
|
1574
|
+
path
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
const entryPoints = {};
|
|
1578
|
+
let entryPointCount = 0;
|
|
1579
|
+
for (const { name, path } of [apps, sites].flat()) {
|
|
1580
|
+
const existingPath = entryPoints[name];
|
|
1581
|
+
if (existingPath) throw new Error(`Entry point '${name}' was defined twice (paths ${existingPath} and ${path}). Remove the duplicated entry.`);
|
|
1582
|
+
entryPoints[name] = path;
|
|
1583
|
+
++entryPointCount;
|
|
1584
|
+
}
|
|
1585
|
+
if (entryPointCount === 0) throw new Error("You must configure at least one site or one app in the pioneer plugin options.");
|
|
1586
|
+
return {
|
|
1587
|
+
entryPoints,
|
|
1588
|
+
appNames: apps.map(({ name }) => name)
|
|
1589
|
+
};
|
|
1950
1590
|
}
|
|
1951
|
-
|
|
1591
|
+
const EXTENSIONS = [
|
|
1592
|
+
".ts",
|
|
1593
|
+
".tsx",
|
|
1594
|
+
".js",
|
|
1595
|
+
".jsx"
|
|
1596
|
+
];
|
|
1952
1597
|
function findMatchingEntryPointFile(appName, prefix) {
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
const extensions = EXTENSIONS.join(", ");
|
|
1960
|
-
throw new Error(
|
|
1961
|
-
`Failed to find a matching entry point file for app '${appName}' at ${prefix}. Supported extensions are ${extensions}. Ensure that the app name is spelled correctly or create the missing file.`
|
|
1962
|
-
);
|
|
1598
|
+
for (const ext of EXTENSIONS) {
|
|
1599
|
+
const candidate = prefix + ext;
|
|
1600
|
+
if (existsSync$1(candidate)) return candidate;
|
|
1601
|
+
}
|
|
1602
|
+
const extensions = EXTENSIONS.join(", ");
|
|
1603
|
+
throw new Error(`Failed to find a matching entry point file for app '${appName}' at ${prefix}. Supported extensions are ${extensions}. Ensure that the app name is spelled correctly or create the missing file.`);
|
|
1963
1604
|
}
|
|
1964
|
-
|
|
1965
|
-
|
|
1605
|
+
//#endregion
|
|
1606
|
+
//#region src/index.ts
|
|
1966
1607
|
function pioneer(options) {
|
|
1967
|
-
|
|
1608
|
+
return [mpaPlugin(options), codegenPlugin()];
|
|
1968
1609
|
}
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
//# sourceMappingURL=index.js.map
|
|
1610
|
+
//#endregion
|
|
1611
|
+
export { pioneer };
|
|
1612
|
+
|
|
1613
|
+
//# sourceMappingURL=index.js.map
|