@lwrjs/core 0.5.11-alpha.1 → 0.6.0-alpha.10

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.
@@ -53,6 +53,7 @@ var DEFAULT_LAYOUTS_DIR = "$rootDir/src/layouts";
53
53
  var DEFAULT_DATA_DIR = "$rootDir/src/data";
54
54
  var DEFAULT_MODULE_PROVIDERS = [
55
55
  "@lwrjs/app-service/moduleProvider",
56
+ "@lwrjs/lwc-ssr/moduleProvider",
56
57
  "@lwrjs/lwc-module-provider",
57
58
  "@lwrjs/npm-module-provider"
58
59
  ];
@@ -63,7 +64,9 @@ var DEFAULT_VIEW_PROVIDERS = [
63
64
  "@lwrjs/markdown-view-provider",
64
65
  "@lwrjs/base-view-provider"
65
66
  ];
67
+ var DEFAULT_VIEW_TRANFORM_PLUGINS = ["@lwrjs/base-view-transformer", "@lwrjs/lwc-ssr/viewTransformer"];
66
68
  var DEFAULT_ASSET_PROVIDERS = ["@lwrjs/fs-asset-provider"];
69
+ var DEFAULT_ASSET_TRANFORM_PLUGINS = ["@lwrjs/asset-transformer"];
67
70
  var DEFAULT_TEMPLATE_ENGINE = "@lwrjs/base-template-engine";
68
71
  var DEFAULT_AMD_LOADER = "lwr/loader";
69
72
  var DEFAULT_AMD_LOADER_LEGACY = "lwr/loaderLegacy";
@@ -73,9 +76,11 @@ var DEFAULT_LWR_MODULES = [
73
76
  {npm: getLWCEngineSpecifier()},
74
77
  {npm: "@lwrjs/client-modules"},
75
78
  {npm: "@lwrjs/loader"},
79
+ {npm: "@lwrjs/o11y"},
76
80
  {npm: "@lwrjs/router"},
77
81
  {npm: "@lwc/synthetic-shadow"}
78
82
  ];
