@lwrjs/module-bundler 0.15.0-alpha.9 → 0.15.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.
@@ -45,12 +45,14 @@ var AmdBundlerProvider = class {
45
45
  env: {NODE_ENV: envMode} = {NODE_ENV: "production"}
46
46
  } = runtimeEnvironment;
47
47
  const minified = !!minify && !debug;
48
+ let bundleCode;
48
49
  if (minified) {
49
- bundle.code = await (0, import_esbuild_utils.minifyJavascript)(bundle.code);
50
+ bundleCode = await (0, import_esbuild_utils.minifyJavascript)(await bundle.getCode());
50
51
  } else {
51
- bundle.code = await (0, import_esbuild_utils.parseJavascript)(bundle.code, {envMode});
52
+ bundleCode = await (0, import_esbuild_utils.parseJavascript)(await bundle.getCode(), {envMode});
52
53
  }
53
- bundle.integrity = (0, import_shared_utils.createIntegrityHash)(bundle.code);
54
+ bundle.integrity = (0, import_shared_utils.createIntegrityHash)(bundleCode);
55
+ bundle.getCode = () => Promise.resolve(bundleCode);
54
56
  return bundle;
55
57
  }
56
58
  }
@@ -41,7 +41,7 @@ var AmdBundlerProvider = class {
41
41
  const minified = minify && !debug;
42
42
  const bundle = await (0, import_amd_common.amdBundler)(moduleId, moduleRegistry, minified, runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
43
43
  if (bundle) {
44
- bundle.integrity = (0, import_shared_utils.createIntegrityHash)(bundle.code);
44
+ bundle.integrity = (0, import_shared_utils.createIntegrityHash)(await bundle.getCode());
45
45
  }
46
46
  return bundle;
47
47
  }
@@ -102,7 +102,7 @@ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
102
102
  namespace,
103
103
  name,
104
104
  version,
105
- code,
105
+ getCode: () => Promise.resolve(code),
106
106
  config: {external, exclude},
107
107
  map: bundleMap,
108
108
  integrity: (0, import_shared_utils.createIntegrityHash)(code),
@@ -27,13 +27,23 @@ __export(exports, {
27
27
  LwrModuleBundler: () => LwrModuleBundler
28
28
  });
29
29
  var import_path = __toModule(require("path"));
30
+ var import_lru_cache = __toModule(require("lru-cache"));
30
31
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
32
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
32
33
  var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
33
34
  var TASK_POOL = new import_shared_utils.TaskPool();
34
35
  var LwrModuleBundler = class {
35
36
  constructor(config, globalConfig) {
36
- this.cache = new Map();
37
+ this.cache = new import_lru_cache.LRUCache({
38
+ max: parseInt(process.env.BUNDLE_CACHE_SIZE ?? "500", 10),
39
+ dispose: (_value, key) => {
40
+ if ((0, import_shared_utils.isLambdaEnv)()) {
41
+ import_diagnostics.logger.warn(`Bundle evicted from cache: "${key}"`);
42
+ } else {
43
+ import_diagnostics.logger.verbose(`Bundle evicted from cache: "${key}"`);
44
+ }
45
+ }
46
+ });
37
47
  this.providers = [];
38
48
  this.transformers = [];
39
49
  this.inflightBundleDefinitions = new import_shared_utils.InflightTasks();
@@ -55,12 +65,14 @@ var LwrModuleBundler = class {
55
65
  }
56
66
  async getModuleBundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
57
67
  const {format, minify, debug} = runtimeEnvironment;
68
+ const ssr = runtimeParams.ssr;
58
69
  const cacheKey = `${moduleId.specifier}|${moduleId.version}|${(0, import_shared_utils.getCacheKeyFromJson)({
59
70
  locale: runtimeParams.locale,
60
71
  format,
61
72
  minify,
62
73
  debug,
63
- bundleConfigOverrides
74
+ bundleConfigOverrides,
75
+ ssr
64
76
  })}`;
65
77
  const cacheDisabled = process.env.NOCACHE === "true";
66
78
  if (!cacheDisabled) {
@@ -69,14 +81,19 @@ var LwrModuleBundler = class {
69
81
  return bundleDef;
70
82
  }
71
83
  }
72
- return (0, import_instrumentation.getTracer)().trace({
73
- name: import_instrumentation.BundleSpan.GetBundle,
74
- attributes: {
75
- specifier: moduleId.specifier
76
- }
77
- }, () => {
78
- return this.inflightBundleDefinitions.execute(cacheKey, () => {
79
- return TASK_POOL.execute(async () => {
84
+ return this.inflightBundleDefinitions.execute(cacheKey, () => {
85
+ return TASK_POOL.execute(async () => {
86
+ return (0, import_instrumentation.getTracer)().trace({
87
+ name: import_instrumentation.BundleSpan.GetBundle,
88
+ attributes: {
89
+ specifier: moduleId.specifier,
90
+ version: moduleId.version ?? "",
91
+ locale: runtimeParams.locale ?? "",
92
+ ssr: ssr ? "TRUE" : "FALSE",
93
+ debug: debug ? "TRUE" : "FALSE"
94
+ }
95
+ }, async () => {
96
+ import_instrumentation.cacheCountStore.incrementCacheKey("missedDefs");
80
97
  for (const bundler of this.providers) {
81
98
  const bundleDef = await bundler.bundle(moduleId, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
82
99
  if (bundleDef) {
@@ -89,8 +106,8 @@ var LwrModuleBundler = class {
89
106
  throw (0, import_diagnostics.createSingleDiagnosticError)({
90
107
  description: import_diagnostics.descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier)
91
108
  }, import_diagnostics.LwrUnresolvableError);
92
- }, this);
93
- });
109
+ });
110
+ }, this);
94
111
  });
95
112
  }
96
113
  async resolveModuleUri(moduleId, runtimeEnvironment, runtimeParams, signature) {
@@ -122,7 +139,7 @@ var LwrModuleBundler = class {
122
139
  };
123
140
  for (const transformPlugin of this.transformers) {
124
141
  const resolveUriResult = await transformPlugin.transformUri?.(bundleUri, bundleDefinition, runtimeEnvironment);
125
- if (resolveUriResult && resolveUriResult.uri) {
142
+ if (resolveUriResult?.uri) {
126
143
  uri = resolveUriResult.uri;
127
144
  }
128
145
  }
@@ -210,13 +210,14 @@ async function amdBundler(rootModuleId, moduleRegistry, minify = false, runtimeE
210
210
  includedModules = cachedBundleGroupCode.includedModules;
211
211
  }
212
212
  const {id, name, namespace, version, specifier} = moduleGraphs.linkedDefinitions[rootModule.specifier];
213
+ const getCode = () => Promise.resolve(bundleCode);
213
214
  return {
214
215
  id,
215
216
  name,
216
217
  namespace,
217
218
  version,
218
219
  specifier,
219
- code: bundleCode,
220
+ getCode,
220
221
  config: {external, exclude},
221
222
  bundleRecord: {
222
223
  imports: Array.from(requiredImports.values()),
@@ -15,13 +15,16 @@ export default class AmdBundlerProvider {
15
15
  // Minification via esbuild for performance
16
16
  const { minify, debug, env: { NODE_ENV: envMode } = { NODE_ENV: 'production' }, } = runtimeEnvironment;
17
17
  const minified = !!minify && !debug;
18
+ let bundleCode;
18
19
  if (minified) {
19
- bundle.code = await minifyJavascript(bundle.code);
20
+ // TODO convert to getCode()
21
+ bundleCode = await minifyJavascript((await bundle.getCode()));
20
22
  }
21
23
  else {
22
- bundle.code = await parseJavascript(bundle.code, { envMode });
24
+ bundleCode = await parseJavascript((await bundle.getCode()), { envMode });
23
25
  }
24
- bundle.integrity = createIntegrityHash(bundle.code);
26
+ bundle.integrity = createIntegrityHash(bundleCode);
27
+ bundle.getCode = () => Promise.resolve(bundleCode);
25
28
  return bundle;
26
29
  }
27
30
  }
@@ -14,7 +14,7 @@ export default class AmdBundlerProvider {
14
14
  const bundle = await amdBundler(moduleId, moduleRegistry, minified, // will minify using rollup/terser if true - MRT runtime friendly
15
15
  runtimeEnvironment, runtimeParams, config, bundleConfigOverrides);
16
16
  if (bundle) {
17
- bundle.integrity = createIntegrityHash(bundle.code);
17
+ bundle.integrity = createIntegrityHash((await bundle.getCode()));
18
18
  }
19
19
  return bundle;
20
20
  }
@@ -76,7 +76,7 @@ async function esmBundler(moduleId, moduleRegistry, runtimeEnvironment, runtimeP
76
76
  namespace,
77
77
  name,
78
78
  version,
79
- code,
79
+ getCode: () => Promise.resolve(code),
80
80
  config: { external, exclude },
81
81
  map: bundleMap,
82
82
  integrity: createIntegrityHash(code),
@@ -1,4 +1,5 @@
1
1
  import type { AbstractModuleId, BundleConfig, BundleConfigOverrides, BundleDefinition, BundleProvider, LwrAppObserver, ModuleBundler, ModuleId, ModuleRegistry, NormalizedLwrGlobalConfig, PublicModuleBundler, RuntimeEnvironment, RuntimeParams, SourceMapRuntimeEnvironment, UriTransformPlugin } from '@lwrjs/types';
2
+ import { LRUCache } from 'lru-cache';
2
3
  interface LwrModuleBundlerConfig {
3
4
  moduleRegistry: ModuleRegistry;
4
5
  appObserver?: LwrAppObserver;
@@ -6,7 +7,7 @@ interface LwrModuleBundlerConfig {
6
7
  export declare class LwrModuleBundler implements ModuleBundler {
7
8
  moduleRegistry: ModuleRegistry;
8
9
  appObserver: LwrAppObserver | undefined;
9
- cache: Map<string, BundleDefinition>;
10
+ cache: LRUCache<string, BundleDefinition, unknown>;
10
11
  providers: BundleProvider[];
11
12
  transformers: UriTransformPlugin[];
12
13
  bundleConfig: BundleConfig;
@@ -19,7 +20,7 @@ export declare class LwrModuleBundler implements ModuleBundler {
19
20
  /**
20
21
  * Resolve the URI to the bundle rooted at the `moduleId`
21
22
  * @param moduleId - The id of the root module for the bundle
22
- * @param runtimeEnvironment - The runtime operating environnment
23
+ * @param runtimeEnvironment - The runtime operating environment
23
24
  * @param runtimeParams - The available runtime parameters provided in context to the request
24
25
  * @param signature - The signature of the bundle instance being referenced
25
26
  * @returns the URI
package/build/es/index.js CHANGED
@@ -1,11 +1,22 @@
1
1
  import { join } from 'path';
2
- import { LwrUnresolvableError, createSingleDiagnosticError, descriptions } from '@lwrjs/diagnostics';
3
- import { signBundle, getCacheKeyFromJson, InflightTasks, TaskPool } from '@lwrjs/shared-utils';
4
- import { getTracer, BundleSpan } from '@lwrjs/instrumentation';
2
+ import { LRUCache } from 'lru-cache';
3
+ import { LwrUnresolvableError, createSingleDiagnosticError, descriptions, logger } from '@lwrjs/diagnostics';
4
+ import { signBundle, getCacheKeyFromJson, InflightTasks, TaskPool, isLambdaEnv } from '@lwrjs/shared-utils';
5
+ import { getTracer, BundleSpan, cacheCountStore } from '@lwrjs/instrumentation';
5
6
  const TASK_POOL = new TaskPool();
6
7
  export class LwrModuleBundler {
7
8
  constructor(config, globalConfig) {
8
- this.cache = new Map();
9
+ this.cache = new LRUCache({
10
+ max: parseInt(process.env.BUNDLE_CACHE_SIZE ?? '500', 10),
11
+ dispose: (_value, key) => {
12
+ if (isLambdaEnv()) {
13
+ logger.warn(`Bundle evicted from cache: "${key}"`);
14
+ }
15
+ else {
16
+ logger.verbose(`Bundle evicted from cache: "${key}"`);
17
+ }
18
+ },
19
+ });
9
20
  this.providers = [];
10
21
  this.transformers = [];
11
22
  // Pending bundle definitions are tracked to prevent concurrent resolution of the same bundle.
@@ -33,32 +44,38 @@ export class LwrModuleBundler {
33
44
  }
34
45
  async getModuleBundle(moduleId, runtimeEnvironment, runtimeParams = {}, bundleConfigOverrides) {
35
46
  const { format, minify, debug } = runtimeEnvironment;
47
+ const ssr = runtimeParams.ssr;
36
48
  const cacheKey = `${moduleId.specifier}|${moduleId.version}|${getCacheKeyFromJson({
37
49
  locale: runtimeParams.locale,
38
50
  format,
39
51
  minify,
40
52
  debug,
41
53
  bundleConfigOverrides,
54
+ ssr,
42
55
  })}`;
43
56
  const cacheDisabled = process.env.NOCACHE === 'true';
44
57
  if (!cacheDisabled) {
45
58
  // Return the cached bundle definition
46
59
  if (this.cache.has(cacheKey)) {
47
- // TODO add to profiling
48
- // console.log('[INFO] Bundle Cache Hit: ', cacheKey);
49
60
  const bundleDef = this.cache.get(cacheKey);
50
61
  return bundleDef;
51
62
  }
52
63
  }
53
- return getTracer().trace({
54
- name: BundleSpan.GetBundle,
55
- attributes: {
56
- specifier: moduleId.specifier,
57
- },
58
- }, () => {
59
- return this.inflightBundleDefinitions.execute(cacheKey, () => {
60
- // Run theses tasks in a task pool to throttle parallel requests.
61
- return TASK_POOL.execute(async () => {
64
+ return this.inflightBundleDefinitions.execute(cacheKey, () => {
65
+ // Run theses tasks in a task pool to throttle parallel requests.
66
+ return TASK_POOL.execute(async () => {
67
+ return getTracer().trace({
68
+ name: BundleSpan.GetBundle,
69
+ attributes: {
70
+ specifier: moduleId.specifier,
71
+ version: moduleId.version ?? '',
72
+ locale: runtimeParams.locale ?? '',
73
+ ssr: ssr ? 'TRUE' : 'FALSE',
74
+ debug: debug ? 'TRUE' : 'FALSE',
75
+ },
76
+ }, async () => {
77
+ // Increment the async def miss counter setup by top level metrics interested in bundle definition misses
78
+ cacheCountStore.incrementCacheKey('missedDefs');
62
79
  for (const bundler of this.providers) {
63
80
  // eslint-disable-next-line no-await-in-loop
64
81
  const bundleDef = await bundler.bundle(moduleId, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
@@ -72,14 +89,14 @@ export class LwrModuleBundler {
72
89
  throw createSingleDiagnosticError({
73
90
  description: descriptions.UNRESOLVABLE.BUNDLE(moduleId.specifier),
74
91
  }, LwrUnresolvableError);
75
- }, this);
76
- });
92
+ });
93
+ }, this);
77
94
  });
78
95
  }
79
96
  /**
80
97
  * Resolve the URI to the bundle rooted at the `moduleId`
81
98
  * @param moduleId - The id of the root module for the bundle
82
- * @param runtimeEnvironment - The runtime operating environnment
99
+ * @param runtimeEnvironment - The runtime operating environment
83
100
  * @param runtimeParams - The available runtime parameters provided in context to the request
84
101
  * @param signature - The signature of the bundle instance being referenced
85
102
  * @returns the URI
@@ -119,7 +136,7 @@ export class LwrModuleBundler {
119
136
  for (const transformPlugin of this.transformers) {
120
137
  // eslint-disable-next-line no-await-in-loop
121
138
  const resolveUriResult = await transformPlugin.transformUri?.(bundleUri, bundleDefinition, runtimeEnvironment);
122
- if (resolveUriResult && resolveUriResult.uri) {
139
+ if (resolveUriResult?.uri) {
123
140
  uri = resolveUriResult.uri;
124
141
  }
125
142
  }
@@ -225,13 +225,14 @@ export async function amdBundler(rootModuleId, moduleRegistry, minify = false, r
225
225
  includedModules = cachedBundleGroupCode.includedModules;
226
226
  }
227
227
  const { id, name, namespace, version, specifier } = moduleGraphs.linkedDefinitions[rootModule.specifier];
228
+ const getCode = () => Promise.resolve(bundleCode);
228
229
  return {
229
230
  id,
230
231
  name,
231
232
  namespace,
232
233
  version,
233
234
  specifier,
234
- code: bundleCode,
235
+ getCode,
235
236
  config: { external, exclude },
236
237
  bundleRecord: {
237
238
  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.15.0-alpha.9",
7
+ "version": "0.15.0",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -47,14 +47,15 @@
47
47
  "build/**/*.d.ts"
48
48
  ],
49
49
  "dependencies": {
50
- "@lwrjs/diagnostics": "0.15.0-alpha.9",
51
- "@lwrjs/instrumentation": "0.15.0-alpha.9",
52
- "@lwrjs/shared-utils": "0.15.0-alpha.9",
50
+ "@lwrjs/diagnostics": "0.15.0",
51
+ "@lwrjs/instrumentation": "0.15.0",
52
+ "@lwrjs/shared-utils": "0.15.0",
53
53
  "@rollup/plugin-replace": "^5.0.7",
54
- "rollup": "^2.78.0"
54
+ "lru-cache": "^10.4.3",
55
+ "rollup": "^2.79.2"
55
56
  },
56
57
  "devDependencies": {
57
- "@lwrjs/types": "0.15.0-alpha.9",
58
+ "@lwrjs/types": "0.15.0",
58
59
  "jest": "^26.6.3",
59
60
  "ts-jest": "^26.5.6"
60
61
  },
@@ -67,5 +68,5 @@
67
68
  "volta": {
68
69
  "extends": "../../../package.json"
69
70
  },
70
- "gitHead": "9783b817b11c29ca385457683fcb87e8179f7902"
71
+ "gitHead": "ee374df435d5342f63e4da126a09461e761837f3"
71
72
  }