@lwrjs/core 0.12.0-alpha.20 → 0.12.0-alpha.22

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.
@@ -51,10 +51,10 @@ function requestProcessorMiddleware(app, context) {
51
51
  requestClass = req.headers[MRT_REQUEST_CLASS_KEY];
52
52
  requestDepth = (0, import_shared_utils.parseRequestDepthHeader)(req.headers);
53
53
  }
54
- if (requestDepth && requestDepth > 1) {
55
- import_diagnostics.logger.warn({
54
+ if (requestDepth && requestDepth > 0) {
55
+ import_diagnostics.logger.error({
56
56
  label: "request-processor-middleware",
57
- message: "Lambda SSR request cycle detected"
57
+ message: `Lambda SSR request cycle detected: ${req.originalUrl}`
58
58
  });
59
59
  return res.status(400).send("Request depth limit reached");
60
60
  }
@@ -36,6 +36,7 @@ var import_fs_extra = __toModule(require("fs-extra"));
36
36
  var import_stream = __toModule(require("./utils/stream.cjs"));
37
37
  var import_dir = __toModule(require("./utils/dir.cjs"));
38
38
  var import_config = __toModule(require("@lwrjs/config"));
39
+ var import_url = __toModule(require("url"));
39
40
  var SiteGenerator = class {
40
41
  async buildStaticApplication(config, dispatcher) {
41
42
  const startTime = import_perf_hooks.performance.now();
@@ -161,6 +162,14 @@ var SiteGenerator = class {
161
162
  }
162
163
  async handleJavascriptResource(url, context, siteConfig, dispatcher) {
163
164
  const {outputDir} = siteConfig;
165
+ const moduleDefinition = context.fs?.metadata?.moduleDefinition;
166
+ const externals = moduleDefinition?.config?.external || {};
167
+ const siteBundles = siteConfig?.siteMetadata?.getSiteBundles()?.bundles;
168
+ const specifier = moduleDefinition?.specifier;
169
+ if (externals && siteBundles && externals[specifier]) {
170
+ this.handleExternalBundle(specifier, siteBundles, externals, outputDir);
171
+ return;
172
+ }
164
173
  const normalizedUrl = decodeURIComponent(url);
165
174
  (0, import_dir.createResourceDir)((0, import_path.dirname)(normalizedUrl), outputDir);
166
175
  const ext = (0, import_path.extname)(normalizedUrl);
@@ -173,17 +182,26 @@ var SiteGenerator = class {
173
182
  siteConfig.urlRewriteMap.set(normalizedUrl.substring(0, normalizedUrl.indexOf("/v/")), normalizedUrl);
174
183
  siteConfig.urlRewriteMap.set(url.substring(0, url.indexOf("%2Fv%2F")), normalizedUrl);
175
184
  }
176
- const moduleDefinition = context.fs?.metadata?.moduleDefinition;
177
185
  if (moduleDefinition) {
178
186
  const imports = moduleDefinition.linkedModuleRecord?.imports || moduleDefinition.bundleRecord?.imports || [];
179
187
  for (const importModule of imports) {
180
- const jsUri = importModule.specifier.startsWith("/") ? importModule.specifier : (0, import_shared_utils.getSpecifier)(importModule);
181
- dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
188
+ const specifier2 = importModule.specifier;
189
+ if (externals[specifier2]) {
190
+ this.handleExternalBundle(specifier2, siteBundles, externals, outputDir);
191
+ } else {
192
+ const jsUri = specifier2.startsWith("/") ? specifier2 : (0, import_shared_utils.getSpecifier)(importModule);
193
+ dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
194
+ }
182
195
  }
183
196
  const dynamicImports = moduleDefinition.linkedModuleRecord?.dynamicImports || moduleDefinition.bundleRecord?.dynamicImports || [];
184
197
  for (const importModule of dynamicImports) {
185
- const jsUri = importModule.specifier.startsWith("/") ? importModule.specifier : (0, import_shared_utils.getSpecifier)(importModule);
186
- dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
198
+ const specifier2 = importModule.specifier;
199
+ if (externals[specifier2]) {
200
+ this.handleExternalBundle(specifier2, siteBundles, externals, outputDir);
201
+ } else {
202
+ const jsUri = specifier2.startsWith("/") ? specifier2 : (0, import_shared_utils.getSpecifier)(importModule);
203
+ dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
204
+ }
187
205
  }
188
206
  if (moduleDefinition.bundleRecord) {
189
207
  this.addBundleToSiteMetadata(moduleDefinition, url, siteConfig);
@@ -195,14 +213,31 @@ var SiteGenerator = class {
195
213
  }
196
214
  await Promise.all(dispatchRequests);
197
215
  }
216
+ handleExternalBundle(specifier, siteBundles, externals, outputDir) {
217
+ if (siteBundles && externals[specifier]?.startsWith(import_shared_utils.PROTOCOL_FILE) && !siteBundles[specifier]) {
218
+ const path = (0, import_shared_utils.normalizeFromFileURL)(externals[specifier], process.cwd());
219
+ const normalizedPath = decodeURIComponent(path);
220
+ (0, import_dir.createResourceDir)((0, import_path.dirname)(normalizedPath), outputDir);
221
+ const ext = (0, import_path.extname)(normalizedPath);
222
+ const fullPath = (0, import_path.join)(outputDir, `${normalizedPath}${ext ? "" : ".js"}`);
223
+ import_fs_extra.default.copyFileSync((0, import_url.fileURLToPath)(externals[specifier]), fullPath);
224
+ const bundleMetadata = {
225
+ specifier,
226
+ path: normalizedPath,
227
+ imports: []
228
+ };
229
+ siteBundles[specifier] = bundleMetadata;
230
+ }
231
+ }
198
232
  addBundleToSiteMetadata(bundleDefinition, url, siteConfig) {
199
233
  if (siteConfig.siteMetadata) {
200
234
  const locale = siteConfig.locale;
201
235
  const specifier = siteConfig.i18n.defaultLocale === locale ? bundleDefinition.specifier : `${bundleDefinition.specifier}|l/${locale}`;
202
236
  const imports = bundleDefinition.bundleRecord.imports?.map((moduleRef) => (0, import_shared_utils.getSpecifier)(moduleRef)) || [];
203
237
  const dynamicImports = bundleDefinition.bundleRecord.dynamicImports?.map((moduleRef) => (0, import_shared_utils.getSpecifier)(moduleRef));
238
+ const version = bundleDefinition.version === import_shared_utils.VERSION_NOT_PROVIDED ? void 0 : bundleDefinition.version;
204
239
  const bundleMetadata = {
205
- version: bundleDefinition.version,
240
+ version,
206
241
  path: decodeURIComponent(url),
207
242
  includedModules: bundleDefinition.bundleRecord.includedModules || [],
208
243
  imports,
@@ -34,10 +34,10 @@ export function requestProcessorMiddleware(app, context) {
34
34
  requestDepth = parseRequestDepthHeader(req.headers);
35
35
  }
36
36
  // TODO Clean up once we have a long term solution
37
- if (requestDepth && requestDepth > 1) {
38
- logger.warn({
37
+ if (requestDepth && requestDepth > 0) {
38
+ logger.error({
39
39
  label: 'request-processor-middleware',
40
- message: 'Lambda SSR request cycle detected',
40
+ message: `Lambda SSR request cycle detected: ${req.originalUrl}`,
41
41
  });
42
42
  // Return 400 Too Many Requests status
43
43
  return res.status(400).send('Request depth limit reached');
@@ -40,6 +40,10 @@ export default class SiteGenerator {
40
40
  * @param dispatcher - Network dispatcher
41
41
  */
42
42
  private handleJavascriptResource;
43
+ /**
44
+ * If this is a file based external copy it to the site folder and add it to the bundle metadata
45
+ */
46
+ private handleExternalBundle;
43
47
  private addBundleToSiteMetadata;
44
48
  private addResourceToSiteMetadata;
45
49
  private addAssetToSiteMetadata;
@@ -1,12 +1,13 @@
1
1
  import { performance } from 'perf_hooks';
2
2
  import { logger } from '@lwrjs/diagnostics';
3
- import { getSpecifier, getFeatureFlags, hashContent, isSelfUrl, getModuleUriPrefix, getMappingUriPrefix, isExternalUrl, mimeLookup, getViewUri, sortLocalesByFallback, } from '@lwrjs/shared-utils';
3
+ import { getSpecifier, getFeatureFlags, hashContent, isSelfUrl, getModuleUriPrefix, getMappingUriPrefix, isExternalUrl, mimeLookup, getViewUri, sortLocalesByFallback, VERSION_NOT_PROVIDED, PROTOCOL_FILE, normalizeFromFileURL, } from '@lwrjs/shared-utils';
4
4
  import { SiteMetadataImpl } from '@lwrjs/static/site-metadata';
5
5
  import { join, dirname, extname, normalize } from 'path';
6
6
  import fs from 'fs-extra';
7
7
  import { writeResponse } from './utils/stream.js';
8
8
  import { createResourceDir } from './utils/dir.js';
9
9
  import { getRuntimeEnvironment } from '@lwrjs/config';
10
+ import { fileURLToPath } from 'url';
10
11
  export default class SiteGenerator {
11
12
  /**
12
13
  * Build a static site in the configured directory
@@ -198,6 +199,14 @@ export default class SiteGenerator {
198
199
  */
199
200
  async handleJavascriptResource(url, context, siteConfig, dispatcher) {
200
201
  const { outputDir } = siteConfig;
202
+ const moduleDefinition = context.fs?.metadata?.moduleDefinition; // LinkedModuleDefinition | BundleDefinition
203
+ const externals = moduleDefinition?.config?.external || {};
204
+ const siteBundles = siteConfig?.siteMetadata?.getSiteBundles()?.bundles;
205
+ const specifier = moduleDefinition?.specifier;
206
+ if (externals && siteBundles && externals[specifier]) {
207
+ this.handleExternalBundle(specifier, siteBundles, externals, outputDir);
208
+ return;
209
+ }
201
210
  const normalizedUrl = decodeURIComponent(url);
202
211
  createResourceDir(dirname(normalizedUrl), outputDir);
203
212
  const ext = extname(normalizedUrl);
@@ -223,26 +232,33 @@ export default class SiteGenerator {
223
232
  siteConfig.urlRewriteMap.set(url.substring(0, url.indexOf('%2Fv%2F')), normalizedUrl);
224
233
  }
225
234
  // Recursively traverse dependencies
226
- const moduleDefinition = context.fs?.metadata?.moduleDefinition; // LinkedModuleDefinition | BundleDefinition
227
235
  if (moduleDefinition) {
228
236
  // Imports
229
237
  const imports = moduleDefinition.linkedModuleRecord?.imports || moduleDefinition.bundleRecord?.imports || [];
230
238
  // /1/module/esm/0/l/en-US/mi/lwc
231
239
  for (const importModule of imports) {
232
- const jsUri = importModule.specifier.startsWith('/')
233
- ? importModule.specifier
234
- : getSpecifier(importModule);
235
- dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
240
+ const specifier = importModule.specifier;
241
+ if (externals[specifier]) {
242
+ this.handleExternalBundle(specifier, siteBundles, externals, outputDir);
243
+ }
244
+ else {
245
+ const jsUri = specifier.startsWith('/') ? specifier : getSpecifier(importModule);
246
+ dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
247
+ }
236
248
  }
237
249
  // Dynamic imports
238
250
  const dynamicImports = moduleDefinition.linkedModuleRecord?.dynamicImports ||
239
251
  moduleDefinition.bundleRecord?.dynamicImports ||
240
252
  [];
241
253
  for (const importModule of dynamicImports) {
242
- const jsUri = importModule.specifier.startsWith('/')
243
- ? importModule.specifier
244
- : getSpecifier(importModule);
245
- dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
254
+ const specifier = importModule.specifier;
255
+ if (externals[specifier]) {
256
+ this.handleExternalBundle(specifier, siteBundles, externals, outputDir);
257
+ }
258
+ else {
259
+ const jsUri = specifier.startsWith('/') ? specifier : getSpecifier(importModule);
260
+ dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
261
+ }
246
262
  }
247
263
  // If this is a bundle add it to the bundle metadata
248
264
  if (moduleDefinition.bundleRecord) {
@@ -257,6 +273,25 @@ export default class SiteGenerator {
257
273
  // -- Dispatch dependencies
258
274
  await Promise.all(dispatchRequests);
259
275
  }
276
+ /**
277
+ * If this is a file based external copy it to the site folder and add it to the bundle metadata
278
+ */
279
+ handleExternalBundle(specifier, siteBundles, externals, outputDir) {
280
+ if (siteBundles && externals[specifier]?.startsWith(PROTOCOL_FILE) && !siteBundles[specifier]) {
281
+ const path = normalizeFromFileURL(externals[specifier], process.cwd());
282
+ const normalizedPath = decodeURIComponent(path);
283
+ createResourceDir(dirname(normalizedPath), outputDir);
284
+ const ext = extname(normalizedPath);
285
+ const fullPath = join(outputDir, `${normalizedPath}${ext ? '' : '.js'}`);
286
+ fs.copyFileSync(fileURLToPath(externals[specifier]), fullPath);
287
+ const bundleMetadata = {
288
+ specifier,
289
+ path: normalizedPath,
290
+ imports: [],
291
+ };
292
+ siteBundles[specifier] = bundleMetadata;
293
+ }
294
+ }
260
295
  addBundleToSiteMetadata(bundleDefinition, url, siteConfig) {
261
296
  if (siteConfig.siteMetadata) {
262
297
  const locale = siteConfig.locale;
@@ -265,8 +300,9 @@ export default class SiteGenerator {
265
300
  : `${bundleDefinition.specifier}|l/${locale}`;
266
301
  const imports = bundleDefinition.bundleRecord.imports?.map((moduleRef) => getSpecifier(moduleRef)) || [];
267
302
  const dynamicImports = bundleDefinition.bundleRecord.dynamicImports?.map((moduleRef) => getSpecifier(moduleRef));
303
+ const version = bundleDefinition.version === VERSION_NOT_PROVIDED ? undefined : bundleDefinition.version;
268
304
  const bundleMetadata = {
269
- version: bundleDefinition.version,
305
+ version,
270
306
  path: decodeURIComponent(url),
271
307
  includedModules: bundleDefinition.bundleRecord.includedModules || [],
272
308
  imports,
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.12.0-alpha.20",
7
+ "version": "0.12.0-alpha.22",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -43,34 +43,34 @@
43
43
  "build": "tsc -b"
44
44
  },
45
45
  "dependencies": {
46
- "@lwrjs/app-service": "0.12.0-alpha.20",
47
- "@lwrjs/asset-registry": "0.12.0-alpha.20",
48
- "@lwrjs/asset-transformer": "0.12.0-alpha.20",
49
- "@lwrjs/base-view-provider": "0.12.0-alpha.20",
50
- "@lwrjs/base-view-transformer": "0.12.0-alpha.20",
51
- "@lwrjs/client-modules": "0.12.0-alpha.20",
52
- "@lwrjs/config": "0.12.0-alpha.20",
53
- "@lwrjs/diagnostics": "0.12.0-alpha.20",
54
- "@lwrjs/esbuild": "0.12.0-alpha.20",
55
- "@lwrjs/fs-asset-provider": "0.12.0-alpha.20",
56
- "@lwrjs/fs-watch": "0.12.0-alpha.20",
57
- "@lwrjs/html-view-provider": "0.12.0-alpha.20",
58
- "@lwrjs/instrumentation": "0.12.0-alpha.20",
59
- "@lwrjs/loader": "0.12.0-alpha.20",
60
- "@lwrjs/lwc-module-provider": "0.12.0-alpha.20",
61
- "@lwrjs/lwc-ssr": "0.12.0-alpha.20",
62
- "@lwrjs/markdown-view-provider": "0.12.0-alpha.20",
63
- "@lwrjs/module-bundler": "0.12.0-alpha.20",
64
- "@lwrjs/module-registry": "0.12.0-alpha.20",
65
- "@lwrjs/npm-module-provider": "0.12.0-alpha.20",
66
- "@lwrjs/nunjucks-view-provider": "0.12.0-alpha.20",
67
- "@lwrjs/o11y": "0.12.0-alpha.20",
68
- "@lwrjs/resource-registry": "0.12.0-alpha.20",
69
- "@lwrjs/router": "0.12.0-alpha.20",
70
- "@lwrjs/server": "0.12.0-alpha.20",
71
- "@lwrjs/shared-utils": "0.12.0-alpha.20",
72
- "@lwrjs/static": "0.12.0-alpha.20",
73
- "@lwrjs/view-registry": "0.12.0-alpha.20",
46
+ "@lwrjs/app-service": "0.12.0-alpha.22",
47
+ "@lwrjs/asset-registry": "0.12.0-alpha.22",
48
+ "@lwrjs/asset-transformer": "0.12.0-alpha.22",
49
+ "@lwrjs/base-view-provider": "0.12.0-alpha.22",
50
+ "@lwrjs/base-view-transformer": "0.12.0-alpha.22",
51
+ "@lwrjs/client-modules": "0.12.0-alpha.22",
52
+ "@lwrjs/config": "0.12.0-alpha.22",
53
+ "@lwrjs/diagnostics": "0.12.0-alpha.22",
54
+ "@lwrjs/esbuild": "0.12.0-alpha.22",
55
+ "@lwrjs/fs-asset-provider": "0.12.0-alpha.22",
56
+ "@lwrjs/fs-watch": "0.12.0-alpha.22",
57
+ "@lwrjs/html-view-provider": "0.12.0-alpha.22",
58
+ "@lwrjs/instrumentation": "0.12.0-alpha.22",
59
+ "@lwrjs/loader": "0.12.0-alpha.22",
60
+ "@lwrjs/lwc-module-provider": "0.12.0-alpha.22",
61
+ "@lwrjs/lwc-ssr": "0.12.0-alpha.22",
62
+ "@lwrjs/markdown-view-provider": "0.12.0-alpha.22",
63
+ "@lwrjs/module-bundler": "0.12.0-alpha.22",
64
+ "@lwrjs/module-registry": "0.12.0-alpha.22",
65
+ "@lwrjs/npm-module-provider": "0.12.0-alpha.22",
66
+ "@lwrjs/nunjucks-view-provider": "0.12.0-alpha.22",
67
+ "@lwrjs/o11y": "0.12.0-alpha.22",
68
+ "@lwrjs/resource-registry": "0.12.0-alpha.22",
69
+ "@lwrjs/router": "0.12.0-alpha.22",
70
+ "@lwrjs/server": "0.12.0-alpha.22",
71
+ "@lwrjs/shared-utils": "0.12.0-alpha.22",
72
+ "@lwrjs/static": "0.12.0-alpha.22",
73
+ "@lwrjs/view-registry": "0.12.0-alpha.22",
74
74
  "chokidar": "^3.6.0",
75
75
  "esbuild": "^0.9.7",
76
76
  "fs-extra": "^11.2.0",
@@ -80,7 +80,7 @@
80
80
  "ws": "^8.16.0"
81
81
  },
82
82
  "devDependencies": {
83
- "@lwrjs/types": "0.12.0-alpha.20",
83
+ "@lwrjs/types": "0.12.0-alpha.22",
84
84
  "@types/ws": "^8.5.10",
85
85
  "mock-fs": "^5.2.0"
86
86
  },
@@ -93,5 +93,5 @@
93
93
  "volta": {
94
94
  "extends": "../../../package.json"
95
95
  },
96
- "gitHead": "ae1793e34eaf4e9b97996d7aea9031908b23a13a"
96
+ "gitHead": "6eb804007e93e513d4eb60fd321c088bad3c698e"
97
97
  }