@lwrjs/module-bundler 0.9.0-alpha.13 → 0.9.0-alpha.14

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 (27) hide show
  1. package/build/cjs/amd-bundle-provider.cjs +50 -0
  2. package/build/cjs/amd-runtime-bundle-provider.cjs +45 -0
  3. package/build/cjs/{esm/esm-bundler.cjs → esm-bundle-provider.cjs} +26 -6
  4. package/build/cjs/index.cjs +19 -14
  5. package/build/cjs/{amd/amd-bundler.cjs → utils/amd-common.cjs} +14 -14
  6. package/build/cjs/utils/esbuild-utils.cjs +50 -0
  7. package/build/cjs/{amd → utils}/rollup-amd-bundler-plugin.cjs +1 -1
  8. package/build/cjs/{esm → utils}/rollup-esm-bundler-plugin.cjs +1 -1
  9. package/build/es/amd-bundle-provider.d.ts +9 -0
  10. package/build/es/amd-bundle-provider.js +24 -0
  11. package/build/es/amd-runtime-bundle-provider.d.ts +9 -0
  12. package/build/es/amd-runtime-bundle-provider.js +18 -0
  13. package/build/es/esm-bundle-provider.d.ts +9 -0
  14. package/build/es/{esm/esm-bundler.js → esm-bundle-provider.js} +24 -5
  15. package/build/es/index.d.ts +12 -3
  16. package/build/es/index.js +30 -16
  17. package/build/es/utils/amd-common.d.ts +3 -0
  18. package/build/es/{amd/amd-bundler.js → utils/amd-common.js} +14 -13
  19. package/build/es/utils/esbuild-utils.d.ts +6 -0
  20. package/build/es/utils/esbuild-utils.js +34 -0
  21. package/build/es/{amd → utils}/rollup-amd-bundler-plugin.d.ts +0 -0
  22. package/build/es/{amd → utils}/rollup-amd-bundler-plugin.js +0 -0
  23. package/build/es/{esm → utils}/rollup-esm-bundler-plugin.d.ts +0 -0
  24. package/build/es/{esm → utils}/rollup-esm-bundler-plugin.js +0 -0
  25. package/package.json +22 -6
  26. package/build/es/amd/amd-bundler.d.ts +0 -3
  27. package/build/es/esm/esm-bundler.d.ts +0 -3
@@ -0,0 +1,50 @@
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 {minify, debug} = runtimeEnvironment;
42
+ const minified = !!minify && !debug;
43
+ if (minified) {
44
+ bundle.code = await (0, import_esbuild_utils.minifyJavascript)(bundle.code);
45
+ }
46
+ return bundle;
47
+ }
48
+ }
49
+ };
50
+ 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,30 @@ 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 EsmBundlerProvider = class {
33
+ constructor(options, {config, moduleRegistry}) {
34
+ this.name = "esm-bundle-provider";
35
+ this.config = config;
36
+ this.moduleRegistry = moduleRegistry;
37
+ }
38
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
39
+ if (runtimeEnvironment.format === "esm") {
40
+ const {moduleRegistry, config} = this;
41
+ return await esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
42
+ }
43
+ }
44
+ };
45
+ var esm_bundle_provider_default = EsmBundlerProvider;
46
+ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, {bundleConfig}, bundleConfigOverride) {
47
+ const {exclude, external = {}, alias = {}} = bundleConfigOverride ?? bundleConfig;
33
48
  const {sourceMapUrl} = runtimeEnvironment;
34
49
  const requiredImports = new Map();
35
50
  const dynamicImports = new Map();
@@ -58,9 +73,14 @@ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
58
73
  });
59
74
  const bundleCode = result.output[0].code;
60
75
  const bundleMap = result.output[0].map;
61
- const code = sourceMapUrl ? `${bundleCode}
76
+ let code = sourceMapUrl ? `${bundleCode}
62
77
  //# sourceMappingURL=${sourceMapUrl}` : bundleCode;
63
78
  const {id, specifier, namespace, name, version} = rootModuleDef;
79
+ const {minify, debug} = runtimeEnvironment;
80
+ const minified = !!minify && !debug;
81
+ if (minified) {
82
+ code = await (0, import_esbuild_utils.minifyJavascript)(code);
83
+ }
64
84
  return {
65
85
  id,
66
86
  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
37
  this.config = globalConfig;
38
38
  this.moduleRegistry = config.moduleRegistry;
39
39
  this.appObserver = config.appObserver;
40
- this.compiler = config.compiler;
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,22 @@ 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
+ resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature) {
81
+ return this.moduleRegistry.resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature);
82
+ }
78
83
  };
