@lwrjs/config 0.9.0-alpha.17 → 0.9.0-alpha.19

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.
@@ -26,7 +26,6 @@ __markAsModule(exports);
26
26
  __export(exports, {
27
27
  DEFAULT_AMD_LOADER: () => DEFAULT_AMD_LOADER,
28
28
  DEFAULT_AMD_LOADER_LEGACY: () => DEFAULT_AMD_LOADER_LEGACY,
29
- DEFAULT_BUNDLE_EXCLUSIONS: () => DEFAULT_BUNDLE_EXCLUSIONS,
30
29
  DEFAULT_ESM_LOADER: () => DEFAULT_ESM_LOADER,
31
30
  DEFAULT_GENERATOR_CONFIG: () => DEFAULT_GENERATOR_CONFIG,
32
31
  DEFAULT_LWR_CONFIG: () => DEFAULT_LWR_CONFIG,
@@ -34,10 +33,12 @@ __export(exports, {
34
33
  DEFAULT_LWR_MODULES: () => DEFAULT_LWR_MODULES,
35
34
  DEFAULT_ROOT_DIR: () => DEFAULT_ROOT_DIR,
36
35
  DEFAULT_SERVICE_PACKAGE_NAME: () => DEFAULT_SERVICE_PACKAGE_NAME,
37
- LWR_VERSION: () => LWR_VERSION
36
+ LWR_VERSION: () => LWR_VERSION,
37
+ getDefaultBundleConfig: () => getDefaultBundleConfig
38
38
  });
39
39
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
40
40
  var import_package = __toModule(require("@lwrjs/config/package"));
41
+ var import_runtime_config = __toModule(require("./runtime-config.cjs"));
41
42
  var PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3e3;
42
43
  var MODE = process.env.MODE || "dev";
43
44
  var DEFAULT_BASE_PATH = "";
@@ -86,13 +87,29 @@ var DEFAULT_LWR_MODULES = [
86
87
  {npm: "@lwrjs/router"},
87
88
  {npm: "@lwc/synthetic-shadow"}
88
89
  ];
89
- var DEFAULT_BUNDLE_EXCLUSIONS = [
90
+ var DEFAULT_ESM_BUNDLE_EXCLUSIONS = [
90
91
  "lwc",
91
92
  "@lwc/synthetic-shadow",
92
93
  "lwr/navigation",
93
94
  "lwr/esmLoader",
94
95
  "lwr/profiler"
95
96
  ];
97
+ var DEFAULT_AMD_BUNDLE_EXCLUSIONS = ["lwc", "@lwc/synthetic-shadow", "lwr/navigation", "lwr/profiler"];
98
+ var DEFAULT_ESM_BUNDLE_EXTERNALS = {};
99
+ var DEFAULT_AMD_BUNDLE_EXTERNALS = {
100
+ "lwr/loader": "lwr-loader-shim.bundle.min.js"
101
+ };
102
+ var DEFAULT_AMD_LEGACY_BUNDLE_EXTERNALS = {
103
+ "lwr/loaderLegacy": "lwr-loader-shim-legacy.bundle.min.js"
104
+ };
105
+ function getDefaultBundleConfig(mode) {
106
+ const format = (0, import_runtime_config.getServerModeConfig)(mode || MODE).format;
107
+ const loaderLegacy = (0, import_shared_utils.getFeatureFlags)().LEGACY_LOADER;
108
+ return {
109
+ exclude: format === "esm" ? DEFAULT_ESM_BUNDLE_EXCLUSIONS : DEFAULT_AMD_BUNDLE_EXCLUSIONS,
110
+ external: format === "esm" ? DEFAULT_ESM_BUNDLE_EXTERNALS : loaderLegacy ? DEFAULT_AMD_LEGACY_BUNDLE_EXTERNALS : DEFAULT_AMD_BUNDLE_EXTERNALS
111
+ };
112
+ }
96
113
  var DEFAULT_LWR_CONFIG = {
97
114
  port: PORT,
98
115
  ignoreLwrConfigFile: false,
@@ -119,7 +136,8 @@ var DEFAULT_LWR_CONFIG = {
119
136
  lwc: {modules: []},
120
137
  routes: [],
121
138
  errorRoutes: [],
122
- bundleConfig: {exclude: DEFAULT_BUNDLE_EXCLUSIONS},
139
+ routeHandlers: {},
140
+ bundleConfig: getDefaultBundleConfig(MODE),
123
141
  serverType: DEFAULT_SERVER_TYPE,
124
142
  locker: import_shared_utils.DEFAULT_LWR_LOCKER_CONFIG
125
143
  };
@@ -90,8 +90,8 @@ function normalizeConfig(config) {
90
90
  resourceProviders: (0, import_services.normalizeServices)(config.resourceProviders),
91
91
  viewProviders: (0, import_services.normalizeServices)(config.viewProviders),
92
92
  viewTransformers: (0, import_services.normalizeServices)(config.viewTransformers),
93
- routes: (0, import_routes.normalizeRoutes)(config.routes),
94
- errorRoutes: (0, import_routes.normalizeRoutes)(config.errorRoutes)
93
+ routes: (0, import_routes.normalizeRoutes)(config.routes, config.routeHandlers),
94
+ errorRoutes: (0, import_routes.normalizeRoutes)(config.errorRoutes, config.routeHandlers)
95
95
  };
96
96
  }
97
97
  function normalizeConfigPaths(config) {
@@ -120,7 +120,8 @@ function normalizeConfigPaths(config) {
120
120
  viewProviders: (0, import_services.normalizeServicePaths)(config.viewProviders, rootDir),
121
121
  viewTransformers: (0, import_services.normalizeServicePaths)(config.viewTransformers, rootDir),
122
122
  routes: (0, import_routes.normalizeRoutePaths)(config.routes, resourcePaths),
123
- errorRoutes: (0, import_routes.normalizeRoutePaths)(config.errorRoutes, resourcePaths)
123
+ errorRoutes: (0, import_routes.normalizeRoutePaths)(config.errorRoutes, resourcePaths),
124
+ routeHandlers: (0, import_routes.normalizeRouteHandlerPaths)(config.routeHandlers, resourcePaths)
124
125
  };
125
126
  }
126
127
  function resolveGlobalConfig(configArg, options) {
@@ -39,8 +39,8 @@ async function executeConfigHooks(hooks, globalConfig, runtimeEnvironment, globa
39
39
  }
40
40
  await hook.initConfigs(globalConfig, globalData, runtimeEnvironment);
41
41
  }
42
- globalConfig.routes = (0, import_routes.normalizeRoutes)(globalConfig.routes);
43
- globalConfig.errorRoutes = (0, import_routes.normalizeRoutes)(globalConfig.errorRoutes);
42
+ globalConfig.routes = (0, import_routes.normalizeRoutes)(globalConfig.routes, globalConfig.routeHandlers);
43
+ globalConfig.errorRoutes = (0, import_routes.normalizeRoutes)(globalConfig.errorRoutes, globalConfig.routeHandlers);
44
44
  (0, import_app_config.validateLwrAppConfig)(globalConfig, "post");
45
45
  }
46
46
  function executeStartHooks(hooks, globalConfig) {
@@ -53,6 +53,6 @@ function executeStartHooks(hooks, globalConfig) {
53
53
  }
54
54
  hook.onStart(globalConfig.routes);
55
55
  }
56
- globalConfig.routes = (0, import_routes.normalizeRoutes)(globalConfig.routes);
56
+ globalConfig.routes = (0, import_routes.normalizeRoutes)(globalConfig.routes, globalConfig.routeHandlers);
57
57
  (0, import_app_config.validateLwrAppConfig)(globalConfig, "post");
58
58
  }
@@ -25,7 +25,7 @@ var __toModule = (module2) => {
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
27
  loadHooks: () => loadHooks,
28
- loadRoutes: () => loadRoutes,
28
+ loadRouteHandlers: () => loadRouteHandlers,
29
29
  loadServices: () => loadServices
30
30
  });
