@lwrjs/lwc-ssr 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.
@@ -10,12 +10,10 @@ __markAsModule(exports);
10
10
  __export(exports, {
11
11
  LWC_SSR_PREFIX: () => LWC_SSR_PREFIX,
12
12
  SSR_PROPS_ATTR: () => SSR_PROPS_ATTR,
13
- SSR_PROPS_KEY: () => SSR_PROPS_KEY,
14
13
  getPropsId: () => getPropsId
15
14
  });
16
15
  var LWC_SSR_PREFIX = "@lwrjs/lwc-ssr/";
17
16
  var SSR_PROPS_ATTR = "data-lwr-props-id";
18
- var SSR_PROPS_KEY = "ssrProps";
19
17
  function getPropsId() {
20
18
  return `lwcprops${Math.floor(Math.random() * 65536).toString(16)}`;
21
19
  }
@@ -89,20 +89,23 @@ async function getCode(runtimeEnvironment, lwrVersion, bundleSpecifier, included
89
89
  lwcSpecifier ? aliasLwcEngine(lwcSpecifier, LWC_SPECIFIERS) : ""
90
90
  ];
91
91
  }
92
- async function getBundle(specifier, moduleBundler, runtimeEnvironment) {
92
+ async function getBundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams) {
93
93
  if ((0, import_shared_utils.getFeatureFlags)().SSR_STATIC_BUNDLES) {
94
- return buildBundle(specifier, moduleBundler, runtimeEnvironment);
94
+ return buildBundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams);
95
95
  }
96
- return bundle(specifier, moduleBundler, runtimeEnvironment, {appendExcludes: false, exclude: ["lwc"]});
96
+ return bundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams, {
97
+ appendExcludes: false,
98
+ exclude: ["lwc"]
99
+ });
97
100
  }