@@ -21,32 +21,32 @@ 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
+ var import_rollup_plugin_terser = __toModule(require("rollup-plugin-terser"));
32
33
  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
- });
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_rollup_plugin_terser.terser)(), (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) {
49
+ async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, {bundleConfig, amdLoader}, bundleConfigOverride) {
50
50
  const {exclude, external} = bundleConfigOverride ?? bundleConfig;
51
51
  const requiredImports = new Map();
52
52
  const dynamicImports = new Map();
@@ -88,7 +88,7 @@ async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
88
88
  if (id2 !== rootModule.specifier) {
89
89
  includedModules.push(id2);
90
90
  }
91
- return bundle(id2, moduleGraphs);
91
+ return bundle(id2, moduleGraphs, minify);
92
92
  }));
93
93
  const {id, name, namespace, version, specifier} = moduleGraphs.linkedDefinitions[rootModule.specifier];
94
94
  return {
@@ -0,0 +1,50 @@
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
+ });
29
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
30
+ var import_esbuild = __toModule(require("esbuild"));
31
+ var esbuild = import_esbuild.default;
32
+ if (!import_esbuild.default) {
33
+ try {
34
+ esbuild = require("esbuild");
35
+ } catch {
36
+ }
37
+ }
38
+ async function minifyJavascript(source) {
39
+ try {
40
+ const {code} = await esbuild.transform(source, {
41
+ loader: "js",
42
+ minify: true,
43
+ define: {"process.env.NODE_ENV": JSON.stringify("production")}
44
+ });
45
+ return code;
46
+ } catch (error) {
47
+ import_shared_utils.logger.debug(error);
48
+ throw error;
49
+ }
50
+ }
@@ -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, BundleConfig, 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?: BundleConfig): Promise<BundleDefinition | undefined>;
8
+ }
9
+ //# sourceMappingURL=amd-bundle-provider.d.ts.map
@@ -0,0 +1,24 @@
1
+ import { amdBundler } from './utils/amd-common.js';
2
+ import { minifyJavascript } 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 } = runtimeEnvironment;
16
+ const minified = !!minify && !debug;
17
+ if (minified) {
18
+ bundle.code = await minifyJavascript(bundle.code);
19
+ }
20
+ return bundle;
21
+ }
22
+ }
23
+ }
24
+ //# sourceMappingURL=amd-bundle-provider.js.map
@@ -0,0 +1,9 @@
1
+ import { AbstractModuleId, BundleConfig, 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?: BundleConfig): 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, BundleConfig, 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?: BundleConfig): Promise<BundleDefinition | undefined>;
8
+ }
9
+ //# sourceMappingURL=esm-bundle-provider.d.ts.map
@@ -1,7 +1,21 @@
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
+ export default class EsmBundlerProvider {
5
+ constructor(options, { config, moduleRegistry }) {
6
+ this.name = 'esm-bundle-provider';
7
+ this.config = config;
8
+ this.moduleRegistry = moduleRegistry;
9
+ }
10
+ async bundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
11
+ if (runtimeEnvironment.format === 'esm') {
12
+ const { moduleRegistry, config } = this;
13
+ return await esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
14
+ }
15
+ }
16
+ }
17
+ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, { bundleConfig }, bundleConfigOverride) {
18
+ const { exclude, external = {}, alias = {} } = bundleConfigOverride ?? bundleConfig;
5
19
  const { sourceMapUrl } = runtimeEnvironment;
6
20
  const requiredImports = new Map();
7
21
  const dynamicImports = new Map();
@@ -32,8 +46,13 @@ export async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, r
32
46
  });
33
47
  const bundleCode = result.output[0].code;
34
48
  const bundleMap = result.output[0].map;
35
- const code = sourceMapUrl ? `${bundleCode}\n//# sourceMappingURL=${sourceMapUrl}` : bundleCode;
49
+ let code = sourceMapUrl ? `${bundleCode}\n//# sourceMappingURL=${sourceMapUrl}` : bundleCode;
36
50
  const { id, specifier, namespace, name, version } = rootModuleDef;
51
+ const { minify, debug } = runtimeEnvironment;
52
+ const minified = !!minify && !debug;
53
+ if (minified) {
54
+ code = await minifyJavascript(code);
55
+ }
37
56
  return {
38
57
  id,
39
58
  specifier,
@@ -50,4 +69,4 @@ export async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, r
50
69
  },
51
70
  };
52
71
  }