31
31
  var import_path = __toModule(require("path"));
@@ -57,26 +57,6 @@ async function loadServiceEntries(entries, rootDir, cacheDir) {
57
57
  return [ctor, config];
58
58
  }));
59
59
  }
60
- async function loadRouteHandlers(routes, rootDir, cacheDir) {
61
- const cache = {};
62
- return Promise.all(routes.map(async (route) => {
63
- if (!route.routeHandler) {
64
- return route;
65
- }
66
- if (cache[route.routeHandler]) {
67
- return {
68
- ...route,
69
- routeHandler: cache[route.routeHandler]
70
- };
71
- }
72
- const routeHandler = await importModule(route.routeHandler, rootDir, import_path.default.join(cacheDir, "routeHandlers"));
73
- cache[route.routeHandler] = routeHandler;
74
- return {
75
- ...route,
76
- routeHandler
77
- };
78
- }));
79
- }
80
60
  async function loadHooks(config) {
81
61
  if (!config.hooks) {
82
62
  return [];
@@ -105,11 +85,12 @@ async function loadServices(config) {
105
85
  viewTransformers
106
86
  };
107
87
  }
108
- async function loadRoutes(config) {
109
- const routes = await loadRouteHandlers(config.routes, config.rootDir, config.cacheDir);
110
- const errorRoutes = await loadRouteHandlers(config.errorRoutes, config.rootDir, config.cacheDir);
111
- return {
112
- routes,
113
- errorRoutes
114
- };
88
+ async function loadRouteHandlers(config) {
89
+ const routeHandlers = {};
90
+ await Promise.all(Object.keys(config.routeHandlers).map(async (id) => {
91
+ const handlerPath = config.routeHandlers[id];
92
+ const handler = await importModule(handlerPath, config.rootDir, import_path.default.join(config.cacheDir, "routeHandlers"));
93
+ routeHandlers[id] = handler;
94
+ }));
95
+ return routeHandlers;
115
96
  }
@@ -59,12 +59,16 @@ function mergeLwcConfig(config1, config2) {
59
59
  };
60
60
  }
61
61
  function mergeBundleConfig(config1, config2) {
62
- const defaultExclusions = config2?.bundleConfig?.UNSAFE_lwrDefaultExclude || config1?.bundleConfig?.UNSAFE_lwrDefaultExclude || import_defaults.DEFAULT_BUNDLE_EXCLUSIONS;
62
+ const mode = config2?.serverMode || config1?.serverMode;
63
+ const DEFAULT_BUNDLE_CONFIG = (0, import_defaults.getDefaultBundleConfig)(mode);
64
+ const defaultExclusions = config2?.bundleConfig?.UNSAFE_lwrDefaultExclude || config1?.bundleConfig?.UNSAFE_lwrDefaultExclude || DEFAULT_BUNDLE_CONFIG.exclude || [];
63
65
  const configExclusions = config2?.bundleConfig?.exclude || config1?.bundleConfig?.exclude || [];
66
+ const configExternals = config2?.bundleConfig?.external || config1?.bundleConfig?.external || DEFAULT_BUNDLE_CONFIG.external || {};
64
67
  return {
65
68
  ...config1?.bundleConfig,
66
69
  ...config2?.bundleConfig,
67
- exclude: [...new Set([...defaultExclusions, ...configExclusions])]
70
+ exclude: [...new Set([...defaultExclusions, ...configExclusions])],
71
+ external: {...configExternals}
68
72
  };
69
73
  }
70
74
  function mergeLockerConfig(jsonConfig, config) {
@@ -24,27 +24,45 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/config/src/utils/routes.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
+ normalizeRouteHandlerPaths: () => normalizeRouteHandlerPaths,
27
28
  normalizeRoutePaths: () => normalizeRoutePaths,
28
29
  normalizeRoutes: () => normalizeRoutes
29
30
  });
