@lwrjs/module-bundler 0.8.0-alpha.1 → 0.8.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,60 +26,83 @@ __markAsModule(exports);
26
26
  __export(exports, {
27
27
  amdBundler: () => amdBundler
28
28
  });
29
+ var import_compiler = __toModule(require("@lwrjs/compiler"));
29
30
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
+ var import_rollup = __toModule(require("rollup"));
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
+ });
42
+ const {output} = await bundler.generate({
43
+ amd: {id, define: import_compiler.AMD_DEFINE},
44
+ exports: "named",
45
+ format: "amd"
46
+ });
47
+ return output[0].code;
48
+ }
30
49
  async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, {bundleConfig, amdLoader}, bundleConfigOverride) {
31
50
  const {exclude, external} = bundleConfigOverride ?? bundleConfig;
32
51
  const requiredImports = new Map();
33
52
  const dynamicImports = new Map();
34
- const visited = new Map();
35
- const moduleGraphs = await (0, import_shared_utils.getModuleGraphs)(moduleId.specifier, {
53
+ const includedModules = [];
54
+ const graphOptions = {
36
55
  includeLinkedDefinitions: true,
37
56
  depth: {
38
57
  static: import_shared_utils.GraphDepth.ALL,
39
58
  dynamic: 0,
40
59
  includeId: (moduleRef) => {
41
- if (exclude && exclude.includes(moduleRef.specifier) || moduleRef.specifier === amdLoader) {
60
+ if (exclude?.includes(moduleRef.specifier) || moduleRef.specifier === amdLoader) {
42
61
  requiredImports.set(`${moduleId.specifier}_${moduleId.version}`, moduleRef);
43
62
  return false;
44
63
  }
45
64
  return true;
46
65
  }
47
66
  }
48
- }, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams, visited);
67
+ };
68
+ const moduleGraphs = await (0, import_shared_utils.getModuleGraphs)(moduleId.specifier, graphOptions, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams);
49
69
  const rootModule = moduleGraphs.graphs[0];
50
- const rootModuleDef = moduleGraphs.linkedDefinitions[rootModule.specifier];
51
- const {id, name, namespace, version, specifier} = rootModuleDef;
52
- rootModuleDef?.linkedModuleRecord.dynamicImports?.forEach((e) => {
53
- dynamicImports.set(`${e.specifier}_${e.version}`, e);
54
- });
55
- moduleGraphs.graphs[0].static.forEach((m) => {
56
- const d = moduleGraphs.linkedDefinitions[m];
57
- d?.linkedModuleRecord.dynamicImports?.forEach((e) => {
58
- dynamicImports.set(`${e.specifier}_${e.version}`, e);
70
+ const modules = [rootModule.specifier, ...moduleGraphs.graphs[0].static];
71
+ const bundles = await Promise.all(modules.reduce((filteredModules, specifier2) => {
72
+ const linkedDefinition = moduleGraphs.linkedDefinitions[specifier2];
73
+ if (!linkedDefinition) {
74
+ return filteredModules;
75
+ }
76
+ linkedDefinition.linkedModuleRecord.dynamicImports?.forEach((dynamicImport) => {
77
+ if (dynamicImport.moduleNameType !== "unresolved") {
78
+ dynamicImports.set(`${dynamicImport.specifier}_${dynamicImport.version}`, dynamicImport);
79
+ }
59
80
  });
60
- });
61
- const code = [];
62
- const moduleSpecifiers = [];
63
- for (let i = rootModule.static.length - 1; i >= 0; i--) {
64
- const s = rootModule.static[i];
65
- if (moduleGraphs.linkedDefinitions[s]) {
66
- moduleSpecifiers.push(s);
67
- code.push(moduleGraphs.linkedDefinitions[s].linkedSource.trimEnd());
81
+ if (specifier2.includes("#")) {
82
+ return filteredModules;
68
83
  }
69
- }
70
- code.push(rootModuleDef.linkedSource.trimEnd());
84
+ filteredModules.unshift(linkedDefinition);
85
+ return filteredModules;
86
+ }, []).map((linkedDefinition) => {
87
+ const id2 = (0, import_shared_utils.getSpecifier)(linkedDefinition);
88
+ if (id2 !== rootModule.specifier) {
89
+ includedModules.push(id2);
90
+ }
91
+ return bundle(id2, moduleGraphs);
92
+ }));
93
+ const {id, name, namespace, version, specifier} = moduleGraphs.linkedDefinitions[rootModule.specifier];
71
94
  return {
72
95
  id,
73
96
  name,
74
97
  namespace,
75
98
  version,
76
99
  specifier,
77
- code: code.join(""),
100
+ code: bundles.join(""),
78
101
  config: {external, exclude},
79
102
  bundleRecord: {
80
103
  imports: Array.from(requiredImports.values()),
81
104
  dynamicImports: Array.from(dynamicImports.values()),
82
- includedModules: moduleSpecifiers
105
+ includedModules
83
106
  }
84
107
  };
85
108
  }
@@ -0,0 +1,37 @@
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/amd/rollup-amd-bundler-plugin.ts
9
+ __markAsModule(exports);
10
+ __export(exports, {
11
+ bundleDefinitions: () => bundleDefinitions
12
+ });
13
+ function bundleDefinitions(options) {
14
+ const {moduleGraphs} = options;
15
+ let root;
16
+ return {
17
+ name: "bundle-plugin",
18
+ resolveDynamicImport() {
19
+ return false;
20
+ },
21
+ resolveId(id, importer) {
22
+ if (!importer) {
23
+ root = id;
24
+ }
25
+ if (importer && id !== root && !id.includes("#")) {
26
+ return {
27
+ id,
28
+ external: true
29
+ };
30
+ }
31
+ return id;
32
+ },
33
+ async load(id) {
34
+ return moduleGraphs.linkedDefinitions[id].linkedSource;
35
+ }
36
+ };
37
+ }
@@ -26,9 +26,9 @@ __markAsModule(exports);
26
26
  __export(exports, {
27
27
  LwrModuleBundler: () => LwrModuleBundler
28
28
  });
29
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
29
30
  var import_amd_bundler = __toModule(require("./amd/amd-bundler.cjs"));
30
31
  var import_esm_bundler = __toModule(require("./esm/esm-bundler.cjs"));
31
- var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
32
32
  var TASK_POOL = new import_shared_utils.TaskPool();
33
33
  var LwrModuleBundler = class {
34
34
  constructor(config, globalConfig) {
@@ -37,6 +37,7 @@ var LwrModuleBundler = class {
37
37
  this.config = globalConfig;
38
38
  this.moduleRegistry = config.moduleRegistry;
39
39
  this.appObserver = config.appObserver;
40
+ this.compiler = config.compiler;
40
41
  this.appObserver.onModuleDefinitionChange(() => {
41
42
  this.cache.clear();
42
43
  });
@@ -58,16 +59,21 @@ var LwrModuleBundler = class {
58
59
  return bundleDef;
59
60
  }
60
61
  }
61
- const createBundlePromiseCtor = async () => {
62
+ return this.inflightBundleDefinitions.execute(cacheKey, () => {
62
63
  return TASK_POOL.execute(async () => {
63
- return 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).then((bundleDef) => {
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
+ pendingBundleDef.then(async (bundleDef) => {
66
+ const minified = !!minify && !debug;
67
+ if (minified) {
68
+ bundleDef.code = (await this.compiler.minifyJavascript(bundleDef.code)).code;
69
+ }
64
70
  if (!cacheDisabled) {
65
71
  this.cache.set(cacheKey, bundleDef);
66
72
  }
67
73
  return bundleDef;
68
74
  });
75
+ return pendingBundleDef;
69
76
  }, this);
70
- };
71
- return this.inflightBundleDefinitions.execute(cacheKey, createBundlePromiseCtor, this);
77
+ });
72
78
  }
73
79
  };
@@ -1,68 +1,88 @@
1
- import { getModuleGraphs, GraphDepth } from '@lwrjs/shared-utils';
1
+ import { AMD_DEFINE } from '@lwrjs/compiler';
2
+ import { GraphDepth, getModuleGraphs, getSpecifier } from '@lwrjs/shared-utils';
3
+ import { rollup } from 'rollup';
4
+ 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
+ });
14
+ const { output } = await bundler.generate({
15
+ amd: { id, define: AMD_DEFINE },
16
+ exports: 'named',
17
+ format: 'amd',
18
+ });
19
+ return output[0].code;
20
+ }
2
21
  export async function amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams = {}, { bundleConfig, amdLoader }, bundleConfigOverride) {
3
22
  const { exclude, external } = bundleConfigOverride ?? bundleConfig;
4
23
  const requiredImports = new Map();
5
24
  const dynamicImports = new Map();
6
- const visited = new Map();
7
- const moduleGraphs = await getModuleGraphs(moduleId.specifier, {
25
+ const includedModules = [];
26
+ const graphOptions = {
8
27
  includeLinkedDefinitions: true,
9
28
  depth: {
10
29
  static: GraphDepth.ALL,
11
30
  dynamic: 0,
12
31
  includeId: (moduleRef) => {
13
- if ((exclude && exclude.includes(moduleRef.specifier)) ||
14
- // loader should be auto bundled with shim already
15
- moduleRef.specifier === amdLoader) {
32
+ // loader should be auto bundled with shim already
33
+ if (exclude?.includes(moduleRef.specifier) || moduleRef.specifier === amdLoader) {
16
34
  requiredImports.set(`${moduleId.specifier}_${moduleId.version}`, moduleRef);
17
35
  return false;
18
36
  }
19
37
  return true;
20
38
  },
21
39
  },
22
- }, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams, visited);
40
+ };
41
+ const moduleGraphs = await getModuleGraphs(moduleId.specifier, graphOptions, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams);
23
42
  const rootModule = moduleGraphs.graphs[0];
24
- // type cast to LinkedModuleDefinition because only the moduleRegistry is used
25
- const rootModuleDef = moduleGraphs.linkedDefinitions[rootModule.specifier];
26
- const { id, name, namespace, version, specifier } = rootModuleDef;
27
- // Collect dynamic imports
28
- // Add dynamic imports from the root module
29
- rootModuleDef?.linkedModuleRecord.dynamicImports?.forEach((e) => {
30
- dynamicImports.set(`${e.specifier}_${e.version}`, e);
31
- });
32
- // Add any dynamic imports from each of the linked static imports in the moduleGraph
33
- moduleGraphs.graphs[0].static.forEach((m) => {
34
- // type cast to LinkedModuleDefinition because only the moduleRegistry is used
35
- const d = moduleGraphs.linkedDefinitions[m];
36
- // D would be null if excluded from the bundle
37
- d?.linkedModuleRecord.dynamicImports?.forEach((e) => {
38
- dynamicImports.set(`${e.specifier}_${e.version}`, e);
43
+ const modules = [rootModule.specifier, ...moduleGraphs.graphs[0].static];
44
+ const bundles = await Promise.all(modules
45
+ .reduce((filteredModules, specifier) => {
46
+ const linkedDefinition = moduleGraphs.linkedDefinitions[specifier];
47
+ // skip modules that do not have a linked definition
48
+ if (!linkedDefinition) {
49
+ return filteredModules;
50
+ }
51
+ // add any dynamic imports from each of the linked definitions in the module graph
52
+ linkedDefinition.linkedModuleRecord.dynamicImports?.forEach((dynamicImport) => {
53
+ if (dynamicImport.moduleNameType !== 'unresolved') {
54
+ dynamicImports.set(`${dynamicImport.specifier}_${dynamicImport.version}`, dynamicImport);
55
+ }
39
56
  });
40
- });
41
- // loop in reverse for correct order
42
- const code = [];
43
- const moduleSpecifiers = [];
44
- for (let i = rootModule.static.length - 1; i >= 0; i--) {
45
- const s = rootModule.static[i];
46
- // only included modules will be present in the linkedDefinitions
47
- if (moduleGraphs.linkedDefinitions[s]) {
48
- moduleSpecifiers.push(s);
49
- // type cast to LinkedModuleDefinition becuase only the moduleRegistry is used
50
- code.push(moduleGraphs.linkedDefinitions[s].linkedSource.trimEnd());
57
+ // skip relative dependencies
58
+ if (specifier.includes('#')) {
59
+ return filteredModules;
60
+ }
61
+ // prepend the linked definition for the correct order
62
+ filteredModules.unshift(linkedDefinition);
63
+ return filteredModules;
64
+ }, [])
65
+ .map((linkedDefinition) => {
66
+ const id = getSpecifier(linkedDefinition);
67
+ if (id !== rootModule.specifier) {
68
+ includedModules.push(id);
51
69
  }
52
- }
53
- code.push(rootModuleDef.linkedSource.trimEnd());
70
+ // bundle all dependencies for the linked definition and convert to AMD
71
+ return bundle(id, moduleGraphs);
72
+ }));
73
+ const { id, name, namespace, version, specifier } = moduleGraphs.linkedDefinitions[rootModule.specifier];
54
74
  return {
55
75
  id,
56
76
  name,
57
77
  namespace,
58
78
  version,
59
79
  specifier,
60
- code: code.join(''),
80
+ code: bundles.join(''),
61
81
  config: { external, exclude },
62
82
  bundleRecord: {
63
83
  imports: Array.from(requiredImports.values()),
64
84
  dynamicImports: Array.from(dynamicImports.values()),
65
- includedModules: moduleSpecifiers,
85
+ includedModules,
66
86
  },
67
87
  };
68
88
  }
@@ -0,0 +1,7 @@
1
+ import { Plugin } from 'rollup';
2
+ import { FlattenedModuleGraphs } from '@lwrjs/types';
3
+ export interface RollupBundlePluginOptions {
4
+ moduleGraphs: FlattenedModuleGraphs;
5
+ }
6
+ export declare function bundleDefinitions(options: RollupBundlePluginOptions): Plugin;
7
+ //# sourceMappingURL=rollup-amd-bundler-plugin.d.ts.map
@@ -0,0 +1,27 @@
1
+ export function bundleDefinitions(options) {
2
+ const { moduleGraphs } = options;
3
+ let root;
4
+ return {
5
+ name: 'bundle-plugin',
6
+ resolveDynamicImport() {
7
+ // NOTE: This might be configurable in the future
8
+ return false;
9
+ },
10
+ resolveId(id, importer) {
11
+ if (!importer) {
12
+ root = id;
13
+ }
14
+ if (importer && id !== root && !id.includes('#')) {
15
+ return {
16
+ id,
17
+ external: true,
18
+ };
19
+ }
20
+ return id;
21
+ },
22
+ async load(id) {
23
+ return moduleGraphs.linkedDefinitions[id].linkedSource;
24
+ },
25
+ };
26
+ }
27
+ //# sourceMappingURL=rollup-amd-bundler-plugin.js.map
@@ -1,9 +1,11 @@
1
- import { AbstractModuleId, BundleConfig, BundleDefinition, LwrAppObserver, ModuleBundler, ModuleRegistry, NormalizedLwrGlobalConfig, RuntimeParams, SourceMapRuntimeEnvironment } from '@lwrjs/types';
1
+ import { AbstractModuleId, BundleConfig, BundleDefinition, LwrAppObserver, ModuleBundler, ModuleRegistry, NormalizedLwrGlobalConfig, RuntimeParams, SourceMapRuntimeEnvironment, Compiler } from '@lwrjs/types';
2
2
  interface LwrModuleBundlerConfig {
3
+ compiler: Compiler;
3
4
  moduleRegistry: ModuleRegistry;
4
5
  appObserver: LwrAppObserver;
5
6
  }
6
7
  export declare class LwrModuleBundler implements ModuleBundler {
8
+ compiler: Compiler;
7
9
  moduleRegistry: ModuleRegistry;
8
10
  appObserver: LwrAppObserver;
9
11
  config: NormalizedLwrGlobalConfig;
package/build/es/index.js CHANGED
@@ -1,6 +1,6 @@
1
+ import { getCacheKeyFromJson, InflightTasks, TaskPool } from '@lwrjs/shared-utils';
1
2
  import { amdBundler } from './amd/amd-bundler.js';
2
3
  import { esmBundler } from './esm/esm-bundler.js';
3
- import { getCacheKeyFromJson, InflightTasks, TaskPool } from '@lwrjs/shared-utils';
4
4
  const TASK_POOL = new TaskPool();
5
5
  export class LwrModuleBundler {
6
6
  constructor(config, globalConfig) {
@@ -12,6 +12,7 @@ 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;
15
16
  this.appObserver.onModuleDefinitionChange(() => {
16
17
  // TODO: This is a very naive approach however
17
18
  // this would only happen in non-prod environments
@@ -38,23 +39,27 @@ export class LwrModuleBundler {
38
39
  return bundleDef;
39
40
  }
40
41
  }
41
- // Generate the bundle definition
42
- const createBundlePromiseCtor = async () => {
42
+ return this.inflightBundleDefinitions.execute(cacheKey, () => {
43
43
  // TODO add to profiling
44
44
  // console.log('[INFO] Create Bundle: ', cacheKey);
45
45
  // Run theses tasks in a task pool to throttle parallel requests.
46
46
  return TASK_POOL.execute(async () => {
47
- return format === 'amd'
47
+ const pendingBundleDef = format === 'amd'
48
48
  ? amdBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides)
49
- : esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides).then((bundleDef) => {
50
- if (!cacheDisabled) {
51
- this.cache.set(cacheKey, bundleDef);
52
- }
53
- return bundleDef;
54
- });
49
+ : esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams, this.config, bundleConfigOverrides);
50
+ 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);
57
+ }
58
+ return bundleDef;
59
+ });
60
+ return pendingBundleDef;
55
61
  }, this);
56
- };
57
- return this.inflightBundleDefinitions.execute(cacheKey, createBundlePromiseCtor, this);
62
+ });
58
63
  }
59
64
  }
60
65
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.8.0-alpha.1",
7
+ "version": "0.8.0-alpha.4",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -30,14 +30,15 @@
30
30
  "build/**/*.d.ts"
31
31
  ],
32
32
  "dependencies": {
33
- "@lwrjs/shared-utils": "0.8.0-alpha.1",
33
+ "@lwrjs/compiler": "0.8.0-alpha.4",
34
+ "@lwrjs/shared-utils": "0.8.0-alpha.4",
34
35
  "rollup": "~2.45.2"
35
36
  },
36
37
  "devDependencies": {
37
- "@lwrjs/types": "0.8.0-alpha.1"
38
+ "@lwrjs/types": "0.8.0-alpha.4"
38
39
  },
39
40
  "engines": {
40
41
  "node": ">=14.15.4 <19"
41
42
  },
42
- "gitHead": "2718cc5abe11d0d6fd5375cf9205167b6f69e244"
43
+ "gitHead": "a0cca59d2ab1e7596f03812d00180f898217ce4d"
43
44
  }