@lwrjs/config 0.8.0-alpha.3

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.
package/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2020, Salesforce.com, Inc.
4
+ All rights reserved.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,339 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/config/src/env-config.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ explodeMode: () => explodeMode,
28
+ normalizeConfig: () => normalizeConfig
29
+ });
30
+ var import_fs = __toModule(require("fs"));
31
+ var import_path = __toModule(require("path"));
32
+ var import_module = __toModule(require("module"));
33
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
34
+ var import_package = __toModule(require("@lwrjs/config/package"));
35
+ var import_app_config = __toModule(require("./validation/app-config.cjs"));
36
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
37
+ var PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3e3;
38
+ var MODE = process.env.MODE || "dev";
39
+ var DEFAULT_BASE_PATH = "";
40
+ var DEFAULT_API_VERSION = "1";
41
+ var LWR_VERSION = import_package.version;
42
+ var DEFAULT_SERVER_TYPE = "express";
43
+ var DEFAULT_LWR_CONFIG_JSON = "$rootDir/lwr.config.json";
44
+ var DEFAULT_GENERATOR_CONFIG = {
45
+ outputDir: "__generated_site__",
46
+ locales: ["en-US"],
47
+ _additionalRoutePaths: []
48
+ };
49
+ var DEFAULT_ROOT_DIR = process.cwd();
50
+ var DEFAULT_CACHE_FOLDER = "$rootDir/__lwr_cache__";
51
+ var DEFAULT_ASSETS_DIR = "$rootDir/src/assets";
52
+ var DEFAULT_CONTENT_DIR = "$rootDir/src/content";
53
+ var DEFAULT_LAYOUTS_DIR = "$rootDir/src/layouts";
54
+ var DEFAULT_DATA_DIR = "$rootDir/src/data";
55
+ var DEFAULT_MODULE_PROVIDERS = [
56
+ "@lwrjs/app-service/moduleProvider",
57
+ "@lwrjs/lwc-ssr/moduleProvider",
58
+ "@lwrjs/router/module-provider",
59
+ "@lwrjs/lwc-module-provider",
60
+ "@lwrjs/npm-module-provider"
61
+ ];
62
+ var DEFAULT_RESOURCE_PROVIDERS = ["@lwrjs/loader"];
63
+ var DEFAULT_VIEW_PROVIDERS = [
64
+ "@lwrjs/nunjucks-view-provider",
65
+ "@lwrjs/html-view-provider",
66
+ "@lwrjs/markdown-view-provider",
67
+ "@lwrjs/base-view-provider"
68
+ ];
69
+ var DEFAULT_VIEW_TRANFORM_PLUGINS = ["@lwrjs/base-view-transformer", "@lwrjs/lwc-ssr/viewTransformer"];
70
+ var DEFAULT_ASSET_PROVIDERS = ["@lwrjs/fs-asset-provider"];
71
+ var DEFAULT_ASSET_TRANFORM_PLUGINS = ["@lwrjs/asset-transformer"];
72
+ var DEFAULT_TEMPLATE_ENGINE = "@lwrjs/base-template-engine";
73
+ var DEFAULT_AMD_LOADER = "lwr/loader";
74
+ var DEFAULT_AMD_LOADER_LEGACY = "lwr/loaderLegacy";
75
+ var DEFAULT_ESM_LOADER = "lwr/esmLoader";
76
+ var DEFAULT_SERVICE_PACKAGE_NAME = /lwr-(\w+)-service/;
77
+ var DEFAULT_LWR_MODULES = [
78
+ {npm: getLWCEngineSpecifier()},
79
+ {npm: "@lwrjs/client-modules"},
80
+ {npm: "@lwrjs/loader"},
81
+ {npm: "@lwrjs/o11y"},
82
+ {npm: "@lwrjs/router"},
83
+ {npm: "@lwc/synthetic-shadow"}
84
+ ];
85
+ var DEFAULT_BUNDLE_EXCLUSIONS = [
86
+ "lwc",
87
+ "@lwc/synthetic-shadow",
88
+ "lwr/navigation",
89
+ "lwr/esmLoader",
90
+ "lwr/profiler"
91
+ ];
92
+ var DEFAULT_LWR_CONFIG = {
93
+ port: PORT,
94
+ ignoreLwrConfigFile: false,
95
+ lwrConfigFile: DEFAULT_LWR_CONFIG_JSON,
96
+ basePath: DEFAULT_BASE_PATH,
97
+ rootDir: DEFAULT_ROOT_DIR,
98
+ cacheDir: DEFAULT_CACHE_FOLDER,
99
+ serverMode: MODE,
100
+ apiVersion: DEFAULT_API_VERSION,
101
+ assets: DEFAULT_ASSETS_DIR,
102
+ assetProviders: DEFAULT_ASSET_PROVIDERS,
103
+ assetTransformers: DEFAULT_ASSET_TRANFORM_PLUGINS,
104
+ contentDir: DEFAULT_CONTENT_DIR,
105
+ layoutsDir: DEFAULT_LAYOUTS_DIR,
106
+ staticSiteGenerator: DEFAULT_GENERATOR_CONFIG,
107
+ globalDataDir: DEFAULT_DATA_DIR,
108
+ globalData: {},
109
+ hooks: [],
110
+ moduleProviders: DEFAULT_MODULE_PROVIDERS,
111
+ resourceProviders: DEFAULT_RESOURCE_PROVIDERS,
112
+ viewProviders: DEFAULT_VIEW_PROVIDERS,
113
+ viewTransformers: DEFAULT_VIEW_TRANFORM_PLUGINS,
114
+ templateEngine: DEFAULT_TEMPLATE_ENGINE,
115
+ environment: {},
116
+ lwc: {modules: []},
117
+ routes: [],
118
+ errorRoutes: [],
119
+ bundleConfig: {exclude: DEFAULT_BUNDLE_EXCLUSIONS},
120
+ serverType: DEFAULT_SERVER_TYPE,
121
+ locker: import_shared_utils.DEFAULT_LWR_LOCKER_CONFIG
122
+ };
123
+ function createCacheFolder(cache, rootDir) {
124
+ const cacheDir = (0, import_shared_utils.normalizeDirectory)(cache, rootDir);
125
+ const absPath = import_path.default.resolve(cacheDir);
126
+ import_fs.default.mkdirSync(import_path.default.join(absPath, import_shared_utils.ASSETS_CACHE_DIR), {recursive: true});
127
+ return absPath;
128
+ }
129
+ function getLwrConfigFromFile(rootDir, customDir = DEFAULT_LWR_CONFIG_JSON) {
130
+ const lwrConfigPath = import_path.default.resolve((0, import_shared_utils.normalizeDirectory)(customDir, rootDir));
131
+ if (import_fs.default.existsSync(lwrConfigPath)) {
132
+ const configAsString = (0, import_shared_utils.readFile)(lwrConfigPath);
133
+ try {
134
+ return (0, import_app_config.validateLwrAppConfig)(configAsString, "file");
135
+ } catch (e) {
136
+ if (e instanceof import_diagnostics.LwrConfigValidationError) {
137
+ if (process.env.UNSAFE_IGNORE_CONFIG_VALIDATION === "true") {
138
+ console.warn("ignoring config validation errors due to UNSAFE_IGNORE_CONFIG_VALIDATION flag...proceed with caution");
139
+ console.dir(e, {depth: null});
140
+ return JSON.parse(configAsString);
141
+ } else {
142
+ throw e;
143
+ }
144
+ }
145
+ return void 0;
146
+ }
147
+ } else {
148
+ console.log(`[Warning] LWR Config not found on "${lwrConfigPath}"`);
149
+ return void 0;
150
+ }
151
+ }
152
+ function explodeMode(mode) {
153
+ const modeConfigFile = import_path.default.join(import_package.rootPath, `./runtime-configs/${mode}.json`);
154
+ if (!import_fs.default.existsSync(modeConfigFile)) {
155
+ throw new Error(`No configuration found for server mode - ${mode}`);
156
+ }
157
+ const modeConfig = JSON.parse((0, import_shared_utils.readFile)(modeConfigFile));
158
+ return modeConfig;
159
+ }
160
+ function normalizeServices(services, rootDir) {
161
+ return services.map((service) => {
162
+ const [serviceName, serviceConfig = void 0] = Array.isArray(service) ? service : [service];
163
+ let rawServiceName = (0, import_shared_utils.normalizeDirectory)(serviceName, rootDir);
164
+ if (!rawServiceName.startsWith("@") && !DEFAULT_SERVICE_PACKAGE_NAME.test(rawServiceName)) {
165
+ rawServiceName = import_path.default.resolve(rawServiceName);
166
+ }
167
+ return [rawServiceName, serviceConfig];
168
+ });
169
+ }
170
+ function normalizeModules(modules, rootDir) {
171
+ return modules.map((m) => {
172
+ const dirRecord = m;
173
+ if (dirRecord.dir) {
174
+ dirRecord.dir = import_path.default.resolve((0, import_shared_utils.normalizeDirectory)(dirRecord.dir, rootDir));
175
+ }
176
+ const aliasRecord = m;
177
+ if (aliasRecord.path) {
178
+ aliasRecord.path = import_path.default.resolve((0, import_shared_utils.normalizeDirectory)(aliasRecord.path, rootDir));
179
+ }
180
+ return m;
181
+ });
182
+ }
183
+ function normalizeRoutes(routes, resourcePaths) {
184
+ return routes.map((route) => {
185
+ const {routeHandler, contentTemplate, layoutTemplate} = route;
186
+ const bootstrap = {
187
+ ...import_shared_utils.DEFAULT_LWR_BOOTSTRAP_CONFIG,
188
+ ...route.bootstrap
189
+ };
190
+ return {
191
+ ...route,
192
+ bootstrap,
193
+ routeHandler: routeHandler && import_path.default.resolve((0, import_shared_utils.normalizeDirectory)(routeHandler, resourcePaths.rootDir)),
194
+ contentTemplate: contentTemplate && import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(contentTemplate, resourcePaths)),
195
+ layoutTemplate: layoutTemplate && import_path.default.resolve((0, import_shared_utils.normalizeResourcePath)(layoutTemplate, resourcePaths))
196
+ };
197
+ });
198
+ }
199
+ function normalizeAssetsDir(assets, rootDir) {
200
+ if (typeof assets === "string") {
201
+ return [
202
+ {
203
+ alias: "assetsDir",
204
+ dir: (0, import_shared_utils.normalizeDirectory)(assets, rootDir),
205
+ urlPath: "/public/assets"
206
+ }
207
+ ];
208
+ } else {
209
+ return assets.map((asset) => {
210
+ const assetDir = asset.dir;
211
+ const assetFile = asset.file;
212
+ return {
213
+ ...asset,
214
+ dir: assetDir && (0, import_shared_utils.normalizeDirectory)(assetDir, rootDir),
215
+ file: assetFile && (0, import_shared_utils.normalizeDirectory)(assetFile, rootDir)
216
+ };
217
+ });
218
+ }
219
+ }
220
+ function mergeLWCConfigs(config1, config2) {
221
+ const modules1 = config1?.lwc?.modules || [];
222
+ const modules2 = config2?.lwc?.modules || [];
223
+ const interchangeable1 = config1?.lwc?.interchangeable || [];
224
+ const interchangeable2 = config2?.lwc?.interchangeable || [];
225
+ const mergedInterchangeable = [...interchangeable1, ...interchangeable2];
226
+ const interchangeableModules1 = config1?.lwc?.interchangeableModules || [];
227
+ const interchangeableModules2 = config2?.lwc?.interchangeableModules || [];
228
+ const mergedInterchangeableModules = [...interchangeableModules1, ...interchangeableModules2];
229
+ return {
230
+ modules: [...modules1, ...modules2],
231
+ interchangeable: mergedInterchangeable.length ? mergedInterchangeable : void 0,
232
+ interchangeableModules: mergedInterchangeableModules.length ? mergedInterchangeableModules : void 0
233
+ };
234
+ }
235
+ function mergeBundleConfig(jsonConfig, config) {
236
+ const defaultExclusions = config?.bundleConfig?.UNSAFE_lwrDefaultExclude || jsonConfig?.bundleConfig?.UNSAFE_lwrDefaultExclude || DEFAULT_BUNDLE_EXCLUSIONS;
237
+ const configExclusions = config?.bundleConfig?.exclude || jsonConfig?.bundleConfig?.exclude || [];
238
+ return {
239
+ ...jsonConfig?.bundleConfig,
240
+ ...config?.bundleConfig,
241
+ exclude: [...new Set([...defaultExclusions, ...configExclusions])]
242
+ };
243
+ }
244
+ function mergeLockerConfig(jsonConfig, config) {
245
+ const defaultNamespaces = import_shared_utils.DEFAULT_LOCKER_TRUSTED_CMP;
246
+ const configNamespaces = config?.locker?.trustedComponents || jsonConfig?.locker?.trustedComponents || [];
247
+ return {
248
+ ...import_shared_utils.DEFAULT_LWR_LOCKER_CONFIG,
249
+ ...jsonConfig?.locker,
250
+ ...config?.locker,
251
+ trustedComponents: [...new Set([...defaultNamespaces, ...configNamespaces])]
252
+ };
253
+ }
254
+ function getLWCEngineSpecifier() {
255
+ const require2 = (0, import_module.createRequire)(import_path.default.join(process.cwd(), "./env-config.js"));
256
+ const {version: version2} = require2("lwc/package.json");
257
+ if (version2 && version2.startsWith("1")) {
258
+ return "@lwc/engine";
259
+ }
260
+ return "@lwc/engine-dom";
261
+ }
262
+ function normalizeLwcConfig(config) {
263
+ return {
264
+ ...config,
265
+ interchangeableModulesMap: config.interchangeableModules ? (0, import_shared_utils.normalizeInterchangeableModuleConfig)(config.interchangeableModules) : void 0
266
+ };
267
+ }
268
+ function trimLwrConfig(config) {
269
+ Object.keys(config).forEach((k) => {
270
+ if (config[k] === void 0)
271
+ delete config[k];
272
+ });
273
+ return config;
274
+ }
275
+ function normalizeConfig(config) {
276
+ if (config !== void 0) {
277
+ config = trimLwrConfig(config);
278
+ try {
279
+ (0, import_app_config.validateLwrAppConfig)(JSON.stringify(config), "pre");
280
+ } catch (e) {
281
+ if (process.env.UNSAFE_IGNORE_CONFIG_VALIDATION === "true") {
282
+ console.warn("ignoring config validation errors due to UNSAFE_IGNORE_CONFIG_VALIDATION flag...proceed with caution");
283
+ console.dir(e, {depth: null});
284
+ } else {
285
+ throw e;
286
+ }
287
+ }
288
+ }
289
+ const rootDir = import_path.default.resolve(config?.rootDir || DEFAULT_ROOT_DIR);
290
+ const lwrJsonConfig = config?.ignoreLwrConfigFile === true ? void 0 : getLwrConfigFromFile(rootDir, config?.lwrConfigFile);
291
+ const mergedLwrGlobalConfig = {
292
+ ...DEFAULT_LWR_CONFIG,
293
+ ...lwrJsonConfig,
294
+ ...config,
295
+ lwc: normalizeLwcConfig(mergeLWCConfigs(lwrJsonConfig, config)),
296
+ bundleConfig: mergeBundleConfig(lwrJsonConfig, config),
297
+ locker: mergeLockerConfig(lwrJsonConfig, config),
298
+ staticSiteGenerator: {
299
+ ...DEFAULT_GENERATOR_CONFIG,
300
+ ...lwrJsonConfig?.staticSiteGenerator,
301
+ ...config?.staticSiteGenerator
302
+ },
303
+ rootDir
304
+ };
305
+ const assets = normalizeAssetsDir(mergedLwrGlobalConfig.assets, rootDir);
306
+ const contentDir = (0, import_shared_utils.normalizeDirectory)(mergedLwrGlobalConfig.contentDir, rootDir);
307
+ const layoutsDir = (0, import_shared_utils.normalizeDirectory)(mergedLwrGlobalConfig.layoutsDir, rootDir);
308
+ const amdLoader = (0, import_shared_utils.getFeatureFlags)().LEGACY_LOADER ? DEFAULT_AMD_LOADER_LEGACY : DEFAULT_AMD_LOADER;
309
+ return {
310
+ ...mergedLwrGlobalConfig,
311
+ assets,
312
+ contentDir,
313
+ layoutsDir,
314
+ globalDataDir: (0, import_shared_utils.normalizeDirectory)(mergedLwrGlobalConfig.globalDataDir, rootDir),
315
+ hooks: normalizeServices(mergedLwrGlobalConfig.hooks, rootDir),
316
+ cacheDir: createCacheFolder(mergedLwrGlobalConfig.cacheDir, rootDir),
317
+ lwc: {
318
+ modules: normalizeModules([...DEFAULT_LWR_MODULES, ...mergedLwrGlobalConfig.lwc.modules], rootDir),
319
+ interchangeable: mergedLwrGlobalConfig.lwc.interchangeable,
320
+ interchangeableModulesMap: mergedLwrGlobalConfig.lwc.interchangeableModulesMap
321
+ },
322
+ moduleProviders: normalizeServices(mergedLwrGlobalConfig.moduleProviders, rootDir),
323
+ assetProviders: normalizeServices(mergedLwrGlobalConfig.assetProviders, rootDir),
324
+ assetTransformers: normalizeServices(mergedLwrGlobalConfig.assetTransformers, rootDir),
325
+ resourceProviders: normalizeServices(mergedLwrGlobalConfig.resourceProviders, rootDir),
326
+ viewProviders: normalizeServices(mergedLwrGlobalConfig.viewProviders, rootDir),
327
+ viewTransformers: normalizeServices(mergedLwrGlobalConfig.viewTransformers, rootDir),
328
+ routes: normalizeRoutes(mergedLwrGlobalConfig.routes, {rootDir, assets, contentDir, layoutsDir}),
329
+ errorRoutes: normalizeRoutes(mergedLwrGlobalConfig.errorRoutes, {
330
+ rootDir,
331
+ assets,
332
+ contentDir,
333
+ layoutsDir
334
+ }),
335
+ amdLoader,
336
+ esmLoader: DEFAULT_ESM_LOADER,
337
+ lwrVersion: LWR_VERSION
338
+ };
339
+ }
@@ -0,0 +1,32 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/config/src/index.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ explodeMode: () => import_env_config.explodeMode,
28
+ normalizeConfig: () => import_env_config.normalizeConfig,
29
+ validateLwrAppConfig: () => import_app_config.validateLwrAppConfig
30
+ });
31
+ var import_env_config = __toModule(require("./env-config.cjs"));
32
+ var import_app_config = __toModule(require("./validation/app-config.cjs"));