30
31
  var import_path = __toModule(require("path"));
31
32
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
32
- function normalizeRoutes(routes = []) {
33
- return routes.map((route) => ({
34
- ...route,
35
- bootstrap: {
36
- ...import_shared_utils.DEFAULT_LWR_BOOTSTRAP_CONFIG,
37
- ...route.bootstrap
33
+ function normalizeRoutes(routes, routeHandlers) {
34
+ return routes.map((route) => {
35
+ if (route.routeHandler && !routeHandlers[route.routeHandler]) {
36
+ const handlerPath = route.routeHandler;
37
+ routeHandlers[handlerPath] = handlerPath;
38
38
  }
39
- }));
39
+ return {
40
+ ...route,
41
+ bootstrap: {
42
+ ...import_shared_utils.DEFAULT_LWR_BOOTSTRAP_CONFIG,
43
+ ...route.bootstrap
44
+ }
45
+ };
46
+ });
40
47
  }
41
48
  function normalizeRoutePaths(routes = [], resourcePaths) {
42
49
  return routes.map((route) => {
43
- const {routeHandler, contentTemplate, layoutTemplate, subRoutes} = route;
44
- route.routeHandler = routeHandler && import_path.default.resolve((0, import_shared_utils.normalizeDirectory)(routeHandler, resourcePaths.rootDir));
45
- route.contentTemplate = contentTemplate && import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(contentTemplate, resourcePaths));
46
- route.layoutTemplate = layoutTemplate && import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(layoutTemplate, resourcePaths));
47
- route.subRoutes = subRoutes && import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(subRoutes, resourcePaths));
50
+ const {contentTemplate, layoutTemplate, subRoutes} = route;
51
+ if (contentTemplate) {
52
+ route.contentTemplate = import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(contentTemplate, resourcePaths));
53
+ }
54
+ if (layoutTemplate) {
55
+ route.layoutTemplate = import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(layoutTemplate, resourcePaths));
56
+ }
57
+ if (subRoutes) {
58
+ route.subRoutes = import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(subRoutes, resourcePaths));
59
+ }
48
60
  return route;
49
61
  });
50
62
  }
63
+ function normalizeRouteHandlerPaths(routeHandlers, resourcePaths) {
64
+ for (const [id, handlerPath] of Object.entries(routeHandlers)) {
65
+ routeHandlers[id] = import_path.default.resolve((0, import_shared_utils.normalizeDirectory)(handlerPath, resourcePaths.rootDir));
66
+ }
67
+ return routeHandlers;
68
+ }
@@ -67,6 +67,7 @@ var ROOT_ATTRIBUTE_KEYS = createKeys("root", [
67
67
  "basePath",
68
68
  "resourceProviders",
69
69
  "rootDir",
70
+ "routeHandlers",
70
71
  "routes",
71
72
  "serverMode",
72
73
  "serverType",
@@ -101,6 +101,11 @@ function validateErrorRoutes(node, validationContext) {
101
101
  }
102
102
  }
103
103
  }
