@lwrjs/lwc-ssr 0.7.0-alpha.6 → 0.7.0-alpha.9
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.
- package/build/cjs/moduleProvider/index.cjs +19 -32
- package/build/cjs/viewTransformer/amd-utils.cjs +88 -0
- package/build/cjs/viewTransformer/index.cjs +21 -11
- package/build/cjs/viewTransformer/ssr-element.cjs +26 -16
- package/build/es/moduleProvider/index.d.ts +19 -5
- package/build/es/moduleProvider/index.js +34 -30
- package/build/es/viewTransformer/amd-utils.d.ts +3 -0
- package/build/es/viewTransformer/amd-utils.js +70 -0
- package/build/es/viewTransformer/index.d.ts +18 -2
- package/build/es/viewTransformer/index.js +40 -14
- package/build/es/viewTransformer/ssr-element.d.ts +15 -2
- package/build/es/viewTransformer/ssr-element.js +40 -15
- package/package.json +5 -5
- package/build/cjs/moduleProvider/utils.cjs +0 -42
- package/build/es/moduleProvider/utils.d.ts +0 -6
- package/build/es/moduleProvider/utils.js +0 -19
|
@@ -24,20 +24,27 @@ var __toModule = (module2) => {
|
|
|
24
24
|
// packages/@lwrjs/lwc-ssr/src/moduleProvider/index.ts
|
|
25
25
|
__markAsModule(exports);
|
|
26
26
|
__export(exports, {
|
|
27
|
+
createSsrBootstrapModule: () => createSsrBootstrapModule,
|
|
27
28
|
default: () => moduleProvider_default
|
|
28
29
|
});
|
|
29
|
-
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
30
30
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
31
|
-
var import_utils = __toModule(require("./utils.cjs"));
|
|
32
31
|
var import_identity = __toModule(require("../identity.cjs"));
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
function createSsrBootstrapModule(rootComponent) {
|
|
33
|
+
return [
|
|
34
|
+
"/* This module is generated and meant to be used in a Server context */",
|
|
35
|
+
'import { renderComponent } from "@lwc/engine-server"',
|
|
36
|
+
`import Ctor from "${rootComponent}"`,
|
|
37
|
+
"try {",
|
|
38
|
+
` const result = renderComponent("${(0, import_shared_utils.moduleSpecifierToKebabCase)(rootComponent)}", Ctor, globalThis.ssrProps);`,
|
|
39
|
+
" globalThis.postback({ result });",
|
|
40
|
+
"} catch(err) {",
|
|
41
|
+
" globalThis.postback({ error: err.toString() });",
|
|
42
|
+
`}`
|
|
43
|
+
].join("\n");
|
|
36
44
|
}
|
|
37
45
|
var LwcSsrModuleProvider = class {
|
|
38
|
-
constructor(
|
|
46
|
+
constructor(providerConfig, {runtimeEnvironment: {lwrVersion}}) {
|
|
39
47
|
this.name = "ssr-module-provider";
|
|
40
|
-
this.config = config;
|
|
41
48
|
this.version = lwrVersion;
|
|
42
49
|
}
|
|
43
50
|
async getModuleEntry({specifier}) {
|
|
@@ -52,22 +59,12 @@ var LwcSsrModuleProvider = class {
|
|
|
52
59
|
};
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
|
-
async
|
|
56
|
-
specifier,
|
|
57
|
-
namespace,
|
|
58
|
-
name
|
|
59
|
-
}) {
|
|
60
|
-
if (!specifier.startsWith(import_identity.LWC_SSR_PREFIX)) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
62
|
+
async getModule({specifier, namespace, name}) {
|
|
63
63
|
const moduleEntry = await this.getModuleEntry({specifier});
|
|
64
64
|
if (!moduleEntry) {
|
|
65
|
-
|
|
66
|
-
description: import_diagnostics.descriptions.UNRESOLVABLE.MODULE(specifier)
|
|
67
|
-
}, import_diagnostics.LwrUnresolvableError);
|
|
65
|
+
return;
|
|
68
66
|
}
|
|
69
|
-
const
|
|
70
|
-
const originalSource = (0, import_utils.createSsrBootstrapModule)(customElementModule);
|
|
67
|
+
const originalSource = createSsrBootstrapModule(specifier.replace(import_identity.LWC_SSR_PREFIX, ""));
|
|
71
68
|
return {
|
|
72
69
|
id: moduleEntry.id,
|
|
73
70
|
namespace,
|
|
@@ -76,18 +73,8 @@ var LwcSsrModuleProvider = class {
|
|
|
76
73
|
specifier,
|
|
77
74
|
moduleEntry,
|
|
78
75
|
ownHash: (0, import_shared_utils.hashContent)(originalSource),
|
|
79
|
-
originalSource
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
async getModule(moduleId) {
|
|
83
|
-
if (!moduleId.specifier.startsWith("@lwrjs/lwc-ssr/")) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const moduleSource = await this.getModuleSource(moduleId);
|
|
87
|
-
const compiledSource = moduleSource.originalSource;
|
|
88
|
-
return {
|
|
89
|
-
...moduleSource,
|
|
90
|
-
compiledSource
|
|
76
|
+
originalSource,
|
|
77
|
+
compiledSource: originalSource
|
|
91
78
|
};
|
|
92
79
|
}
|
|
93
80
|
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, {get: all[name], enumerable: true});
|
|
11
|
+
};
|
|
12
|
+
var __exportStar = (target, module2, desc) => {
|
|
13
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(module2))
|
|
15
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
16
|
+
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
|
|
17
|
+
}
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
20
|
+
var __toModule = (module2) => {
|
|
21
|
+
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// packages/@lwrjs/lwc-ssr/src/viewTransformer/amd-utils.ts
|
|
25
|
+
__markAsModule(exports);
|
|
26
|
+
__export(exports, {
|
|
27
|
+
default: () => getCode
|
|
28
|
+
});
|
|
29
|
+
var import_loader = __toModule(require("@lwrjs/loader"));
|
|
30
|
+
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
31
|
+
async function readableToString(readable) {
|
|
32
|
+
let result = "";
|
|
33
|
+
for await (const chunk of readable) {
|
|
34
|
+
result += chunk;
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
var loaderShimSource;
|
|
39
|
+
async function getLoaderShim(runtimeEnvironment) {
|
|
40
|
+
if (loaderShimSource !== void 0) {
|
|
41
|
+
return loaderShimSource;
|
|
42
|
+
}
|
|
43
|
+
const amdloaderShimService = new import_loader.default({}, {
|
|
44
|
+
runtimeEnvironment,
|
|
45
|
+
resourceRegistry: {resolveResourceUri: () => "NOT IMPLEMENTED"}
|
|
46
|
+
});
|
|
47
|
+
const specifier = (0, import_shared_utils.getFeatureFlags)().LEGACY_LOADER ? "lwr-loader-shim-legacy.bundle.js" : "lwr-loader-shim.bundle.js";
|
|
48
|
+
const resource = await amdloaderShimService.getResource({specifier}, runtimeEnvironment);
|
|
49
|
+
if (resource) {
|
|
50
|
+
const {stream} = resource;
|
|
51
|
+
if (stream) {
|
|
52
|
+
loaderShimSource = await readableToString(stream);
|
|
53
|
+
return loaderShimSource;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function getLwrConfig(bundleSpecifier, lwrVersion) {
|
|
58
|
+
return Object.assign({}, {
|
|
59
|
+
autoBoot: true,
|
|
60
|
+
bootstrapModule: `${bundleSpecifier}/v/${lwrVersion}`,
|
|
61
|
+
disableInitDefer: true,
|
|
62
|
+
endpoints: {
|
|
63
|
+
uris: {mapping: "/1/mapping/amd/1/l/en-US/mp/"}
|
|
64
|
+
},
|
|
65
|
+
rootComponents: [`${bundleSpecifier.split("@lwrjs/lwc-ssr/")[1]}/v/${lwrVersion}`]
|
|
66
|
+
}, (0, import_shared_utils.getFeatureFlags)().LEGACY_LOADER ? {
|
|
67
|
+
baseUrl: "ssr"
|
|
68
|
+
} : {
|
|
69
|
+
baseUrl: "/",
|
|
70
|
+
imports: {
|
|
71
|
+
"any/thing.js": ["any/thing"]
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function lwcDefineOverride(lwcSpecifier) {
|
|
76
|
+
return `LWR.define("${lwcSpecifier}", ["${lwcSpecifier.replace("lwc", "@lwc/engine-server")}"], function(lwcEngine) { return lwcEngine; });`;
|
|
77
|
+
}
|
|
78
|
+
var GLOBALTHIS_LWR = `globalThis.LWR = globalThis.LWR || {};`;
|
|
79
|
+
async function getCode(runtimeEnvironment, lwrVersion, lwcSpecifier, bundleSpecifier) {
|
|
80
|
+
const loaderShimSource2 = await getLoaderShim(runtimeEnvironment);
|
|
81
|
+
const lwrConfigString = JSON.stringify(getLwrConfig(bundleSpecifier, lwrVersion));
|
|
82
|
+
return [
|
|
83
|
+
GLOBALTHIS_LWR,
|
|
84
|
+
`Object.assign(globalThis.LWR, ${lwrConfigString});`,
|
|
85
|
+
loaderShimSource2 ? loaderShimSource2 : "",
|
|
86
|
+
lwcSpecifier ? lwcDefineOverride(lwcSpecifier) : ""
|
|
87
|
+
];
|
|
88
|
+
}
|
|
@@ -29,22 +29,32 @@ __export(exports, {
|
|
|
29
29
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
30
30
|
var import_identity = __toModule(require("../identity.cjs"));
|
|
31
31
|
var import_ssr_element = __toModule(require("./ssr-element.cjs"));
|
|
32
|
-
function lwcSsrViewTranformer(options,
|
|
33
|
-
const {moduleBundler} = lwrGlobalContext;
|
|
32
|
+
function lwcSsrViewTranformer(options, {moduleBundler}) {
|
|
34
33
|
return {
|
|
35
34
|
name: "ssr-lwc-transformer",
|
|
36
|
-
async link(stringBuilder, viewContext,
|
|
35
|
+
async link(stringBuilder, viewContext, {customElements}) {
|
|
36
|
+
if (process.env.LOCKER === "true") {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
37
39
|
if (viewContext.view.bootstrap?.experimentalSSR) {
|
|
38
|
-
const
|
|
39
|
-
for (const
|
|
40
|
-
if (
|
|
41
|
-
const {startOffset, endOffset} =
|
|
42
|
-
const moduleSpecifier = (0, import_shared_utils.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
const ssrModules = [];
|
|
41
|
+
for (const {tagName, location, props = {}} of customElements) {
|
|
42
|
+
if (location) {
|
|
43
|
+
const {startOffset, endOffset} = location;
|
|
44
|
+
const moduleSpecifier = (0, import_shared_utils.kebabCaseToModuleSpecifer)(tagName);
|
|
45
|
+
ssrModules.push({
|
|
46
|
+
startOffset,
|
|
47
|
+
endOffset,
|
|
48
|
+
props,
|
|
49
|
+
specifier: `${import_identity.LWC_SSR_PREFIX}${moduleSpecifier}`
|
|
50
|
+
});
|
|
46
51
|
}
|
|
47
52
|
}
|
|
53
|
+
return Promise.all(ssrModules.map(({specifier, props, startOffset, endOffset}) => {
|
|
54
|
+
return (0, import_ssr_element.ssrElement)({specifier, props}, moduleBundler, viewContext.runtimeEnvironment).then((ssrResult) => {
|
|
55
|
+
stringBuilder.overwrite(startOffset, endOffset, ssrResult);
|
|
56
|
+
});
|
|
57
|
+
})).then(() => void 0);
|
|
48
58
|
}
|
|
49
59
|
}
|
|
50
60
|
};
|
|
@@ -27,34 +27,44 @@ __export(exports, {
|
|
|
27
27
|
ssrElement: () => ssrElement
|
|
28
28
|
});
|
|
29
29
|
var import_worker_threads = __toModule(require("worker_threads"));
|
|
30
|
+
var import_amd_utils = __toModule(require("./amd-utils.cjs"));
|
|
30
31
|
var bundleConfigOverrides = {
|
|
31
32
|
exclude: [],
|
|
32
33
|
alias: {
|
|
33
34
|
lwc: "@lwc/engine-server"
|
|
34
35
|
}
|
|
35
36
|
};
|
|
36
|
-
var
|
|
37
|
-
var
|
|
38
|
-
function runCodeOnWorker(
|
|
39
|
-
const workerCode = [
|
|
40
|
-
return new Promise((resolve
|
|
37
|
+
var WORKER_CONTEXT_PARENTPORT = `const { parentPort } = require('worker_threads');`;
|
|
38
|
+
var WORKER_CONTEXT_POSTBACK = `globalThis.postback = (result) => parentPort.postMessage(result);`;
|
|
39
|
+
function runCodeOnWorker(codes) {
|
|
40
|
+
const workerCode = [WORKER_CONTEXT_PARENTPORT, WORKER_CONTEXT_POSTBACK, ...codes].join("\n");
|
|
41
|
+
return new Promise((resolve) => {
|
|
41
42
|
const worker = new import_worker_threads.Worker(workerCode, {eval: true});
|
|
42
43
|
worker.on("message", resolve);
|
|
43
|
-
worker.on("error",
|
|
44
|
-
worker.on("exit", (
|
|
45
|
-
if (
|
|
46
|
-
|
|
44
|
+
worker.on("error", ({message}) => resolve({error: `SSR worker exited with error: ${message}`}));
|
|
45
|
+
worker.on("exit", (code) => {
|
|
46
|
+
if (code !== 0) {
|
|
47
|
+
resolve({error: `SSR worker stopped with exit code: ${code}`});
|
|
47
48
|
}
|
|
48
49
|
});
|
|
49
50
|
});
|
|
50
51
|
}
|
|
51
|
-
async function ssrElement(
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
async function ssrElement({specifier, props}, moduleBundler, runtimeEnvironment) {
|
|
53
|
+
runtimeEnvironment.bundle = false;
|
|
54
|
+
const {
|
|
55
|
+
bundleRecord,
|
|
56
|
+
code,
|
|
57
|
+
specifier: bundleSpecifier,
|
|
58
|
+
version
|
|
59
|
+
} = await moduleBundler.getModuleBundle({specifier}, runtimeEnvironment, void 0, bundleConfigOverrides);
|
|
60
|
+
const {error, result} = runtimeEnvironment.format === "amd" ? await runCodeOnWorker([
|
|
61
|
+
...await (0, import_amd_utils.default)(runtimeEnvironment, version.replace(/\./g, "_"), bundleRecord.includedModules.find((m) => m.startsWith("lwc/v")), bundleSpecifier),
|
|
62
|
+
`globalThis.ssrProps = ${JSON.stringify(props)};`,
|
|
63
|
+
code
|
|
64
|
+
]) : await runCodeOnWorker([`globalThis.ssrProps = ${JSON.stringify(props)};`, code]);
|
|
65
|
+
if (error) {
|
|
66
|
+
throw new Error(error);
|
|
57
67
|
} else {
|
|
58
|
-
return result
|
|
68
|
+
return result;
|
|
59
69
|
}
|
|
60
70
|
}
|
|
@@ -1,11 +1,25 @@
|
|
|
1
|
-
import { ModuleCompiled, ModuleEntry, ModuleProvider,
|
|
1
|
+
import { ModuleCompiled, ModuleEntry, ModuleProvider, ProviderContext, AbstractModuleId } from '@lwrjs/types';
|
|
2
|
+
/**
|
|
3
|
+
* This module provider generates code which server-side renders a given component.
|
|
4
|
+
* It handles module specifiers in the form: "@lwrjs/lwc-ssr/${component specifier}".
|
|
5
|
+
* eg: "@lwrjs/lwc-ssr/my/appRoot"
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Create the virtual source for a module which server-side renders the given component.
|
|
9
|
+
* This code is meant to be executed on the server; it is run in a worker in "lwc-ssr/viewTransformer#ssr-element" during linking.
|
|
10
|
+
* The result is posted to the parentPort (via globalThis.postback() defined in the worker) and the Promise in the main thread resolves.
|
|
11
|
+
* The properties are stored in "globalThis.ssrProps" and used in the SSR module.
|
|
12
|
+
* The postback function "globalThis.postback" must be set prior to executing the SSR module.
|
|
13
|
+
* @param rootComponent - The specifier for the component to SSR
|
|
14
|
+
* @param globalThis.ssrProps - The properties to pass to the component, set in "lwc-ssr/viewTransformer#ssr-element"
|
|
15
|
+
* @returns the generated module source
|
|
16
|
+
*/
|
|
17
|
+
export declare function createSsrBootstrapModule(rootComponent: string): string;
|
|
2
18
|
export default class LwcSsrModuleProvider implements ModuleProvider {
|
|
3
19
|
name: string;
|
|
4
20
|
version: string;
|
|
5
|
-
|
|
6
|
-
constructor(appPluginConfig: unknown, { config, runtimeEnvironment: { hmrEnabled, lwrVersion } }: ProviderContext);
|
|
21
|
+
constructor(providerConfig: unknown, { runtimeEnvironment: { lwrVersion } }: ProviderContext);
|
|
7
22
|
getModuleEntry({ specifier }: AbstractModuleId): Promise<ModuleEntry | undefined>;
|
|
8
|
-
|
|
9
|
-
getModule(moduleId: AbstractModuleId): Promise<ModuleCompiled | undefined>;
|
|
23
|
+
getModule({ specifier, namespace, name }: AbstractModuleId): Promise<ModuleCompiled | undefined>;
|
|
10
24
|
}
|
|
11
25
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,15 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { hashContent } from '@lwrjs/shared-utils';
|
|
3
|
-
import { createSsrBootstrapModule } from './utils.js';
|
|
1
|
+
import { hashContent, moduleSpecifierToKebabCase } from '@lwrjs/shared-utils';
|
|
4
2
|
import { LWC_SSR_PREFIX } from '../identity.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
/**
|
|
4
|
+
* This module provider generates code which server-side renders a given component.
|
|
5
|
+
* It handles module specifiers in the form: "@lwrjs/lwc-ssr/${component specifier}".
|
|
6
|
+
* eg: "@lwrjs/lwc-ssr/my/appRoot"
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Create the virtual source for a module which server-side renders the given component.
|
|
10
|
+
* This code is meant to be executed on the server; it is run in a worker in "lwc-ssr/viewTransformer#ssr-element" during linking.
|
|
11
|
+
* The result is posted to the parentPort (via globalThis.postback() defined in the worker) and the Promise in the main thread resolves.
|
|
12
|
+
* The properties are stored in "globalThis.ssrProps" and used in the SSR module.
|
|
13
|
+
* The postback function "globalThis.postback" must be set prior to executing the SSR module.
|
|
14
|
+
* @param rootComponent - The specifier for the component to SSR
|
|
15
|
+
* @param globalThis.ssrProps - The properties to pass to the component, set in "lwc-ssr/viewTransformer#ssr-element"
|
|
16
|
+
* @returns the generated module source
|
|
17
|
+
*/
|
|
18
|
+
export function createSsrBootstrapModule(rootComponent) {
|
|
19
|
+
return [
|
|
20
|
+
'/* This module is generated and meant to be used in a Server context */',
|
|
21
|
+
'import { renderComponent } from "@lwc/engine-server"',
|
|
22
|
+
`import Ctor from "${rootComponent}"`,
|
|
23
|
+
'try {',
|
|
24
|
+
` const result = renderComponent("${moduleSpecifierToKebabCase(rootComponent)}", Ctor, globalThis.ssrProps);`,
|
|
25
|
+
' globalThis.postback({ result });',
|
|
26
|
+
'} catch(err) {',
|
|
27
|
+
' globalThis.postback({ error: err.toString() });',
|
|
28
|
+
`}`,
|
|
29
|
+
].join('\n');
|
|
8
30
|
}
|
|
9
31
|
export default class LwcSsrModuleProvider {
|
|
10
|
-
constructor(
|
|
32
|
+
constructor(providerConfig, { runtimeEnvironment: { lwrVersion } }) {
|
|
11
33
|
this.name = 'ssr-module-provider';
|
|
12
|
-
this.config = config;
|
|
13
34
|
this.version = lwrVersion;
|
|
14
35
|
}
|
|
15
36
|
async getModuleEntry({ specifier }) {
|
|
@@ -24,19 +45,13 @@ export default class LwcSsrModuleProvider {
|
|
|
24
45
|
};
|
|
25
46
|
}
|
|
26
47
|
}
|
|
27
|
-
async
|
|
28
|
-
if (!specifier.startsWith(LWC_SSR_PREFIX)) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
// Fetch the Module Entry and corresponding route config object
|
|
48
|
+
async getModule({ specifier, namespace, name }) {
|
|
32
49
|
const moduleEntry = await this.getModuleEntry({ specifier });
|
|
33
50
|
if (!moduleEntry) {
|
|
34
|
-
|
|
35
|
-
description: descriptions.UNRESOLVABLE.MODULE(specifier),
|
|
36
|
-
}, LwrUnresolvableError);
|
|
51
|
+
return;
|
|
37
52
|
}
|
|
38
|
-
|
|
39
|
-
const originalSource = createSsrBootstrapModule(
|
|
53
|
+
// Generate the module source which SSRs a lwc, and return the Module
|
|
54
|
+
const originalSource = createSsrBootstrapModule(specifier.replace(LWC_SSR_PREFIX, ''));
|
|
40
55
|
return {
|
|
41
56
|
id: moduleEntry.id,
|
|
42
57
|
namespace,
|
|
@@ -46,18 +61,7 @@ export default class LwcSsrModuleProvider {
|
|
|
46
61
|
moduleEntry,
|
|
47
62
|
ownHash: hashContent(originalSource),
|
|
48
63
|
originalSource,
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
async getModule(moduleId) {
|
|
52
|
-
if (!moduleId.specifier.startsWith('@lwrjs/lwc-ssr/')) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
// Get the source to compile
|
|
56
|
-
const moduleSource = (await this.getModuleSource(moduleId));
|
|
57
|
-
const compiledSource = moduleSource.originalSource;
|
|
58
|
-
return {
|
|
59
|
-
...moduleSource,
|
|
60
|
-
compiledSource,
|
|
64
|
+
compiledSource: originalSource,
|
|
61
65
|
};
|
|
62
66
|
}
|
|
63
67
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import AmdLoaderShimService from '@lwrjs/loader';
|
|
2
|
+
import { getFeatureFlags } from '@lwrjs/shared-utils';
|
|
3
|
+
async function readableToString(readable) {
|
|
4
|
+
let result = '';
|
|
5
|
+
for await (const chunk of readable) {
|
|
6
|
+
result += chunk;
|
|
7
|
+
}
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
let loaderShimSource;
|
|
11
|
+
async function getLoaderShim(runtimeEnvironment) {
|
|
12
|
+
if (loaderShimSource !== undefined) {
|
|
13
|
+
return loaderShimSource;
|
|
14
|
+
}
|
|
15
|
+
const amdloaderShimService = new AmdLoaderShimService({}, {
|
|
16
|
+
runtimeEnvironment,
|
|
17
|
+
resourceRegistry: { resolveResourceUri: () => 'NOT IMPLEMENTED' },
|
|
18
|
+
});
|
|
19
|
+
const specifier = getFeatureFlags().LEGACY_LOADER
|
|
20
|
+
? 'lwr-loader-shim-legacy.bundle.js'
|
|
21
|
+
: 'lwr-loader-shim.bundle.js';
|
|
22
|
+
const resource = await amdloaderShimService.getResource({ specifier }, runtimeEnvironment);
|
|
23
|
+
if (resource) {
|
|
24
|
+
const { stream } = resource;
|
|
25
|
+
if (stream) {
|
|
26
|
+
loaderShimSource = await readableToString(stream);
|
|
27
|
+
return loaderShimSource;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function getLwrConfig(bundleSpecifier, lwrVersion) {
|
|
32
|
+
// This is the minimum LWR config required for the loader to be created and mountApp to succeed
|
|
33
|
+
return Object.assign({}, {
|
|
34
|
+
autoBoot: true,
|
|
35
|
+
bootstrapModule: `${bundleSpecifier}/v/${lwrVersion}`,
|
|
36
|
+
disableInitDefer: true,
|
|
37
|
+
endpoints: {
|
|
38
|
+
uris: { mapping: '/1/mapping/amd/1/l/en-US/mp/' },
|
|
39
|
+
},
|
|
40
|
+
rootComponents: [`${bundleSpecifier.split('@lwrjs/lwc-ssr/')[1]}/v/${lwrVersion}`],
|
|
41
|
+
}, getFeatureFlags().LEGACY_LOADER
|
|
42
|
+
? {
|
|
43
|
+
baseUrl: 'ssr',
|
|
44
|
+
}
|
|
45
|
+
: {
|
|
46
|
+
baseUrl: '/',
|
|
47
|
+
imports: {
|
|
48
|
+
'any/thing.js': ['any/thing'],
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
// lwc/v/2_13_0 -> @lwc/engine-server/v/2_13_0
|
|
53
|
+
function lwcDefineOverride(lwcSpecifier) {
|
|
54
|
+
return `LWR.define("${lwcSpecifier}", ["${lwcSpecifier.replace('lwc', '@lwc/engine-server')}"], function(lwcEngine) { return lwcEngine; });`;
|
|
55
|
+
}
|
|
56
|
+
const GLOBALTHIS_LWR = `globalThis.LWR = globalThis.LWR || {};`;
|
|
57
|
+
export default async function getCode(runtimeEnvironment, lwrVersion, lwcSpecifier, bundleSpecifier) {
|
|
58
|
+
const loaderShimSource = await getLoaderShim(runtimeEnvironment);
|
|
59
|
+
const lwrConfigString = JSON.stringify(getLwrConfig(bundleSpecifier, lwrVersion));
|
|
60
|
+
// Order matters:
|
|
61
|
+
// 1. "globalThis.LWR" must be defined prior to executing the shim and loader
|
|
62
|
+
// 2. the lwc module override needs to be defined before lwc is [re]defined in the custom element code (first define wins)
|
|
63
|
+
return [
|
|
64
|
+
GLOBALTHIS_LWR,
|
|
65
|
+
`Object.assign(globalThis.LWR, ${lwrConfigString});`,
|
|
66
|
+
loaderShimSource ? loaderShimSource : '',
|
|
67
|
+
lwcSpecifier ? lwcDefineOverride(lwcSpecifier) : '',
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=amd-utils.js.map
|
|
@@ -1,7 +1,23 @@
|
|
|
1
|
-
import { ProviderContext, ViewTransformPlugin } from '@lwrjs/types';
|
|
1
|
+
import type { ProviderContext, ViewTransformPlugin } from '@lwrjs/types';
|
|
2
2
|
interface SsrPluginOptions {
|
|
3
3
|
shareWorker?: boolean;
|
|
4
4
|
}
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* This is a view transformer run by the view registry during linking of a page document/route (configured in lwr.config.json[routes]).
|
|
7
|
+
* If the "experimentalSSR" bootstrap flag is on for the route, it will server-side render (SSR) each custom element found in the page HTML.
|
|
8
|
+
*
|
|
9
|
+
* SSR Flow:
|
|
10
|
+
* 1. There is a request to generate a view (ie: page document) via the UI middleware or static site generation
|
|
11
|
+
* 2. During view generation, the view registry runs all the registered view transformers (including this one)
|
|
12
|
+
* 3. This view transformer links the SSRed string for EVERY custom element (ie: root component) found in the page document:
|
|
13
|
+
* a) It requests a module which SSRs a given custom element, generated by "lwc-ssr/moduleProvider"
|
|
14
|
+
* b) A bundle is created for the generated SSR module (see "./ssr-element")
|
|
15
|
+
* c) The bundle code is run inside a worker (see "./ssr-element"), with properties stored in "globalThis.ssrProps"
|
|
16
|
+
* d) The generated SSR module (running the worker) passes the SSRed code string back to the main thread (ie: "globalThis.ssrWorkerResult")
|
|
17
|
+
* e) The SSRed string is used to overwrite/link each custom element (eg: "<c-app></c-app>") in the document (see "stringBuild.overwrite")
|
|
18
|
+
* 4. The view/page document now contains SSRed components, which will be sent to the client
|
|
19
|
+
* 5. During bootstrap on the client, the "lwr/initSsr" module will hydrate ALL the custom elements on the page
|
|
20
|
+
*/
|
|
21
|
+
export default function lwcSsrViewTranformer(options: SsrPluginOptions, { moduleBundler }: ProviderContext): ViewTransformPlugin;
|
|
6
22
|
export {};
|
|
7
23
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,24 +1,50 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { kebabCaseToModuleSpecifer } from '@lwrjs/shared-utils';
|
|
2
2
|
import { LWC_SSR_PREFIX } from '../identity.js';
|
|
3
3
|
import { ssrElement } from './ssr-element.js';
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* This is a view transformer run by the view registry during linking of a page document/route (configured in lwr.config.json[routes]).
|
|
6
|
+
* If the "experimentalSSR" bootstrap flag is on for the route, it will server-side render (SSR) each custom element found in the page HTML.
|
|
7
|
+
*
|
|
8
|
+
* SSR Flow:
|
|
9
|
+
* 1. There is a request to generate a view (ie: page document) via the UI middleware or static site generation
|
|
10
|
+
* 2. During view generation, the view registry runs all the registered view transformers (including this one)
|
|
11
|
+
* 3. This view transformer links the SSRed string for EVERY custom element (ie: root component) found in the page document:
|
|
12
|
+
* a) It requests a module which SSRs a given custom element, generated by "lwc-ssr/moduleProvider"
|
|
13
|
+
* b) A bundle is created for the generated SSR module (see "./ssr-element")
|
|
14
|
+
* c) The bundle code is run inside a worker (see "./ssr-element"), with properties stored in "globalThis.ssrProps"
|
|
15
|
+
* d) The generated SSR module (running the worker) passes the SSRed code string back to the main thread (ie: "globalThis.ssrWorkerResult")
|
|
16
|
+
* e) The SSRed string is used to overwrite/link each custom element (eg: "<c-app></c-app>") in the document (see "stringBuild.overwrite")
|
|
17
|
+
* 4. The view/page document now contains SSRed components, which will be sent to the client
|
|
18
|
+
* 5. During bootstrap on the client, the "lwr/initSsr" module will hydrate ALL the custom elements on the page
|
|
19
|
+
*/
|
|
20
|
+
export default function lwcSsrViewTranformer(options, { moduleBundler }) {
|
|
6
21
|
return {
|
|
7
22
|
name: 'ssr-lwc-transformer',
|
|
8
|
-
async link(stringBuilder, viewContext,
|
|
23
|
+
async link(stringBuilder, viewContext, { customElements }) {
|
|
24
|
+
// SSR currently does not support locker because the module constructor returns undefined
|
|
25
|
+
// and the call to renderComponent would fail
|
|
26
|
+
if (process.env.LOCKER === 'true') {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
9
29
|
if (viewContext.view.bootstrap?.experimentalSSR) {
|
|
10
|
-
const
|
|
11
|
-
for (const
|
|
12
|
-
if (
|
|
13
|
-
const { startOffset, endOffset } =
|
|
14
|
-
const moduleSpecifier =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
const ssrModules = [];
|
|
31
|
+
for (const { tagName, location, props = {} } of customElements) {
|
|
32
|
+
if (location) {
|
|
33
|
+
const { startOffset, endOffset } = location;
|
|
34
|
+
const moduleSpecifier = kebabCaseToModuleSpecifer(tagName);
|
|
35
|
+
ssrModules.push({
|
|
36
|
+
startOffset,
|
|
37
|
+
endOffset,
|
|
38
|
+
props,
|
|
39
|
+
specifier: `${LWC_SSR_PREFIX}${moduleSpecifier}`,
|
|
40
|
+
});
|
|
20
41
|
}
|
|
21
42
|
}
|
|
43
|
+
return Promise.all(ssrModules.map(({ specifier, props, startOffset, endOffset }) => {
|
|
44
|
+
return ssrElement({ specifier, props }, moduleBundler, viewContext.runtimeEnvironment).then((ssrResult) => {
|
|
45
|
+
stringBuilder.overwrite(startOffset, endOffset, ssrResult);
|
|
46
|
+
});
|
|
47
|
+
})).then(() => undefined); // we need to ultimately return "void" here, there is nothing to return
|
|
22
48
|
}
|
|
23
49
|
},
|
|
24
50
|
};
|
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import type { ModuleBundler, RuntimeEnvironment } from '@lwrjs/types';
|
|
2
|
+
/**
|
|
3
|
+
* Create a bundle for the given SSR module and run the code in a worker.
|
|
4
|
+
* @param moduleInfo - specifier: The ID of the module, generated by "lwc-ssr/moduleProvider", which SSRs a component
|
|
5
|
+
* props: A map of the key:value property pairs parsed from the custom element attributes (ie: all string values)
|
|
6
|
+
* @param moduleBundler
|
|
7
|
+
* @param runtimeEnvironment
|
|
8
|
+
* @returns a promise to the SSRed code string
|
|
9
|
+
*/
|
|
10
|
+
export declare function ssrElement<RuntimeEnv extends RuntimeEnvironment>({ specifier, props }: {
|
|
11
|
+
specifier: string;
|
|
12
|
+
props: {
|
|
13
|
+
[name: string]: string;
|
|
14
|
+
};
|
|
15
|
+
}, moduleBundler: ModuleBundler, runtimeEnvironment: RuntimeEnv): Promise<string>;
|
|
3
16
|
//# sourceMappingURL=ssr-element.d.ts.map
|
|
@@ -1,34 +1,59 @@
|
|
|
1
1
|
import { Worker } from 'worker_threads';
|
|
2
|
+
import getCode from './amd-utils.js';
|
|
2
3
|
const bundleConfigOverrides = {
|
|
3
4
|
exclude: [],
|
|
4
5
|
alias: {
|
|
5
|
-
lwc: '@lwc/engine-server',
|
|
6
|
+
lwc: '@lwc/engine-server', // override the default "@lwc/engine-dom" package
|
|
6
7
|
},
|
|
7
8
|
};
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const WORKER_CONTEXT_PARENTPORT = `const { parentPort } = require('worker_threads');`;
|
|
10
|
+
const WORKER_CONTEXT_POSTBACK = `globalThis.postback = (result) => parentPort.postMessage(result);`;
|
|
11
|
+
/**
|
|
12
|
+
* Run the SSR process in a worker, and return the results to the main thread.
|
|
13
|
+
* The "globalThis.postback" is a function to allow SSR module to post results back to the parentPort, and the Promise in the main thread resolves.
|
|
14
|
+
* The properties are stored in "globalThis.ssrProps" and used in the SSR module.
|
|
15
|
+
* @param codes - Code strings which SSR a root component
|
|
16
|
+
* @returns a promise to the SSRed code string, or an error message
|
|
17
|
+
*/
|
|
18
|
+
function runCodeOnWorker(codes) {
|
|
19
|
+
const workerCode = [WORKER_CONTEXT_PARENTPORT, WORKER_CONTEXT_POSTBACK, ...codes].join('\n');
|
|
20
|
+
return new Promise((resolve) => {
|
|
13
21
|
const worker = new Worker(workerCode, { eval: true });
|
|
14
22
|
worker.on('message', resolve);
|
|
15
|
-
worker.on('error',
|
|
23
|
+
worker.on('error', ({ message }) => resolve({ error: `SSR worker exited with error: ${message}` }));
|
|
16
24
|
worker.on('exit', (code) => {
|
|
17
25
|
if (code !== 0) {
|
|
18
|
-
|
|
26
|
+
resolve({ error: `SSR worker stopped with exit code: ${code}` });
|
|
19
27
|
}
|
|
20
28
|
});
|
|
21
29
|
});
|
|
22
30
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Create a bundle for the given SSR module and run the code in a worker.
|
|
33
|
+
* @param moduleInfo - specifier: The ID of the module, generated by "lwc-ssr/moduleProvider", which SSRs a component
|
|
34
|
+
* props: A map of the key:value property pairs parsed from the custom element attributes (ie: all string values)
|
|
35
|
+
* @param moduleBundler
|
|
36
|
+
* @param runtimeEnvironment
|
|
37
|
+
* @returns a promise to the SSRed code string
|
|
38
|
+
*/
|
|
39
|
+
export async function ssrElement({ specifier, props }, moduleBundler, runtimeEnvironment) {
|
|
40
|
+
// Ensure the bundle flag is always off,
|
|
41
|
+
// otherwise TOO much gets bundled in the module registry
|
|
42
|
+
// in ESM, resulting lwc clashes/duplication
|
|
43
|
+
runtimeEnvironment.bundle = false;
|
|
44
|
+
const { bundleRecord, code, specifier: bundleSpecifier, version, } = await moduleBundler.getModuleBundle({ specifier }, runtimeEnvironment, undefined, bundleConfigOverrides);
|
|
45
|
+
const { error, result } = runtimeEnvironment.format === 'amd'
|
|
46
|
+
? await runCodeOnWorker([
|
|
47
|
+
...(await getCode(runtimeEnvironment, version.replace(/\./g, '_'), bundleRecord.includedModules.find((m) => m.startsWith('lwc/v')), bundleSpecifier)),
|
|
48
|
+
`globalThis.ssrProps = ${JSON.stringify(props)};`,
|
|
49
|
+
code,
|
|
50
|
+
])
|
|
51
|
+
: await runCodeOnWorker([`globalThis.ssrProps = ${JSON.stringify(props)};`, code]);
|
|
52
|
+
if (error) {
|
|
53
|
+
throw new Error(error);
|
|
29
54
|
}
|
|
30
55
|
else {
|
|
31
|
-
return result
|
|
56
|
+
return result;
|
|
32
57
|
}
|
|
33
58
|
}
|
|
34
59
|
//# sourceMappingURL=ssr-element.js.map
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.7.0-alpha.
|
|
7
|
+
"version": "0.7.0-alpha.9",
|
|
8
8
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
"build/**/*.d.ts"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@lwrjs/diagnostics": "0.7.0-alpha.
|
|
37
|
-
"@lwrjs/shared-utils": "0.7.0-alpha.
|
|
36
|
+
"@lwrjs/diagnostics": "0.7.0-alpha.9",
|
|
37
|
+
"@lwrjs/shared-utils": "0.7.0-alpha.9"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@lwrjs/types": "0.7.0-alpha.
|
|
40
|
+
"@lwrjs/types": "0.7.0-alpha.9"
|
|
41
41
|
},
|
|
42
42
|
"engines": {
|
|
43
43
|
"node": ">=14.15.4 <17"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "522665298cc74f1898da6ed018fa72504bd72645"
|
|
46
46
|
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, {get: all[name], enumerable: true});
|
|
11
|
-
};
|
|
12
|
-
var __exportStar = (target, module2, desc) => {
|
|
13
|
-
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(module2))
|
|
15
|
-
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
16
|
-
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
|
|
17
|
-
}
|
|
18
|
-
return target;
|
|
19
|
-
};
|
|
20
|
-
var __toModule = (module2) => {
|
|
21
|
-
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// packages/@lwrjs/lwc-ssr/src/moduleProvider/utils.ts
|
|
25
|
-
__markAsModule(exports);
|
|
26
|
-
__export(exports, {
|
|
27
|
-
createSsrBootstrapModule: () => createSsrBootstrapModule
|
|
28
|
-
});
|
|
29
|
-
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
30
|
-
function createSsrBootstrapModule(rootComponent) {
|
|
31
|
-
return [
|
|
32
|
-
"/* This module is meant to be used in a Server context */",
|
|
33
|
-
'import { renderComponent } from "@lwc/engine-server"',
|
|
34
|
-
`import Ctor from "${rootComponent}"`,
|
|
35
|
-
"try {",
|
|
36
|
-
` const result = renderComponent("${(0, import_shared_utils.moduleSpecifierToKebabCase)(rootComponent)}", Ctor, {});`,
|
|
37
|
-
" globalThis.workerResult = { result };",
|
|
38
|
-
"} catch(err) {",
|
|
39
|
-
" globalThis.workerResult = { error: err.toString() };",
|
|
40
|
-
`}`
|
|
41
|
-
].join("\n");
|
|
42
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { moduleSpecifierToKebabCase } from '@lwrjs/shared-utils';
|
|
2
|
-
/**
|
|
3
|
-
* Create the virtual source for the application bootstrap module
|
|
4
|
-
* @returns the generated source
|
|
5
|
-
*/
|
|
6
|
-
export function createSsrBootstrapModule(rootComponent) {
|
|
7
|
-
return [
|
|
8
|
-
'/* This module is meant to be used in a Server context */',
|
|
9
|
-
'import { renderComponent } from "@lwc/engine-server"',
|
|
10
|
-
`import Ctor from "${rootComponent}"`,
|
|
11
|
-
'try {',
|
|
12
|
-
` const result = renderComponent("${moduleSpecifierToKebabCase(rootComponent)}", Ctor, {});`,
|
|
13
|
-
' globalThis.workerResult = { result };',
|
|
14
|
-
'} catch(err) {',
|
|
15
|
-
' globalThis.workerResult = { error: err.toString() };',
|
|
16
|
-
`}`,
|
|
17
|
-
].join('\n');
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=utils.js.map
|