Skip to content

Commit

Permalink
feat: experimental SSR compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
divmain committed Apr 25, 2024
1 parent 62f01ca commit 4f4cc72
Show file tree
Hide file tree
Showing 45 changed files with 3,448 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/@lwc/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@lwc/babel-plugin-component": "6.5.3",
"@lwc/errors": "6.5.3",
"@lwc/shared": "6.5.3",
"@lwc/ssr-compiler": "6.5.3",
"@lwc/style-compiler": "6.5.3",
"@lwc/template-compiler": "6.5.3"
}
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { transform, transformSync } from './transformers/transformer';

export { TransformResult } from './transformers/transformer';
export {
NormalizedTransformOptions,
TransformOptions,
StylesheetConfig,
CustomPropertiesResolution,
Expand Down
2 changes: 2 additions & 0 deletions packages/@lwc/compiler/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export interface TransformOptions {
instrumentation?: InstrumentationObject;
/** API version to associate with the compiled module. Values correspond to Salesforce platform releases. */
apiVersion?: number;
targetSSR?: boolean;
}

type RequiredTransformOptions = Omit<
Expand Down Expand Up @@ -243,5 +244,6 @@ function normalizeOptions(options: TransformOptions): NormalizedTransformOptions
outputConfig,
experimentalDynamicComponent,
apiVersion,
targetSSR: !!options.targetSSR,
};
}
5 changes: 3 additions & 2 deletions packages/@lwc/compiler/src/transformers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
invariant,
CompilerDiagnostic,
} from '@lwc/errors';
import { compileComponentForSSR, compileTemplateForSSR } from '@lwc/ssr-compiler';

import { NormalizedTransformOptions, TransformOptions, validateTransformOptions } from '../options';
import styleTransform from './style';
Expand Down Expand Up @@ -114,7 +115,7 @@ function transformFile(

switch (path.extname(filename)) {
case '.html':
transformer = templateTransformer;
transformer = options.targetSSR ? compileTemplateForSSR : templateTransformer;
break;

case '.css':
Expand All @@ -125,7 +126,7 @@ function transformFile(
case '.jsx':
case '.ts':
case '.js':
transformer = scriptTransformer;
transformer = options.targetSSR ? compileComponentForSSR : scriptTransformer;
break;

default:
Expand Down
1,231 changes: 1,231 additions & 0 deletions packages/@lwc/engine-server/src/__tests__/fixtures/deeply-nested/expected.html

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-component';
export { default } from 'x/component';
export * from 'x/component';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div>
<template lwc:if={isPositive}>
<template lwc:if={isDivisibleByThree}>
<template for:each={someChildren} for:item="child">
<span key={child}>
<x-component label={child} remaining={minusOne}></x-component>
</span>
</template>
</template>
<template lwc:elseif={isDivisibleByTwo}>
<div>
<span>two</span>
<x-component remaining={minusOne}></x-component>
</div>
</template>
<template lwc:else>
<x-component remaining={minusOne}></x-component>
</template>
</template>
<template lwc:else>
terminal node
</template>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { api, LightningElement } from 'lwc';

export default class Component extends LightningElement {
@api remaining = 9;
@api label = 'default';

get someChildren() {
return ['a', 'b', 'c'];
}

get isPositive() {
return this.remaining > 0;
}

get isDivisibleByThree() {
return (this.remaining % 3) === 0
}

get isDivisibleByTwo() {
return (this.remaining % 3) === 0
}

get minusOne() {
return this.remaining - 1;
}
}
4 changes: 4 additions & 0 deletions packages/@lwc/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { APIVersion, getAPIVersionFromNumber } from '@lwc/shared';
import type { CompilerDiagnostic } from '@lwc/errors';

export interface RollupLwcOptions {
/** A boolean indicating whether to compile for SSR runtime target. */
targetSSR?: boolean;
/** A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should transform on. By default all files are targeted. */
include?: FilterPattern;
/** A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should not transform. By default no files are ignored. */
Expand Down Expand Up @@ -154,6 +156,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {

let { rootDir, modules = [] } = pluginOptions;
const {
targetSSR,
stylesheetConfig,
sourcemap = false,
preserveHtmlComments,
Expand Down Expand Up @@ -336,6 +339,7 @@ export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
...('enableStaticContentOptimization' in pluginOptions && {
enableStaticContentOptimization: pluginOptions.enableStaticContentOptimization,
}),
targetSSR,
});

if (warnings) {
Expand Down
12 changes: 12 additions & 0 deletions packages/@lwc/ssr-compiler/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
const BASE_CONFIG = require('../../../scripts/jest/base.config');

module.exports = {
...BASE_CONFIG,
displayName: 'lwc-ssr-compiler',
};
59 changes: 59 additions & 0 deletions packages/@lwc/ssr-compiler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"//": [
"THIS FILE IS AUTOGENERATED. If you modify it, it will be rewritten by check-and-rewrite-package-json.js",
"You can safely modify dependencies, devDependencies, keywords, etc., but other props will be overwritten."
],
"name": "@lwc/ssr-compiler",
"version": "6.5.3",
"description": "Compile component for use during server-side rendering",
"keywords": [
"lwc"
],
"homepage": "https://lwc.dev",
"repository": {
"type": "git",
"url": "https://github.com/salesforce/lwc.git",
"directory": "packages/@lwc/ssr-compiler"
},
"bugs": {
"url": "https://github.com/salesforce/lwc/issues"
},
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "rollup --config ../../../scripts/rollup/rollup.config.js",
"dev": "rollup --config ../../../scripts/rollup/rollup.config.js --watch --no-watch.clearScreen",
"test-manual": "jest ./src/**/*.spec.ts"
},
"nx": {
"targets": {
"build": {
"outputs": [
"{projectRoot}/dist"
]
}
}
},
"dependencies": {
"@lwc/metadata": "6.5.3-0",
"@lwc/sfdc-compiler-utils": "6.5.3-0",
"@lwc/style-compiler": "6.5.3",
"@lwc/template-compiler": "6.5.3",
"acorn": "~8.10.0",
"astring": "^1.8.6",
"estree-toolkit": "^1.7.3",
"immer": "^10.0.3",
"meriyah": "^4.3.8"
},
"devDependencies": {
"@types/estree": "^1.0.5"
}
}
Loading

0 comments on commit 4f4cc72

Please sign in to comment.