@lwrjs/module-bundler 0.9.0-alpha.8 → 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 (30) hide show
  1. package/build/cjs/amd-bundle-provider.cjs +56 -0
  2. package/build/cjs/amd-runtime-bundle-provider.cjs +45 -0
  3. package/build/cjs/{esm/esm-bundler.cjs → esm-bundle-provider.cjs} +27 -6
  4. package/build/cjs/index.cjs +43 -15
  5. package/build/cjs/{amd/amd-bundler.cjs → utils/amd-common.cjs} +20 -17
  6. package/build/cjs/utils/bundle-common.cjs +44 -0
  7. package/build/cjs/utils/esbuild-utils.cjs +73 -0
  8. package/build/cjs/{amd → utils}/rollup-amd-bundler-plugin.cjs +1 -1
  9. package/build/cjs/{esm → utils}/rollup-esm-bundler-plugin.cjs +1 -1
  10. package/build/es/amd-bundle-provider.d.ts +9 -0
  11. package/build/es/amd-bundle-provider.js +27 -0
  12. package/build/es/amd-runtime-bundle-provider.d.ts +9 -0
  13. package/build/es/amd-runtime-bundle-provider.js +18 -0
  14. package/build/es/esm-bundle-provider.d.ts +9 -0
  15. package/build/es/{esm/esm-bundler.js → esm-bundle-provider.js} +25 -5
  16. package/build/es/index.d.ts +15 -5
  17. package/build/es/index.js +57 -17
  18. package/build/es/utils/amd-common.d.ts +3 -0
  19. package/build/es/{amd/amd-bundler.js → utils/amd-common.js} +29 -17
  20. package/build/es/utils/bundle-common.d.ts +3 -0
  21. package/build/es/utils/bundle-common.js +39 -0
  22. package/build/es/utils/esbuild-utils.d.ts +14 -0
  23. package/build/es/utils/esbuild-utils.js +63 -0
  24. package/package.json +27 -8
  25. package/build/es/amd/amd-bundler.d.ts +0 -3
  26. package/build/es/esm/esm-bundler.d.ts +0 -3
  27. /package/build/es/{amd → utils}/rollup-amd-bundler-plugin.d.ts +0 -0
  28. /package/build/es/{amd → utils}/rollup-amd-bundler-plugin.js +0 -0
  29. /package/build/es/{esm → utils}/rollup-esm-bundler-plugin.d.ts +0 -0
  30. /package/build/es/{esm → utils}/rollup-esm-bundler-plugin.js +0 -0
@@ -0,0 +1,56 @@
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/module-bundler/src/amd-bundle-provider.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ default: () => amd_bundle_provider_default
28
+ });
29
+ var import_amd_common = __toModule(require("./utils/amd-common.cjs"));
30
+ var import_esbuild_utils = __toModule(require("./utils/esbuild-utils.cjs"));
31
+ var AmdBundlerProvider = class {
32
+ constructor(options, {config, moduleRegistry}) {
33
+ this.name = "amd-bundle-provider";
34
+ this.config = config;
35
+ this.moduleRegistry = moduleRegistry;
36
+ }
37
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
38
+ if (runtimeEnvironment.format === "amd") {
39
+ const {moduleRegistry, config} = this;
40
+ const bundle = await (0, import_amd_common.amdBundler)(moduleId, moduleRegistry, false, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
41
+ const {
42
+ minify,
43
+ debug,
44
+ env: {NODE_ENV: envMode} = {NODE_ENV: "production"}
45
+ } = runtimeEnvironment;
46
+ const minified = !!minify && !debug;
47
+ if (minified) {
48
+ bundle.code = await (0, import_esbuild_utils.minifyJavascript)(bundle.code);
49
+ } else {
50
+ bundle.code = await (0, import_esbuild_utils.parseJavascript)(bundle.code, {envMode});
51
+ }
52
+ return bundle;
53
+ }
54
+ }
55
+ };
56
+ var amd_bundle_provider_default = AmdBundlerProvider;
@@ -0,0 +1,45 @@
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/module-bundler/src/amd-runtime-bundle-provider.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ default: () => amd_runtime_bundle_provider_default
28
+ });
29
+ var import_amd_common = __toModule(require("./utils/amd-common.cjs"));
30
+ var AmdBundlerProvider = class {
31
+ constructor(options, {config, moduleRegistry}) {
32
+ this.name = "amd-runtime-bundle-provider";
33
+ this.config = config;
34
+ this.moduleRegistry = moduleRegistry;
35
+ }
36
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
37
+ if (runtimeEnvironment.format === "amd") {
38
+ const {moduleRegistry, config} = this;
39
+ const {minify, debug} = runtimeEnvironment;
40
+ const minified = minify && !debug;
41
+ return await (0, import_amd_common.amdBundler)(moduleId, moduleRegistry, minified, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
42
+ }
43
+ }
44
+ };
45
+ var amd_runtime_bundle_provider_default = AmdBundlerProvider;
@@ -21,15 +21,31 @@ var __toModule = (module2) => {
21
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
22
  };
23
23
 
24
- // packages/@lwrjs/module-bundler/src/esm/esm-bundler.ts
24
+ // packages/@lwrjs/module-bundler/src/esm-bundle-provider.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
- esmBundler: () => esmBundler
27
+ default: () => esm_bundle_provider_default
28
28
  });
29
29
  var import_rollup = __toModule(require("rollup"));
