@lwrjs/static 0.10.2 → 0.11.0-alpha.1

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.
@@ -41,9 +41,10 @@ var StaticBundleProvider = class {
41
41
  this.siteRootDir = context.siteMetadata.getSiteRootDir();
42
42
  this.bundleConfig = context.config.bundleConfig;
43
43
  }
44
- async bundle(moduleId, runtimeEnvironment) {
44
+ async bundle(moduleId, runtimeEnvironment, runtimeParams) {
45
45
  const {specifier, name, namespace, version} = moduleId;
46
- const metadata = this.siteBundles.bundles[specifier];
46
+ const localeId = runtimeParams?.locale || runtimeEnvironment.defaultLocale;
47
+ const metadata = (0, import_static_utils.getLocalizedBundle)(specifier, this.siteBundles, localeId, runtimeEnvironment.defaultLocale);
47
48
  if (!metadata) {
48
49
  if (!specifier.startsWith(LWR_SSR_BUNDLE_PREFIX)) {
49
50
  import_shared_utils.logger.warn(`[${this.name}] did not find requested specifier ${specifier}`);
@@ -38,20 +38,23 @@ var StaticModuleProvider = class {
38
38
  this.externals = Object.keys(context.config.bundleConfig.external || {});
39
39
  this.siteBundles = context.siteMetadata.getSiteBundles();
40
40
  this.siteRootDir = context.siteMetadata.getSiteRootDir();
41
+ this.runtimeEnvironment = context.runtimeEnvironment;
41
42
  this.fingerprintIndex = buildFingerprintsIndex(context);
42
43
  }
43
- async getModule(moduleId) {
44
+ async getModule(moduleId, runtimeParams) {
44
45
  const {specifier} = moduleId;
45
- const metadata = this.siteBundles.bundles[specifier];
46
+ const localeId = runtimeParams?.locale || this.runtimeEnvironment.defaultLocale;
47
+ const metadata = (0, import_static_utils.getLocalizedBundle)(specifier, this.siteBundles, localeId, this.runtimeEnvironment.defaultLocale);
46
48
  if (metadata) {
47
49
  import_shared_utils.logger.warn(`[${this.name}] We should not be asking for module source we have in our site metadata ${moduleId.specifier}`);
48
50
  return void 0;
49
51
  }
50
52
  return void 0;
51
53
  }
52
- async getModuleEntry(moduleId) {
54
+ async getModuleEntry(moduleId, runtimeParams) {
53
55
  const {specifier, version} = moduleId;
54
- const metadata = this.siteBundles.bundles[specifier];
56
+ const localeId = runtimeParams?.locale || this.runtimeEnvironment.defaultLocale;
57
+ const metadata = (0, import_static_utils.getLocalizedBundle)(specifier, this.siteBundles, localeId, this.runtimeEnvironment.defaultLocale);
55
58
  if (metadata) {
56
59
  import_shared_utils.logger.info(`[${this.name}] Module Entry request for static bundle ${specifier}`);
57
60
  const bundlePath = import_path.default.join(this.siteRootDir, metadata.path);
@@ -39,7 +39,7 @@ var StaticResourceProvider = class {
39
39
  this.siteResources = context.siteMetadata.getSiteResources();
40
40
  this.siteRootDir = context.siteMetadata.getSiteRootDir();
41
41
  }
42
- async getResource(resourceIdentity, runtimeEnvironment) {
42
+ async getResource(resourceIdentity, _runtimeEnvironment) {
43
43
  const metadata = this.siteResources.resources[resourceIdentity.specifier];
44
44
  if (!metadata) {
45
45
  import_shared_utils.logger.warn(`[${this.name}] Did not find requested specifier ${resourceIdentity.specifier}`);
@@ -55,7 +55,7 @@ var SiteMetadataImpl = class {
55
55
  async persistSiteMetadata() {
56
56
  const siteMetadataPath = import_path.default.join(this.options.rootDir, SITE_METADATA_PATH);
57
57
  try {
58
- if (!await import_fs_extra.default.pathExists(siteMetadataPath)) {
58
+ if (!import_fs_extra.default.existsSync(siteMetadataPath)) {
59
59
  await import_fs_extra.default.mkdir(siteMetadataPath, {recursive: true});
60
60
  }
61
61
  const bundleMetadataPath = import_path.default.join(this.options.rootDir, STATIC_BUNDLE_METADATA_PATH);
@@ -24,9 +24,29 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/static/src/utils/static-utils.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
+ getLocalizedBundle: () => getLocalizedBundle,
27
28
  resolveStaticBundleVersion: () => resolveStaticBundleVersion
28
29
  });
29
30
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
30
31
  function resolveStaticBundleVersion(metadataVersion, requestedVersion) {
31
32
  return metadataVersion || requestedVersion || import_shared_utils.VERSION_NOT_PROVIDED;
32
33
  }
34
+ function getLocalizedBundle(specifier, siteBundles, localeId, defaultLocaleId) {
35
+ if (localeId === defaultLocaleId) {
36
+ return siteBundles.bundles[specifier];
37
+ }
38
+ const localizedBundle = siteBundles.bundles[`${specifier}|l/${localeId}`];
39
+ if (localizedBundle) {
40
+ return localizedBundle;
41
+ }
42
+ if ((0, import_shared_utils.hasCountryCode)(localeId)) {
43
+ const stripedCode = (0, import_shared_utils.stripCountryCode)(localeId);
44
+ if (stripedCode) {
45
+ const localizedBundle2 = siteBundles.bundles[`${specifier}|l/${stripedCode}`];
46
+ if (localizedBundle2) {
47
+ return localizedBundle2;
48
+ }
49
+ }
50
+ }
51
+ return siteBundles.bundles[specifier];
52
+ }
@@ -1,10 +1,10 @@
1
- import type { AbstractModuleId, BundleDefinition, BundleProvider, ProviderContext, RuntimeEnvironment } from '@lwrjs/types';
1
+ import type { AbstractModuleId, BundleDefinition, BundleProvider, ProviderContext, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
2
  export default class StaticBundleProvider implements BundleProvider {
3
3
  name: string;
4
4
  siteBundles: import("@lwrjs/types").SiteBundles;
5
5
  siteRootDir: string;
6
6
  bundleConfig: import("@lwrjs/types").BundleConfig;
7
7
  constructor(_config: {}, context: ProviderContext);
8
- bundle<BundleIdentifier extends AbstractModuleId, RE extends RuntimeEnvironment>(moduleId: BundleIdentifier, runtimeEnvironment: RE): Promise<BundleDefinition | undefined>;
8
+ bundle<BundleIdentifier extends AbstractModuleId, RE extends RuntimeEnvironment>(moduleId: BundleIdentifier, runtimeEnvironment: RE, runtimeParams: RuntimeParams): Promise<BundleDefinition | undefined>;
9
9
  }
10
10
  //# sourceMappingURL=static-bundle-provider.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import { explodeSpecifier, getSpecifier, logger } from '@lwrjs/shared-utils';
2
2
  import path from 'path';
3
3
  import fs from 'fs-extra';
4
- import { resolveStaticBundleVersion } from '../utils/static-utils.js';
4
+ import { getLocalizedBundle, resolveStaticBundleVersion } from '../utils/static-utils.js';
5
5
  const LWR_SSR_BUNDLE_PREFIX = '@lwrjs/lwc-ssr/';
6
6
  export default class StaticBundleProvider {
7
7
  constructor(_config, context) {
@@ -13,9 +13,10 @@ export default class StaticBundleProvider {
13
13
  this.siteRootDir = context.siteMetadata.getSiteRootDir();
14
14
  this.bundleConfig = context.config.bundleConfig;
15
15
  }
16
- async bundle(moduleId, runtimeEnvironment) {
16
+ async bundle(moduleId, runtimeEnvironment, runtimeParams) {
17
17
  const { specifier, name, namespace, version } = moduleId;
18
- const metadata = this.siteBundles.bundles[specifier];
18
+ const localeId = (runtimeParams?.locale || runtimeEnvironment.defaultLocale);
19
+ const metadata = getLocalizedBundle(specifier, this.siteBundles, localeId, runtimeEnvironment.defaultLocale);
19
20
  if (!metadata) {
20
21
  // ignore ssr module requests
21
22
  if (!specifier.startsWith(LWR_SSR_BUNDLE_PREFIX)) {
@@ -1,12 +1,13 @@
1
- import type { AbstractModuleId, ModuleCompiled, ModuleEntry, ModuleProvider, ProviderContext } from '@lwrjs/types';
1
+ import type { AbstractModuleId, ModuleCompiled, ModuleEntry, ModuleProvider, ProviderContext, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
2
  export default class StaticModuleProvider implements ModuleProvider {
3
3
  name: string;
4
4
  siteBundles: import("@lwrjs/types").SiteBundles;
5
5
  siteRootDir: string;
6
6
  externals: string[];
7
7
  fingerprintIndex: Record<string, ModuleEntry>;
8
+ runtimeEnvironment: RuntimeEnvironment;
8
9
  constructor(_config: {}, context: ProviderContext);
9
- getModule<T extends AbstractModuleId>(moduleId: T): Promise<ModuleCompiled | undefined>;
10
- getModuleEntry<T extends AbstractModuleId>(moduleId: T): Promise<ModuleEntry | undefined>;
10
+ getModule<T extends AbstractModuleId>(moduleId: T, runtimeParams: RuntimeParams): Promise<ModuleCompiled | undefined>;
11
+ getModuleEntry<T extends AbstractModuleId>(moduleId: T, runtimeParams: RuntimeParams): Promise<ModuleEntry | undefined>;
11
12
  }
12
13
  //# sourceMappingURL=static-module-provider.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import { explodeSpecifier, getSpecifier, logger } from '@lwrjs/shared-utils';
2
2
  import path from 'path';
3
- import { resolveStaticBundleVersion } from '../utils/static-utils.js';
3
+ import { getLocalizedBundle, resolveStaticBundleVersion } from '../utils/static-utils.js';
4
4
  export default class StaticModuleProvider {
5
5
  constructor(_config, context) {
6
6
  this.name = 'static-module-provider';
@@ -10,12 +10,14 @@ export default class StaticModuleProvider {
10
10
  this.externals = Object.keys(context.config.bundleConfig.external || {});
11
11
  this.siteBundles = context.siteMetadata.getSiteBundles();
12
12
  this.siteRootDir = context.siteMetadata.getSiteRootDir();
13
+ this.runtimeEnvironment = context.runtimeEnvironment;
13
14
  // If we are using fingerprints collect all the specifiers in the bundles and add them to an index for creating the mapping identities
14
15
  this.fingerprintIndex = buildFingerprintsIndex(context);
15
16
  }
16
- async getModule(moduleId) {
17
+ async getModule(moduleId, runtimeParams) {
17
18
  const { specifier } = moduleId;
18
- const metadata = this.siteBundles.bundles[specifier];
19
+ const localeId = (runtimeParams?.locale || this.runtimeEnvironment.defaultLocale);
20
+ const metadata = getLocalizedBundle(specifier, this.siteBundles, localeId, this.runtimeEnvironment.defaultLocale);
19
21
  if (metadata) {
20
22
  logger.warn(`[${this.name}] We should not be asking for module source we have in our site metadata ${moduleId.specifier}`);
21
23
  // proceed to next provider
@@ -24,10 +26,11 @@ export default class StaticModuleProvider {
24
26
  // proceed to next provider
25
27
  return undefined;
26
28
  }
27
- async getModuleEntry(moduleId) {
29
+ async getModuleEntry(moduleId, runtimeParams) {
28
30
  // TODO shouldn't we be passing the runtime environment here to test?
29
31
  const { specifier, version } = moduleId;
30
- const metadata = this.siteBundles.bundles[specifier];
32
+ const localeId = (runtimeParams?.locale || this.runtimeEnvironment.defaultLocale);
33
+ const metadata = getLocalizedBundle(specifier, this.siteBundles, localeId, this.runtimeEnvironment.defaultLocale);
31
34
  if (metadata) {
32
35
  logger.info(`[${this.name}] Module Entry request for static bundle ${specifier}`);
33
36
  // Have to make the bundle code available for SSR
@@ -5,6 +5,6 @@ export default class StaticResourceProvider implements ResourceProvider {
5
5
  siteRootDir: string;
6
6
  resourceRegistry: import("@lwrjs/types").PublicResourceRegistry;
7
7
  constructor(_config: {}, context: ProviderContext);
8
- getResource<Identifier extends ResourceIdentifier, RuntimeEnvironment extends BootstrapRuntimeEnvironment>(resourceIdentity: Identifier, runtimeEnvironment: RuntimeEnvironment): Promise<ResourceDefinition | undefined>;
8
+ getResource<Identifier extends ResourceIdentifier, RuntimeEnvironment extends BootstrapRuntimeEnvironment>(resourceIdentity: Identifier, _runtimeEnvironment: RuntimeEnvironment): Promise<ResourceDefinition | undefined>;
9
9
  }
10
10
  //# sourceMappingURL=static-resource-provider.d.ts.map
@@ -11,7 +11,7 @@ export default class StaticResourceProvider {
11
11
  this.siteResources = context.siteMetadata.getSiteResources();
12
12
  this.siteRootDir = context.siteMetadata.getSiteRootDir();
13
13
  }
14
- async getResource(resourceIdentity, runtimeEnvironment) {
14
+ async getResource(resourceIdentity, _runtimeEnvironment) {
15
15
  const metadata = this.siteResources.resources[resourceIdentity.specifier];
16
16
  if (!metadata) {
17
17
  logger.warn(`[${this.name}] Did not find requested specifier ${resourceIdentity.specifier}`);
@@ -28,7 +28,7 @@ export class SiteMetadataImpl {
28
28
  // Create the metadata directory if if does not exist
29
29
  const siteMetadataPath = path.join(this.options.rootDir, SITE_METADATA_PATH);
30
30
  try {
31
- if (!(await fs.pathExists(siteMetadataPath))) {
31
+ if (!fs.existsSync(siteMetadataPath)) {
32
32
  await fs.mkdir(siteMetadataPath, { recursive: true });
33
33
  }
34
34
  // Save Bundle Metadata
@@ -1,7 +1,12 @@
1
+ import type { SiteBundle, SiteBundles } from '@lwrjs/types';
1
2
  /**
2
3
  * Return the version for a static module bundle.
3
4
  *
4
5
  * Version defined in the metadata > Requested Version > 'version-not-provided'
5
6
  */
6
7
  export declare function resolveStaticBundleVersion(metadataVersion?: string, requestedVersion?: string): string;
8
+ /**
9
+ * Get the most best match for a localized bundle
10
+ */
11
+ export declare function getLocalizedBundle(specifier: string, siteBundles: SiteBundles, localeId: string, defaultLocaleId: string): SiteBundle | undefined;
7
12
  //# sourceMappingURL=static-utils.d.ts.map
@@ -2,7 +2,7 @@
2
2
  * Try to add any code that is mrt specific here so that we can isolate details that would have to be refactored to run lambdas
3
3
  * on a different platform
4
4
  */
5
- import { VERSION_NOT_PROVIDED } from '@lwrjs/shared-utils';
5
+ import { hasCountryCode, stripCountryCode, VERSION_NOT_PROVIDED } from '@lwrjs/shared-utils';
6
6
  /**
7
7
  * Return the version for a static module bundle.
8
8
  *
@@ -11,4 +11,30 @@ import { VERSION_NOT_PROVIDED } from '@lwrjs/shared-utils';
11
11
  export function resolveStaticBundleVersion(metadataVersion, requestedVersion) {
12
12
  return metadataVersion || requestedVersion || VERSION_NOT_PROVIDED;
13
13
  }
14
+ /**
15
+ * Get the most best match for a localized bundle
16
+ */
17
+ export function getLocalizedBundle(specifier, siteBundles, localeId, defaultLocaleId) {
18
+ // check if the locale is the default or not an additional locale
19
+ if (localeId === defaultLocaleId) {
20
+ return siteBundles.bundles[specifier];
21
+ }
22
+ // Check if there was an exact match for the preferred locale
23
+ const localizedBundle = siteBundles.bundles[`${specifier}|l/${localeId}`];
24
+ if (localizedBundle) {
25
+ return localizedBundle;
26
+ }
27
+ // Else check if there is a match without the county code
28
+ if (hasCountryCode(localeId)) {
29
+ const stripedCode = stripCountryCode(localeId);
30
+ if (stripedCode) {
31
+ const localizedBundle = siteBundles.bundles[`${specifier}|l/${stripedCode}`];
32
+ if (localizedBundle) {
33
+ return localizedBundle;
34
+ }
35
+ }
36
+ }
37
+ // Else fall back to the non-localized bundle
38
+ return siteBundles.bundles[specifier];
39
+ }
14
40
  //# sourceMappingURL=static-utils.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.10.2",
7
+ "version": "0.11.0-alpha.1",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -48,13 +48,15 @@
48
48
  "build/**/*.d.ts"
49
49
  ],
50
50
  "dependencies": {
51
- "@lwrjs/shared-utils": "0.10.2"
51
+ "@lwrjs/shared-utils": "0.11.0-alpha.1"
52
52
  },
53
53
  "devDependencies": {
54
- "@lwrjs/types": "0.10.2",
54
+ "@lwrjs/types": "0.11.0-alpha.1",
55
55
  "@types/express": "^4.17.17",
56
+ "jest": "^26.6.3",
56
57
  "jest-express": "^1.12.0",
57
- "mock-res": "^0.6.0"
58
+ "mock-res": "^0.6.0",
59
+ "ts-jest": "^26.5.6"
58
60
  },
59
61
  "engines": {
60
62
  "node": ">=16.0.0"
@@ -62,5 +64,5 @@
62
64
  "volta": {
63
65
  "extends": "../../../package.json"
64
66
  },
65
- "gitHead": "40988dfc093a3b9dea859f1bf61a49bca1edb0c5"
67
+ "gitHead": "6a840d4694fac9f0be5c3a21707015379cbadad7"
66
68
  }