53
- //# sourceMappingURL=esm-bundler.js.map
72
+ //# sourceMappingURL=esm-bundle-provider.js.map
@@ -1,18 +1,27 @@
1
- import { AbstractModuleId, BundleConfig, BundleDefinition, LwrAppObserver, ModuleBundler, ModuleRegistry, NormalizedLwrGlobalConfig, RuntimeParams, SourceMapRuntimeEnvironment, Compiler } from '@lwrjs/types';
1
+ import { AbstractModuleId, BundleConfig, BundleDefinition, BundleProvider, LwrAppObserver, ModuleBundler, ModuleId, ModuleRegistry, NormalizedLwrGlobalConfig, 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
9
  config: NormalizedLwrGlobalConfig;
12
10
  cache: Map<string, BundleDefinition>;
11
+ providers: BundleProvider[];
13
12
  private inflightBundleDefinitions;
14
13
  constructor(config: LwrModuleBundlerConfig, globalConfig: NormalizedLwrGlobalConfig);
14
+ addBundleProviders(providers: BundleProvider[]): void;
15
15
  getModuleBundle<T extends AbstractModuleId>(moduleId: T, runtimeEnvironment: SourceMapRuntimeEnvironment, runtimeParams?: RuntimeParams, bundleConfigOverrides?: BundleConfig): 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<R extends RuntimeEnvironment, S extends string | undefined>(moduleId: Required<Pick<ModuleId, 'specifier' | 'version'>>, runtimeEnvironment: R, runtimeParams?: RuntimeParams, signature?: S): S extends string ? string : Promise<string>;
16
25
  }
17
26
  export {};
18
27
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,10 +1,10 @@
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.
@@ -12,15 +12,16 @@ export class LwrModuleBundler {
12
12
  this.config = globalConfig;
13
13
  this.moduleRegistry = config.moduleRegistry;
14
14
  this.appObserver = config.appObserver;
15
- this.compiler = config.compiler;
16
15
  this.appObserver?.onModuleDefinitionChange(() => {
17
16
  // TODO: This is a very naive approach however
18
17
  // this would only happen in non-prod environments
19
18
  this.cache.clear();
20
19
  });
21
20
  }
21
+ addBundleProviders(providers) {
22
+ this.providers.push(...providers);
23
+ }
22
24
  async getModuleBundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
23
- const { moduleRegistry } = this;
24
25
  const { format, minify, debug } = runtimeEnvironment;