104
+ function validateRouteHandlers(node, validationContext) {
105
+ if (node) {
106
+ validationContext.assertIsObject(node, "routeHandlers");
107
+ }
108
+ }
104
109
  function validateAssets(node, validationContext, preMerge) {
105
110
  if (node) {
106
111
  if (preMerge && node.type === "string") {
@@ -147,6 +152,7 @@ function validateRoot(node, validationContext, preMerge) {
147
152
  validationContext.assertUniqueIds([...routes?.children || [], ...errorRoutes?.children || []], "routes");
148
153
  validateRoutes(routes, validationContext, preMerge);
149
154
  validateErrorRoutes(errorRoutes, validationContext);
155
+ validateRouteHandlers((0, import_jsonc_parser.findNodeAtLocation)(node, ["routeHandlers"]), validationContext);
150
156
  validateAssets((0, import_jsonc_parser.findNodeAtLocation)(node, ["assets"]), validationContext, preMerge);
151
157
  validateLocker((0, import_jsonc_parser.findNodeAtLocation)(node, ["locker"]), validationContext);
152
158
  validationContext.assertNotEmptyString((0, import_jsonc_parser.findNodeAtLocation)(node, ["apiVersion"]), "apiVersion");
@@ -1,4 +1,4 @@
1
- import { LwrGlobalConfig } from '@lwrjs/types';
1
+ import type { BundleConfig, LwrGlobalConfig } from '@lwrjs/types';
2
2
  export declare const LWR_VERSION: string;
3
3
  export declare const DEFAULT_LWR_CONFIG_JSON = "$rootDir/lwr.config.json";
4
4
  export declare const DEFAULT_GENERATOR_CONFIG: {
@@ -16,6 +16,6 @@ export declare const DEFAULT_SERVICE_PACKAGE_NAME: RegExp;
16
16
  export declare const DEFAULT_LWR_MODULES: {
17
17
  npm: string;
18
18
  }[];
19
- export declare const DEFAULT_BUNDLE_EXCLUSIONS: string[];
19
+ export declare function getDefaultBundleConfig(mode?: string): BundleConfig;
20
20
  export declare const DEFAULT_LWR_CONFIG: Required<LwrGlobalConfig>;
21
21
  //# sourceMappingURL=defaults.d.ts.map
@@ -1,5 +1,6 @@
1
- import { DEFAULT_LWR_LOCKER_CONFIG } from '@lwrjs/shared-utils';
1
+ import { DEFAULT_LWR_LOCKER_CONFIG, getFeatureFlags } from '@lwrjs/shared-utils';
2
2
  import { version } from '@lwrjs/config/package';
3
+ import { getServerModeConfig } from './runtime-config.js';
3
4
  const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
4
5
  const MODE = process.env.MODE || 'dev';
5
6
  const DEFAULT_BASE_PATH = '';
@@ -50,13 +51,33 @@ export const DEFAULT_LWR_MODULES = [
50
51
  { npm: '@lwrjs/router' },
51
52
  { npm: '@lwc/synthetic-shadow' },
52
53
  ];
53
- export const DEFAULT_BUNDLE_EXCLUSIONS = [
54
+ const DEFAULT_ESM_BUNDLE_EXCLUSIONS = [
54
55
  'lwc',
55
56
  '@lwc/synthetic-shadow',
56
57
  'lwr/navigation',
57
58
  'lwr/esmLoader',
58
59
  'lwr/profiler',
59
60
  ];
61
+ const DEFAULT_AMD_BUNDLE_EXCLUSIONS = ['lwc', '@lwc/synthetic-shadow', 'lwr/navigation', 'lwr/profiler'];
62
+ const DEFAULT_ESM_BUNDLE_EXTERNALS = {};
63
+ const DEFAULT_AMD_BUNDLE_EXTERNALS = {
64
+ 'lwr/loader': 'lwr-loader-shim.bundle.min.js',
65
+ };
66
+ const DEFAULT_AMD_LEGACY_BUNDLE_EXTERNALS = {
67
+ 'lwr/loaderLegacy': 'lwr-loader-shim-legacy.bundle.min.js',
68
+ };
69
+ export function getDefaultBundleConfig(mode) {
70
+ const format = getServerModeConfig(mode || MODE).format;
71
+ const loaderLegacy = getFeatureFlags().LEGACY_LOADER;
72
+ return {
73
+ exclude: format === 'esm' ? DEFAULT_ESM_BUNDLE_EXCLUSIONS : DEFAULT_AMD_BUNDLE_EXCLUSIONS,
74
+ external: format === 'esm'
75
+ ? DEFAULT_ESM_BUNDLE_EXTERNALS
76
+ : loaderLegacy
77
+ ? DEFAULT_AMD_LEGACY_BUNDLE_EXTERNALS
78
+ : DEFAULT_AMD_BUNDLE_EXTERNALS,
79
+ };
80
+ }
60
81
  export const DEFAULT_LWR_CONFIG = {
61
82
  port: PORT,
62
83
  ignoreLwrConfigFile: false,
@@ -83,7 +104,8 @@ export const DEFAULT_LWR_CONFIG = {
83
104
  lwc: { modules: [] },
84
105
  routes: [],
85
106
  errorRoutes: [],
86
- bundleConfig: { exclude: DEFAULT_BUNDLE_EXCLUSIONS },
107
+ routeHandlers: {},
108
+ bundleConfig: getDefaultBundleConfig(MODE),
87
109
  serverType: DEFAULT_SERVER_TYPE,
88
110
  locker: DEFAULT_LWR_LOCKER_CONFIG,
89
111
  };
@@ -6,7 +6,7 @@ import { DEFAULT_AMD_LOADER, DEFAULT_AMD_LOADER_LEGACY, DEFAULT_ESM_LOADER, DEFA
6
6
  import { validateLwrAppConfig } from './validation/app-config.js';
7
7
  import { normalizeAssetPaths, normalizeAssets } from './utils/assets.js';
8
8
  import { normalizeServicePaths, normalizeServices } from './utils/services.js';
9
- import { normalizeRoutePaths, normalizeRoutes } from './utils/routes.js';
9
+ import { normalizeRouteHandlerPaths, normalizeRoutePaths, normalizeRoutes } from './utils/routes.js';
10
10
  import { mergeBundleConfig, mergeLockerConfig, mergeLwcConfig, mergeStaticGenerationConfig, trimLwrConfig, } from './utils/merge.js';
11
11
  import { normalizeLwcConfig, normalizeModulePaths } from './utils/lwc.js';
12
12
  import { getRuntimeEnvironment } from './runtime-config.js';
@@ -118,9 +118,9 @@ function normalizeConfig(config) {
118
118
  resourceProviders: normalizeServices(config.resourceProviders),
119
119
  viewProviders: normalizeServices(config.viewProviders),
120
120
  viewTransformers: normalizeServices(config.viewTransformers),
121
- // normalize all routes
122
- routes: normalizeRoutes(config.routes),
123
- errorRoutes: normalizeRoutes(config.errorRoutes),
121
+ // normalize routes and capture all route handlers
122
+ routes: normalizeRoutes(config.routes, config.routeHandlers),
123
+ errorRoutes: normalizeRoutes(config.errorRoutes, config.routeHandlers),
124
124
  };
125
125
  }
126
126
  /**
@@ -162,6 +162,7 @@ function normalizeConfigPaths(config) {
162
162
  // normalize all route paths
163
163
  routes: normalizeRoutePaths(config.routes, resourcePaths),
164
164
  errorRoutes: normalizeRoutePaths(config.errorRoutes, resourcePaths),
165
+ routeHandlers: normalizeRouteHandlerPaths(config.routeHandlers, resourcePaths),
165
166
  };
166
167
  }
167
168
  /**
package/build/es/hooks.js CHANGED
@@ -25,8 +25,8 @@ export async function executeConfigHooks(hooks, globalConfig, runtimeEnvironment
25
25
  // eslint-disable-next-line no-await-in-loop
26
26
  await hook.initConfigs(globalConfig, globalData, runtimeEnvironment);
27
27
  }
28
- globalConfig.routes = normalizeRoutes(globalConfig.routes);
29
- globalConfig.errorRoutes = normalizeRoutes(globalConfig.errorRoutes);
28
+ globalConfig.routes = normalizeRoutes(globalConfig.routes, globalConfig.routeHandlers);
29
+ globalConfig.errorRoutes = normalizeRoutes(globalConfig.errorRoutes, globalConfig.routeHandlers);
30
30
  validateLwrAppConfig(globalConfig, 'post');
31
31
  }
32
32
  /**
@@ -53,7 +53,7 @@ export function executeStartHooks(hooks, globalConfig) {
53
53
  }
54
54
  hook.onStart(globalConfig.routes);
55
55
  }
56
- globalConfig.routes = normalizeRoutes(globalConfig.routes);
56
+ globalConfig.routes = normalizeRoutes(globalConfig.routes, globalConfig.routeHandlers);
57
57
  validateLwrAppConfig(globalConfig, 'post');
58
58
  }
59
59
  //# sourceMappingURL=hooks.js.map
@@ -1,8 +1,4 @@
1
- import type { HooksPlugin, NormalizedLwrErrorRoute, NormalizedLwrGlobalConfig, NormalizedLwrRoute, Route, Services } from '@lwrjs/types';
2
- export interface Routes {
3
- routes: Route<NormalizedLwrRoute>[];
4
- errorRoutes: Route<NormalizedLwrErrorRoute>[];
5
- }
1
+ import type { HooksPlugin, NormalizedLwrGlobalConfig, RouteHandlers, Services } from '@lwrjs/types';
6
2
  /**
7
3
  * Import and instantiate hooks defined in the global config
8
4
  *
@@ -30,7 +26,7 @@ export declare function loadServices(config: NormalizedLwrGlobalConfig): Promise
30
26
  * The optional dependency `esbuild` MUST be installed to load route handler modules.
31
27
  *
32
28
  * @param config - global config
33
- * @returns {Routes} all routes with resolved route handlers
29
+ * @returns {Routes} resolved route handlers mapped by id
34
30
  */
35
- export declare function loadRoutes(config: NormalizedLwrGlobalConfig): Promise<Routes>;
31
+ export declare function loadRouteHandlers(config: NormalizedLwrGlobalConfig): Promise<RouteHandlers>;
36
32
  //# sourceMappingURL=modules.d.ts.map
@@ -30,26 +30,6 @@ async function loadServiceEntries(entries, rootDir, cacheDir) {
30
30
  return [ctor, config];
31
31
  }));
32
32
  }
33
- async function loadRouteHandlers(routes, rootDir, cacheDir) {
34
- const cache = {};
35
- return Promise.all(routes.map(async (route) => {
36
- if (!route.routeHandler) {
37
- return route;
38
- }
39
- if (cache[route.routeHandler]) {
40
- return {
41
- ...route,
42
- routeHandler: cache[route.routeHandler],
43
- };
44
- }
45
- const routeHandler = await importModule(route.routeHandler, rootDir, path.join(cacheDir, 'routeHandlers'));
46
- cache[route.routeHandler] = routeHandler;
47
- return {
48
- ...route,
49
- routeHandler,
50
- };
51
- }));
52
- }
53
33
  /**
54
34
  * Import and instantiate hooks defined in the global config
55
35
  *
@@ -103,14 +83,15 @@ export async function loadServices(config) {
103
83
  * The optional dependency `esbuild` MUST be installed to load route handler modules.
104
84
  *
105
85
  * @param config - global config
106
- * @returns {Routes} all routes with resolved route handlers
86
+ * @returns {Routes} resolved route handlers mapped by id
107
87
  */
108
- export async function loadRoutes(config) {
109
- const routes = await loadRouteHandlers(config.routes, config.rootDir, config.cacheDir);
110
- const errorRoutes = await loadRouteHandlers(config.errorRoutes, config.rootDir, config.cacheDir);
111
- return {
112
- routes,
113
- errorRoutes,
114
- };
88
+ export async function loadRouteHandlers(config) {
89
+ const routeHandlers = {};
90
+ await Promise.all(Object.keys(config.routeHandlers).map(async (id) => {
91
+ const handlerPath = config.routeHandlers[id];
92
+ const handler = await importModule(handlerPath, config.rootDir, path.join(config.cacheDir, 'routeHandlers'));
93
+ routeHandlers[id] = handler;
94
+ }));
95
+ return routeHandlers;
115
96
  }
116
97
  //# sourceMappingURL=modules.js.map
@@ -1,5 +1,5 @@
1
1
  import { DEFAULT_LOCKER_TRUSTED_CMP, DEFAULT_LWR_LOCKER_CONFIG } from '@lwrjs/shared-utils';
2
- import { DEFAULT_BUNDLE_EXCLUSIONS, DEFAULT_GENERATOR_CONFIG, DEFAULT_LWR_MODULES } from '../defaults.js';
2
+ import { getDefaultBundleConfig, DEFAULT_GENERATOR_CONFIG, DEFAULT_LWR_MODULES } from '../defaults.js';
3
3
  function isNormalizedLwrGlobalConfig(config) {
4
4
  return config?.lwrVersion !== undefined;
5
5
  }
@@ -34,14 +34,22 @@ export function mergeLwcConfig(config1, config2) {
34
34
  }
35
35
  // merge default bundle exclusions with any bundle exclusions specified in config
36
36
  export function mergeBundleConfig(config1, config2) {
37
+ const mode = config2?.serverMode || config1?.serverMode;
38
+ const DEFAULT_BUNDLE_CONFIG = getDefaultBundleConfig(mode);
37
39
  const defaultExclusions = config2?.bundleConfig?.UNSAFE_lwrDefaultExclude ||
38
40
  config1?.bundleConfig?.UNSAFE_lwrDefaultExclude ||
39
- DEFAULT_BUNDLE_EXCLUSIONS;
41
+ DEFAULT_BUNDLE_CONFIG.exclude ||
42
+ [];
40
43
  const configExclusions = config2?.bundleConfig?.exclude || config1?.bundleConfig?.exclude || [];
44
+ const configExternals = config2?.bundleConfig?.external ||
45
+ config1?.bundleConfig?.external ||
46
+ DEFAULT_BUNDLE_CONFIG.external ||
47
+ {};
41
48
  return {
42
49
  ...config1?.bundleConfig,
43
50
  ...config2?.bundleConfig,
44
51
  exclude: [...new Set([...defaultExclusions, ...configExclusions])],
52
+ external: { ...configExternals },
45
53
  };
46
54
  }
47
55
  // merge default locker trusted namespaces/cmps with any trusted namespaces/cmps specified in config
@@ -1,7 +1,8 @@
1
- import type { LwrErrorRoute, LwrRoute, NormalizedLwrErrorRoute, NormalizedLwrRoute, ResourcePaths } from '@lwrjs/types';
1
+ import type { LwrErrorRoute, LwrRoute, NormalizedLwrErrorRoute, NormalizedLwrRoute, ResourcePaths, RouteHandlersConfig } from '@lwrjs/types';
2
2
  declare type ViewOrErrorRoute = LwrRoute | LwrErrorRoute;
3
3
  declare type NormalizedRoute<T extends ViewOrErrorRoute> = T extends LwrRoute ? NormalizedLwrRoute : NormalizedLwrErrorRoute;
4
- export declare function normalizeRoutes<T extends ViewOrErrorRoute>(routes?: T[]): NormalizedRoute<T>[];
4
+ export declare function normalizeRoutes<T extends ViewOrErrorRoute>(routes: T[], routeHandlers: RouteHandlersConfig): NormalizedRoute<T>[];
5
5
  export declare function normalizeRoutePaths<T extends ViewOrErrorRoute>(routes: T[] | undefined, resourcePaths: ResourcePaths): NormalizedRoute<T>[];
6
+ export declare function normalizeRouteHandlerPaths(routeHandlers: RouteHandlersConfig, resourcePaths: ResourcePaths): RouteHandlersConfig;
6
7
  export {};
7
8
  //# sourceMappingURL=routes.d.ts.map
@@ -1,25 +1,42 @@
1
1
  import path from 'path';
2
2
  import { DEFAULT_LWR_BOOTSTRAP_CONFIG, normalizeDirectory, normalizeResourcePath } from '@lwrjs/shared-utils';
3
- export function normalizeRoutes(routes = []) {
4
- return routes.map((route) => ({
5
- ...route,
6
- bootstrap: {
7
- ...DEFAULT_LWR_BOOTSTRAP_CONFIG,
8
- ...route.bootstrap,
9
- },
10
- }));
3
+ export function normalizeRoutes(routes, routeHandlers) {
4
+ return routes.map((route) => {
5
+ // when a route handler is set, ensure it is set in the route handler config
6
+ if (route.routeHandler && !routeHandlers[route.routeHandler]) {
7
+ const handlerPath = route.routeHandler;
8
+ // the non-normalized path to the handler will be the id used when invoking the route handler
9
+ routeHandlers[handlerPath] = handlerPath;
10
+ }
11
+ return {
12
+ ...route,
13
+ bootstrap: {
14
+ ...DEFAULT_LWR_BOOTSTRAP_CONFIG,
15
+ ...route.bootstrap,
16
+ },
17
+ };
18
+ });
11
19
  }
12
20
  export function normalizeRoutePaths(routes = [], resourcePaths) {
13
21
  return routes.map((route) => {
14
- const { routeHandler, contentTemplate, layoutTemplate, subRoutes } = route;
15
- route.routeHandler =
16
- routeHandler && path.resolve(normalizeDirectory(routeHandler, resourcePaths.rootDir));
17
- route.contentTemplate =
18
- contentTemplate && path.resolve(normalizeResourcePath(contentTemplate, resourcePaths));
19
- route.layoutTemplate =
20
- layoutTemplate && path.resolve(normalizeResourcePath(layoutTemplate, resourcePaths));
21
- route.subRoutes = subRoutes && path.resolve(normalizeResourcePath(subRoutes, resourcePaths));
22
+ // route handler paths are NOT normalized here to maintain the id lookup for route handler invocation
23
+ const { contentTemplate, layoutTemplate, subRoutes } = route;
24
+ if (contentTemplate) {
25
+ route.contentTemplate = path.resolve(normalizeResourcePath(contentTemplate, resourcePaths));
26
+ }
27
+ if (layoutTemplate) {
28
+ route.layoutTemplate = path.resolve(normalizeResourcePath(layoutTemplate, resourcePaths));
29
+ }
30
+ if (subRoutes) {
31
+ route.subRoutes = path.resolve(normalizeResourcePath(subRoutes, resourcePaths));
32
+ }
22
33
  return route;
23
34
  });
24
35
  }
36
+ export function normalizeRouteHandlerPaths(routeHandlers, resourcePaths) {
37
+ for (const [id, handlerPath] of Object.entries(routeHandlers)) {
38
+ routeHandlers[id] = path.resolve(normalizeDirectory(handlerPath, resourcePaths.rootDir));
39
+ }
40
+ return routeHandlers;
41
+ }
25
42
  //# sourceMappingURL=routes.js.map
@@ -1,4 +1,4 @@
1
- import { AssetDirConfig, AssetFileConfig, LwrErrorRoute, LwrRoute, NormalizedLwrGlobalConfig, NormalizedLwrAppBootstrapConfig, LwrLockerConfig } from '@lwrjs/types';
1
+ import { AssetDirConfig, AssetFileConfig, LwrErrorRoute, LwrRoute, NormalizedLwrGlobalConfig, NormalizedLwrAppBootstrapConfig, LwrLockerConfig, RouteHandlersConfig } from '@lwrjs/types';
2
2
  import { Node } from 'jsonc-parser';
3
3
  import { Diagnostic } from '@lwrjs/diagnostics';
4
4
  declare type RequiredAssetDirConfig = Required<AssetDirConfig>;
@@ -12,10 +12,11 @@ interface ConfigMap {
12
12
  assetFile: RequiredAssetFileConfig;
13
13
  routes: RequiredLwrRoute;
14
14
  errorRoutes: RequiredLwrErrorRoute;
15
+ routeHandlers: RouteHandlersConfig;
15
16
  bootstrap: NormalizedLwrAppBootstrapConfig;
16
17
  locker: RequiredLwrLockerConfig;
17
18
  }
18
- export declare const ROOT_ATTRIBUTE_KEYS: ["amdLoader", "apiVersion", "assets", "assetProviders", "assetTransformers", "bundleConfig", "cacheDir", "contentDir", "environment", "errorRoutes", "esmLoader", "staticSiteGenerator", "globalData", "globalDataDir", "hooks", "ignoreLwrConfigFile", "lwrConfigFile", "layoutsDir", "locker", "lwc", "lwrVersion", "moduleProviders", "port", "basePath", "resourceProviders", "rootDir", "routes", "serverMode", "serverType", "viewProviders", "viewTransformers"];
19
+ export declare const ROOT_ATTRIBUTE_KEYS: ["amdLoader", "apiVersion", "assets", "assetProviders", "assetTransformers", "bundleConfig", "cacheDir", "contentDir", "environment", "errorRoutes", "esmLoader", "staticSiteGenerator", "globalData", "globalDataDir", "hooks", "ignoreLwrConfigFile", "lwrConfigFile", "layoutsDir", "locker", "lwc", "lwrVersion", "moduleProviders", "port", "basePath", "resourceProviders", "rootDir", "routeHandlers", "routes", "serverMode", "serverType", "viewProviders", "viewTransformers"];
19
20
  export declare const ASSET_DIR_ATTRIBUTE_KEYS: ["alias", "dir", "urlPath"];
20
21
  export declare const ASSET_FILE_ATTRIBUTE_KEYS: ["alias", "file", "urlPath"];
21
22
  export declare const LOCKER_ATTRIBUTE_KEYS: ["enabled", "trustedComponents", "clientOnly"];
@@ -33,6 +33,7 @@ export const ROOT_ATTRIBUTE_KEYS = createKeys('root', [
33
33
  'basePath',
34
34
  'resourceProviders',
35
35
  'rootDir',
36
+ 'routeHandlers',
36
37
  'routes',
37
38
  'serverMode',
38
39
  'serverType',
@@ -106,6 +106,11 @@ function validateErrorRoutes(node, validationContext) {
106
106
  }
107
107
  }
108
108
  }
109
+ function validateRouteHandlers(node, validationContext) {
110
+ if (node) {
111
+ validationContext.assertIsObject(node, 'routeHandlers');
112
+ }
113
+ }
109
114
  /**
110
115
  * Check string config.asset OR
111
116
  * Check array config.assets[]:
@@ -196,6 +201,7 @@ function validateRoot(node, validationContext, preMerge) {
196
201
  validationContext.assertUniqueIds([...(routes?.children || []), ...(errorRoutes?.children || [])], 'routes');
197
202
  validateRoutes(routes, validationContext, preMerge);
198
203
  validateErrorRoutes(errorRoutes, validationContext);
204
+ validateRouteHandlers(findNode(node, ['routeHandlers']), validationContext);
199
205
  validateAssets(findNode(node, ['assets']), validationContext, preMerge);
200
206
  validateLocker(findNode(node, ['locker']), validationContext);
201
207
  validationContext.assertNotEmptyString(findNode(node, ['apiVersion']), 'apiVersion');
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0-alpha.17",
7
+ "version": "0.9.0-alpha.19",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -36,16 +36,16 @@
36
36
  "package.cjs"
37
37
  ],
38
38
  "dependencies": {
39
- "@lwrjs/diagnostics": "0.9.0-alpha.17",
40
- "@lwrjs/shared-utils": "0.9.0-alpha.17",
39
+ "@lwrjs/diagnostics": "0.9.0-alpha.19",
40
+ "@lwrjs/shared-utils": "0.9.0-alpha.19",
41
41
  "fs-extra": "^10.1.0",
42
42
  "jsonc-parser": "^3.0.0"
43
43
  },
44
44
  "devDependencies": {
45
- "@lwrjs/types": "0.9.0-alpha.17"
45
+ "@lwrjs/types": "0.9.0-alpha.19"
46
46
  },
47
47
  "engines": {
48
48
  "node": ">=14.15.4 <19"
49
49
  },
50
- "gitHead": "447417ff091802927b25cfc1bcb01da02264f41b"
50
+ "gitHead": "65623ff6f03a217321d7d342df391fb05c90eb19"
51
51
  }