98
- async function bundle(specifier, moduleBundler, runtimeEnvironment, bundleConfigOverrides) {
99
- return await moduleBundler.getModuleBundle({specifier}, runtimeEnvironment, void 0, bundleConfigOverrides);
101
+ async function bundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams, bundleConfigOverrides) {
102
+ return await moduleBundler.getModuleBundle({specifier}, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
100
103
  }
101
- async function bundleImports(bundleCode, imports = [], visited, moduleBundler, runtimeEnvironment) {
104
+ async function bundleImports(bundleCode, imports = [], visited, moduleBundler, runtimeEnvironment, runtimeParams) {
102
105
  for (const {specifier} of imports) {
103
106
  if (!visited.has(specifier)) {
104
107
  visited.add(specifier);
105
- const {code, bundleRecord, version} = await bundle(specifier, moduleBundler, runtimeEnvironment);
108
+ const {code, bundleRecord, version} = await bundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams);
106
109
  let bundledCode;
107
110
  if (runtimeEnvironment.featureFlags?.EXPERIMENTAL_UNVERSIONED_ALIASES) {
108
111
  const versionedSpecifier = (0, import_shared_utils.getSpecifier)({specifier, version});
@@ -111,15 +114,15 @@ async function bundleImports(bundleCode, imports = [], visited, moduleBundler, r
111
114
  } else {
112
115
  bundledCode = code;
113
116
  }
114
- bundleCode = await bundleImports(bundledCode, bundleRecord.imports, visited, moduleBundler, runtimeEnvironment) + bundleCode;
117
+ bundleCode = await bundleImports(bundledCode, bundleRecord.imports, visited, moduleBundler, runtimeEnvironment, runtimeParams) + bundleCode;
115
118
  }
116
119
  }
117
120
  return bundleCode;
118
121
  }
119
- async function buildBundle(ssrSpecifier, moduleBundler, runtimeEnvironment) {
122
+ async function buildBundle(ssrSpecifier, moduleBundler, runtimeEnvironment, runtimeParams) {
120
123
  const rootSpecifier = ssrSpecifier.replace(import_identity.LWC_SSR_PREFIX, "");
121
- const {code: _rootCode, bundleRecord: rootBundleRecord} = await bundle(rootSpecifier, moduleBundler, runtimeEnvironment);
122
- let rootCode = await bundleImports(_rootCode, rootBundleRecord.imports, new Set(["lwc", rootSpecifier]), moduleBundler, runtimeEnvironment);
124
+ const {code: _rootCode, bundleRecord: rootBundleRecord} = await bundle(rootSpecifier, moduleBundler, runtimeEnvironment, runtimeParams);
125
+ let rootCode = await bundleImports(_rootCode, rootBundleRecord.imports, new Set(["lwc", rootSpecifier]), moduleBundler, runtimeEnvironment, runtimeParams);
123
126
  if (runtimeEnvironment.featureFlags?.EXPERIMENTAL_UNVERSIONED_ALIASES) {
124
127
  const aliasSpecifier = (0, import_shared_utils.getSpecifier)({
125
128
  specifier: rootSpecifier,
@@ -128,12 +131,12 @@ async function buildBundle(ssrSpecifier, moduleBundler, runtimeEnvironment) {
128
131
  const aliasCode = (0, import_shared_utils.createAmdAlias)(aliasSpecifier, rootSpecifier);
129
132
  rootCode = [aliasCode, rootCode].join("");
130
133
  }
131
- const {code: lwcEngineCode, version: lwcVersion} = await bundle(LWC_SPECIFIERS.ssr, moduleBundler, runtimeEnvironment);
134
+ const {code: lwcEngineCode, version: lwcVersion} = await bundle(LWC_SPECIFIERS.ssr, moduleBundler, runtimeEnvironment, runtimeParams);
132
135
  const {
133
136
  bundleRecord,
134
137
  code: ssrCode,
135
138
  version: lwrVersion
136
- } = await bundle(ssrSpecifier, moduleBundler, runtimeEnvironment, {
139
+ } = await bundle(ssrSpecifier, moduleBundler, runtimeEnvironment, runtimeParams, {
137
140
  appendExcludes: true,
138
141
  exclude: [LWC_SPECIFIERS.ssr, rootSpecifier]
139
142
  });
@@ -32,12 +32,16 @@ var import_ssr_element = __toModule(require("./ssr-element.cjs"));
32
32
  function lwcSsrViewTransformer(options, {moduleBundler, resourceRegistry}) {
33
33
  return {
34
34
  name: "ssr-lwc-transformer",
35
- async link(stringBuilder, viewContext, {customElements}) {
35
+ async link(stringBuilder, viewContext, metadata) {
36
36
  if (!viewContext.view.bootstrap?.ssr) {
37
37
  return {};
38
38
  }
39
39
  import_shared_utils.logger.debug("[lwcSsrViewTransformer] link");
40
40
  import_shared_utils.logger.verbose("[lwcSsrViewTransformer] link input", stringBuilder);
41
+ if (!metadata.ssrProps) {
42
+ metadata.ssrProps = {};
43
+ }
44
+ const {customElements, ssrProps} = metadata;
41
45
  const ssrModules = [];
42
46
  for (const [index, {tagName, location, props}] of customElements.entries()) {
43
47
  const isCsr = (0, import_shared_utils.isCsrIsland)(props);
@@ -58,7 +62,6 @@ function lwcSsrViewTransformer(options, {moduleBundler, resourceRegistry}) {
58
62
  });
59
63
  }
60
64
  }
61
- const ssrProps = {};
62
65
  let ssrLinks = "";
63
66
  let pageTtl;
64
67
  await Promise.all(ssrModules.map(({index, specifier, tagName, props: rawProps = {}, startOffset, endOffset}) => {
@@ -93,9 +96,6 @@ function lwcSsrViewTransformer(options, {moduleBundler, resourceRegistry}) {
93
96
  import_shared_utils.logger.warn(`Server-side rendering for "${specifier}" failed. Falling back to client-side rendering. Reason: `, err.stack);
94
97
  });
95
98
  }));
96
- if (Object.keys(ssrProps).length) {
97
- stringBuilder.prependLeft(ssrModules[0].startOffset, `<script type="application/javascript">globalThis.LWR = globalThis.LWR || {};globalThis.LWR.${import_identity.SSR_PROPS_KEY} = ${JSON.stringify(ssrProps)};</script>`);
98
- }
99
99
  if (ssrLinks) {
100
100
  const headIndex = stringBuilder.original.indexOf("</head>");
101
101
  if (headIndex >= 0) {
@@ -39,12 +39,12 @@ async function ssrElement({specifier, props: templateProps}, moduleBundler, reso
39
39
  code,
40
40
  specifier: bundleSpecifier,
41
41
  version
42
- } = format === "esm" ? await moduleBundler.getModuleBundle({specifier}, {...runtimeEnvironment, bundle: false}, void 0, {
42
+ } = format === "esm" ? await moduleBundler.getModuleBundle({specifier}, {...runtimeEnvironment, bundle: false}, runtimeParams, {
43
43
  exclude: [],
44
44
  alias: {
45
45
  lwc: "@lwc/engine-server"
46
46
  }
47
- }) : await (0, import_amd_utils.getBundle)(specifier, moduleBundler, runtimeEnvironment);
47
+ }) : await (0, import_amd_utils.getBundle)(specifier, moduleBundler, runtimeEnvironment, runtimeParams);
48
48
  const context = {
49
49
  props: templateProps,
50
50
  params: runtimeParams.params || {},
@@ -1,5 +1,4 @@
1
1
  export declare const LWC_SSR_PREFIX = "@lwrjs/lwc-ssr/";
2
2
  export declare const SSR_PROPS_ATTR = "data-lwr-props-id";
3
- export declare const SSR_PROPS_KEY = "ssrProps";
4
3
  export declare function getPropsId(): string;
5
4
  //# sourceMappingURL=identity.d.ts.map
@@ -1,6 +1,5 @@
1
1
  export const LWC_SSR_PREFIX = '@lwrjs/lwc-ssr/';
2
2
  export const SSR_PROPS_ATTR = 'data-lwr-props-id';
3
- export const SSR_PROPS_KEY = 'ssrProps';
4
3
  export function getPropsId() {
5
4
  return `lwcprops${Math.floor(Math.random() * 0x10000).toString(16)}`;
6
5
  }
@@ -1,7 +1,7 @@
1
- import type { BundleDefinition, PublicModuleBundler, PublicResourceRegistry, RuntimeEnvironment } from '@lwrjs/types';
1
+ import type { BundleDefinition, PublicModuleBundler, PublicResourceRegistry, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
2
  export declare function getCode(runtimeEnvironment: RuntimeEnvironment, lwrVersion: string, bundleSpecifier: string, includedModules: string[], resourceRegistry: PublicResourceRegistry): Promise<string[]>;
3
3
  type PartialBundleDefinition = Pick<BundleDefinition, 'bundleRecord' | 'code' | 'specifier' | 'version'>;
4
4
  export declare function getBundle(specifier: string, // e.g. "@lwrjs/lwc-ssr/root/component"
5
- moduleBundler: PublicModuleBundler, runtimeEnvironment: RuntimeEnvironment): Promise<PartialBundleDefinition>;
5
+ moduleBundler: PublicModuleBundler, runtimeEnvironment: RuntimeEnvironment, runtimeParams: RuntimeParams): Promise<PartialBundleDefinition>;
6
6
  export {};
7
7
  //# sourceMappingURL=amd-utils.d.ts.map
@@ -73,25 +73,28 @@ export async function getCode(runtimeEnvironment, lwrVersion, bundleSpecifier, i
73
73
  ];
74
74
  }
75
75
  export async function getBundle(specifier, // e.g. "@lwrjs/lwc-ssr/root/component"
76
- moduleBundler, runtimeEnvironment) {
76
+ moduleBundler, runtimeEnvironment, runtimeParams) {
77
77
  if (getFeatureFlags().SSR_STATIC_BUNDLES) {
78
78
  // concatenate existing bundles SSGed at build time
79
- return buildBundle(specifier, moduleBundler, runtimeEnvironment);
79
+ return buildBundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams);
80
80
  }
81
81
  // create a new SSR bundle
82
82
  // "lwc" is aliased as "@lwc/engine-server" in the AMD worker code
83
- return bundle(specifier, moduleBundler, runtimeEnvironment, { appendExcludes: false, exclude: ['lwc'] });
83
+ return bundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams, {
84
+ appendExcludes: false,
85
+ exclude: ['lwc'],
86
+ });
84
87
  }
85
- async function bundle(specifier, moduleBundler, runtimeEnvironment, bundleConfigOverrides) {
86
- return await moduleBundler.getModuleBundle({ specifier }, runtimeEnvironment, undefined, bundleConfigOverrides);
88
+ async function bundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams, bundleConfigOverrides) {
89
+ return await moduleBundler.getModuleBundle({ specifier }, runtimeEnvironment, runtimeParams, bundleConfigOverrides);
87
90
  }
88
91
  // Recursively bundle the static imports of a root bundle into a single bundle
89
- async function bundleImports(bundleCode, imports = [], visited, moduleBundler, runtimeEnvironment) {
92
+ async function bundleImports(bundleCode, imports = [], visited, moduleBundler, runtimeEnvironment, runtimeParams) {
90
93
  for (const { specifier } of imports) {
91
94
  if (!visited.has(specifier)) {
92
95
  visited.add(specifier);
93
96
  // eslint-disable-next-line no-await-in-loop
94
- const { code, bundleRecord, version } = await bundle(specifier, moduleBundler, runtimeEnvironment);
97
+ const { code, bundleRecord, version } = await bundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams);
95
98
  let bundledCode;
96
99
  if (runtimeEnvironment.featureFlags?.EXPERIMENTAL_UNVERSIONED_ALIASES) {
97
100
  const versionedSpecifier = getSpecifier({ specifier, version });
@@ -103,7 +106,7 @@ async function bundleImports(bundleCode, imports = [], visited, moduleBundler, r
103
106
  }
104
107
  bundleCode =
105
108
  // eslint-disable-next-line no-await-in-loop
106
- (await bundleImports(bundledCode, bundleRecord.imports, visited, moduleBundler, runtimeEnvironment)) + bundleCode;
109
+ (await bundleImports(bundledCode, bundleRecord.imports, visited, moduleBundler, runtimeEnvironment, runtimeParams)) + bundleCode;
107
110
  }
108
111
  }
109
112
  return bundleCode;
@@ -112,15 +115,15 @@ async function bundleImports(bundleCode, imports = [], visited, moduleBundler, r
112
115
  // - the root component bundle and static imports
113
116
  // - @lwc/engine-server
114
117
  // - the SSR bundle for the root specifier
115
- async function buildBundle(ssrSpecifier, moduleBundler, runtimeEnvironment) {
118
+ async function buildBundle(ssrSpecifier, moduleBundler, runtimeEnvironment, runtimeParams) {
116
119
  // 1. Get the bundle for the root component, including all static dependencies
117
120
  const rootSpecifier = ssrSpecifier.replace(LWC_SSR_PREFIX, '');
118
- const { code: _rootCode, bundleRecord: rootBundleRecord } = await bundle(rootSpecifier, moduleBundler, runtimeEnvironment);
121
+ const { code: _rootCode, bundleRecord: rootBundleRecord } = await bundle(rootSpecifier, moduleBundler, runtimeEnvironment, runtimeParams);
119
122
  let rootCode = await bundleImports(_rootCode, rootBundleRecord.imports, new Set(['lwc', rootSpecifier]), // visited (lwc is excluded)
120
- moduleBundler, runtimeEnvironment);
123
+ moduleBundler, runtimeEnvironment, runtimeParams);
121
124
  // TODO: remove pending TD-0148452
122
125
  if (runtimeEnvironment.featureFlags?.EXPERIMENTAL_UNVERSIONED_ALIASES) {
123
- // Create an AMD alias to the unversioned root module
126
+ // Create an AMD alias to the un-versioned root module
124
127
  // if the bundle-metadata for the module doesn't have a version, it will have /v/version-not-provided`
125
128
  const aliasSpecifier = getSpecifier({
126
129
  specifier: rootSpecifier,
@@ -130,10 +133,10 @@ async function buildBundle(ssrSpecifier, moduleBundler, runtimeEnvironment) {
130
133
  rootCode = [aliasCode, rootCode].join('');
131
134
  }
132
135
  // 2. Get the bundle for the LWC engine
133
- const { code: lwcEngineCode, version: lwcVersion } = await bundle(LWC_SPECIFIERS.ssr, moduleBundler, runtimeEnvironment);
136
+ const { code: lwcEngineCode, version: lwcVersion } = await bundle(LWC_SPECIFIERS.ssr, moduleBundler, runtimeEnvironment, runtimeParams);
134
137
  // 3. Get the bundle for the SSR root component
135
138
  // Exclude the LWC engine and root component from this bundle. Use the pre-built bundles fetched above instead.
136
- const { bundleRecord, code: ssrCode, version: lwrVersion, } = await bundle(ssrSpecifier, moduleBundler, runtimeEnvironment, {
139
+ const { bundleRecord, code: ssrCode, version: lwrVersion, } = await bundle(ssrSpecifier, moduleBundler, runtimeEnvironment, runtimeParams, {
137
140
  appendExcludes: true,
138
141
  exclude: [LWC_SPECIFIERS.ssr, rootSpecifier],
139
142
  });
@@ -1,5 +1,5 @@
1
1
  import { HYDRATE_CLIENT_VALUE, HYDRATE_DIRECTIVE, isCsrIsland, isHydrateOnLoad, kebabCaseToModuleSpecifier, logger, shortestTtl, } from '@lwrjs/shared-utils';
2
- import { LWC_SSR_PREFIX, SSR_PROPS_ATTR, SSR_PROPS_KEY, getPropsId } from '../identity.js';
2
+ import { LWC_SSR_PREFIX, SSR_PROPS_ATTR, getPropsId } from '../identity.js';
3
3
  import { ssrElement } from './ssr-element.js';
4
4
  /**
5
5
  * This is a view transformer run by the view registry during linking of a page document/route (configured in lwr.config.json[routes]).
@@ -22,12 +22,16 @@ import { ssrElement } from './ssr-element.js';
22
22
  export default function lwcSsrViewTransformer(options, { moduleBundler, resourceRegistry }) {
23
23
  return {
24
24
  name: 'ssr-lwc-transformer',
25
- async link(stringBuilder, viewContext, { customElements }) {
25
+ async link(stringBuilder, viewContext, metadata) {
26
26
  if (!viewContext.view.bootstrap?.ssr) {
27
27
  return {}; // no SSR
28
28
  }
29
29
  logger.debug('[lwcSsrViewTransformer] link');
30
30
  logger.verbose('[lwcSsrViewTransformer] link input', stringBuilder);
31
+ if (!metadata.ssrProps) {
32
+ metadata.ssrProps = {};
33
+ }
34
+ const { customElements, ssrProps } = metadata;
31
35
  // Gather all the SSRable custom elements (ie: root components) into 1 list
32
36
  const ssrModules = [];
33
37
  for (const [index, { tagName, location, props }] of customElements.entries()) {
@@ -51,7 +55,6 @@ export default function lwcSsrViewTransformer(options, { moduleBundler, resource
51
55
  }
52
56
  }
53
57
  // SSR and gather the properties and links for each eligible custom element, in parallel
54
- const ssrProps = {};
55
58
  let ssrLinks = '';
56
59
  let pageTtl;
57
60
  await Promise.all(ssrModules.map(({ index, specifier, tagName, props: rawProps = {}, startOffset, endOffset }) => {
@@ -96,11 +99,6 @@ export default function lwcSsrViewTransformer(options, { moduleBundler, resource
96
99
  logger.warn(`Server-side rendering for "${specifier}" failed. Falling back to client-side rendering. Reason: `, err.stack);
97
100
  });
98
101
  }));
99
- if (Object.keys(ssrProps).length) {
100
- // Serialize all root component properties into a single script for the page
101
- // Append the script before the custom elements; it MUST appear before the AMD shim to avoid timing issues
102
- stringBuilder.prependLeft(ssrModules[0].startOffset, `<script type="application/javascript">globalThis.LWR = globalThis.LWR || {};globalThis.LWR.${SSR_PROPS_KEY} = ${JSON.stringify(ssrProps)};</script>`);
103
- }
104
102
  if (ssrLinks) {
105
103
  // Add all the links to the <head> section of the base document
106
104
  const headIndex = stringBuilder.original.indexOf('</head>');
@@ -19,14 +19,14 @@ export async function ssrElement({ specifier, props: templateProps }, moduleBund
19
19
  // Ensure the bundle flag is always off in ESM,
20
20
  // otherwise TOO much gets bundled in the module registry
21
21
  // in ESM, resulting lwc clashes/duplication
22
- { ...runtimeEnvironment, bundle: false }, undefined, {
22
+ { ...runtimeEnvironment, bundle: false }, runtimeParams, {
23
23
  exclude: [],
24
24
  alias: {
25
25
  // override the default "@lwc/engine-dom" package
26
26
  lwc: '@lwc/engine-server',
27
27
  },
28
28
  })
29
- : await getBundle(specifier, moduleBundler, runtimeEnvironment);
29
+ : await getBundle(specifier, moduleBundler, runtimeEnvironment, runtimeParams);
30
30
  // Gather context to send into the SSR sandbox
31
31
  const context = {
32
32
  props: templateProps,
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",
@@ -27,6 +27,11 @@
27
27
  "require": "./build/cjs/viewTransformer/index.cjs"
28
28
  }
29
29
  },
30
+ "scripts": {
31
+ "build": "tsc -b",
32
+ "clean": "rm -rf build node_modules",
33
+ "test": "jest"
34
+ },
30
35
  "files": [
31
36
  "build/**/*.js",
32
37
  "build/**/*.cjs",
@@ -34,16 +39,21 @@
34
39
  ],
35
40
  "dependencies": {
36
41
  "@locker/near-membrane-node": "^0.12.15",
37
- "@lwrjs/diagnostics": "0.10.2",
38
- "@lwrjs/instrumentation": "0.10.2",
39
- "@lwrjs/shared-utils": "0.10.2",
42
+ "@lwrjs/diagnostics": "0.11.0-alpha.1",
43
+ "@lwrjs/instrumentation": "0.11.0-alpha.1",
44
+ "@lwrjs/shared-utils": "0.11.0-alpha.1",
40
45
  "node-fetch": "^2.6.8"
41
46
  },
42
47
  "devDependencies": {
43
- "@lwrjs/types": "0.10.2"
48
+ "@lwrjs/types": "0.11.0-alpha.1",
49
+ "jest": "^26.6.3",
50
+ "ts-jest": "^26.5.6"
44
51
  },
45
52
  "engines": {
46
53
  "node": ">=16.0.0"
47
54
  },
48
- "gitHead": "40988dfc093a3b9dea859f1bf61a49bca1edb0c5"
55
+ "volta": {
56
+ "extends": "../../../package.json"
57
+ },
58
+ "gitHead": "6a840d4694fac9f0be5c3a21707015379cbadad7"
49
59
  }