25
26
  const cacheKey = `${moduleId.specifier}|${moduleId.version}|${getCacheKeyFromJson({
26
27
  locale: runtimeParams.locale,
@@ -44,21 +45,34 @@ export class LwrModuleBundler {
44
45
  // console.log('[INFO] Create Bundle: ', cacheKey);
45
46
  // Run theses tasks in a task pool to throttle parallel requests.
46
47
  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);
48
+ for (const bundler of this.providers) {
49
+ // eslint-disable-next-line no-await-in-loop
50
+ const bundleDef = await bundler.bundle(moduleId, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
51
+ if (bundleDef) {
52
+ if (!cacheDisabled) {
53
+ this.cache.set(cacheKey, bundleDef);
54
+ }
55
+ return bundleDef;
57
56
  }
58
- return bundleDef;
59
- });
57
+ }
58
+ throw createSingleDiagnosticError({
59
+ description: descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier),
60
+ }, LwrUnresolvableError);
60
61
  }, this);
61
62
  });
62
63
  }
64
+ /**
65
+ * Resolve the URI to the bundle rooted at the `moduleId`
66
+ * @param moduleId - The id of the root module for the bundle
67
+ * @param runtimeEnvironment - The runtime operating environnment
68
+ * @param runtimeParams - The available runtime parameters provided in context to the request
69
+ * @param signature - The signature of the bundle instance being referenced
70
+ * @returns the URI
71
+ */
72
+ resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature) {
73
+ // TODO: Integrate resolving from a site caches.
74
+ // Fallback to resolving the URI from the registry.
75
+ return this.moduleRegistry.resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature);
76
+ }
63
77
  }
64
78
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { AbstractModuleId, BundleConfig, 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, amdLoader }: ProviderAppConfig, bundleConfigOverride?: BundleConfig): Promise<BundleDefinition>;
3
+ //# sourceMappingURL=amd-common.d.ts.map
@@ -1,16 +1,17 @@
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
+ const AMD_DEFINE = 'LWR.define';
7
+ async function bundle(id, moduleGraphs, minify = false) {
8
+ const plugins = [bundleDefinitions({ moduleGraphs })];
9
+ minify &&
10
+ plugins.push(terser(), replace({
11
+ 'process.env.NODE_ENV': JSON.stringify('production'),
12
+ preventAssignment: false,
13
+ }));
14
+ const bundler = await rollup({ input: id, plugins });
14
15
  const { output } = await bundler.generate({
15
16
  amd: { id, define: AMD_DEFINE },
16
17
  exports: 'named',
@@ -18,7 +19,7 @@ async function bundle(id, moduleGraphs) {
18
19
  });
19
20
  return output[0].code;
20
21
  }
21
- export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, { bundleConfig, amdLoader }, bundleConfigOverride) {
22
+ export async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, { bundleConfig, amdLoader }, bundleConfigOverride) {
22
23
  const { exclude, external } = bundleConfigOverride ?? bundleConfig;
23
24
  const requiredImports = new Map();
24
25
  const dynamicImports = new Map();
@@ -68,7 +69,7 @@ export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, r
68
69
  includedModules.push(id);
69
70
  }
70
71
  // bundle all dependencies for the linked definition and convert to AMD
71
- return bundle(id, moduleGraphs);
72
+ return bundle(id, moduleGraphs, minify);
72
73
  }));
73
74
  const { id, name, namespace, version, specifier } = moduleGraphs.linkedDefinitions[rootModule.specifier];
74
75
  return {
@@ -86,4 +87,4 @@ export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, r
86
87
  },
87
88
  };
88
89
  }
89
- //# sourceMappingURL=amd-bundler.js.map
90
+ //# sourceMappingURL=amd-common.js.map
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Minification via esbuild.
3
+ * When importing this module make sure the optional library 'esbuild' is installed.
4
+ */
5
+ export declare function minifyJavascript(source: string): Promise<string>;
6
+ //# sourceMappingURL=esbuild-utils.d.ts.map
@@ -0,0 +1,34 @@
1
+ import { logger } from '@lwrjs/shared-utils';
2
+ import esbuildEsm from 'esbuild';
3
+ // https://github.com/evanw/esbuild/issues/706
4
+ // Fixed in 0.11.0 but upgrading past 0.9.7 has caused breaking changes for consumers...
5
+ // https://github.com/salesforce-experience-platform/lwr/issues/1014
6
+ let esbuild = esbuildEsm;
7
+ if (!esbuildEsm) {
8
+ try {
9
+ esbuild = require('esbuild');
10
+ }
11
+ catch {
12
+ /* this is to support mjs/cjs dual impl */
13
+ }
14
+ }
15
+ /**
16
+ * Minification via esbuild.
17
+ * When importing this module make sure the optional library 'esbuild' is installed.
18
+ */
19
+ export async function minifyJavascript(source) {
20
+ try {
21
+ const { code } = await esbuild.transform(source, {
22
+ loader: 'js',
23
+ minify: true,
24
+ // Remove assertions by tree shaking the computed expression
25
+ define: { 'process.env.NODE_ENV': JSON.stringify('production') },
26
+ });
27
+ return code;
28
+ }
29
+ catch (error) {
30
+ logger.debug(error);
31
+ throw error;
32
+ }
33
+ }
34
+ //# sourceMappingURL=esbuild-utils.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.9.0-alpha.13",
7
+ "version": "0.9.0-alpha.14",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -22,6 +22,18 @@
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
  },
27
39
  "files": [
@@ -30,15 +42,19 @@
30
42
  "build/**/*.d.ts"
31
43
  ],
32
44
  "dependencies": {
33
- "@lwrjs/compiler": "0.9.0-alpha.13",
34
- "@lwrjs/shared-utils": "0.9.0-alpha.13",
35
- "rollup": "~2.45.2"
45
+ "@lwrjs/shared-utils": "0.9.0-alpha.14",
46
+ "@rollup/plugin-replace": "^2.4.2",
47
+ "rollup": "~2.45.2",
48
+ "rollup-plugin-terser": "^7.0.2"
36
49
  },
37
50
  "devDependencies": {
38
- "@lwrjs/types": "0.9.0-alpha.13"
51
+ "@lwrjs/types": "0.9.0-alpha.14"
52
+ },
53
+ "optionalDependencies": {
54
+ "esbuild": "^0.9.7"
39
55
  },
40
56
  "engines": {
41
57
  "node": ">=14.15.4 <19"
42
58
  },
43
- "gitHead": "fa30915a685f6e8c5c2895d0c053d59145780123"
59
+ "gitHead": "64e0ba617151429da6e09f1a9686628f64183d25"
44
60
  }
@@ -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