30
- var import_rollup_esm_bundler_plugin = __toModule(require("./rollup-esm-bundler-plugin.cjs"));
31
- async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, lwrConfig, bundleConfigOverride) {
32
- const {exclude, external = {}, alias = {}} = bundleConfigOverride ?? lwrConfig.bundleConfig;
30
+ var import_rollup_esm_bundler_plugin = __toModule(require("./utils/rollup-esm-bundler-plugin.cjs"));
31
+ var import_esbuild_utils = __toModule(require("./utils/esbuild-utils.cjs"));
32
+ var import_bundle_common = __toModule(require("./utils/bundle-common.cjs"));
33
+ var EsmBundlerProvider = class {
34
+ constructor(options, {config, moduleRegistry}) {
35
+ this.name = "esm-bundle-provider";
36
+ this.config = config;
37
+ this.moduleRegistry = moduleRegistry;
38
+ }
39
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
40
+ if (runtimeEnvironment.format === "esm") {
41
+ const {moduleRegistry, config} = this;
42
+ return await esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
43
+ }
44
+ }
45
+ };
46
+ var esm_bundle_provider_default = EsmBundlerProvider;
47
+ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, {bundleConfig}, bundleConfigOverrides) {
48
+ const {exclude, external = {}, alias = {}} = (0, import_bundle_common.overrideBundleConfig)(bundleConfig, bundleConfigOverrides);
33
49
  const {sourceMapUrl} = runtimeEnvironment;
34
50
  const requiredImports = new Map();
35
51
  const dynamicImports = new Map();
@@ -58,9 +74,14 @@ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
58
74
  });
59
75
  const bundleCode = result.output[0].code;
60
76
  const bundleMap = result.output[0].map;
61
- const code = sourceMapUrl ? `${bundleCode}
77
+ let code = sourceMapUrl ? `${bundleCode}
62
78
  //# sourceMappingURL=${sourceMapUrl}` : bundleCode;
63
79
  const {id, specifier, namespace, name, version} = rootModuleDef;
