@lwrjs/static 0.12.0-alpha.27 → 0.12.0-alpha.28

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.
@@ -31,7 +31,6 @@ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
31
  var import_path = __toModule(require("path"));
32
32
  var import_fs_extra = __toModule(require("fs-extra"));
33
33
  var import_static_utils = __toModule(require("../utils/static-utils.cjs"));
34
- var LWR_SSR_BUNDLE_PREFIX = "@lwrjs/lwc-ssr/";
35
34
  var StaticBundleProvider = class {
36
35
  constructor(_config, context) {
37
36
  this.name = "static-bundle-provider";
@@ -54,17 +53,10 @@ var StaticBundleProvider = class {
54
53
  const siteBundles = debug && this.debugSiteBundles ? this.debugSiteBundles : this.siteBundles;
55
54
  const metadata = await (0, import_static_utils.getLocalizedBundle)(specifier, siteBundles, localeId, this.i18n);
56
55
  if (!metadata) {
57
- if (!specifier.startsWith(LWR_SSR_BUNDLE_PREFIX)) {
58
- import_diagnostics.logger.debug({
59
- label: `${this.name}`,
60
- message: `did not find requested specifier ${specifier}`
61
- });
62
- }
63
56
  return void 0;
64
57
  }
65
58
  const bundlePath = import_path.default.join(this.siteRootDir, metadata.path);
66
- const isLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== void 0;
67
- const code = debug && isLambda ? 'throw new Error("Unexpected code reference");' : (await import_fs_extra.default.readFile(bundlePath)).toString("utf-8");
59
+ const code = await this.getCode(bundlePath, debug, specifier, localeId);
68
60
  const imports = metadata.imports.map((specifier2) => {
69
61
  const importModule = (0, import_shared_utils.explodeSpecifier)(specifier2);
70
62
  if (!importModule.version) {
@@ -105,5 +97,30 @@ var StaticBundleProvider = class {
105
97
  src: bundlePath
106
98
  };
107
99
  }
100
+ async getCode(bundlePath, debug, specifier, localeId) {
101
+ const isLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== void 0;
102
+ let bundleSourcePath = bundlePath;
103
+ try {
104
+ if (debug && isLambda) {
105
+ const siteBundles = this.siteBundles;
106
+ const metadata = await (0, import_static_utils.getLocalizedBundle)(specifier, siteBundles, localeId, this.i18n);
107
+ if (!metadata) {
108
+ import_diagnostics.logger.warn({
109
+ label: "static-bundle-provider",
110
+ message: `Unexpected code reference: ${specifier}`
111
+ });
112
+ return `throw new Error('Unexpected code reference: ${specifier}');`;
113
+ }
114
+ bundleSourcePath = import_path.default.join(this.siteRootDir, metadata.path);
115
+ }
116
+ return (await import_fs_extra.default.readFile(bundleSourcePath)).toString("utf-8");
117
+ } catch (err) {
118
+ import_diagnostics.logger.warn({
119
+ label: "static-bundle-provider",
120
+ message: `Unexpected code reference: ${specifier} ${bundleSourcePath}`
121
+ }, err);
122
+ return `throw new Error('Unexpected code reference: ${specifier} ${bundleSourcePath}');`;
123
+ }
124
+ }
108
125
  };
109
126
  var static_bundle_provider_default = StaticBundleProvider;
@@ -8,5 +8,15 @@ export default class StaticBundleProvider implements BundleProvider {
8
8
  i18n: import("@lwrjs/types").I18NConfig;
9
9
  constructor(_config: {}, context: ProviderContext);
10
10
  bundle<BundleIdentifier extends AbstractModuleId, RE extends RuntimeEnvironment>(moduleId: BundleIdentifier, runtimeEnvironment: RE, runtimeParams: RuntimeParams): Promise<BundleDefinition | undefined>;
11
+ /**
12
+ * Get the source code for the a static bundle
13
+ * 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
14
+ *
15
+ * @param bundlePath The default path for the bundle for prod read from .metadata/bundle-metadata.json, for debug .metadata/bundle-metadata-debug.json
16
+ * @param debug Is the request in debug mode?
17
+ * @param specifier Root specifier for the requested bundle
18
+ * @param localeId Locale id (e.g. en-US) for the current request
19
+ */
20
+ getCode(bundlePath: string, debug: boolean, specifier: string, localeId: string): Promise<string>;
11
21
  }
12
22
  //# sourceMappingURL=static-bundle-provider.d.ts.map
@@ -3,7 +3,6 @@ import { explodeSpecifier, getSpecifier } from '@lwrjs/shared-utils';
3
3
  import path from 'path';
4
4
  import fs from 'fs-extra';
5
5
  import { getLocalizedBundle, resolveStaticBundleVersion } from '../utils/static-utils.js';
6
- const LWR_SSR_BUNDLE_PREFIX = '@lwrjs/lwc-ssr/';
7
6
  export default class StaticBundleProvider {
8
7
  constructor(_config, context) {
9
8
  this.name = 'static-bundle-provider';
@@ -23,22 +22,12 @@ export default class StaticBundleProvider {
23
22
  const siteBundles = debug && this.debugSiteBundles ? this.debugSiteBundles : this.siteBundles;
24
23
  const metadata = await getLocalizedBundle(specifier, siteBundles, localeId, this.i18n);
25
24
  if (!metadata) {
26
- // ignore ssr module requests
27
- if (!specifier.startsWith(LWR_SSR_BUNDLE_PREFIX)) {
28
- logger.debug({
29
- label: `${this.name}`,
30
- message: `did not find requested specifier ${specifier}`,
31
- });
32
- }
33
25
  return undefined;
34
26
  }
35
- // Have to make the bundle code available for SSR
27
+ // Default bundle source path
36
28
  const bundlePath = path.join(this.siteRootDir, metadata.path);
37
- const isLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined;
38
- // debug bundles don't exist for SSR when deployed to MRT, so skip them
39
- const code = debug && isLambda
40
- ? 'throw new Error("Unexpected code reference");'
41
- : (await fs.readFile(bundlePath)).toString('utf-8');
29
+ // Get the associated bundle source code
30
+ const code = await this.getCode(bundlePath, debug, specifier, localeId);
42
31
  const imports = metadata.imports.map((specifier) => {
43
32
  const importModule = explodeSpecifier(specifier);
44
33
  if (!importModule.version) {
@@ -83,5 +72,50 @@ export default class StaticBundleProvider {
83
72
  src: bundlePath,
84
73
  };
85
74
  }
75
+ /**
76
+ * Get the source code for the a static bundle
77
+ * 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
78
+ *
79
+ * @param bundlePath The default path for the bundle for prod read from .metadata/bundle-metadata.json, for debug .metadata/bundle-metadata-debug.json
80
+ * @param debug Is the request in debug mode?
81
+ * @param specifier Root specifier for the requested bundle
82
+ * @param localeId Locale id (e.g. en-US) for the current request
83
+ */
84
+ async getCode(bundlePath, debug, specifier, localeId) {
85
+ // Flag is used to indicate that we are running on a lambda
86
+ const isLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined;
87
+ // Default source code path determined from metadata based on debug mode
88
+ let bundleSourcePath = bundlePath;
89
+ try {
90
+ // This is the special case where the request is in debug mode and we are on the lambda
91
+ // So we will look up the prod source code instead of the debug source code
92
+ if (debug && isLambda) {
93
+ const siteBundles = this.siteBundles;
94
+ const metadata = await getLocalizedBundle(specifier, siteBundles, localeId, this.i18n);
95
+ if (!metadata) {
96
+ // We did not find the bundle prod bundle even though we did find it in the debug metadata before
97
+ logger.warn({
98
+ label: 'static-bundle-provider',
99
+ message: `Unexpected code reference: ${specifier}`,
100
+ });
101
+ // Returning source code that throws and error is someone tries to evaluate it
102
+ return `throw new Error('Unexpected code reference: ${specifier}');`;
103
+ }
104
+ // Overwrite the default source code path the prod source code path
105
+ bundleSourcePath = path.join(this.siteRootDir, metadata.path);
106
+ }
107
+ // Read the bundle source code.
108
+ return (await fs.readFile(bundleSourcePath)).toString('utf-8');
109
+ }
110
+ catch (err) {
111
+ // Ran it an un-expected error reading the bundle source code
112
+ logger.warn({
113
+ label: 'static-bundle-provider',
114
+ message: `Unexpected code reference: ${specifier} ${bundleSourcePath}`,
115
+ }, err);
116
+ // Returning source code that throws and error is someone tries to evaluate it
117
+ return `throw new Error('Unexpected code reference: ${specifier} ${bundleSourcePath}');`;
118
+ }
119
+ }
86
120
  }
87
121
  //# sourceMappingURL=static-bundle-provider.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.12.0-alpha.27",
7
+ "version": "0.12.0-alpha.28",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -48,11 +48,11 @@
48
48
  "build/**/*.d.ts"
49
49
  ],
50
50
  "dependencies": {
51
- "@lwrjs/diagnostics": "0.12.0-alpha.27",
52
- "@lwrjs/shared-utils": "0.12.0-alpha.27"
51
+ "@lwrjs/diagnostics": "0.12.0-alpha.28",
52
+ "@lwrjs/shared-utils": "0.12.0-alpha.28"
53
53
  },
54
54
  "devDependencies": {
55
- "@lwrjs/types": "0.12.0-alpha.27",
55
+ "@lwrjs/types": "0.12.0-alpha.28",
56
56
  "@types/express": "^4.17.21",
57
57
  "jest": "^26.6.3",
58
58
  "jest-express": "^1.12.0",
@@ -65,5 +65,5 @@
65
65
  "volta": {
66
66
  "extends": "../../../package.json"
67
67
  },
68
- "gitHead": "84406699a78e2a81882df74a50467e5376a00a90"
68
+ "gitHead": "5228219171bb2d28a4c7b32f1f6238e396ac3cbd"
69
69
  }