Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 320 321 322 | 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 30x 27x 3x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 22x 22x 22x 1x 69x 69x 69x 69x 69x 31x 24x 31x 7x 7x 7x 7x 7x 31x 69x 69x 1x 31x 31x 31x 31x 31x 34x 34x 31x 31x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1x 36x 36x 36x 36x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 36x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 36x 36x 1x 12x 12x 12x 12x 12x 1x 12x 12x 12x 12x 12x 12x 12x 3x 3x 3x 3x 9x 9x 9x 12x 12x 12x 1x 18x 18x 18x 18x 18x 2x 2x 16x 18x 1x 18x 12x 12x 3x 3x 1x 14x 14x 14x 14x 14x 18x 11x 11x 18x 7x 7x 7x 14x 14x 14x 1x 32x 32x 32x 32x 32x 10x 10x 10x 32x 32x 1x 32x 32x 32x 32x 32x 74x 6x 1x 1x 6x 6x 32x 32x 32x 1x 38x 38x 38x 8x 8x 8x 8x 8x 8x 38x 6x 6x 6x 6x 6x 6x 6x 30x 24x 24x 24x 24x 24x 24x 38x | import path from 'node:path';
import type { BuildEnvironment } from './core';
export interface ModuleConfig {
lib?: boolean;
links?: Record<string, string>;
imports?: ModuleConfigImportMapping;
scopes?: Record<string, ModuleConfigImportMapping>;
exports?: ModuleConfigExportExports;
}
export type ModuleConfigImportMapping = Record<
string,
string | Record<BuildEnvironment, string>
>;
export type ModuleConfigExportExports = ModuleConfigExportExport[];
export type ModuleConfigExportExport = string | ModuleConfigExportObject;
export type ModuleConfigExportObject = Record<
string,
ModuleConfigExportObjectValue
>;
export type ModuleConfigExportObjectValue =
| string
| Record<BuildEnvironment, string | boolean>;
export interface ParsedModuleConfig {
name: string;
root: string;
lib: boolean;
links: Record<string, ParsedModuleConfigLink>;
environments: {
client: ParsedModuleConfigEnvironment;
server: ParsedModuleConfigEnvironment;
};
}
export type ParsedModuleConfigExports = Record<
string,
ParsedModuleConfigExport
>;
export interface ParsedModuleConfigExport {
name: string;
file: string;
pkg: boolean;
}
export interface ParsedModuleConfigEnvironment {
imports: Record<string, string>;
exports: ParsedModuleConfigExports;
scopes: Record<string, Record<string, string>>;
}
export interface ParsedModuleConfigLink {
name: string;
root: string;
client: string;
clientManifestJson: string;
server: string;
serverManifestJson: string;
}
export function parseModuleConfig(
name: string,
root: string,
config: ModuleConfig = {}
): ParsedModuleConfig {
return {
name,
root,
lib: config.lib ?? false,
links: getLinks(name, root, config),
environments: {
client: getEnvironments(config, 'client', name),
server: getEnvironments(config, 'server', name)
}
};
}
export function getLinks(
name: string,
root: string,
config: ModuleConfig
): Record<string, ParsedModuleConfigLink> {
const result: Record<string, ParsedModuleConfigLink> = {};
Object.entries({
[name]: path.resolve(root, 'dist'),
...config.links
}).forEach(([name, value]) => {
const serverRoot = path.isAbsolute(value)
? value
: path.resolve(root, value);
result[name] = {
name: name,
root: value,
client: path.resolve(serverRoot, 'client'),
clientManifestJson: path.resolve(
serverRoot,
'client/manifest.json'
),
server: path.resolve(serverRoot, 'server'),
serverManifestJson: path.resolve(serverRoot, 'server/manifest.json')
};
});
return result;
}
export function getEnvironmentImports(
environment: BuildEnvironment,
imports: ModuleConfigImportMapping = {}
): Record<string, string> {
const result: Record<string, string> = {};
for (const [key, value] of Object.entries(imports)) {
if (typeof value === 'string') {
result[key] = value;
} else {
const environmentValue = value[environment];
if (environmentValue !== undefined) {
result[key] = environmentValue;
}
}
}
return result;
}
export function getEnvironmentScopes(
environment: BuildEnvironment,
scopes: Record<string, ModuleConfigImportMapping> = {}
): Record<string, Record<string, string>> {
const result: Record<string, Record<string, string>> = {};
for (const [scopeName, scopeImports] of Object.entries(scopes)) {
result[scopeName] = getEnvironmentImports(environment, scopeImports);
}
return result;
}
export function getEnvironments(
config: ModuleConfig,
env: BuildEnvironment,
moduleName: string
): ParsedModuleConfigEnvironment {
const imports = getEnvironmentImports(env, config.imports);
const exports = getEnvironmentExports(config, env);
const scopes = getEnvironmentScopes(env, {
...config.scopes,
'': {
...config.scopes?.[''],
...imports
}
});
addPackageExportsToScopes(exports, scopes, moduleName);
return {
imports,
exports,
scopes
};
}
export function createDefaultExports(
env: BuildEnvironment
): ParsedModuleConfigExports {
switch (env) {
case 'client':
return {
'src/entry.client': {
name: 'src/entry.client',
file: './src/entry.client',
pkg: false
},
'src/entry.server': {
name: 'src/entry.server',
file: '',
pkg: false
}
};
case 'server':
return {
'src/entry.client': {
name: 'src/entry.client',
file: '',
pkg: false
},
'src/entry.server': {
name: 'src/entry.server',
file: './src/entry.server',
pkg: false
}
};
}
}
export function processStringExport(
exportString: string
): ParsedModuleConfigExports {
const parsedValue = parsedExportValue(exportString);
return { [parsedValue.name]: parsedValue };
}
export function processObjectExport(
exportObject: ModuleConfigExportObject,
env: BuildEnvironment
): ParsedModuleConfigExports {
const exports: ParsedModuleConfigExports = {};
Object.keys(exportObject).forEach((name) => {
const config = exportObject[name];
if (typeof config === 'string') {
const parsedValue = parsedExportValue(config);
exports[name] = { ...parsedValue, name };
return;
}
const filePath = resolveExportFile(config, env, name);
const parsedValue = parsedExportValue(filePath);
exports[name] = { ...parsedValue, name };
});
return exports;
}
export function resolveExportFile(
config: ModuleConfigExportObjectValue,
env: BuildEnvironment,
name: string
): string {
if (typeof config === 'string') {
return config;
}
const value = config[env];
if (typeof value === 'boolean') {
return value === true ? name : '';
} else if (typeof value === 'string') {
return value || name;
}
return name;
}
export function processExportArray(
exportArray: ModuleConfigExportExports,
env: BuildEnvironment
): ParsedModuleConfigExports {
const exports: ParsedModuleConfigExports = {};
exportArray.forEach((item) => {
if (typeof item === 'string') {
const itemExports = processStringExport(item);
Object.assign(exports, itemExports);
} else {
const itemExports = processObjectExport(item, env);
Object.assign(exports, itemExports);
}
});
return exports;
}
export function getEnvironmentExports(
config: ModuleConfig,
env: BuildEnvironment
): ParsedModuleConfigExports {
const exports = config.lib ? {} : createDefaultExports(env);
if (config.exports) {
const userExports = processExportArray(config.exports, env);
Object.assign(exports, userExports);
}
return exports;
}
export function addPackageExportsToScopes(
exports: ParsedModuleConfigExports,
scopes: Record<string, Record<string, string>>,
moduleName: string
): Record<string, Record<string, string>> {
Object.entries(exports).forEach(([exportName, exportConfig]) => {
if (exportConfig.pkg) {
if (!scopes['']) {
scopes[''] = {};
}
scopes[''][exportName] = moduleName + '/' + exportName;
}
});
return scopes;
}
export function parsedExportValue(value: string): ParsedModuleConfigExport {
const FILE_EXT_REGEX =
/\.(js|mjs|cjs|jsx|mjsx|cjsx|ts|mts|cts|tsx|mtsx|ctsx)$/i;
if (value.startsWith('pkg:')) {
const item = value.substring('pkg:'.length);
return {
name: item,
pkg: true,
file: item
};
} else if (value.startsWith('root:')) {
const item = value.substring('root:'.length);
const name = item.replace(FILE_EXT_REGEX, '');
return {
name: name,
pkg: false,
file: './' + item
};
} else {
return {
name: value,
pkg: false,
file: value
};
}
}
|