80
+ const {minify, debug} = runtimeEnvironment;
81
+ const minified = !!minify && !debug;
82
+ if (minified) {
83
+ code = await (0, import_esbuild_utils.minifyJavascript)(code);
84
+ }
64
85
  return {
65
86
  id,
66
87
  specifier,
@@ -26,24 +26,25 @@ __markAsModule(exports);
26
26
  __export(exports, {
27
27
  LwrModuleBundler: () => LwrModuleBundler
28
28
  });
29
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
29
30
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
30
- var import_amd_bundler = __toModule(require("./amd/amd-bundler.cjs"));
31
- var import_esm_bundler = __toModule(require("./esm/esm-bundler.cjs"));
32
31
  var TASK_POOL = new import_shared_utils.TaskPool();
33
32
  var LwrModuleBundler = class {
34
33
  constructor(config, globalConfig) {
35
34
  this.cache = new Map();
35
+ this.providers = [];
36
36
  this.inflightBundleDefinitions = new import_shared_utils.InflightTasks();
37
- this.config = globalConfig;
38
37
  this.moduleRegistry = config.moduleRegistry;
39
38
  this.appObserver = config.appObserver;
40
- this.compiler = config.compiler;
39
+ this.bundleConfig = Object.freeze(globalConfig.bundleConfig);
41
40
  this.appObserver?.onModuleDefinitionChange(() => {
42
41
  this.cache.clear();
43
42
  });
44
43
  }
44
+ addBundleProviders(providers) {
45
+ this.providers.push(...providers);
46
+ }
45
47
  async getModuleBundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
46
- const {moduleRegistry} = this;
47
48
  const {format, minify, debug} = runtimeEnvironment;
48
49
  const cacheKey = `${moduleId.specifier}|${moduleId.version}|${(0, import_shared_utils.getCacheKeyFromJson)({
49
50
  locale: runtimeParams.locale,
@@ -61,18 +62,45 @@ var LwrModuleBundler = class {
61
62
  }
62
63
  return this.inflightBundleDefinitions.execute(cacheKey, () => {
63
64
  return TASK_POOL.execute(async () => {
64
- const pendingBundleDef = format === "amd" ? (0, import_amd_bundler.amdBundler)(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides) : (0, import_esm_bundler.esmBundler)(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides);
65
- return pendingBundleDef.then(async (bundleDef) => {
66
- const minified = !!minify && !debug;
67
- if (minified) {
68
- bundleDef.code = (await this.compiler.minifyJavascript(bundleDef.code)).code;
69
- }
70
- if (!cacheDisabled) {
71
- this.cache.set(cacheKey, bundleDef);
65
+ for (const bundler of this.providers) {
66
+ const bundleDef = await bundler.bundle(moduleId, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
67
+ if (bundleDef) {
68
+ if (!cacheDisabled) {
69
+ this.cache.set(cacheKey, bundleDef);
70
+ }
71
+ return bundleDef;
72
72
  }
73
- return bundleDef;
74
- });
73
+ }
74
+ throw (0, import_diagnostics.createSingleDiagnosticError)({
75
+ description: import_diagnostics.descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier)
76
+ }, import_diagnostics.LwrUnresolvableError);
75
77
  }, this);
76
78
  });
77
79
  }
80
+ async resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature) {
81
+ const bundleDefinition = await this.getModuleBundle(moduleId, runtimeEnvironment, runtimeParams);
82
+ if (!bundleDefinition) {
83
+ throw (0, import_diagnostics.createSingleDiagnosticError)({
84
+ description: import_diagnostics.descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier)
85
+ }, import_diagnostics.LwrUnresolvableError);
86
+ }
87
+ let uri;
88
+ if (bundleDefinition.src) {
89
+ uri = bundleDefinition.src;
90
+ } else {
91
+ let resolvedVersion = moduleId.version || bundleDefinition.version;
92
+ if (!resolvedVersion) {
93
+ const {version} = await this.moduleRegistry.getModuleEntry(moduleId);
94
+ resolvedVersion = version;
95
+ }
96
+ uri = String(await this.moduleRegistry.resolveModuleUri({...moduleId, version: resolvedVersion}, runtimeEnvironment, runtimeParams, signature));
97
+ }
98
+ return uri;
99
+ }
100
+ getPublicApi() {
101
+ return {
102
+ getModuleBundle: this.getModuleBundle.bind(this),
103
+ resolveModuleUri: this.resolveModuleUri.bind(this)
104
+ };
105
+ }
78
106
  };
@@ -21,33 +21,34 @@ var __toModule = (module2) => {
21
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
22
  };
23
23
 
24
- // packages/@lwrjs/module-bundler/src/amd/amd-bundler.ts
24
+ // packages/@lwrjs/module-bundler/src/utils/amd-common.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
27
  amdBundler: () => amdBundler
28
28
  });
29
- var import_compiler = __toModule(require("@lwrjs/compiler"));
30
29
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
30
  var import_rollup = __toModule(require("rollup"));
31
+ var import_plugin_replace = __toModule(require("@rollup/plugin-replace"));
32
32
  var import_rollup_amd_bundler_plugin = __toModule(require("./rollup-amd-bundler-plugin.cjs"));
33
- async function bundle(id, moduleGraphs) {
34
- const bundler = await (0, import_rollup.rollup)({
35
- input: id,
36
- plugins: [
37
- (0, import_rollup_amd_bundler_plugin.bundleDefinitions)({
38
- moduleGraphs
39
- })
40
- ]
41
- });
33
+ var import_bundle_common = __toModule(require("./bundle-common.cjs"));
34
+ var AMD_DEFINE = "LWR.define";
35
+ async function bundle(id, moduleGraphs, minify = false) {
36
+ const plugins = [(0, import_rollup_amd_bundler_plugin.bundleDefinitions)({moduleGraphs})];
37
+ minify && plugins.push((0, import_plugin_replace.default)({
38
+ "process.env.NODE_ENV": JSON.stringify("production"),
39
+ preventAssignment: false
40
+ }));
41
+ const bundler = await (0, import_rollup.rollup)({input: id, plugins});
42
42
  const {output} = await bundler.generate({
43
- amd: {id, define: import_compiler.AMD_DEFINE},
43
+ amd: {id, define: AMD_DEFINE},
44
44
  exports: "named",
45
45
  format: "amd"
46
46
  });
47
47
  return output[0].code;
48
48
  }
49
- async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, {bundleConfig, amdLoader}, bundleConfigOverride) {
50
- const {exclude, external} = bundleConfigOverride ?? bundleConfig;
49
+ async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, {bundleConfig}, bundleConfigOverrides) {
50
+ const {exclude, external = {}} = (0, import_bundle_common.overrideBundleConfig)(bundleConfig, bundleConfigOverrides);
51
+ const externalsArray = Object.keys(external);
51
52
  const requiredImports = new Map();
52
53
  const dynamicImports = new Map();
53
54
  const includedModules = [];
@@ -57,8 +58,10 @@ async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
57
58
  static: import_shared_utils.GraphDepth.ALL,
58
59
  dynamic: 0,
59
60
  includeId: (moduleRef) => {
60
- if (exclude?.includes(moduleRef.specifier) || moduleRef.specifier === amdLoader) {
61
- requiredImports.set(`${moduleId.specifier}_${moduleId.version}`, moduleRef);
61
+ if (externalsArray.includes(moduleRef.specifier)) {
62
+ return false;
63
+ } else if (exclude?.includes(moduleRef.specifier)) {
64
+ requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
62
65
  return false;
63
66
  }
64
67
  return true;
@@ -88,7 +91,7 @@ async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
88
91
  if (id2 !== rootModule.specifier) {
89
92
  includedModules.push(id2);
90
93
  }
91
- return bundle(id2, moduleGraphs);
94
+ return bundle(id2, moduleGraphs, minify);
92
95
  }));
93
96
  const {id, name, namespace, version, specifier} = moduleGraphs.linkedDefinitions[rootModule.specifier];
94
97
  return {
@@ -0,0 +1,44 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {get: all[name], enumerable: true});
6
+ };
7
+
8
+ // packages/@lwrjs/module-bundler/src/utils/bundle-common.ts
9
+ __markAsModule(exports);
10
+ __export(exports, {
11
+ overrideBundleConfig: () => overrideBundleConfig
12
+ });
13
+ function overrideBundleConfig(bundleConfig, bundleConfigOverrides) {
14
+ if (!bundleConfigOverrides) {
15
+ return bundleConfig;
16
+ }
17
+ const newBundleConfig = {
18
+ UNSAFE_lwrDefaultExclude: [
19
+ ...bundleConfigOverrides.UNSAFE_lwrDefaultExclude || [
20
+ ...bundleConfig.UNSAFE_lwrDefaultExclude || []
21
+ ]
22
+ ],
23
+ external: {...bundleConfigOverrides.external || {...bundleConfig.external || {}}},
24
+ alias: {...bundleConfigOverrides.alias || {...bundleConfig.alias || {}}}
25
+ };
26
+ if (bundleConfigOverrides.appendExcludes) {
27
+ newBundleConfig.exclude = appendAndDedupArrays(bundleConfig.exclude, bundleConfigOverrides.exclude);
28
+ } else {
29
+ newBundleConfig.exclude = [...bundleConfigOverrides.exclude || [...bundleConfig.exclude || []]];
30
+ }
31
+ return newBundleConfig;
32
+ }
33
+ function appendAndDedupArrays(a1, a2) {
34
+ let ret;
35
+ if (!a1 && a2) {
36
+ return a2.filter((item, pos) => a2.indexOf(item) === pos);
37
+ } else if (!a2 && a1) {
38
+ return a1.filter((item, pos) => a1.indexOf(item) === pos);
39
+ } else if (a1 && a2) {
40
+ const c = a1?.concat(a2);
41
+ return c.filter((item, pos) => c.indexOf(item) === pos);
42
+ }
43
+ return ret;
44
+ }
@@ -0,0 +1,73 @@
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/module-bundler/src/utils/esbuild-utils.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ minifyJavascript: () => minifyJavascript,
28
+ parseJavascript: () => parseJavascript
29
+ });
30
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
+ var import_esbuild = __toModule(require("esbuild"));
32
+ var import_features = __toModule(require("@lwc/features"));
33
+ var lwcFeatureFlags = "default" in import_features.default ? import_features.default.default : import_features.default;
34
+ var lwcFeatureFlagDefinitions = Object.fromEntries(Object.keys(lwcFeatureFlags).map((feature) => [`lwcRuntimeFlags.${feature}`, "false"]));
35
+ var esbuild = import_esbuild.default;
36
+ if (!import_esbuild.default) {
37
+ try {
38
+ esbuild = require("esbuild");
39
+ } catch {
40
+ }
41
+ }
42
+ async function minifyJavascript(source) {
43
+ try {
44
+ const {code} = await esbuild.transform(source, {
45
+ loader: "js",
46
+ minify: true,
47
+ define: {
48
+ "process.env.NODE_ENV": JSON.stringify("production"),
49
+ ...lwcFeatureFlagDefinitions
50
+ }
51
+ });
52
+ return code;
53
+ } catch (error) {
54
+ import_shared_utils.logger.debug(error);
55
+ throw error;
56
+ }
57
+ }
58
+ async function parseJavascript(source, options) {
59
+ try {
60
+ const {code} = await esbuild.transform(source, {
61
+ loader: "js",
62
+ minify: false,
63
+ define: {
64
+ "process.env.NODE_ENV": JSON.stringify(options.envMode),
65
+ ...lwcFeatureFlagDefinitions
66
+ }
67
+ });
68
+ return code;
69
+ } catch (error) {
70
+ import_shared_utils.logger.debug(error);
71
+ throw error;
72
+ }
73
+ }
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  __defProp(target, name, {get: all[name], enumerable: true});
6
6
  };
7
7
 
8
- // packages/@lwrjs/module-bundler/src/amd/rollup-amd-bundler-plugin.ts
8
+ // packages/@lwrjs/module-bundler/src/utils/rollup-amd-bundler-plugin.ts
9
9
  __markAsModule(exports);
10
10
  __export(exports, {
11
11
  bundleDefinitions: () => bundleDefinitions
@@ -21,7 +21,7 @@ var __toModule = (module2) => {
21
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
22
  };
23
23
 
24
- // packages/@lwrjs/module-bundler/src/esm/rollup-esm-bundler-plugin.ts
24
+ // packages/@lwrjs/module-bundler/src/utils/rollup-esm-bundler-plugin.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
27
  bundleDefinitions: () => bundleDefinitions
@@ -0,0 +1,9 @@
1
+ import { AbstractModuleId, BundleConfigOverrides, BundleDefinition, BundleProvider, ProviderAppConfig, ProviderContext, PublicModuleRegistry, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
+ export default class AmdBundlerProvider implements BundleProvider {
3
+ name: string;
4
+ config: ProviderAppConfig;
5
+ moduleRegistry: PublicModuleRegistry;
6
+ constructor(options: {}, { config, moduleRegistry }: ProviderContext);
7
+ bundle<T extends AbstractModuleId, R extends RuntimeEnvironment>(moduleId: T, runtimeEnvironment: R, runtimeParams?: RuntimeParams, bundleConfigOverrides?: BundleConfigOverrides): Promise<BundleDefinition | undefined>;
8
+ }
9
+ //# sourceMappingURL=amd-bundle-provider.d.ts.map
@@ -0,0 +1,27 @@
1
+ import { amdBundler } from './utils/amd-common.js';
2
+ import { minifyJavascript, parseJavascript } from './utils/esbuild-utils.js';
3
+ export default class AmdBundlerProvider {
4
+ constructor(options, { config, moduleRegistry }) {
5
+ this.name = 'amd-bundle-provider';
6
+ this.config = config;
7
+ this.moduleRegistry = moduleRegistry;
8
+ }
9
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
10
+ if (runtimeEnvironment.format === 'amd') {
11
+ const { moduleRegistry, config } = this;
12
+ const bundle = await amdBundler(moduleId, moduleRegistry, false, // we NEVER want to minify via rollup/terser with this bundler; it's too slow
13
+ runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
14
+ // Minification via esbuild for performance
15
+ const { minify, debug, env: { NODE_ENV: envMode } = { NODE_ENV: 'production' }, } = runtimeEnvironment;
16
+ const minified = !!minify && !debug;
17
+ if (minified) {
18
+ bundle.code = await minifyJavascript(bundle.code);
19
+ }
20
+ else {
21
+ bundle.code = await parseJavascript(bundle.code, { envMode });
22
+ }
23
+ return bundle;
24
+ }
25
+ }
26
+ }
27
+ //# sourceMappingURL=amd-bundle-provider.js.map
@@ -0,0 +1,9 @@
1
+ import { AbstractModuleId, BundleConfigOverrides, BundleDefinition, BundleProvider, ProviderAppConfig, ProviderContext, PublicModuleRegistry, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
+ export default class AmdBundlerProvider implements BundleProvider {
3
+ name: string;
4
+ config: ProviderAppConfig;
5
+ moduleRegistry: PublicModuleRegistry;
6
+ constructor(options: {}, { config, moduleRegistry }: ProviderContext);
7
+ bundle<T extends AbstractModuleId, R extends RuntimeEnvironment>(moduleId: T, runtimeEnvironment: R, runtimeParams?: RuntimeParams, bundleConfigOverrides?: BundleConfigOverrides): Promise<BundleDefinition | undefined>;
8
+ }
9
+ //# sourceMappingURL=amd-runtime-bundle-provider.d.ts.map
@@ -0,0 +1,18 @@
1
+ import { amdBundler } from './utils/amd-common.js';
2
+ export default class AmdBundlerProvider {
3
+ constructor(options, { config, moduleRegistry }) {
4
+ this.name = 'amd-runtime-bundle-provider';
5
+ this.config = config;
6
+ this.moduleRegistry = moduleRegistry;
7
+ }
8
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
9
+ if (runtimeEnvironment.format === 'amd') {
10
+ const { moduleRegistry, config } = this;
11
+ const { minify, debug } = runtimeEnvironment;
12
+ const minified = minify && !debug;
13
+ return await amdBundler(moduleId, moduleRegistry, minified, // will minify using rollup/terser if true - MRT runtime friendly
14
+ runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
15
+ }
16
+ }
17
+ }
18
+ //# sourceMappingURL=amd-runtime-bundle-provider.js.map
@@ -0,0 +1,9 @@
1
+ import type { AbstractModuleId, BundleConfigOverrides, BundleDefinition, BundleProvider, ProviderAppConfig, ProviderContext, PublicModuleRegistry, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
+ export default class EsmBundlerProvider implements BundleProvider {
3
+ name: string;
4
+ config: ProviderAppConfig;
5
+ moduleRegistry: PublicModuleRegistry;
6
+ constructor(options: {}, { config, moduleRegistry }: ProviderContext);
7
+ bundle<T extends AbstractModuleId, R extends RuntimeEnvironment>(moduleId: T, runtimeEnvironment: R, runtimeParams?: RuntimeParams, bundleConfigOverrides?: BundleConfigOverrides): Promise<BundleDefinition | undefined>;
8
+ }
9
+ //# sourceMappingURL=esm-bundle-provider.d.ts.map
@@ -1,7 +1,22 @@
1
1
  import { rollup } from 'rollup';
2
- import { bundleDefinitions } from './rollup-esm-bundler-plugin.js';
3
- export async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, lwrConfig, bundleConfigOverride) {
4
- const { exclude, external = {}, alias = {} } = bundleConfigOverride ?? lwrConfig.bundleConfig;
2
+ import { bundleDefinitions } from './utils/rollup-esm-bundler-plugin.js';
3
+ import { minifyJavascript } from './utils/esbuild-utils.js';
4
+ import { overrideBundleConfig } from './utils/bundle-common.js';
5
+ export default class EsmBundlerProvider {
6
+ constructor(options, { config, moduleRegistry }) {
7
+ this.name = 'esm-bundle-provider';
8
+ this.config = config;
9
+ this.moduleRegistry = moduleRegistry;
10
+ }
11
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
12
+ if (runtimeEnvironment.format === 'esm') {
13
+ const { moduleRegistry, config } = this;
14
+ return await esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
15
+ }
16
+ }
17
+ }
18
+ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, { bundleConfig }, bundleConfigOverrides) {
19
+ const { exclude, external = {}, alias = {} } = overrideBundleConfig(bundleConfig, bundleConfigOverrides);
5
20
  const { sourceMapUrl } = runtimeEnvironment;
6
21
  const requiredImports = new Map();
7
22
  const dynamicImports = new Map();
@@ -32,8 +47,13 @@ export async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, r
32
47
  });
33
48
  const bundleCode = result.output[0].code;
34
49
  const bundleMap = result.output[0].map;
35
- const code = sourceMapUrl ? `${bundleCode}\n//# sourceMappingURL=${sourceMapUrl}` : bundleCode;
50
+ let code = sourceMapUrl ? `${bundleCode}\n//# sourceMappingURL=${sourceMapUrl}` : bundleCode;
36
51
  const { id, specifier, namespace, name, version } = rootModuleDef;
52
+ const { minify, debug } = runtimeEnvironment;
53
+ const minified = !!minify && !debug;
54
+ if (minified) {
55
+ code = await minifyJavascript(code);
56
+ }
37
57
  return {
38
58
  id,
39
59
  specifier,
@@ -50,4 +70,4 @@ export async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, r
50
70
  },
51
71
  };
52
72
  }
