@lwrjs/config 0.9.0-alpha.9 → 0.9.0

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.
Files changed (35) hide show
  1. package/build/cjs/defaults.cjs +40 -10
  2. package/build/cjs/{env-config.cjs → global-config.cjs} +71 -21
  3. package/build/cjs/hooks.cjs +69 -0
  4. package/build/cjs/index.cjs +11 -3
  5. package/build/cjs/modules.cjs +92 -0
  6. package/build/cjs/runtime-config.cjs +22 -3
  7. package/build/cjs/utils/global-data.cjs +54 -0
  8. package/build/cjs/utils/merge.cjs +22 -8
  9. package/build/cjs/utils/routes.cjs +30 -12
  10. package/build/cjs/validation/app-config-context.cjs +6 -2
  11. package/build/cjs/validation/app-config.cjs +14 -4
  12. package/build/es/defaults.d.ts +7 -2
  13. package/build/es/defaults.js +41 -9
  14. package/build/es/global-config.d.ts +65 -0
  15. package/build/es/{env-config.js → global-config.js} +92 -15
  16. package/build/es/hooks.d.ts +32 -0
  17. package/build/es/hooks.js +70 -0
  18. package/build/es/index.d.ts +5 -4
  19. package/build/es/index.js +5 -5
  20. package/build/es/modules.d.ts +32 -0
  21. package/build/es/modules.js +92 -0
  22. package/build/es/runtime-config.d.ts +12 -5
  23. package/build/es/runtime-config.js +30 -6
  24. package/build/es/utils/global-data.d.ts +3 -0
  25. package/build/es/utils/global-data.js +29 -0
  26. package/build/es/utils/merge.d.ts +1 -1
  27. package/build/es/utils/merge.js +28 -9
  28. package/build/es/utils/routes.d.ts +3 -2
  29. package/build/es/utils/routes.js +33 -16
  30. package/build/es/validation/app-config-context.d.ts +4 -3
  31. package/build/es/validation/app-config-context.js +5 -1
  32. package/build/es/validation/app-config.js +20 -5
  33. package/package.cjs +21 -2
  34. package/package.json +16 -9
  35. package/build/es/env-config.d.ts +0 -22
