@lwrjs/static 0.17.2-alpha.9 → 0.17.3

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.
@@ -60,26 +60,45 @@ var StaticBundleProvider = class {
60
60
  }
61
61
  }
62
62
  async bundle(moduleId, runtimeEnvironment, runtimeParams) {
63
- const {specifier, name, namespace, version} = moduleId;
63
+ const {specifier, version} = moduleId;
64
64
  const {
65
- debug,
66
65
  i18n: {defaultLocale}
67
66
  } = runtimeEnvironment;
68
67
  const localeId = runtimeParams?.locale || defaultLocale;
69
68
  const ssr = runtimeParams?.ssr;
70
- const metadata = this.getBundleMetadata({moduleId, localeId, debug, ssr});
69
+ let debug = runtimeEnvironment.debug;
70
+ let metadata = this.getBundleMetadata({moduleId, localeId, debug, ssr});
71
+ if (!metadata && debug) {
72
+ metadata = this.getBundleMetadata({moduleId, localeId, debug: false, ssr});
73
+ if (metadata)
74
+ debug = false;
75
+ }
76
+ if (!metadata && (0, import_shared_utils.isExternalSpecifier)(moduleId.specifier, this.bundleConfig)) {
77
+ const {specifier: unversionedSepcifier} = (0, import_shared_utils.explodeSpecifier)(moduleId.specifier);
78
+ metadata = {
79
+ path: this.bundleConfig?.external?.[unversionedSepcifier],
80
+ imports: [],
81
+ dynamicImports: [],
82
+ version: moduleId.version
83
+ };
84
+ return this.createBundleDefinition(moduleId, metadata, localeId, debug, ssr, async () => "", (0, import_shared_utils.normalizeFromFileURL)(metadata.path, this.siteRootDir) ?? metadata.path);
85
+ }
71
86
  if (!metadata) {
87
+ this.logBundleError(moduleId, localeId, debug, ssr);
72
88
  return void 0;
73
89
  }
74
90
  const bundlePath = import_path.default.join(this.siteRootDir, metadata.path);
75
- const resolvedBundlePath = this.getCodePath(bundlePath, debug, specifier, version, localeId, ssr);
76
- const codePromiser = this.getCodePromiser(resolvedBundlePath, {
91
+ const codePromiser = this.getCodePromiser(bundlePath, {
77
92
  specifier,
78
93
  version,
79
94
  locale: localeId,
80
95
  ssr,
81
96
  debug
82
97
  });
98
+ return this.createBundleDefinition(moduleId, metadata, localeId, debug, ssr, codePromiser, bundlePath);
99
+ }
100
+ async createBundleDefinition(moduleId, metadata, localeId, debug, ssr, codePromiser, bundlePath) {
101
+ const {specifier, name, namespace, version} = moduleId;
83
102
  const imports = metadata.imports.map((importSpecifier) => this.getModuleReference(importSpecifier, localeId, debug, false));
84
103
  const dynamicImports = metadata.dynamicImports?.map((importSpecifier) => this.getModuleReference(importSpecifier, localeId, debug, false));
85
104
  const id = (0, import_shared_utils.getSpecifier)(moduleId);
@@ -179,26 +198,29 @@ var StaticBundleProvider = class {
179
198
  return code;
180
199
  };
181
200
  }
182
- getCodePath(bundlePath, debug, specifier, version, localeId, ssr) {
183
- const isLambda = (0, import_shared_utils.isLambdaEnv)();
184
- let bundleSourcePath = bundlePath;
185
- if (debug && isLambda) {
186
- const metadata = this.getBundleMetadata({
187
- moduleId: {specifier, version},
201
+ logBundleError(moduleId, localeId, debug, ssr) {
202
+ const moduleSpecifier = moduleId.namespace ? `${moduleId.namespace}/${moduleId.name}` : moduleId.name;
203
+ const siteBundleId = (0, import_site_metadata.getSiteBundleId)(moduleId, localeId, ssr, this.i18n);
204
+ const siteBundles = this.siteMetadata.getSiteBundles()?.bundles || {};
205
+ const siteDebugBundles = this.siteMetadata.getDebugSiteBundles()?.bundles || {};
206
+ const runtimeBundles = Object.keys(siteBundles).filter((key) => key.startsWith(moduleSpecifier));
207
+ const debugBundles = Object.keys(siteDebugBundles).filter((key) => key.startsWith(moduleSpecifier));
208
+ import_diagnostics.logger.error({
209
+ message: JSON.stringify({
210
+ message: "Failed to find static bundle",
211
+ moduleId,
188
212
  localeId,
189
- debug: false,
190
- ssr
191
- });
192
- if (!metadata) {
193
- import_diagnostics.logger.error({
194
- label: "static-bundle-provider",
195
- message: `Unexpected code reference: ${specifier}`
196
- });
197
- return BUNDLE_SOURCE_NOT_FOUND;
198
- }
199
- bundleSourcePath = import_path.default.join(this.siteRootDir, metadata.path);
200
- }
201
- return bundleSourcePath;
213
+ debug,
214
+ ssr,
215
+ moduleSpecifier,
216
+ siteBundleId,
217
+ debugBundles,
218
+ runtimeBundles,
219
+ totalBundlesCount: Object.keys(siteBundles).length,
220
+ totalDebugBundlesCount: Object.keys(siteDebugBundles).length
221
+ }),
222
+ label: "static-bundle-provider"
223
+ });
202
224
  }
203
225
  };
204
226
  var static_bundle_provider_default = StaticBundleProvider;
@@ -43,7 +43,7 @@ function mrtStaticUriTransformer() {
43
43
  };
44
44
  }
45
45
  let basePathlessUri = uriDef.uri;
46
- if (runtimeEnvironment?.basePath) {
46
+ if (runtimeEnvironment?.basePath && uriDef.uri.startsWith(runtimeEnvironment?.basePath)) {
47
47
  basePathlessUri = uriDef.uri.replace(runtimeEnvironment?.basePath, "");
48
48
  }
49
49
  const uri = (0, import_shared_utils.getMrtArtifactUrl)(runtimeEnvironment?.basePath || "", basePathlessUri);
@@ -3,8 +3,13 @@ import { hashContent, mimeLookup, normalizeResourcePath } from '@lwrjs/shared-ut
3
3
  import path from 'path';
4
4
  import fs from 'fs-extra';
5
5
  export default class StaticAssetProvider {
6
+ name = 'static-asset-provider';
7
+ siteAssets;
8
+ resourcePaths;
9
+ siteRootDir;
10
+ basePath;
11
+ assetsOnLambda;
6
12
  constructor(_config, context) {
7
- this.name = 'static-asset-provider';
8
13
  if (!context.siteMetadata) {
9
14
  throw new Error(`[${this.name}] Site metadata was not found`);
10
15
  }
@@ -12,6 +12,7 @@ export default class StaticBundleProvider implements BundleProvider {
12
12
  bundleCacheSize?: number;
13
13
  }, context: ProviderContext);
14
14
  bundle<BundleIdentifier extends AbstractModuleId, RE extends RuntimeEnvironment>(moduleId: BundleIdentifier, runtimeEnvironment: RE, runtimeParams: RuntimeParams): Promise<BundleDefinition | undefined>;
15
+ private createBundleDefinition;
15
16
  getBundleMetadata({ moduleId, localeId, debug, ssr, }: {
16
17
  moduleId: Partial<AbstractModuleId>;
17
18
  localeId: string;
@@ -30,16 +31,17 @@ export default class StaticBundleProvider implements BundleProvider {
30
31
  debug: boolean;
31
32
  }): () => Promise<string>;
32
33
  /**
33
- * Get the local source code path for the a static bundle
34
- * If we are running in a lambda and the mode is debug we will return the prod source code instead of the debug source code
34
+ * Logs an error when a bundle fails to load and provides debugging metadata.
35
35
  *
36
- * @param bundlePath The default path for the bundle for prod read from .metadata/bundle-metadata.json, for debug .metadata/bundle-metadata-debug.json
37
- * @param debug Is the request in debug mode?
38
- * @param specifier Root specifier for the requested bundle
39
- * @param version Root specifier version
40
- * @param localeId Locale id (e.g. en-US) for the current request
41
- * @param ssr True if this is a server bundle
36
+ * This function logs details about the failed bundle load attempt, including the module ID,
37
+ * locale, debug mode, and SSR status. It also attempts to retrieve additional metadata
38
+ * about the site bundles and debug bundles associated with the module.
39
+ *
40
+ * @param {Partial<AbstractModuleId>} moduleId - The module identifier, which may include a namespace and name.
41
+ * @param {string} localeId - The locale associated with the bundle.
42
+ * @param {boolean} debug - Indicates whether debug mode is enabled.
43
+ * @param {boolean} ssr - Indicates whether the bundle is for server-side rendering (SSR).
42
44
  */
43
- getCodePath(bundlePath: string, debug: boolean, specifier: string, version: string | undefined, localeId: string, ssr: boolean): string;
45
+ logBundleError(moduleId: Partial<AbstractModuleId>, localeId: string, debug: boolean, ssr: boolean): void;
44
46
  }
45
47
  //# sourceMappingURL=static-bundle-provider.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@lwrjs/diagnostics';
2
- import { VERSION_SIGIL, explodeSpecifier, getSpecifier, isLambdaEnv } from '@lwrjs/shared-utils';
2
+ import { VERSION_SIGIL, explodeSpecifier, getSpecifier, isExternalSpecifier, isLambdaEnv, normalizeFromFileURL, } from '@lwrjs/shared-utils';
3
3
  import path from 'path';
4
4
  import fs from 'fs-extra';
5
5
  import { LRUCache } from 'lru-cache';
@@ -8,8 +8,14 @@ import { cacheCountStore } from '@lwrjs/instrumentation';
8
8
  import { getTracer, BundleSpan } from '@lwrjs/instrumentation';
9
9
  const BUNDLE_SOURCE_NOT_FOUND = 'Bundle Source Path Not Found';
10
10
  export default class StaticBundleProvider {
11
+ name = 'static-bundle-provider';
12
+ codeCache;
13
+ siteRootDir;
14
+ bundleConfig;
15
+ i18n;
16
+ siteMetadata;
17
+ bundleCacheSize;
11
18
  constructor(config, context) {
12
- this.name = 'static-bundle-provider';
13
19
  if (!context.siteMetadata) {
14
20
  throw new Error(`[${this.name}] Site metadata was not found`);
15
21
  }
@@ -34,25 +40,48 @@ export default class StaticBundleProvider {
34
40
  }
35
41
  }
36
42
  async bundle(moduleId, runtimeEnvironment, runtimeParams) {
37
- const { specifier, name, namespace, version } = moduleId;
38
- const { debug, i18n: { defaultLocale }, } = runtimeEnvironment;
43
+ const { specifier, version } = moduleId;
44
+ const { i18n: { defaultLocale }, } = runtimeEnvironment;
39
45
  const localeId = (runtimeParams?.locale || defaultLocale);
40
46
  const ssr = runtimeParams?.ssr;
41
- const metadata = this.getBundleMetadata({ moduleId, localeId, debug, ssr });
47
+ let debug = runtimeEnvironment.debug;
48
+ let metadata = this.getBundleMetadata({ moduleId, localeId, debug, ssr });
49
+ if (!metadata && debug) {
50
+ // Fallback to use prod bundles if the debug variant does not exist
51
+ // eg: debug bundles are not shipped to the Lambda
52
+ // eg: locally, an MRT bundle may or may not have debug metadata, depending on how it's generated
53
+ metadata = this.getBundleMetadata({ moduleId, localeId, debug: false, ssr });
54
+ if (metadata)
55
+ debug = false;
56
+ }
57
+ if (!metadata && isExternalSpecifier(moduleId.specifier, this.bundleConfig)) {
58
+ const { specifier: unversionedSepcifier } = explodeSpecifier(moduleId.specifier);
59
+ metadata = {
60
+ path: this.bundleConfig?.external?.[unversionedSepcifier],
61
+ imports: [],
62
+ dynamicImports: [],
63
+ version: moduleId.version,
64
+ };
65
+ return this.createBundleDefinition(moduleId, metadata, localeId, debug, ssr, async () => '', normalizeFromFileURL(metadata.path, this.siteRootDir) ?? metadata.path);
66
+ }
42
67
  if (!metadata) {
68
+ this.logBundleError(moduleId, localeId, debug, ssr);
43
69
  return undefined;
44
70
  }
45
71
  // Default bundle source path
46
72
  const bundlePath = path.join(this.siteRootDir, metadata.path);
47
73
  // Get the associated bundle source code
48
- const resolvedBundlePath = this.getCodePath(bundlePath, debug, specifier, version, localeId, ssr);
49
- const codePromiser = this.getCodePromiser(resolvedBundlePath, {
74
+ const codePromiser = this.getCodePromiser(bundlePath, {
50
75
  specifier,
51
76
  version,
52
77
  locale: localeId,
53
78
  ssr,
54
79
  debug,
55
80
  });
81
+ return this.createBundleDefinition(moduleId, metadata, localeId, debug, ssr, codePromiser, bundlePath);
82
+ }
83
+ async createBundleDefinition(moduleId, metadata, localeId, debug, ssr, codePromiser, bundlePath) {
84
+ const { specifier, name, namespace, version } = moduleId;
56
85
  const imports = metadata.imports.map((importSpecifier) => this.getModuleReference(importSpecifier, localeId, debug, false));
57
86
  const dynamicImports = metadata.dynamicImports?.map((importSpecifier) => this.getModuleReference(importSpecifier, localeId, debug, false));
58
87
  const id = getSpecifier(moduleId);
@@ -155,42 +184,44 @@ export default class StaticBundleProvider {
155
184
  };
156
185
  }
157
186
  /**
158
- * Get the local source code path for the a static bundle
159
- * If we are running in a lambda and the mode is debug we will return the prod source code instead of the debug source code
187
+ * Logs an error when a bundle fails to load and provides debugging metadata.
188
+ *
189
+ * This function logs details about the failed bundle load attempt, including the module ID,
190
+ * locale, debug mode, and SSR status. It also attempts to retrieve additional metadata
191
+ * about the site bundles and debug bundles associated with the module.
160
192
  *
161
- * @param bundlePath The default path for the bundle for prod read from .metadata/bundle-metadata.json, for debug .metadata/bundle-metadata-debug.json
162
- * @param debug Is the request in debug mode?
163
- * @param specifier Root specifier for the requested bundle
164
- * @param version Root specifier version
165
- * @param localeId Locale id (e.g. en-US) for the current request
166
- * @param ssr True if this is a server bundle
193
+ * @param {Partial<AbstractModuleId>} moduleId - The module identifier, which may include a namespace and name.
194
+ * @param {string} localeId - The locale associated with the bundle.
195
+ * @param {boolean} debug - Indicates whether debug mode is enabled.
196
+ * @param {boolean} ssr - Indicates whether the bundle is for server-side rendering (SSR).
167
197
  */
168
- getCodePath(bundlePath, debug, specifier, version, localeId, ssr) {
169
- // Flag is used to indicate that we are running on a lambda
170
- const isLambda = isLambdaEnv();
171
- // Default source code path determined from metadata based on debug mode
172
- let bundleSourcePath = bundlePath;
173
- // This is the special case where the request is in debug mode and we are on the lambda
174
- // So we will look up the prod source code instead of the debug source code
175
- if (debug && isLambda) {
176
- const metadata = this.getBundleMetadata({
177
- moduleId: { specifier, version },
198
+ logBundleError(moduleId, localeId, debug, ssr) {
199
+ const moduleSpecifier = moduleId.namespace
200
+ ? `${moduleId.namespace}/${moduleId.name}`
201
+ : moduleId.name;
202
+ const siteBundleId = getSiteBundleId(moduleId, localeId, ssr, this.i18n);
203
+ // Retrieve site bundles and debug bundles
204
+ const siteBundles = this.siteMetadata.getSiteBundles()?.bundles || {};
205
+ const siteDebugBundles = this.siteMetadata.getDebugSiteBundles()?.bundles || {};
206
+ // Extract keys and filter bundles by module specifier
207
+ const runtimeBundles = Object.keys(siteBundles).filter((key) => key.startsWith(moduleSpecifier));
208
+ const debugBundles = Object.keys(siteDebugBundles).filter((key) => key.startsWith(moduleSpecifier));
209
+ logger.error({
210
+ message: JSON.stringify({
211
+ message: 'Failed to find static bundle',
212
+ moduleId,
178
213
  localeId,
179
- debug: false,
214
+ debug,
180
215
  ssr,
181
- });
182
- if (!metadata) {
183
- // We did not find the bundle prod bundle even though we did find it in the debug metadata before
184
- logger.error({
185
- label: 'static-bundle-provider',
186
- message: `Unexpected code reference: ${specifier}`,
187
- });
188
- return BUNDLE_SOURCE_NOT_FOUND;
189
- }
190
- // Overwrite the default source code path the prod source code path
191
- bundleSourcePath = path.join(this.siteRootDir, metadata.path);
192
- }
193
- return bundleSourcePath;
216
+ moduleSpecifier,
217
+ siteBundleId,
218
+ debugBundles,
219
+ runtimeBundles,
220
+ totalBundlesCount: Object.keys(siteBundles).length,
221
+ totalDebugBundlesCount: Object.keys(siteDebugBundles).length,
222
+ }),
223
+ label: 'static-bundle-provider',
224
+ });
194
225
  }
195
226
  }
196
227
  //# sourceMappingURL=static-bundle-provider.js.map
@@ -3,8 +3,13 @@ import { VERSION_SIGIL, explodeSpecifier, getSpecifier, isLambdaEnv } from '@lwr
3
3
  import path from 'path';
4
4
  import { getSiteBundleId, parseSiteId, resolveStaticBundleVersion } from '../site-metadata.js';
5
5
  export default class StaticModuleProvider {
6
+ name = 'static-module-provider';
7
+ siteRootDir;
8
+ externals;
9
+ fingerprintIndex;
10
+ i18n;
11
+ siteMetadata;
6
12
  constructor(_config, context) {
7
- this.name = 'static-module-provider';
8
13
  if (!context.siteMetadata) {
9
14
  throw new Error(`[${this.name}] Site metadata was not found`);
10
15
  }
@@ -4,8 +4,11 @@ import path from 'path';
4
4
  import fs from 'fs-extra';
5
5
  import { getSiteResourceId } from '../site-metadata.js';
6
6
  export default class StaticResourceProvider {
7
+ name = 'static-resource-provider';
8
+ siteRootDir;
9
+ siteMetadata;
10
+ resourceRegistry;
7
11
  constructor(_config, context) {
8
- this.name = 'static-resource-provider';
9
12
  if (!context.siteMetadata) {
10
13
  throw new Error(`[${this.name}] Site metadata was not found`);
11
14
  }
@@ -13,6 +13,14 @@ export const SITE_VERSION_PREFIX = `|${VERSION_SIGIL}/`;
13
13
  export const SITE_LOCALE_PREFIX = `|${LOCALE_SIGIL}/`;
14
14
  export const SITE_SSR_PREFIX = `|${SSR_SIGIL}`;
15
15
  export class SiteMetadataImpl {
16
+ options;
17
+ siteBundles;
18
+ debugSiteBundles;
19
+ siteResources;
20
+ debugSiteResources;
21
+ siteAssets;
22
+ bundleDecisionTree;
23
+ resourceDecisionTree;
16
24
  constructor(options) {
17
25
  this.options = options;
18
26
  this.siteBundles = this.readStaticBundleMetadata(options.rootDir, STATIC_BUNDLE_METADATA_PATH);
@@ -20,7 +20,7 @@ export default function mrtStaticUriTransformer() {
20
20
  }
21
21
  // Remove the basePath from the uri
22
22
  let basePathlessUri = uriDef.uri;
23
- if (runtimeEnvironment?.basePath) {
23
+ if (runtimeEnvironment?.basePath && uriDef.uri.startsWith(runtimeEnvironment?.basePath)) {
24
24
  basePathlessUri = uriDef.uri.replace(runtimeEnvironment?.basePath, '');
25
25
  }
26
26
  const uri = getMrtArtifactUrl(runtimeEnvironment?.basePath || '', basePathlessUri);
@@ -33,9 +33,7 @@ const CHOICE_PROD = 'prod';
33
33
  const CHOICE_DEBUG = 'debug';
34
34
  // Tree of decisions to lead you to the right artifact
35
35
  export default class DecisionTreeImpl {
36
- constructor() {
37
- this.root = new TreeNode(); // Root node does not hold any decision value
38
- }
36
+ root = new TreeNode(); // Root node does not hold any decision value
39
37
  // Insert an artifact into the tree based on a path of decisions
40
38
  insert(siteArtifactId, artifact, debug, localeFallbacks) {
41
39
  // The decision path is the set of choices needed to get to the right metadata
@@ -179,9 +177,12 @@ export default class DecisionTreeImpl {
179
177
  * It maintains the rank of all choices at this node, allowing a later node with a higher rank to replace the current one as the best option.
180
178
  */
181
179
  class TreeNode {
180
+ decisionValue; // The decision value at this node (version ['1_0' or WILDCARD etc], localeId ['en-MX' or WILDCARD etc], etc.)
181
+ children = new Map(); // Maps a decision key to the next TreeNode
182
+ artifact = undefined; // Final artifact at a leaf node
183
+ parentPath; // parent path (i.e. foo/bar|DEBUG|1_0|WILD_CARD) just used for debugging
184
+ rank; // choice rank to prioritize lower ranked choices
182
185
  constructor(value = '', parentPath = '') {
183
- this.children = new Map(); // Maps a decision key to the next TreeNode
184
- this.artifact = undefined; // Final artifact at a leaf node
185
186
  this.decisionValue = value;
186
187
  this.parentPath = parentPath;
187
188
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.17.2-alpha.9",
7
+ "version": "0.17.3",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -56,25 +56,25 @@
56
56
  "build/**/*.d.ts"
57
57
  ],
58
58
  "dependencies": {
59
- "@lwrjs/diagnostics": "0.17.2-alpha.9",
60
- "@lwrjs/instrumentation": "0.17.2-alpha.9",
61
- "@lwrjs/shared-utils": "0.17.2-alpha.9",
59
+ "@lwrjs/diagnostics": "0.17.3",
60
+ "@lwrjs/instrumentation": "0.17.3",
61
+ "@lwrjs/shared-utils": "0.17.3",
62
62
  "lru-cache": "^10.4.3"
63
63
  },
64
64
  "devDependencies": {
65
- "@lwrjs/types": "0.17.2-alpha.9",
65
+ "@lwrjs/types": "0.17.3",
66
66
  "@types/express": "^4.17.21",
67
- "jest": "^26.6.3",
67
+ "jest": "29.7.0",
68
68
  "jest-express": "^1.12.0",
69
69
  "memfs": "^4.13.0",
70
70
  "mock-res": "^0.6.0",
71
- "ts-jest": "^26.5.6"
71
+ "ts-jest": "^29.2.6"
72
72
  },
73
73
  "engines": {
74
- "node": ">=18.0.0"
74
+ "node": ">=20.0.0"
75
75
  },
76
76
  "volta": {
77
77
  "extends": "../../../package.json"
78
78
  },
79
- "gitHead": "7ede5d04a97513b9869b13e66155bc4f3920bb99"
79
+ "gitHead": "9adba8b8dc0e24690ac99106eae138db2f429834"
80
80
  }