53
- //# sourceMappingURL=esm-bundler.js.map
73
+ //# sourceMappingURL=esm-bundle-provider.js.map
@@ -1,18 +1,28 @@
1
- import { AbstractModuleId, BundleConfig, BundleDefinition, LwrAppObserver, ModuleBundler, ModuleRegistry, NormalizedLwrGlobalConfig, RuntimeParams, SourceMapRuntimeEnvironment, Compiler } from '@lwrjs/types';
1
+ import { AbstractModuleId, BundleConfig, BundleConfigOverrides, BundleDefinition, BundleProvider, LwrAppObserver, ModuleBundler, ModuleId, ModuleRegistry, NormalizedLwrGlobalConfig, PublicModuleBundler, RuntimeEnvironment, RuntimeParams, SourceMapRuntimeEnvironment } from '@lwrjs/types';
2
2
  interface LwrModuleBundlerConfig {
3
- compiler: Compiler;
4
3
  moduleRegistry: ModuleRegistry;
5
4
  appObserver?: LwrAppObserver;
6
5
  }
7
6
  export declare class LwrModuleBundler implements ModuleBundler {
8
- compiler: Compiler;
9
7
  moduleRegistry: ModuleRegistry;
10
8
  appObserver: LwrAppObserver | undefined;
11
- config: NormalizedLwrGlobalConfig;
12
9
  cache: Map<string, BundleDefinition>;
10
+ providers: BundleProvider[];
11
+ bundleConfig: BundleConfig;
13
12
  private inflightBundleDefinitions;
14
13
  constructor(config: LwrModuleBundlerConfig, globalConfig: NormalizedLwrGlobalConfig);
15
- getModuleBundle<T extends AbstractModuleId>(moduleId: T, runtimeEnvironment: SourceMapRuntimeEnvironment, runtimeParams?: RuntimeParams, bundleConfigOverrides?: BundleConfig): Promise<BundleDefinition>;
14
+ addBundleProviders(providers: BundleProvider[]): void;
15
+ getModuleBundle<T extends AbstractModuleId>(moduleId: T, runtimeEnvironment: SourceMapRuntimeEnvironment, runtimeParams?: RuntimeParams, bundleConfigOverrides?: BundleConfigOverrides): Promise<BundleDefinition>;
16
+ /**
17
+ * Resolve the URI to the bundle rooted at the `moduleId`
18
+ * @param moduleId - The id of the root module for the bundle
19
+ * @param runtimeEnvironment - The runtime operating environnment
20
+ * @param runtimeParams - The available runtime parameters provided in context to the request
21
+ * @param signature - The signature of the bundle instance being referenced
22
+ * @returns the URI
23
+ */
24
+ resolveModuleUri(moduleId: Required<Pick<ModuleId, 'specifier' | 'version'>>, runtimeEnvironment: RuntimeEnvironment, runtimeParams?: RuntimeParams, signature?: string): Promise<string>;
25
+ getPublicApi(): PublicModuleBundler;
16
26
  }