83
+ var DEFAULT_BUNDLE_EXCLUSIONS = ["lwc", "@lwc/synthetic-shadow", "lwr/navigation", "lwr/esmLoader"];
79
84
  var DEFAULT_LWR_CONFIG = {
80
85
  port: PORT,
81
86
  ignoreLwrConfigFile: false,
@@ -85,6 +90,8 @@ var DEFAULT_LWR_CONFIG = {
85
90
  serverMode: MODE,
86
91
  apiVersion: DEFAULT_API_VERSION,
87
92
  assets: DEFAULT_ASSETS_DIR,
93
+ assetProviders: DEFAULT_ASSET_PROVIDERS,
94
+ assetTransformers: DEFAULT_ASSET_TRANFORM_PLUGINS,
88
95
  contentDir: DEFAULT_CONTENT_DIR,
89
96
  layoutsDir: DEFAULT_LAYOUTS_DIR,
90
97
  staticSiteGenerator: DEFAULT_GENERATOR_CONFIG,
@@ -94,12 +101,13 @@ var DEFAULT_LWR_CONFIG = {
94
101
  moduleProviders: DEFAULT_MODULE_PROVIDERS,
95
102
  resourceProviders: DEFAULT_RESOURCE_PROVIDERS,
96
103
  viewProviders: DEFAULT_VIEW_PROVIDERS,
104
+ viewTransformers: DEFAULT_VIEW_TRANFORM_PLUGINS,
97
105
  templateEngine: DEFAULT_TEMPLATE_ENGINE,
98
106
  environment: {},
99
107
  lwc: {modules: []},
100
108
  routes: [],
101
109
  errorRoutes: [],
102
- bundleConfig: {},
110
+ bundleConfig: {exclude: DEFAULT_BUNDLE_EXCLUSIONS},
103
111
  serverType: DEFAULT_SERVER_TYPE,
104
112
  locker: import_shared_utils.DEFAULT_LWR_LOCKER_CONFIG
105
113
  };
@@ -208,6 +216,15 @@ function mergeLWCConfigs(config1, config2) {
208
216
  interchangeableModules: mergedInterchangeableModules.length ? mergedInterchangeableModules : void 0
209
217
  };
210
218
  }
219
+ function mergeBundleConfig(jsonConfig, config) {
220
+ const defaultExclusions = DEFAULT_BUNDLE_EXCLUSIONS;
221
+ const configExclusions = config?.bundleConfig?.exclude || jsonConfig?.bundleConfig?.exclude || [];
222
+ return {
223
+ ...jsonConfig?.bundleConfig,
224
+ ...config?.bundleConfig,
225
+ exclude: [...new Set([...defaultExclusions, ...configExclusions])]
226
+ };
227
+ }
211
228
  function mergeLockerConfig(jsonConfig, config) {
212
229
  const defaultNamespaces = import_shared_utils.DEFAULT_LOCKER_TRUSTED_CMP;
213
230
  const configNamespaces = config?.locker?.trustedComponents || jsonConfig?.locker?.trustedComponents || [];
@@ -232,8 +249,16 @@ function normalizeLwcConfig(config) {
232
249
  interchangeableModulesMap: config.interchangeableModules ? (0, import_shared_utils.normalizeInterchangeableModuleConfig)(config.interchangeableModules) : void 0
233
250
  };
234
251
  }
252
+ function trimLwrConfig(config) {
253
+ Object.keys(config).forEach((k) => {
254
+ if (config[k] === void 0)
255
+ delete config[k];
256
+ });
257
+ return config;
258
+ }
235
259
  function normalizeConfig(config) {
236
- if (config) {
260
+ if (config !== void 0) {
261
+ config = trimLwrConfig(config);
237
262
  (0, import_app_config.validateLwrAppConfig)(JSON.stringify(config), "pre");
238
263
  }
239
264
  const rootDir = import_path.default.resolve(config?.rootDir || DEFAULT_ROOT_DIR);
@@ -243,6 +268,7 @@ function normalizeConfig(config) {
243
268
  ...lwrJsonConfig,
244
269
  ...config,
245
270
  lwc: normalizeLwcConfig(mergeLWCConfigs(lwrJsonConfig, config)),
271
+ bundleConfig: mergeBundleConfig(lwrJsonConfig, config),
246
272
  locker: mergeLockerConfig(lwrJsonConfig, config),
247
273
  rootDir
248
274
  };
@@ -264,9 +290,11 @@ function normalizeConfig(config) {
264
290
  interchangeableModulesMap: mergedLwrGlobalConfig.lwc.interchangeableModulesMap
265
291
  },
266
292
  moduleProviders: normalizeServices(mergedLwrGlobalConfig.moduleProviders, rootDir),
267
- assetProviders: normalizeServices(DEFAULT_ASSET_PROVIDERS, rootDir),
293
+ assetProviders: normalizeServices(mergedLwrGlobalConfig.assetProviders, rootDir),
294
+ assetTransformers: normalizeServices(mergedLwrGlobalConfig.assetTransformers, rootDir),
268
295
  resourceProviders: normalizeServices(mergedLwrGlobalConfig.resourceProviders, rootDir),
269
296
  viewProviders: normalizeServices(mergedLwrGlobalConfig.viewProviders, rootDir),
297
+ viewTransformers: normalizeServices(mergedLwrGlobalConfig.viewTransformers, rootDir),
270
298
  routes: normalizeRoutes(mergedLwrGlobalConfig.routes, {rootDir, assets, contentDir, layoutsDir}),
271
299
  errorRoutes: normalizeRoutes(mergedLwrGlobalConfig.errorRoutes, {
272
300
  rootDir,
@@ -35,6 +35,7 @@ var import_module_registry = __toModule(require("@lwrjs/module-registry"));
35
35
  var import_resource_registry = __toModule(require("@lwrjs/resource-registry"));
36
36
  var import_asset_registry = __toModule(require("@lwrjs/asset-registry"));
37
37
  var import_view_registry = __toModule(require("@lwrjs/view-registry"));
38
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
38
39
  var import_env_config = __toModule(require("./env-config.cjs"));
39
40
  var import_lwr_app_observer = __toModule(require("./lwr-app-observer.cjs"));
40
41
  var import_locale_middleware = __toModule(require("./middlewares/locale-middleware.cjs"));
@@ -77,10 +78,12 @@ async function initContext(app, server, rawLwrConfig) {
77
78
  };
78
79
  const hookProviders = await getServices(rawLwrConfig.hooks, void 0, rawLwrConfig);
79
80
  const {lwrConfig, dataConfig, runtimeConfig} = await (0, import_hooks.runConfigurationsHook)(hookProviders, rawLwrConfig, rawDataConfig, rawRuntimeEnvConfig);
81
+ const assetTransformers = await getServices(rawLwrConfig.assetTransformers, void 0, rawLwrConfig);
80
82
  const appObserver = new import_lwr_app_observer.LwrApplicationObserver();
81
83
  const appEmitter = appObserver.createLwrEmitter();
82
84
  const compiler = new import_compiler.LwrCompiler();
83
85
  const assetRegistry = new import_asset_registry.LwrAssetRegistry({
86
+ assetTransformers,
84
87
  appObserver,
85
88
  appEmitter,
86
89
  runtimeEnvironment: runtimeConfig
@@ -136,9 +139,9 @@ async function initContext(app, server, rawLwrConfig) {
136
139
  notifyViewSourceChanged,
137
140
  notifyAssetSourceChanged
138
141
  } = appEmitter;
139
- const providerContext = (0, import_shared_utils.deepFreeze)({
142
+ const providerContext = {
140
143
  compiler,
141
- appObserver: {onModuleDefinitionChange, onModuleSourceChange},
144
+ appObserver: (0, import_shared_utils.deepFreeze)({onModuleDefinitionChange, onModuleSourceChange}),
142
145
  appEmitter: {
143
146
  notifyModuleDefinitionChanged: (payload) => notifyModuleDefinitionChanged.call(appEmitter, payload),
144
147
  notifyModuleSourceChanged: (payload) => notifyModuleSourceChanged.call(appEmitter, payload),
@@ -146,10 +149,11 @@ async function initContext(app, server, rawLwrConfig) {
146
149
  notifyAssetSourceChanged: (payload) => notifyAssetSourceChanged.call(appEmitter, payload)
147
150
  },
148
151
  moduleRegistry: moduleRegistry.getPublicApi(),
152
+ moduleBundler,
149
153
  resourceRegistry: resourceRegistry.getPublicApi(),
150
154
  viewRegistry: viewRegistry.getPublicApi(),
151
155
  assetRegistry: assetRegistry.getPublicApi(),
152
- config: {
156
+ config: (0, import_shared_utils.deepFreeze)({
153
157
  cacheDir,
154
158
  modules,
155
159
  routes,
@@ -161,15 +165,17 @@ async function initContext(app, server, rawLwrConfig) {
161
165
  amdLoader,
162
166
  esmLoader,
163
167
  environment
164
- },
165
- runtimeEnvironment: runtimeConfig
166
- });
168
+ }),
169
+ runtimeEnvironment: (0, import_shared_utils.deepFreeze)(runtimeConfig)
170
+ };
167
171
  const moduleProviders = await getServices(lwrConfig.moduleProviders, providerContext, lwrConfig);
168
172
  moduleRegistry.addModuleProviders(moduleProviders);
169
173
  const resourceProviders = await getServices(lwrConfig.resourceProviders, providerContext, lwrConfig);
170
174
  resourceRegistry.addResourceProviders(resourceProviders);
171
175
  const viewProviders = await getServices(lwrConfig.viewProviders, providerContext, lwrConfig);
176
+ const viewTransformers = await getServices(rawLwrConfig.viewTransformers, providerContext, rawLwrConfig);
172
177
  viewRegistry.addViewProviders(viewProviders);
178
+ viewRegistry.addViewTransformers(viewTransformers);
173
179
  await viewRegistry.initializeViewProviders();
174
180
  const assetProviders = await getServices(lwrConfig.assetProviders, providerContext, lwrConfig);
175
181
  assetRegistry.addAssetProviders(assetProviders);
@@ -190,9 +196,14 @@ var LwrApp = class {
190
196
  }
191
197
  async init() {
192
198
  if (!this.initialized) {
193
- const context = await initContext(this.app, this.server, this.config);
194
- initMiddlewares(this.app, this.server, context);
195
199
  this.initialized = true;
200
+ try {
201
+ const context = await initContext(this.app, this.server, this.config);
202
+ initMiddlewares(this.app, this.server, context);
203
+ } catch (e) {
204
+ this.initialized = false;
205
+ throw e;
206
+ }
196
207
  }
197
208
  }
198
209
  async listen(callback) {
@@ -201,13 +212,19 @@ var LwrApp = class {
201
212
  const {serverMode, port} = config;
202
213
  server.listen(port || config.port, async () => {
203
214
  if (process.env.WARMUP?.toLowerCase() === "true") {
204
- await (0, import_server_warmup.warmupServer)(config, app.getInternalRequestKey());
215
+ try {
216
+ await (0, import_server_warmup.warmupServer)(config, app.getInternalRequestKey());
217
+ } catch (err) {
218
+ throw (0, import_diagnostics.createSingleDiagnosticError)({
219
+ description: import_diagnostics.descriptions.SERVER.WARMUP_ERROR(err.message)
220
+ }, import_diagnostics.LwrServerError);
221
+ }
205
222
  }
206
223
  callback?.({serverMode, port});
207
224
  });
208
225
  }
209
226
  async close() {
210
- await this.server.close();
227
+ this.server?.close && await this.server.close();
211
228
  }
212
229
  getInternalServer() {
213
230
  return this.app.getImpl();
@@ -220,7 +237,21 @@ async function generateStaticSite(config) {
220
237
  config = config || {};
221
238
  config.serverType = "fs";
222
239
  const lwrApp = createServer(config);
240
+ overrideConfigAsSrc(lwrApp);
223
241
  await lwrApp.init();
224
242
  const dispatcher = lwrApp.getInternalServer();
225
243
  await new import_static_generation.default().buildStaticApplication(lwrApp.getConfig(), dispatcher);
226
244
  }
245
+ function overrideConfigAsSrc(lwrApp) {
246
+ if ((0, import_shared_utils.getExperimentalFeatures)().ENABLE_FINGERPRINTS) {
247
+ const normalizedConfig = lwrApp.getConfig();
248
+ const routes = normalizedConfig.routes || [];
249
+ for (const route of routes) {
250
+ route.bootstrap = {
251
+ ...import_shared_utils.DEFAULT_LWR_BOOTSTRAP_CONFIG,
252
+ ...route.bootstrap,
253
+ configAsSrc: true
254
+ };
255
+ }
256
+ }
257
+ }
@@ -82,17 +82,17 @@ function apiMiddleware(app, context) {
82
82
  const resolvedUris = [];
83
83
  if (req.isSiteGeneration()) {
84
84
  if (bundleDef.bundleRecord.imports) {
85
- for (let index = 0; index < bundleDef.bundleRecord.imports.length; index++) {
86
- const theImport = bundleDef.bundleRecord.imports[index];
87
- const id = await (0, import_shared_utils.getVersionedModuleId)(theImport.specifier, moduleRegistry);
85
+ for (const theImport of bundleDef.bundleRecord.imports) {
86
+ const childSpecifier = theImport.specifier;
87
+ const id = await (0, import_shared_utils.getVersionedModuleId)(childSpecifier, moduleRegistry);
88
88
  const uri = await moduleRegistry.resolveModuleUri(id, runtimeEnvironment, runtimeParams);
89
89
  resolvedUris.push(uri);
90
90
  }
91
91
  }
92
92
  if (bundleDef.bundleRecord.dynamicImports) {
93
- for (let index = 0; index < bundleDef.bundleRecord.dynamicImports.length; index++) {
94
- const theImport = bundleDef.bundleRecord.dynamicImports[index];
95
- const id = await (0, import_shared_utils.getVersionedModuleId)(theImport.specifier, moduleRegistry);
93
+ for (const theImport of bundleDef.bundleRecord.dynamicImports) {
94
+ const childSpecifier = theImport.specifier;
95
+ const id = await (0, import_shared_utils.getVersionedModuleId)(childSpecifier, moduleRegistry);
96
96
  const uri = await moduleRegistry.resolveModuleUri(id, runtimeEnvironment, runtimeParams);
97
97
  resolvedUris.push(uri);
98
98
  }
@@ -278,7 +278,8 @@ function apiMiddleware(app, context) {
278
278
  ({moduleIds} = (0, import_shared_utils.getMappingIdentity)(req));
279
279
  const {runtimeEnvironment, runtimeParams} = requestContext;
280
280
  const importMetadata = await (0, import_shared_utils.getImportMetadataMappings)(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler);
281
- res.type("application/json").send(importMetadata);
281
+ res.setMetadata({importMetadata});
282
+ res.status(200).type("application/json").send(importMetadata);
282
283
  } catch (e) {
283
284
  console.log(e);
284
285
  const error = (0, import_utils.createReturnStatus)(`mappings for "${JSON.stringify(moduleIds || req.params?.specifiers)}"`, e);
@@ -30,13 +30,14 @@ var import_ms = __toModule(require("ms"));
30
30
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
31
  var import_view_registry = __toModule(require("@lwrjs/view-registry"));
32
32
  var import_utils = __toModule(require("./utils.cjs"));
33
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
33
34
  function uiMiddleware(app, context) {
34
35
  const {viewRegistry, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment} = context;
35
36
  const {environment: environmentConfig, routes, errorRoutes} = context.appConfig;
36
37
  const route404 = errorRoutes.find((r) => r.status === 404);
37
38
  const route500 = errorRoutes.find((r) => r.status === 500);
38
39
  const viewHandler = new import_view_registry.LwrViewHandler({viewRegistry, moduleRegistry}, context.appConfig);
39
- async function sendResponse(req, res, route, defaultStatus = 200) {
40
+ async function sendViewResponse(req, res, route, defaultStatus = 200) {
40
41
  if (!req.validateJsonRequest()) {
41
42
  res.status(400).send({error: "Accept header and json query parameter are incompatible"});
42
43
  return;
@@ -81,23 +82,27 @@ function uiMiddleware(app, context) {
81
82
  console.error(error);
82
83
  }
83
84
  if (route500 && defaultStatus !== 500) {
84
- await sendResponse(req, res, route500, 500);
85
+ await sendViewResponse(req, res, route500, 500);
85
86
  } else {
86
87
  res.status(500).send(`500 - Error retrieving view for route "${route.id}"`);
87
88
  }
88
89
  }
89
90
  }
90
- async function sendConfigurationResponse(req, res, route, defaultStatus = 200) {
91
+ async function sendConfigurationResponse(req, res, defaultStatus = 200) {
91
92
  const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
92
- const {originalUrl, path} = req;
93
- const {apiVersion, format, appId, locale} = req.params;
94
- const rootCfgPath = `/${apiVersion}/application/${format}/l/${locale}/ai/${appId}/configuration/ci`;
95
- const url = originalUrl.replace(rootCfgPath, "");
96
- const requestPath = path.replace(rootCfgPath, "");
93
+ const {appId, encodedViewPath} = req.params;
94
+ const route = routes.find((route2) => route2.id === appId);
95
+ if (!route) {
96
+ res.status(404).send({error: `LWR configuration for "${appId} is not available"`});
97
+ return;
98
+ }
97
99
  try {
100
+ const url = decodeURIComponent(encodedViewPath);
101
+ const requestPath = route.path;
102
+ const params = (0, import_shared_utils.extractRequestParams)(requestPath, url, req.params);
98
103
  const viewRequest = {
99
104
  url,
100
- params: req.params,
105
+ params,
101
106
  query: req.query,
102
107
  requestPath
103
108
  };
@@ -126,7 +131,7 @@ function uiMiddleware(app, context) {
126
131
  console.error(error);
127
132
  }
128
133
  if (route500 && defaultStatus !== 500) {
129
- await sendResponse(req, res, route500, 500);
134
+ await sendViewResponse(req, res, route500, 500);
130
135
  } else {
131
136
  res.status(500).send(`500 - Error retrieving route "${route.id}"`);
132
137
  }
@@ -135,30 +140,30 @@ function uiMiddleware(app, context) {
135
140
  routes.forEach((route) => {
136
141
  if (route.method === "post") {
137
142
  app.post(route.path, async (req, res) => {
138
- await sendResponse(req, res, route);
143
+ await sendViewResponse(req, res, route);
139
144
  });
140
145
  } else {
141
146
  app.get(route.path, async (req, res) => {
142
- await sendResponse(req, res, route);
147
+ await sendViewResponse(req, res, route);
143
148
  });
144
149
  app.get([
145
150
  `/:apiVersion/application/:format/l/:locale/ai/:appId${route.path}`,
146
- `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${route.path}`
151
+ `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${route.path}`,
152
+ `/:apiVersion/application/:format/ai/:appId${route.path}`,
153
+ `/:apiVersion/application/:format/e/:environment/ai/:appId${route.path}`
147
154
  ], async (req, res) => {
148
- await sendResponse(req, res, route);
149
- });
150
- app.get([
151
- `/:apiVersion/application/:format/l/:locale/ai/:appId/configuration/ci${route.path}`,
152
- `/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId/configuration/ci${route.path}`
153
- ], async (req, res) => {
154
- await sendConfigurationResponse(req, res, route);
155
+ await sendViewResponse(req, res, route);
155
156
  });
156
157
  }
157
158
  });
159
+ const clientBootstrapConfigurationRoutes = (0, import_shared_utils.getClientBootstrapConfigurationRoutes)();
160
+ app.get(clientBootstrapConfigurationRoutes, async (req, res) => {
161
+ await sendConfigurationResponse(req, res);
162
+ });
158
163
  app.initRoutes();
159
164
  app.use(async (req, res) => {
160
165
  if (route404) {
161
- await sendResponse(req, res, route404, 404);
166
+ await sendViewResponse(req, res, route404, 404);
162
167
  } else {
163
168
  res.status(404).send("404 - This page does not exist");
164
169
  }
@@ -37,7 +37,7 @@ var window = new import_jsdom.JSDOM("").window;
37
37
  var DOMPurify = (0, import_dompurify.default)(window);
38
38
  function createReturnStatus(name, error) {
39
39
  let returnStatus = {status: 501, message: ""};
40
- if (error.code === "NO_LWC_MODULE_FOUND") {
40
+ if ((0, import_diagnostics.isNodeError)(error) && error.code === "NO_LWC_MODULE_FOUND") {
41
41
  returnStatus = {status: 404, message: import_diagnostics.descriptions.UNRESOLVABLE.LWC_MODULE(name).message};
42
42
  } else if (error instanceof import_diagnostics.LwrUnresolvableError && error.diagnostics[0].description.category === "lwrUnresolvable/invalid") {
43
43
  returnStatus = {status: 400, message: error.message};