@@ -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
+ }
@@ -47,6 +47,7 @@ var ROOT_ATTRIBUTE_KEYS = createKeys("root", [
47
47
  "assetProviders",
48
48
  "assetTransformers",
49
49
  "bundleConfig",
50
+ "bundleProviders",
50
51
  "cacheDir",
51
52
  "contentDir",
52
53
  "environment",
@@ -67,10 +68,11 @@ var ROOT_ATTRIBUTE_KEYS = createKeys("root", [
67
68
  "basePath",
68
69
  "resourceProviders",
69
70
  "rootDir",
71
+ "routeHandlers",
70
72
  "routes",
71
73
  "serverMode",
74
+ "minify",
72
75
  "serverType",
73
- "templateEngine",
74
76
  "viewProviders",
75
77
  "viewTransformers"
76
78
  ]);
@@ -108,7 +110,9 @@ var BOOTSTRAP_ATTRIBUTE_KEYS = createKeys("bootstrap", [
108
110
  "workers",
109
111
  "services",
110
112
  "configAsSrc",
111
- "ssr"
113
+ "ssr",
114
+ "module",
115
+ "preloadModules"
112
116
  ]);
113
117
  var SPECIFIER_REGEX = /^@?[\w-]+(\/[\w-]+)*$/;
114
118
  function isNotEmptyString(node) {
@@ -41,9 +41,11 @@ function validateBootstrap(node, validationContext, propPrefix) {
41
41
  validationContext.assertIsObject(node, "bootstrap");
42
42
  validationContext.assertValidKeys(node, "bootstrap", import_app_config_context.BOOTSTRAP_ATTRIBUTE_KEYS);
43
43
  validationContext.assertArrayOfSpecifiers((0, import_jsonc_parser.findNodeAtLocation)(node, ["services"]), `${propPrefix}.services`);
44
+ validationContext.assertArrayOfSpecifiers((0, import_jsonc_parser.findNodeAtLocation)(node, ["preloadModules"]), `${propPrefix}.preloadModules`);
44
45
  validationContext.assertIsBoolean((0, import_jsonc_parser.findNodeAtLocation)(node, ["autoBoot"]), `${propPrefix}.autoBoot`);
45
46
  validationContext.assertIsBoolean((0, import_jsonc_parser.findNodeAtLocation)(node, ["ssr"]), `${propPrefix}.ssr`);
46
47
  validationContext.assertIsBoolean((0, import_jsonc_parser.findNodeAtLocation)(node, ["configAsSrc"]), `${propPrefix}.configAsSrc`);
48
+ validationContext.assertIsSpecifier((0, import_jsonc_parser.findNodeAtLocation)(node, ["module"]), `${propPrefix}.module`);
47
49
  validationContext.assertIsBoolean((0, import_jsonc_parser.findNodeAtLocation)(node, ["syntheticShadow"]), `${propPrefix}.syntheticShadow`);
48
50
  const workers = (0, import_jsonc_parser.findNodeAtLocation)(node, ["workers"]);
49
51
  if (workers && workers.children) {
@@ -68,9 +70,11 @@ function validateRouteCommon(node, validationContext, propPrefix) {
68
70
  validationContext.assertNotEmptyString((0, import_jsonc_parser.findNodeAtLocation)(node, ["routeHandler"]), `${propPrefix}.routeHandler`);
69
71
  validateBootstrap((0, import_jsonc_parser.findNodeAtLocation)(node, ["bootstrap"]), validationContext, `${propPrefix}.bootstrap`);
70
72
  }
71
- function validateRoutes(node, validationContext) {
73
+ function validateRoutes(node, validationContext, preMerge) {
72
74
  if (node) {
73
- validationContext.assertNotEmptyArray(node, "routes");
75
+ if (!preMerge) {
76
+ validationContext.assertNotEmptyArray(node, "routes");
77
+ }
74
78
  if (node.children) {
75
79
  node.children.forEach((n, index) => {
76
80
  const propPrefix = `routes[${index}]`;
@@ -99,6 +103,11 @@ function validateErrorRoutes(node, validationContext) {
99
103
  }
100
104
  }
101
105
  }
106
+ function validateRouteHandlers(node, validationContext) {
107
+ if (node) {
108
+ validationContext.assertIsObject(node, "routeHandlers");
109
+ }
110
+ }
102
111
  function validateAssets(node, validationContext, preMerge) {
103
112
  if (node) {
104
113
  if (preMerge && node.type === "string") {
@@ -143,8 +152,9 @@ function validateRoot(node, validationContext, preMerge) {
143
152
  const routes = (0, import_jsonc_parser.findNodeAtLocation)(node, ["routes"]);
144
153
  const errorRoutes = (0, import_jsonc_parser.findNodeAtLocation)(node, ["errorRoutes"]);
145
154
  validationContext.assertUniqueIds([...routes?.children || [], ...errorRoutes?.children || []], "routes");
146
- validateRoutes(routes, validationContext);
155
+ validateRoutes(routes, validationContext, preMerge);
147
156
  validateErrorRoutes(errorRoutes, validationContext);
157
+ validateRouteHandlers((0, import_jsonc_parser.findNodeAtLocation)(node, ["routeHandlers"]), validationContext);
148
158
  validateAssets((0, import_jsonc_parser.findNodeAtLocation)(node, ["assets"]), validationContext, preMerge);
149
159
  validateLocker((0, import_jsonc_parser.findNodeAtLocation)(node, ["locker"]), validationContext);
150
160
  validationContext.assertNotEmptyString((0, import_jsonc_parser.findNodeAtLocation)(node, ["apiVersion"]), "apiVersion");
@@ -161,7 +171,7 @@ function validateRoot(node, validationContext, preMerge) {
161
171
  validationContext.assertNotEmptyString((0, import_jsonc_parser.findNodeAtLocation)(node, ["layoutsDir"]), "layoutsDir");
162
172
  validationContext.assertNotEmptyString((0, import_jsonc_parser.findNodeAtLocation)(node, ["globalDataDir"]), "globalDataDir");
163
173
  validationContext.assertArrayOfServices((0, import_jsonc_parser.findNodeAtLocation)(node, ["hooks"]), "hooks");
164
- validationContext.assertNotEmptyString((0, import_jsonc_parser.findNodeAtLocation)(node, ["templateEngine"]), "templateEngine");
174
+ validationContext.assertArrayOfServices((0, import_jsonc_parser.findNodeAtLocation)(node, ["bundleProviders"]), "bundleProviders");
165
175
  validationContext.assertArrayOfServices((0, import_jsonc_parser.findNodeAtLocation)(node, ["moduleProviders"]), "moduleProviders");
166
176
  validationContext.assertArrayOfServices((0, import_jsonc_parser.findNodeAtLocation)(node, ["viewProviders"]), "viewProviders");
167
177
  validationContext.assertArrayOfServices((0, import_jsonc_parser.findNodeAtLocation)(node, ["resourceProviders"]), "resourceProviders");
@@ -1,12 +1,17 @@
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: {
5
5
  outputDir: string;
6
6
  locales: string[];
7
+ skipCleanOutputDir: boolean;
8
+ skipBaseDocumentGeneration: boolean;
9
+ _additionalModules: never[];
7
10
  _additionalRoutePaths: never[];
8
11
  };
9
12
  export declare const DEFAULT_ROOT_DIR: string;
13
+ export declare const SSR_MODULE_PROVIDER = "@lwrjs/lwc-ssr/moduleProvider";
14
+ export declare const SSR_VIEW_TRANSFORM_PLUGIN = "@lwrjs/lwc-ssr/viewTransformer";
10
15
  export declare const DEFAULT_AMD_LOADER = "lwr/loader";
11
16
  export declare const DEFAULT_AMD_LOADER_LEGACY = "lwr/loaderLegacy";
12
17
  export declare const DEFAULT_ESM_LOADER = "lwr/esmLoader";
@@ -14,6 +19,6 @@ export declare const DEFAULT_SERVICE_PACKAGE_NAME: RegExp;
14
19
  export declare const DEFAULT_LWR_MODULES: {
15
20
  npm: string;
16
21
  }[];
17
- export declare const DEFAULT_BUNDLE_EXCLUSIONS: string[];
22
+ export declare function getDefaultBundleConfig(mode?: string): BundleConfig;
18
23
  export declare const DEFAULT_LWR_CONFIG: Required<LwrGlobalConfig>;
19
24
  //# sourceMappingURL=defaults.d.ts.map
@@ -1,15 +1,19 @@
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
- const MODE = process.env.MODE || 'dev';
5
+ const MODE = process.env.MODE || 'prod';
5
6
  const DEFAULT_BASE_PATH = '';
6
7
  const DEFAULT_API_VERSION = '1';
7
8
  export const LWR_VERSION = version;
8
9
  const DEFAULT_SERVER_TYPE = 'express';
9
10
  export const DEFAULT_LWR_CONFIG_JSON = '$rootDir/lwr.config.json';
10
11
  export const DEFAULT_GENERATOR_CONFIG = {
11
- outputDir: '__generated_site__',
12
+ outputDir: 'site',
12
13
  locales: ['en-US'],
14
+ skipCleanOutputDir: false,
15
+ skipBaseDocumentGeneration: false,
16
+ _additionalModules: [],
13
17
  _additionalRoutePaths: [],
14
18
  };
15
19
  // Directories
@@ -22,11 +26,11 @@ const DEFAULT_DATA_DIR = '$rootDir/src/data';
22
26
  // Providers
23
27
  const DEFAULT_MODULE_PROVIDERS = [
24
28
  '@lwrjs/app-service/moduleProvider',
25
- '@lwrjs/lwc-ssr/moduleProvider',
26
29
  '@lwrjs/router/module-provider',
27
30
  '@lwrjs/lwc-module-provider',
28
31
  '@lwrjs/npm-module-provider',
29
32
  ];
33
+ export const SSR_MODULE_PROVIDER = '@lwrjs/lwc-ssr/moduleProvider';
30
34
  const DEFAULT_RESOURCE_PROVIDERS = ['@lwrjs/loader'];
31
35
  const DEFAULT_VIEW_PROVIDERS = [
32
36
  '@lwrjs/nunjucks-view-provider',
@@ -34,10 +38,14 @@ const DEFAULT_VIEW_PROVIDERS = [
34
38
  '@lwrjs/markdown-view-provider',
35
39
  '@lwrjs/base-view-provider',
36
40
  ];
37
- const DEFAULT_VIEW_TRANSFORM_PLUGINS = ['@lwrjs/base-view-transformer', '@lwrjs/lwc-ssr/viewTransformer'];
41
+ const DEFAULT_BUNDLE_PROVIDERS = [
42
+ '@lwrjs/module-bundler/amd-bundle-provider',
43
+ '@lwrjs/module-bundler/esm-bundle-provider',
44
+ ];
45
+ const DEFAULT_VIEW_TRANSFORM_PLUGINS = ['@lwrjs/base-view-transformer'];
46
+ export const SSR_VIEW_TRANSFORM_PLUGIN = '@lwrjs/lwc-ssr/viewTransformer';
38
47
  const DEFAULT_ASSET_PROVIDERS = ['@lwrjs/fs-asset-provider'];
39
48
  const DEFAULT_ASSET_TRANSFORM_PLUGINS = ['@lwrjs/asset-transformer'];
40
- const DEFAULT_TEMPLATE_ENGINE = '@lwrjs/base-template-engine';
41
49
  export const DEFAULT_AMD_LOADER = 'lwr/loader';
42
50
  export const DEFAULT_AMD_LOADER_LEGACY = 'lwr/loaderLegacy';
43
51
  export const DEFAULT_ESM_LOADER = 'lwr/esmLoader';
@@ -50,13 +58,35 @@ export const DEFAULT_LWR_MODULES = [
50
58
  { npm: '@lwrjs/router' },
51
59
  { npm: '@lwc/synthetic-shadow' },
52
60
  ];
53
- export const DEFAULT_BUNDLE_EXCLUSIONS = [
61
+ const DEFAULT_ESM_BUNDLE_EXCLUSIONS = [
54
62
  'lwc',
55
63
  '@lwc/synthetic-shadow',
56
64
  'lwr/navigation',
57
65
  'lwr/esmLoader',
58
66
  'lwr/profiler',
59
67
  ];
68
+ const DEFAULT_AMD_BUNDLE_EXCLUSIONS = ['lwc', 'lwr/navigation'];
69
+ const DEFAULT_ESM_BUNDLE_EXTERNALS = {};
70
+ const DEFAULT_AMD_BUNDLE_EXTERNALS = {
71
+ 'lwr/loader': 'lwr-loader-shim.bundle.min.js',
72
+ 'lwr/profiler': 'lwr-loader-shim.bundle.min.js',
73
+ };
74
+ const DEFAULT_AMD_LEGACY_BUNDLE_EXTERNALS = {
75
+ 'lwr/loaderLegacy': 'lwr-loader-shim-legacy.bundle.min.js',
76
+ 'lwr/profiler': 'lwr-loader-shim-legacy.bundle.min.js',
77
+ };
78
+ export function getDefaultBundleConfig(mode) {
79
+ const format = getServerModeConfig(mode || MODE).format;
80
+ const loaderLegacy = getFeatureFlags().LEGACY_LOADER;
81
+ return {
82
+ exclude: format === 'esm' ? DEFAULT_ESM_BUNDLE_EXCLUSIONS : DEFAULT_AMD_BUNDLE_EXCLUSIONS,
83
+ external: format === 'esm'
84
+ ? DEFAULT_ESM_BUNDLE_EXTERNALS
85
+ : loaderLegacy
86
+ ? DEFAULT_AMD_LEGACY_BUNDLE_EXTERNALS
87
+ : DEFAULT_AMD_BUNDLE_EXTERNALS,
88
+ };
89
+ }
60
90
  export const DEFAULT_LWR_CONFIG = {
61
91
  port: PORT,
62
92
  ignoreLwrConfigFile: false,
@@ -65,6 +95,7 @@ export const DEFAULT_LWR_CONFIG = {
65
95
  rootDir: DEFAULT_ROOT_DIR,
66
96
  cacheDir: DEFAULT_CACHE_FOLDER,
67
97
  serverMode: MODE,
98
+ minify: null,
68
99
  apiVersion: DEFAULT_API_VERSION,
69
100
  assets: DEFAULT_ASSETS_DIR,
70
101
  assetProviders: DEFAULT_ASSET_PROVIDERS,
@@ -75,16 +106,17 @@ export const DEFAULT_LWR_CONFIG = {
75
106
  globalDataDir: DEFAULT_DATA_DIR,
76
107
  globalData: {},
77
108
  hooks: [],
109
+ bundleProviders: DEFAULT_BUNDLE_PROVIDERS,
78
110
  moduleProviders: DEFAULT_MODULE_PROVIDERS,
79
111
  resourceProviders: DEFAULT_RESOURCE_PROVIDERS,
80
112
  viewProviders: DEFAULT_VIEW_PROVIDERS,
81
113
  viewTransformers: DEFAULT_VIEW_TRANSFORM_PLUGINS,
82
- templateEngine: DEFAULT_TEMPLATE_ENGINE,
83
114
  environment: {},
84
115
  lwc: { modules: [] },
85
116
  routes: [],
86
117
  errorRoutes: [],
87
- bundleConfig: { exclude: DEFAULT_BUNDLE_EXCLUSIONS },
118
+ routeHandlers: {},
119
+ bundleConfig: getDefaultBundleConfig(MODE),
88
120
  serverType: DEFAULT_SERVER_TYPE,
89
121
  locker: DEFAULT_LWR_LOCKER_CONFIG,
90
122
  };
@@ -0,0 +1,65 @@
1
+ import type { GlobalData, LwrGlobalConfig, NormalizedLwrGlobalConfig, RuntimeEnvironment } from '@lwrjs/types';
2
+ export interface ResolveConfigOptions {
3
+ skipDirNormalization?: boolean;
4
+ skipCacheDirCreation?: boolean;
5
+ useStaticProviders?: boolean;
6
+ }
7
+ export interface Configurations {
8
+ appConfig: NormalizedLwrGlobalConfig;
9
+ runtimeEnvironment: RuntimeEnvironment;
10
+ globalData: GlobalData;
11
+ }
12
+ /**
13
+ * Load and validate the global config file.
14
+ *
15
+ * @remarks
16
+ * By default, this file is expected to be named `lwr.config.json` and be located at the root directory.
17
+ * This default expectation can be override with the `lwrConfigFile` configuration.
18
+ *
19
+ * @throws {LwrConfigValidationError} Validation errors will be swallowed when the
20
+ * `UNSAFE_IGNORE_CONFIG_VALIDATION` flag is set.
21
+ *
22
+ * @param {string} lwrConfigPath - config file path override
23
+ * @param {string} rootDir - the directory used to resolve `$rootDir`
24
+ * @returns {LwrGlobalConfig | undefined} the validated config file contents if it exists
25
+ */
26
+ export declare function getLwrConfigFromFile(rootDir: string, lwrConfigPath?: string): LwrGlobalConfig | undefined;
27
+ /**
28
+ * Add the SSR module provider and view transformer, if any route has `ssr` turned on.
29
+ *
30
+ * @param {NormalizedLwrGlobalConfig} config - a normalized global config to check / mutate
31
+ * @returns {NormalizedLwrGlobalConfig} an SSR-enabled normalized global config, if needed
32
+ */
33
+ export declare function applySsrConfig(config: NormalizedLwrGlobalConfig): NormalizedLwrGlobalConfig;
34
+ /**
35
+ * Apply Static Providers for MRT runtime (used exclusively by buildServer)
36
+ *
37
+ * @param {NormalizedLwrGlobalConfig} config - a normalized global config to check / mutate
38
+ * @returns {NormalizedLwrGlobalConfig} an SSR-enabled normalized global config, if needed
39
+ */
40
+ export declare function applyStaticProviderConfig(config: NormalizedLwrGlobalConfig): NormalizedLwrGlobalConfig;
41
+ /**
42
+ * Load, merge, and normalize all of the config sources.
43
+ *
44
+ * @remarks
45
+ * Config hooks are not loaded or applied during config resolution.
46
+ *
47
+ * @privateRemarks
48
+ * The `skipDirNormalization` and `skipCacheDirCreation` options are useful for generating
49
+ * a portable global config. The directories can be normalized and the cache directory can
50
+ * be created at runtime.
51
+ *
52
+ * @param {LwrGlobalConfig} configArg - programmatic global config
53
+ * @param {ResolveConfigOptions} options - config resolution mutations
54
+ * @returns {NormalizedLwrGlobalConfig} a normalized global config relative to the provided options
55
+ */
56
+ export declare function resolveGlobalConfig(configArg?: LwrGlobalConfig, options?: ResolveConfigOptions): NormalizedLwrGlobalConfig;
57
+ /**
58
+ * Resolve all application configurations by loading and merging global data, applying config hooks, and
59
+ * importing configurable paths to javascript(ie. services and route handlers).
60
+ *
61
+ * @param {LwrGlobalConfig} config - programmatic global config
62
+ * @returns {Configurations} all of the fully resolved configurations
63
+ */
64
+ export declare function loadConfig(config?: LwrGlobalConfig, options?: ResolveConfigOptions): Configurations;
65
+ //# sourceMappingURL=global-config.d.ts.map
@@ -1,14 +1,17 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
+ import { parse } from 'jsonc-parser';
3
4
  import { readFile, normalizeDirectory, getFeatureFlags, logger, ASSETS_CACHE_DIR } from '@lwrjs/shared-utils';
5
+ import { SSR_MODULE_PROVIDER, SSR_VIEW_TRANSFORM_PLUGIN } from './defaults.js';
4
6
  import { DEFAULT_AMD_LOADER, DEFAULT_AMD_LOADER_LEGACY, DEFAULT_ESM_LOADER, DEFAULT_LWR_CONFIG, DEFAULT_LWR_CONFIG_JSON, DEFAULT_ROOT_DIR, LWR_VERSION, } from './defaults.js';
5
7
  import { validateLwrAppConfig } from './validation/app-config.js';
6
8
  import { normalizeAssetPaths, normalizeAssets } from './utils/assets.js';
7
9
  import { normalizeServicePaths, normalizeServices } from './utils/services.js';
8
- import { normalizeRoutePaths, normalizeRoutes } from './utils/routes.js';
10
+ import { normalizeRouteHandlerPaths, normalizeRoutePaths, normalizeRoutes } from './utils/routes.js';
9
11
  import { mergeBundleConfig, mergeLockerConfig, mergeLwcConfig, mergeStaticGenerationConfig, trimLwrConfig, } from './utils/merge.js';
10
12
  import { normalizeLwcConfig, normalizeModulePaths } from './utils/lwc.js';
11
- import { parse } from 'jsonc-parser';
13
+ import { getRuntimeEnvironment } from './runtime-config.js';
14
+ import { getGlobalData } from './utils/global-data.js';
12
15
  /**
13
16
  * Load and validate the global config file.
14
17
  *
@@ -23,7 +26,7 @@ import { parse } from 'jsonc-parser';
23
26
  * @param {string} rootDir - the directory used to resolve `$rootDir`
24
27
  * @returns {LwrGlobalConfig | undefined} the validated config file contents if it exists
25
28
  */
26
- function getLwrConfigFromFile(rootDir, lwrConfigPath = DEFAULT_LWR_CONFIG_JSON) {
29
+ export function getLwrConfigFromFile(rootDir, lwrConfigPath = DEFAULT_LWR_CONFIG_JSON) {
27
30
  const resolvedLwrConfigPath = path.resolve(normalizeDirectory(lwrConfigPath, rootDir));
28
31
  if (!fs.existsSync(resolvedLwrConfigPath)) {
29
32
  logger.warn(`LWR Config not found on "${resolvedLwrConfigPath}"`);
@@ -59,7 +62,7 @@ function createCacheFolder(cache, rootDir) {
59
62
  * @param {LwrGlobalConfig} configArg - programmatic global config
60
63
  * @returns {Required<LwrGlobalConfig>} a complete(but not normalized) global config
61
64
  */
62
- function loadConfig(configArg) {
65
+ function mergeConfig(configArg) {
63
66
  // process programmatic config
64
67
  if (configArg) {
65
68
  // the programmatic config is trimmed to prevent overriding properties with an undefined value
@@ -110,15 +113,16 @@ function normalizeConfig(config) {
110
113
  lwc: normalizeLwcConfig(config.lwc),
111
114
  // normalize all services
112
115
  hooks: normalizeServices(config.hooks),
116
+ bundleProviders: normalizeServices(config.bundleProviders),
113
117
  moduleProviders: normalizeServices(config.moduleProviders),
114
118
  assetProviders: normalizeServices(config.assetProviders),
115
119
  assetTransformers: normalizeServices(config.assetTransformers),
116
120
  resourceProviders: normalizeServices(config.resourceProviders),
117
121
  viewProviders: normalizeServices(config.viewProviders),
118
122
  viewTransformers: normalizeServices(config.viewTransformers),
119
- // normalize all routes
120
- routes: normalizeRoutes(config.routes),
121
- errorRoutes: normalizeRoutes(config.errorRoutes),
123
+ // normalize routes and capture all route handlers
124
+ routes: normalizeRoutes(config.routes, config.routeHandlers),
125
+ errorRoutes: normalizeRoutes(config.errorRoutes, config.routeHandlers),
122
126
  };
123
127
  }
124
128
  /**
@@ -160,6 +164,53 @@ function normalizeConfigPaths(config) {
160
164
  // normalize all route paths
161
165
  routes: normalizeRoutePaths(config.routes, resourcePaths),
162
166
  errorRoutes: normalizeRoutePaths(config.errorRoutes, resourcePaths),
167
+ routeHandlers: normalizeRouteHandlerPaths(config.routeHandlers, resourcePaths),
168
+ };
169
+ }
170
+ /**
171
+ * Add the SSR module provider and view transformer, if any route has `ssr` turned on.
172
+ *
173
+ * @param {NormalizedLwrGlobalConfig} config - a normalized global config to check / mutate
174
+ * @returns {NormalizedLwrGlobalConfig} an SSR-enabled normalized global config, if needed
175
+ */
176
+ export function applySsrConfig(config) {
177
+ // check if any route has SSR turned on
178
+ const hasSSR = config.routes.some((r) => r.bootstrap.ssr);
179
+ if (!hasSSR) {
180
+ return config;
181
+ }
182
+ // add the SSR configuration, if it's not already there
183
+ // note: this is a noop when called from an onStart hook of an LWR@MRT app,
184
+ // since the app has already been built / rolled up
185
+ const hasSsrModuleProvider = config.moduleProviders.some(([m]) => m === SSR_MODULE_PROVIDER);
186
+ const hasSsrViewTransformer = config.viewTransformers.some(([v]) => v === SSR_VIEW_TRANSFORM_PLUGIN);
187
+ return {
188
+ ...config,
189
+ moduleProviders: hasSsrModuleProvider
190
+ ? config.moduleProviders
191
+ : [[SSR_MODULE_PROVIDER, undefined], ...config.moduleProviders],
192
+ viewTransformers: hasSsrViewTransformer
193
+ ? config.viewTransformers
194
+ : [...config.viewTransformers, [SSR_VIEW_TRANSFORM_PLUGIN, undefined]],
195
+ };
196
+ }
197
+ /**
198
+ * Apply Static Providers for MRT runtime (used exclusively by buildServer)
199
+ *
200
+ * @param {NormalizedLwrGlobalConfig} config - a normalized global config to check / mutate
201
+ * @returns {NormalizedLwrGlobalConfig} an SSR-enabled normalized global config, if needed
202
+ */
203
+ export function applyStaticProviderConfig(config) {
204
+ return {
205
+ ...config,
206
+ resourceProviders: [['@lwrjs/lambda/mrt-static-resource-provider', {}]],
207
+ moduleProviders: [['@lwrjs/lambda/mrt-static-module-provider', undefined]],
208
+ assetProviders: [['@lwrjs/lambda/mrt-static-asset-provider', undefined]],
209
+ bundleProviders: [
210
+ ['@lwrjs/lambda/mrt-static-bundle-provider', undefined],
211
+ ['@lwrjs/module-bundler/amd-runtime-bundle-provider', undefined],
212
+ ],
213
+ assetTransformers: [],
163
214
  };
164
215
  }
165
216
  /**
@@ -177,19 +228,45 @@ function normalizeConfigPaths(config) {
177
228
  * @param {ResolveConfigOptions} options - config resolution mutations
178
229
  * @returns {NormalizedLwrGlobalConfig} a normalized global config relative to the provided options
179
230
  */
180
- export function resolveConfig(configArg, options) {
181
- const skipDirNormalization = options?.skipDirNormalization;
182
- const skipCacheDirCreation = options?.skipDirNormalization;
183
- const mergedConfig = loadConfig(configArg);
184
- const normalizedConfig = normalizeConfig(mergedConfig);
231
+ export function resolveGlobalConfig(configArg, options) {
232
+ const mergedConfig = mergeConfig(configArg);
233
+ let normalizedConfig = normalizeConfig(mergedConfig);
234
+ // Add static providers if applicable
235
+ if (options?.useStaticProviders) {
236
+ normalizedConfig = applyStaticProviderConfig(normalizedConfig);
237
+ }
238
+ // Add SSR config if applicable
239
+ normalizedConfig = applySsrConfig(normalizedConfig);
185
240
  // skip cache dir creation when the option is set
186
- if (!skipCacheDirCreation) {
241
+ if (!options?.skipCacheDirCreation) {
187
242
  createCacheFolder(normalizedConfig.cacheDir, normalizedConfig.rootDir);
188
243
  }
189
244
  // skip dir normalization when the option is set
190
- if (skipDirNormalization) {
245
+ if (options?.skipDirNormalization) {
191
246
  return normalizedConfig;
192
247
  }
193
248
  return normalizeConfigPaths(normalizedConfig);
194
249
  }
195
- //# sourceMappingURL=env-config.js.map
250
+ /**
251
+ * Resolve all application configurations by loading and merging global data, applying config hooks, and
252
+ * importing configurable paths to javascript(ie. services and route handlers).
253
+ *
254
+ * @param {LwrGlobalConfig} config - programmatic global config
255
+ * @returns {Configurations} all of the fully resolved configurations
256
+ */
257
+ export function loadConfig(config, options) {
258
+ const appConfig = resolveGlobalConfig(config, options);
259
+ const runtimeEnvironment = getRuntimeEnvironment(appConfig);
260
+ const globalData = getGlobalData(appConfig.globalDataDir, appConfig.globalData);
261
+ // ensure post config hook validation is ran even if config hooks don't exist
262
+ if (!appConfig.hooks.length) {
263
+ // route length validation happens in the `post` phase of validation
264
+ validateLwrAppConfig(appConfig, 'post');
265
+ }
266
+ return {
267
+ appConfig,
268
+ runtimeEnvironment,
269
+ globalData,
270
+ };
271
+ }
272
+ //# sourceMappingURL=global-config.js.map
@@ -0,0 +1,32 @@
1
+ import type { GlobalData, HooksPlugin, NormalizedLwrGlobalConfig, RuntimeEnvironment } from '@lwrjs/types';
2
+ /**
3
+ * Run `initConfigs` hooks
4
+ *
5
+ * @remarks
6
+ * Route normalization and validation will be executed after all config hooks have been executed.
7
+ *
8
+ * @privateRemarks
9
+ * Changes to configurations are made by reference.
10
+ *
11
+ * @param hooks - hooks plugins
12
+ * @param globalConfig - global configuration
13
+ * @param runtimeEnvironment - runtime environment resolved from programmatic config and config file
14
+ * @param globalData - resolved global data
15
+ */
16
+ export declare function executeConfigHooks(hooks: HooksPlugin[], globalConfig: NormalizedLwrGlobalConfig, runtimeEnvironment: RuntimeEnvironment, globalData: GlobalData, skipValidate?: boolean): Promise<void>;
17
+ /**
18
+ * Run `onStart` hooks
19
+ *
20
+ * @remarks
21
+ * Route normalization and validation will be executed after all config hooks have been executed.
22
+ *
23
+ * @privateRemarks
24
+ * Changes to configurations are made by reference.
25
+ *
26
+ * These hooks can only modify the `routes` property in the global config.
27
+ *
28
+ * @param hooks - hooks plugins
29
+ * @param globalConfig - global configuration
30
+ */
31
+ export declare function executeStartHooks(hooks: HooksPlugin[], globalConfig: NormalizedLwrGlobalConfig, skipValidate?: boolean): void;
32
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1,70 @@
1
+ import { normalizeRoutes } from './utils/routes.js';
2
+ import { validateLwrAppConfig } from './validation/app-config.js';
3
+ import { applySsrConfig } from './global-config.js';
4
+ /**
5
+ * Run `initConfigs` hooks
6
+ *
7
+ * @remarks
8
+ * Route normalization and validation will be executed after all config hooks have been executed.
9
+ *
10
+ * @privateRemarks
11
+ * Changes to configurations are made by reference.
12
+ *
13
+ * @param hooks - hooks plugins
14
+ * @param globalConfig - global configuration
15
+ * @param runtimeEnvironment - runtime environment resolved from programmatic config and config file
16
+ * @param globalData - resolved global data
17
+ */
18
+ export async function executeConfigHooks(hooks, globalConfig, runtimeEnvironment, globalData, skipValidate = false) {
19
+ if (!hooks.length) {
20
+ return;
21
+ }
22
+ for (const hook of hooks) {
23
+ if (!hook.initConfigs) {
24
+ continue;
25
+ }
26
+ // eslint-disable-next-line no-await-in-loop
27
+ await hook.initConfigs(globalConfig, globalData, runtimeEnvironment);
28
+ }
29
+ globalConfig.routes = normalizeRoutes(globalConfig.routes, globalConfig.routeHandlers);
30
+ globalConfig.errorRoutes = normalizeRoutes(globalConfig.errorRoutes, globalConfig.routeHandlers);
31
+ const ssrConfig = applySsrConfig(globalConfig);
32
+ globalConfig.moduleProviders = ssrConfig.moduleProviders;
33
+ globalConfig.viewTransformers = ssrConfig.viewTransformers;
34
+ if (!skipValidate) {
35
+ validateLwrAppConfig(globalConfig, 'post');
36
+ }
37
+ }
38
+ /**
39
+ * Run `onStart` hooks
40
+ *
41
+ * @remarks
42
+ * Route normalization and validation will be executed after all config hooks have been executed.
43
+ *
44
+ * @privateRemarks
45
+ * Changes to configurations are made by reference.
46
+ *
47
+ * These hooks can only modify the `routes` property in the global config.
48
+ *
49
+ * @param hooks - hooks plugins
50
+ * @param globalConfig - global configuration
51
+ */
52
+ export function executeStartHooks(hooks, globalConfig, skipValidate = false) {
53
+ if (!hooks.length) {
54
+ return;
55
+ }
56
+ for (const hook of hooks) {
57
+ if (!hook.onStart) {
58
+ continue;
59
+ }
60
+ hook.onStart(globalConfig.routes);
61
+ }
62
+ globalConfig.routes = normalizeRoutes(globalConfig.routes, globalConfig.routeHandlers);
63
+ const ssrConfig = applySsrConfig(globalConfig);
64
+ globalConfig.moduleProviders = ssrConfig.moduleProviders;
65
+ globalConfig.viewTransformers = ssrConfig.viewTransformers;
66
+ if (!skipValidate) {
67
+ validateLwrAppConfig(globalConfig, 'post');
68
+ }
69
+ }
70
+ //# sourceMappingURL=hooks.js.map
@@ -1,5 +1,6 @@
1
- import { resolveConfig as normalizeConfig } from './env-config.js';
2
- import { getServerModeConfig as explodeMode } from './runtime-config.js';
3
- import { validateLwrAppConfig } from './validation/app-config.js';
4
- export { normalizeConfig, explodeMode, validateLwrAppConfig };
1
+ export { version as LWR_VERSION, lwcVersion as LWC_VERSION } from '@lwrjs/config/package';
2
+ export { loadConfig, resolveGlobalConfig as normalizeConfig, getLwrConfigFromFile } from './global-config.js';
3
+ export { getRuntimeEnvironment } from './runtime-config.js';
4
+ export { executeConfigHooks, executeStartHooks } from './hooks.js';
5
+ export { validateLwrAppConfig } from './validation/app-config.js';
5
6
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { resolveConfig as normalizeConfig } from './env-config.js';
2
- import { getServerModeConfig as explodeMode } from './runtime-config.js';
3
- import { validateLwrAppConfig } from './validation/app-config.js';
4
- // Only export the functions we utilize internally
5
- export { normalizeConfig, explodeMode, validateLwrAppConfig };
1
+ export { version as LWR_VERSION, lwcVersion as LWC_VERSION } from '@lwrjs/config/package';
2
+ export { loadConfig, resolveGlobalConfig as normalizeConfig, getLwrConfigFromFile } from './global-config.js';
3
+ export { getRuntimeEnvironment } from './runtime-config.js';
4
+ export { executeConfigHooks, executeStartHooks } from './hooks.js';
5
+ export { validateLwrAppConfig } from './validation/app-config.js';
6
6
  //# sourceMappingURL=index.js.map