@lwrjs/module-bundler 0.10.0-alpha.13 → 0.10.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.
@@ -32,7 +32,8 @@ 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
33
  var import_bundle_common = __toModule(require("./bundle-common.cjs"));
34
34
  var AMD_DEFINE = "LWR.define";
35
- async function bundle(id, moduleGraphs, minify = false) {
35
+ var groupieCodeCache = new Map();
36
+ async function bundle(id, moduleGraphs, minify = false, unVersionedAliases = false) {
36
37
  const plugins = [(0, import_rollup_amd_bundler_plugin.bundleDefinitions)({moduleGraphs})];
37
38
  minify && plugins.push((0, import_plugin_replace.default)({
38
39
  "process.env.NODE_ENV": JSON.stringify("production"),
@@ -44,35 +45,18 @@ async function bundle(id, moduleGraphs, minify = false) {
44
45
  exports: "named",
45
46
  format: "amd"
46
47
  });
48
+ if (unVersionedAliases) {
49
+ const idObject = (0, import_shared_utils.explodeSpecifier)(id);
50
+ const specifier = idObject.specifier;
51
+ const aliasModule = ` LWR.define('${specifier}',['${id}'],function(m){return m;});`;
52
+ output[0].code += aliasModule;
53
+ }
47
54
  return output[0].code;
48
55
  }
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);
52
- const requiredImports = new Map();
53
- const dynamicImports = new Map();
54
- const includedModules = [];
55
- const graphOptions = {
56
- includeLinkedDefinitions: true,
57
- depth: {
58
- static: import_shared_utils.GraphDepth.ALL,
59
- dynamic: 0,
60
- includeId: (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);
65
- return false;
66
- }
67
- return true;
68
- }
69
- }
70
- };
71
- const moduleGraphs = await (0, import_shared_utils.getModuleGraphs)(moduleId.specifier, graphOptions, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams);
72
- const rootModule = moduleGraphs.graphs[0];
73
- const modules = [rootModule.specifier, ...moduleGraphs.graphs[0].static];
74
- const bundles = await Promise.all(modules.reduce((filteredModules, specifier2) => {
75
- const linkedDefinition = moduleGraphs.linkedDefinitions[specifier2];
56
+ async function getBundleCode(rootModule, moduleGraphs, includedModules, bundleGroupsIncludedModules, dynamicImports, minify, unVersionedAliases) {
57
+ const modules = [rootModule, ...moduleGraphs.graphs[0].static];
58
+ const bundles = await Promise.all(modules.reduce((filteredModules, specifier) => {
59
+ const linkedDefinition = moduleGraphs.linkedDefinitions[specifier];
76
60
  if (!linkedDefinition) {
77
61
  return filteredModules;
78
62
  }
@@ -81,18 +65,95 @@ async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvir
81
65
  dynamicImports.set(`${dynamicImport.specifier}_${dynamicImport.version}`, dynamicImport);
82
66
  }
83
67
  });
84
- if (specifier2.includes("#")) {
68
+ if (specifier.includes("#")) {
85
69
  return filteredModules;
86
70
  }
87
71
  filteredModules.unshift(linkedDefinition);
88
72
  return filteredModules;
89
73
  }, []).map((linkedDefinition) => {
90
- const id2 = (0, import_shared_utils.getSpecifier)(linkedDefinition);
91
- if (id2 !== rootModule.specifier) {
92
- includedModules.push(id2);
74
+ const id = (0, import_shared_utils.getSpecifier)(linkedDefinition);
75
+ if (id !== rootModule) {
76
+ includedModules.push(id);
77
+ } else {
78
+ bundleGroupsIncludedModules.push(id);
93
79
  }
94
- return bundle(id2, moduleGraphs, minify);
80
+ return bundle(id, moduleGraphs, minify, unVersionedAliases);
95
81
  }));
82
+ return bundles;
83
+ }
84
+ async function amdBundler(rootModuleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, {bundleConfig}, bundleConfigOverrides) {
85
+ const {exclude, external = {}, groups = {}} = (0, import_bundle_common.overrideBundleConfig)(bundleConfig, bundleConfigOverrides);
86
+ const externalsArray = Object.keys(external);
87
+ const requiredImports = new Map();
88
+ const dynamicImports = new Map();
89
+ const groupName = (0, import_shared_utils.getGroupName)(rootModuleId.specifier, groups);
90
+ const groupies = groupName && groups[groupName];
91
+ const cachedGroupieCode = groupName && groupieCodeCache.get(groupName);
92
+ const getModuleGraphsWrapper = (graphSpecifier) => {
93
+ const graphOptions = {
94
+ includeLinkedDefinitions: true,
95
+ depth: {
96
+ static: import_shared_utils.GraphDepth.ALL,
97
+ dynamic: 0,
98
+ includeId: (moduleRef) => {
99
+ if (externalsArray.includes(moduleRef.specifier)) {
100
+ return false;
101
+ }
102
+ const moduleRefIsGroupie = (0, import_shared_utils.isGroupie)(moduleRef.specifier, groups);
103
+ const rootModuleIsGroupie = (0, import_shared_utils.isGroupie)(graphSpecifier, groups);
104
+ const moduleIsNotRoot = graphSpecifier !== moduleRef.specifier;
105
+ if (moduleRefIsGroupie) {
106
+ if (moduleIsNotRoot && !rootModuleIsGroupie) {
107
+ requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
108
+ }
109
+ return false;
110
+ } else if (exclude?.includes(moduleRef.specifier)) {
111
+ if (moduleIsNotRoot) {
112
+ requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
113
+ }
114
+ return false;
115
+ }
116
+ return true;
117
+ }
118
+ }
119
+ };
120
+ return (0, import_shared_utils.getModuleGraphs)(graphSpecifier, graphOptions, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams);
121
+ };
122
+ const moduleGraphs = await getModuleGraphsWrapper(rootModuleId.specifier);
123
+ const rootModule = moduleGraphs.graphs[0];
124
+ const groupModuleGraphsMap = new Map();
125
+ if (!cachedGroupieCode && groupies && groupies.length) {
126
+ for (const specifier2 of groupies) {
127
+ if (specifier2 !== rootModuleId.specifier) {
128
+ const groupieModuleGraph = await getModuleGraphsWrapper(specifier2);
129
+ groupModuleGraphsMap.set(specifier2, groupieModuleGraph);
130
+ }
131
+ }
132
+ }
133
+ let bundleCode, includedModules, bundleGroupsIncludedModules;
134
+ const cachedBundleGroupCode = cachedGroupieCode;
135
+ if (!cachedBundleGroupCode) {
136
+ includedModules = [], bundleGroupsIncludedModules = [];
137
+ const bundles = await getBundleCode(rootModule.specifier, moduleGraphs, includedModules, bundleGroupsIncludedModules, dynamicImports, minify, !!runtimeEnvironment.featureFlags?.UNVERSIONED_ALIASES);
138
+ if (groupName) {
139
+ for (const rawGroupieSpecifier of groupies) {
140
+ if (rawGroupieSpecifier !== rootModuleId.specifier) {
141
+ const groupieModuleGraph = groupModuleGraphsMap.get(rawGroupieSpecifier);
142
+ const groupieSpecifer = groupieModuleGraph.graphs[0].specifier;
143
+ const groupieBundles = await getBundleCode(groupieSpecifer, groupieModuleGraph, includedModules, bundleGroupsIncludedModules, dynamicImports, minify, !!runtimeEnvironment.featureFlags?.UNVERSIONED_ALIASES);
144
+ bundles.push(...groupieBundles);
145
+ }
146
+ }
147
+ }
148
+ bundleCode = bundles.join("");
149
+ if (groupName) {
150
+ includedModules.push(...bundleGroupsIncludedModules);
151
+ groupieCodeCache.set(groupName, {code: bundleCode, includedModules});
152
+ }
153
+ } else {
154
+ bundleCode = cachedBundleGroupCode.code;
155
+ includedModules = cachedBundleGroupCode.includedModules;
156
+ }
96
157
  const {id, name, namespace, version, specifier} = moduleGraphs.linkedDefinitions[rootModule.specifier];
97
158
  return {
98
159
  id,
@@ -100,7 +161,7 @@ async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvir
100
161
  namespace,
101
162
  version,
102
163
  specifier,
103
- code: bundles.join(""),
164
+ code: bundleCode,
104
165
  config: {external, exclude},
105
166
  bundleRecord: {
106
167
  imports: Array.from(requiredImports.values()),
@@ -1,3 +1,3 @@
1
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>;
2
+ export declare function amdBundler(rootModuleId: AbstractModuleId, moduleRegistry: ModuleRegistry, minify: boolean | undefined, runtimeEnvironment: RuntimeEnvironment, runtimeParams: RuntimeParams | undefined, { bundleConfig }: ProviderAppConfig, bundleConfigOverrides?: BundleConfigOverrides): Promise<BundleDefinition>;
3
3
  //# sourceMappingURL=amd-common.d.ts.map
@@ -1,14 +1,15 @@
1
- import { GraphDepth, getModuleGraphs, getSpecifier } from '@lwrjs/shared-utils';
1
+ import { GraphDepth, explodeSpecifier, getModuleGraphs, getSpecifier, getGroupName, isGroupie, } from '@lwrjs/shared-utils';
2
2
  import { rollup } from 'rollup';
3
3
  import replace from '@rollup/plugin-replace';
4
4
  import { bundleDefinitions } from './rollup-amd-bundler-plugin.js';
5
5
  import { overrideBundleConfig } from './bundle-common.js';
6
6
  const AMD_DEFINE = 'LWR.define';
7
- async function bundle(id, moduleGraphs, minify = false) {
7
+ const groupieCodeCache = new Map();
8
+ async function bundle(id, moduleGraphs, minify = false, unVersionedAliases = false) {
8
9
  const plugins = [bundleDefinitions({ moduleGraphs })];
9
10
  minify &&
10
11
  plugins.push(
11
- // terser(), // Do not minify until needed for client modules. Server/SSR modules do not need to be minifed.
12
+ // terser(), // Do not minify until needed for client modules. Server/SSR modules do not need to be minified.
12
13
  replace({
13
14
  'process.env.NODE_ENV': JSON.stringify('production'),
14
15
  preventAssignment: false,
@@ -19,39 +20,16 @@ async function bundle(id, moduleGraphs, minify = false) {
19
20
  exports: 'named',
20
21
  format: 'amd',
21
22
  });
23
+ if (unVersionedAliases) {
24
+ const idObject = explodeSpecifier(id);
25
+ const specifier = idObject.specifier;
26
+ const aliasModule = ` LWR.define('${specifier}',['${id}'],function(m){return m;});`;
27
+ output[0].code += aliasModule;
28
+ }
22
29
  return output[0].code;
23
30
  }
24
- export async function amdBundler(moduleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, { bundleConfig }, bundleConfigOverrides) {
25
- const { exclude, external = {} } = overrideBundleConfig(bundleConfig, bundleConfigOverrides);
26
- const externalsArray = Object.keys(external);
27
- const requiredImports = new Map();
28
- const dynamicImports = new Map();
29
- const includedModules = [];
30
- const graphOptions = {
31
- includeLinkedDefinitions: true,
32
- depth: {
33
- static: GraphDepth.ALL,
34
- dynamic: 0,
35
- includeId: (moduleRef) => {
36
- // Do not bundle externals, including the loader module, which is auto bundled
37
- // with the shim + loader combo
38
- if (externalsArray.includes(moduleRef.specifier)) {
39
- // Do not include externals in the required imports but also return false to indicate it should not be in the bundle
40
- return false;
41
- }
42
- else if (exclude?.includes(moduleRef.specifier)) {
43
- // If this is a bundle exclude return false to indicate it should not be in the bundle
44
- // but add it to the requriedImports so it shows up as a static dependency of the bundle.
45
- requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
46
- return false;
47
- }
48
- return true;
49
- },
50
- },
51
- };
52
- const moduleGraphs = await getModuleGraphs(moduleId.specifier, graphOptions, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams);
53
- const rootModule = moduleGraphs.graphs[0];
54
- const modules = [rootModule.specifier, ...moduleGraphs.graphs[0].static];
31
+ async function getBundleCode(rootModule, moduleGraphs, includedModules, bundleGroupsIncludedModules, dynamicImports, minify, unVersionedAliases) {
32
+ const modules = [rootModule, ...moduleGraphs.graphs[0].static];
55
33
  const bundles = await Promise.all(modules
56
34
  .reduce((filteredModules, specifier) => {
57
35
  const linkedDefinition = moduleGraphs.linkedDefinitions[specifier];
@@ -75,12 +53,107 @@ export async function amdBundler(moduleId, moduleRegistry, minify = false, runti
75
53
  }, [])
76
54
  .map((linkedDefinition) => {
77
55
  const id = getSpecifier(linkedDefinition);
78
- if (id !== rootModule.specifier) {
56
+ if (id !== rootModule) {
79
57
  includedModules.push(id);
80
58
  }
59
+ else {
60
+ // we need to still keep track of roots for bundle groups
61
+ bundleGroupsIncludedModules.push(id);
62
+ }
81
63
  // bundle all dependencies for the linked definition and convert to AMD
82
- return bundle(id, moduleGraphs, minify);
64
+ return bundle(id, moduleGraphs, minify, unVersionedAliases);
83
65
  }));
66
+ return bundles;
67
+ }
68
+ export async function amdBundler(rootModuleId, moduleRegistry, minify = false, runtimeEnvironment, runtimeParams = {}, { bundleConfig }, bundleConfigOverrides) {
69
+ const { exclude, external = {}, groups = {} } = overrideBundleConfig(bundleConfig, bundleConfigOverrides);
70
+ const externalsArray = Object.keys(external);
71
+ // Note: the maps must be cleared each time we call getModuleGraph
72
+ const requiredImports = new Map();
73
+ const dynamicImports = new Map();
74
+ const groupName = getGroupName(rootModuleId.specifier, groups);
75
+ const groupies = groupName && groups[groupName];
76
+ const cachedGroupieCode = groupName && groupieCodeCache.get(groupName);
77
+ const getModuleGraphsWrapper = (graphSpecifier) => {
78
+ const graphOptions = {
79
+ includeLinkedDefinitions: true,
80
+ depth: {
81
+ static: GraphDepth.ALL,
82
+ dynamic: 0,
83
+ includeId: (moduleRef) => {
84
+ // Do not bundle externals, including the loader module, which is auto bundled
85
+ // with the shim + loader combo
86
+ if (externalsArray.includes(moduleRef.specifier)) {
87
+ // Do not include externals in the required imports but also return false to indicate it should not be in the bundle
88
+ return false;
89
+ }
90
+ const moduleRefIsGroupie = isGroupie(moduleRef.specifier, groups);
91
+ const rootModuleIsGroupie = isGroupie(graphSpecifier, groups);
92
+ const moduleIsNotRoot = graphSpecifier !== moduleRef.specifier;
93
+ if (moduleRefIsGroupie) {
94
+ // If this is part of bundle group return false to indicate it should not be in the bundle
95
+ // but add it to the requriedImports so it shows up as a static dependency of the bundle.
96
+ // However, skip this if the requested specifier is also part of the same group.
97
+ if (moduleIsNotRoot && !rootModuleIsGroupie) {
98
+ requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
99
+ }
100
+ return false;
101
+ }
102
+ else if (exclude?.includes(moduleRef.specifier)) {
103
+ // If this is a bundle exclude return false to indicate it should not be in the bundle
104
+ // but add it to the requriedImports so it shows up as a static dependency of the bundle.
105
+ if (moduleIsNotRoot) {
106
+ requiredImports.set(`${moduleRef.specifier}_${moduleRef.version}`, moduleRef);
107
+ }
108
+ return false;
109
+ }
110
+ return true;
111
+ },
112
+ },
113
+ };
114
+ return getModuleGraphs(graphSpecifier, graphOptions, moduleRegistry, moduleRegistry, runtimeEnvironment, runtimeParams);
115
+ };
116
+ const moduleGraphs = await getModuleGraphsWrapper(rootModuleId.specifier);
117
+ const rootModule = moduleGraphs.graphs[0];
118
+ // we also need to get moduleGraphs for any group members this module belongs to
119
+ const groupModuleGraphsMap = new Map();
120
+ if (!cachedGroupieCode && groupies && groupies.length) {
121
+ for (const specifier of groupies) {
122
+ if (specifier !== rootModuleId.specifier) {
123
+ // eslint-disable-next-line no-await-in-loop
124
+ const groupieModuleGraph = await getModuleGraphsWrapper(specifier);
125
+ groupModuleGraphsMap.set(specifier, groupieModuleGraph);
126
+ }
127
+ }
128
+ }
129
+ let bundleCode, includedModules, bundleGroupsIncludedModules;
130
+ // we don't need to recompute the bundle code if it already exists for this group
131
+ const cachedBundleGroupCode = cachedGroupieCode;
132
+ if (!cachedBundleGroupCode) {
133
+ (includedModules = []), (bundleGroupsIncludedModules = []);
134
+ const bundles = await getBundleCode(rootModule.specifier, moduleGraphs, includedModules, bundleGroupsIncludedModules, dynamicImports, minify, !!runtimeEnvironment.featureFlags?.UNVERSIONED_ALIASES);
135
+ if (groupName) {
136
+ // add groupies to bundles
137
+ for (const rawGroupieSpecifier of groupies) {
138
+ if (rawGroupieSpecifier !== rootModuleId.specifier) {
139
+ const groupieModuleGraph = groupModuleGraphsMap.get(rawGroupieSpecifier);
140
+ const groupieSpecifer = groupieModuleGraph.graphs[0].specifier;
141
+ // eslint-disable-next-line no-await-in-loop
142
+ const groupieBundles = await getBundleCode(groupieSpecifer, groupieModuleGraph, includedModules, bundleGroupsIncludedModules, dynamicImports, minify, !!runtimeEnvironment.featureFlags?.UNVERSIONED_ALIASES);
143
+ bundles.push(...groupieBundles);
144
+ }
145
+ }
146
+ }
147
+ bundleCode = bundles.join('');
148
+ if (groupName) {
149
+ includedModules.push(...bundleGroupsIncludedModules);
150
+ groupieCodeCache.set(groupName, { code: bundleCode, includedModules });
151
+ }
152
+ }
153
+ else {
154
+ bundleCode = cachedBundleGroupCode.code;
155
+ includedModules = cachedBundleGroupCode.includedModules;
156
+ }
84
157
  const { id, name, namespace, version, specifier } = moduleGraphs.linkedDefinitions[rootModule.specifier];
85
158
  return {
86
159
  id,
@@ -88,7 +161,7 @@ export async function amdBundler(moduleId, moduleRegistry, minify = false, runti
88
161
  namespace,
89
162
  version,
90
163
  specifier,
91
- code: bundles.join(''),
164
+ code: bundleCode,
92
165
  config: { external, exclude },
93
166
  bundleRecord: {
94
167
  imports: Array.from(requiredImports.values()),
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.10.0-alpha.13",
7
+ "version": "0.10.0-alpha.14",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -37,7 +37,9 @@
37
37
  }
38
38
  },
39
39
  "scripts": {
40
- "build": "tsc -b"
40
+ "build": "tsc -b",
41
+ "clean": "rm -rf build",
42
+ "test": "jest"
41
43
  },
42
44
  "files": [
43
45
  "build/**/*.js",
@@ -46,12 +48,15 @@
46
48
  ],
47
49
  "dependencies": {
48
50
  "@lwc/features": "2.45.2",
49
- "@lwrjs/shared-utils": "0.10.0-alpha.13",
51
+ "@lwrjs/shared-utils": "0.10.0-alpha.14",
50
52
  "@rollup/plugin-replace": "^2.4.2",
51
53
  "rollup": "^2.78.0"
52
54
  },
53
55
  "devDependencies": {
54
- "@lwrjs/types": "0.10.0-alpha.13"
56
+ "@lwrjs/types": "0.10.0-alpha.14",
57
+ "jest": "^26.6.3",
58
+ "ts-jest": "^26.5.6",
59
+ "typescript": "~4.3.5"
55
60
  },
56
61
  "optionalDependencies": {
57
62
  "esbuild": "^0.9.7"
@@ -59,5 +64,8 @@
59
64
  "engines": {
60
65
  "node": ">=16.0.0 <20"
61
66
  },
62
- "gitHead": "f6d142d5a027554cb1685389e0b173734149683d"
67
+ "volta": {
68
+ "extends": "../../../package.json"
69
+ },
70
+ "gitHead": "f80dc1c18719b77c183f339027313be11d69f9dc"
63
71
  }