17
27
  export {};
18
28
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,26 +1,28 @@
1
+ import { LwrUnresolvableError, createSingleDiagnosticError, descriptions } from '@lwrjs/diagnostics';
1
2
  import { getCacheKeyFromJson, InflightTasks, TaskPool } from '@lwrjs/shared-utils';
2
- import { amdBundler } from './amd/amd-bundler.js';
3
- import { esmBundler } from './esm/esm-bundler.js';
4
3
  const TASK_POOL = new TaskPool();
5
4
  export class LwrModuleBundler {
6
5
  constructor(config, globalConfig) {
7
6
  this.cache = new Map();
7
+ this.providers = [];
8
8
  // Pending bundle definitions are tracked to prevent concurrent resolution of the same bundle.
9
9
  // Subsequent requests for the same bundle will await the original promise.
10
10
  // Cache entries will be removed once the bundle is resolved.
11
11
  this.inflightBundleDefinitions = new InflightTasks();
12
- this.config = globalConfig;
13
12
  this.moduleRegistry = config.moduleRegistry;
14
13
  this.appObserver = config.appObserver;
15
- this.compiler = config.compiler;
14
+ // Freeze the bundle config to make sure it is not modified
15
+ this.bundleConfig = Object.freeze(globalConfig.bundleConfig);
16
16
  this.appObserver?.onModuleDefinitionChange(() => {
17
17
  // TODO: This is a very naive approach however
18
18
  // this would only happen in non-prod environments
19
19
  this.cache.clear();
20
20
  });
21
21
  }
22
+ addBundleProviders(providers) {
23
+ this.providers.push(...providers);
24
+ }
22
25
  async getModuleBundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
23
- const { moduleRegistry } = this;
24
26
  const { format, minify, debug } = runtimeEnvironment;
25
27
  const cacheKey = `${moduleId.specifier}|${moduleId.version}|${getCacheKeyFromJson({
26
28
  locale: runtimeParams.locale,
@@ -44,21 +46,59 @@ export class LwrModuleBundler {
44
46
  // console.log('[INFO] Create Bundle: ', cacheKey);
45
47
  // Run theses tasks in a task pool to throttle parallel requests.
46
48
  return TASK_POOL.execute(async () => {
47
- const pendingBundleDef = format === 'amd'
48
- ? amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides)
49
- : esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides);
50
- return pendingBundleDef.then(async (bundleDef) => {
51
- const minified = !!minify && !debug;
52
- if (minified) {
53
- bundleDef.code = (await this.compiler.minifyJavascript(bundleDef.code)).code;
54
- }
55
- if (!cacheDisabled) {
56
- this.cache.set(cacheKey, bundleDef);
49
+ for (const bundler of this.providers) {
50
+ // eslint-disable-next-line no-await-in-loop
51
+ const bundleDef = await bundler.bundle(moduleId, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
52
+ if (bundleDef) {
53
+ if (!cacheDisabled) {
54
+ this.cache.set(cacheKey, bundleDef);
55
+ }
56
+ return bundleDef;
57
57
  }
58
- return bundleDef;
59
- });
58
+ }
59
+ throw createSingleDiagnosticError({
60
+ description: descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier),
61
+ }, LwrUnresolvableError);
60
62
  }, this);
61
63
  });
62
64
  }
65
+ /**
66
+ * Resolve the URI to the bundle rooted at the `moduleId`
67
+ * @param moduleId - The id of the root module for the bundle
68
+ * @param runtimeEnvironment - The runtime operating environnment
69
+ * @param runtimeParams - The available runtime parameters provided in context to the request
70
+ * @param signature - The signature of the bundle instance being referenced
71
+ * @returns the URI
72
+ */
73
+ async resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature) {
74
+ // First get the bundle definition to see if a src attribute is set.
75
+ const bundleDefinition = await this.getModuleBundle(moduleId, runtimeEnvironment, runtimeParams);
76
+ if (!bundleDefinition) {
77
+ throw createSingleDiagnosticError({
78
+ description: descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier),
79
+ }, LwrUnresolvableError);
80
+ }
81
+ let uri;
82
+ if (bundleDefinition.src) {
83
+ // If source is provided on the bundle definition use that
84
+ uri = bundleDefinition.src;
85
+ }
86
+ else {
87
+ let resolvedVersion = moduleId.version || bundleDefinition.version;
88
+ // If we do not know the verison, look it up.
89
+ if (!resolvedVersion) {
90
+ const { version } = await this.moduleRegistry.getModuleEntry(moduleId);
91
+ resolvedVersion = version;
92
+ }
93
+ uri = String(await this.moduleRegistry.resolveModuleUri({ ...moduleId, version: resolvedVersion }, runtimeEnvironment, runtimeParams, signature));
94
+ }
95
+ return uri;
96
+ }
97
+ getPublicApi() {
98
+ return {
99
+ getModuleBundle: this.getModuleBundle.bind(this),
100
+ resolveModuleUri: this.resolveModuleUri.bind(this),
101
+ };
102
+ }
63
103
  }
64
104
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import type { AbstractModuleId, BundleConfigOverrides, BundleDefinition, ModuleRegistry, ProviderAppConfig, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
+ export declare function amdBundler(moduleId: AbstractModuleId, moduleRegistry: ModuleRegistry, minify: boolean | undefined, runtimeEnvironment: RuntimeEnvironment, runtimeParams: RuntimeParams | undefined, { bundleConfig }: ProviderAppConfig, bundleConfigOverrides?: BundleConfigOverrides): Promise<BundleDefinition>;
3
+ //# sourceMappingURL=amd-common.d.ts.map
@@ -1,16 +1,20 @@
1
- import { AMD_DEFINE } from '@lwrjs/compiler';
2
1
  import { GraphDepth, getModuleGraphs, getSpecifier } from '@lwrjs/shared-utils';
3
2
  import { rollup } from 'rollup';
3
+ import replace from '@rollup/plugin-replace';
4
+ // import { terser } from 'rollup-plugin-terser';
4
5
  import { bundleDefinitions } from './rollup-amd-bundler-plugin.js';
5
- async function bundle(id, moduleGraphs) {
6
- const bundler = await rollup({
7
- input: id,
8
- plugins: [
9
- bundleDefinitions({
10
- moduleGraphs,
11
- }),
12
- ],
13
- });
6
+ import { overrideBundleConfig } from './bundle-common.js';
7
+ const AMD_DEFINE = 'LWR.define';
8
+ async function bundle(id, moduleGraphs, minify = false) {
9
+ const plugins = [bundleDefinitions({ moduleGraphs })];
10
+ minify &&
11
+ plugins.push(
12
+ // terser(), // Do not minify until needed for client modules. Server/SSR modules do not need to be minifed.
13
+ replace({
14
+ 'process.env.NODE_ENV': JSON.stringify('production'),
15
+ preventAssignment: false,
16
+ }));
17
+ const bundler = await rollup({ input: id, plugins });
14
18
  const { output } = await bundler.generate({
15
19
  amd: { id, define: AMD_DEFINE },
16
20
  exports: 'named',
@@ -18,8 +22,9 @@ async function bundle(id, moduleGraphs) {
18
22
  });
19
23
  return output[0].code;
20
24
  }
21
- export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, { bundleConfig, amdLoader }, bundleConfigOverride) {
22
- const { exclude, external } = bundleConfigOverride ?? bundleConfig;
25
+ export async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, { bundleConfig }, bundleConfigOverrides) {
26
+ const { exclude, external = {} } = overrideBundleConfig(bundleConfig, bundleConfigOverrides);
27
+ const externalsArray = Object.keys(external);
23
28
  const requiredImports = new Map();
24
29
  const dynamicImports = new Map();
25
30
  const includedModules = [];
@@ -29,9 +34,16 @@ export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, r
29
34
  static: GraphDepth.ALL,
30
35
  dynamic: 0,
31
36
  includeId: (moduleRef) => {
32
- // loader should be auto bundled with shim already
33
- if (exclude?.includes(moduleRef.specifier) || moduleRef.specifier === amdLoader) {
34
- requiredImports.set(`${moduleId.specifier}_${moduleId.version}`, moduleRef);
37
+ // Do not bundle externals, including the loader module, which is auto bundled
38
+ // with the shim + loader combo
39
+ if (externalsArray.includes(moduleRef.specifier)) {
40
+ // Do not include externals in the required imports but also return false to indicate it should not be in the bundle
41
+ return false;
42
+ }
43
+ else if (exclude?.includes(moduleRef.specifier)) {
44
+ // If this is a bundle exclude return false to indicate it should not be in the bundle
45
+ // but add it to the requriedImports so it shows up as a static dependency of the bundle.
46
+ requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
35
47
  return false;
36
48
  }
37
49
  return true;
@@ -68,7 +80,7 @@ export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, r
68
80
  includedModules.push(id);
69
81
  }
70
82
  // bundle all dependencies for the linked definition and convert to AMD
71
- return bundle(id, moduleGraphs);
83
+ return bundle(id, moduleGraphs, minify);
72
84
  }));
73
85
  const { id, name, namespace, version, specifier } = moduleGraphs.linkedDefinitions[rootModule.specifier];
74
86
  return {
@@ -86,4 +98,4 @@ export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, r
86
98
  },
87
99
  };
88
100
  }
89
- //# sourceMappingURL=amd-bundler.js.map
101
+ //# sourceMappingURL=amd-common.js.map
@@ -0,0 +1,3 @@
1
+ import type { BundleConfig, BundleConfigOverrides } from '@lwrjs/types';
2
+ export declare function overrideBundleConfig(bundleConfig: BundleConfig, bundleConfigOverrides?: BundleConfigOverrides): BundleConfig;
3
+ //# sourceMappingURL=bundle-common.d.ts.map
@@ -0,0 +1,39 @@
1
+ export function overrideBundleConfig(bundleConfig, bundleConfigOverrides) {
2
+ if (!bundleConfigOverrides) {
3
+ return bundleConfig;
4
+ }
5
+ // Replace properties that are overridden
6
+ const newBundleConfig = {
7
+ UNSAFE_lwrDefaultExclude: [
8
+ ...(bundleConfigOverrides.UNSAFE_lwrDefaultExclude || [
9
+ ...(bundleConfig.UNSAFE_lwrDefaultExclude || []),
10
+ ]),
11
+ ],
12
+ external: { ...(bundleConfigOverrides.external || { ...(bundleConfig.external || {}) }) },
13
+ alias: { ...(bundleConfigOverrides.alias || { ...(bundleConfig.alias || {}) }) },
14
+ };
15
+ // Append Excludes
16
+ if (bundleConfigOverrides.appendExcludes) {
17
+ newBundleConfig.exclude = appendAndDedupArrays(bundleConfig.exclude, bundleConfigOverrides.exclude);
18
+ // or Replace Excludes
19
+ }
20
+ else {
21
+ newBundleConfig.exclude = [...(bundleConfigOverrides.exclude || [...(bundleConfig.exclude || [])])];
22
+ }
23
+ return newBundleConfig;
24
+ }
25
+ function appendAndDedupArrays(a1, a2) {
26
+ let ret;
27
+ if (!a1 && a2) {
28
+ return a2.filter((item, pos) => a2.indexOf(item) === pos);
29
+ }
30
+ else if (!a2 && a1) {
31
+ return a1.filter((item, pos) => a1.indexOf(item) === pos);
32
+ }
33
+ else if (a1 && a2) {
34
+ const c = a1?.concat(a2);
35
+ return c.filter((item, pos) => c.indexOf(item) === pos);
36
+ }
37
+ return ret;
38
+ }
39
+ //# sourceMappingURL=bundle-common.js.map
@@ -0,0 +1,14 @@
1
+ interface ParseOptions {
2
+ envMode: string;
3
+ }
4
+ /**
5
+ * Minification via esbuild.
6
+ * When importing this module make sure the optional library 'esbuild' is installed.
7
+ */
8
+ export declare function minifyJavascript(source: string): Promise<string>;
9
+ /**
10
+ * Parse source to remove process.env.NODE_ENV without minify
11
+ */
12
+ export declare function parseJavascript(source: string, options: ParseOptions): Promise<string>;
13
+ export {};
14
+ //# sourceMappingURL=esbuild-utils.d.ts.map
@@ -0,0 +1,63 @@
1
+ import { logger } from '@lwrjs/shared-utils';
2
+ import esbuildEsm from 'esbuild';
3
+ import features from '@lwc/features';
4
+ // In certain envs, this default import is not compiled correctly
5
+ const lwcFeatureFlags = 'default' in features ? features.default : features;
6
+ // Set every LWC feature flag to false, so that the unused code can be tree-shaken
7
+ const lwcFeatureFlagDefinitions = Object.fromEntries(Object.keys(lwcFeatureFlags).map((feature) => [`lwcRuntimeFlags.${feature}`, 'false']));
8
+ // https://github.com/evanw/esbuild/issues/706
9
+ // Fixed in 0.11.0 but upgrading past 0.9.7 has caused breaking changes for consumers...
10
+ // https://github.com/salesforce-experience-platform-emu/lwr/issues/1014
11
+ let esbuild = esbuildEsm;
12
+ if (!esbuildEsm) {
13
+ try {
14
+ esbuild = require('esbuild');
15
+ }
16
+ catch {
17
+ /* this is to support mjs/cjs dual impl */
18
+ }
19
+ }
20
+ /**
21
+ * Minification via esbuild.
22
+ * When importing this module make sure the optional library 'esbuild' is installed.
23
+ */
24
+ export async function minifyJavascript(source) {
25
+ try {
26
+ const { code } = await esbuild.transform(source, {
27
+ loader: 'js',
28
+ minify: true,
29
+ // Remove assertions by tree shaking the computed expression
30
+ define: {
31
+ 'process.env.NODE_ENV': JSON.stringify('production'),
32
+ ...lwcFeatureFlagDefinitions,
33
+ },
34
+ });
35
+ return code;
36
+ }
37
+ catch (error) {
38
+ logger.debug(error);
39
+ throw error;
40
+ }
41
+ }
42
+ /**
43
+ * Parse source to remove process.env.NODE_ENV without minify
44
+ */
45
+ export async function parseJavascript(source, options) {
46
+ try {
47
+ const { code } = await esbuild.transform(source, {
48
+ loader: 'js',
49
+ minify: false,
50
+ // Remove assertions by tree shaking the computed expression
51
+ define: {
52
+ 'process.env.NODE_ENV': JSON.stringify(options.envMode),
53
+ ...lwcFeatureFlagDefinitions,
54
+ },
55
+ });
56
+ return code;
57
+ }
58
+ catch (error) {
59
+ logger.debug(error);
60
+ throw error;
61
+ }
62
+ }
63
+ //# sourceMappingURL=esbuild-utils.js.map
package/package.json CHANGED
@@ -4,15 +4,15 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0-alpha.8",
7
+ "version": "0.9.0",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/salesforce/lwr.git",
11
+ "url": "https://github.com/salesforce-experience-platform-emu/lwr.git",
12
12
  "directory": "packages/@lwrjs/module-registry"
13
13
  },
14
14
  "bugs": {
15
- "url": "https://github.com/salesforce/lwr/issues"
15
+ "url": "https://github.com/salesforce-experience-platform-emu/lwr/issues"
16
16
  },
17
17
  "type": "module",
18
18
  "types": "build/es/index.d.ts",
@@ -22,23 +22,42 @@
22
22
  ".": {
23
23
  "import": "./build/es/index.js",
24
24
  "require": "./build/cjs/index.cjs"
25
+ },
26
+ "./amd-bundle-provider": {
27
+ "import": "./build/es/amd-bundle-provider.js",
28
+ "require": "./build/cjs/amd-bundle-provider.cjs"
29
+ },
30
+ "./amd-runtime-bundle-provider": {
31
+ "import": "./build/es/amd-runtime-bundle-provider.js",
32
+ "require": "./build/cjs/amd-runtime-bundle-provider.cjs"
33
+ },
34
+ "./esm-bundle-provider": {
35
+ "import": "./build/es/esm-bundle-provider.js",
36
+ "require": "./build/cjs/esm-bundle-provider.cjs"
25
37
  }
26
38
  },
39
+ "scripts": {
40
+ "build": "tsc -b"
41
+ },
27
42
  "files": [
28
43
  "build/**/*.js",
29
44
  "build/**/*.cjs",
30
45
  "build/**/*.d.ts"
31
46
  ],
32
47
  "dependencies": {
33
- "@lwrjs/compiler": "0.9.0-alpha.8",
34
- "@lwrjs/shared-utils": "0.9.0-alpha.8",
48
+ "@lwc/features": "2.38.1",
49
+ "@lwrjs/shared-utils": "0.9.0",
50
+ "@rollup/plugin-replace": "^2.4.2",
35
51
  "rollup": "~2.45.2"
36
52
  },
37
53
  "devDependencies": {
38
- "@lwrjs/types": "0.9.0-alpha.8"
54
+ "@lwrjs/types": "0.9.0"
55
+ },
56
+ "optionalDependencies": {
57
+ "esbuild": "^0.9.7"
39
58
  },
40
59
  "engines": {
41
- "node": ">=14.15.4 <19"
60
+ "node": ">=16.0.0 <20"
42
61
  },
43
- "gitHead": "013fcee240ff2e1c23ef304e70fc39d6ef13fb8b"
62
+ "gitHead": "4e42b0dc5453f92b36b42aa8132c5bc281e616b7"
44
63
  }
@@ -1,3 +0,0 @@
1
- import { AbstractModuleId, BundleConfig, BundleDefinition, ModuleRegistry, NormalizedLwrGlobalConfig, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
- export declare function amdBundler(moduleId: AbstractModuleId, moduleRegistry: ModuleRegistry, runtimeEnvironment: RuntimeEnvironment, runtimeParams: RuntimeParams | undefined, { bundleConfig, amdLoader }: NormalizedLwrGlobalConfig, bundleConfigOverride?: BundleConfig): Promise<BundleDefinition>;
3
- //# sourceMappingURL=amd-bundler.d.ts.map
@@ -1,3 +0,0 @@
1
- import { AbstractModuleId, ModuleRegistry, BundleDefinition, RuntimeParams, SourceMapRuntimeEnvironment, BundleConfig, NormalizedLwrGlobalConfig } from '@lwrjs/types';
2
- export declare function esmBundler(moduleId: AbstractModuleId, moduleRegistry: ModuleRegistry, runtimeEnvironment: SourceMapRuntimeEnvironment, runtimeParams: RuntimeParams | undefined, lwrConfig: NormalizedLwrGlobalConfig, bundleConfigOverride?: BundleConfig): Promise<BundleDefinition>;
3
- //# sourceMappingURL=esm-bundler.d.ts.map