Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr-compiler): implement scoped styles and scope tokens #4567

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ const bGenerateMarkup = esTemplate<ExportNamedDeclaration>`
instance.__internal__setState(props, __REFLECTED_PROPS__, attrs);
instance.isConnected = true;
instance.connectedCallback?.();
const tmplFn = ${isIdentOrRenderCall} ?? __fallbackTmpl;
yield \`<\${tagName}\`;
yield tmplFn.stylesheetScopeTokenHostClass;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope tokens are associated with templates, not with components. This is the main mistake that the previous code was making. Here I am grabbing the scope token from the tmplFn, and I'm also not passing it into the tmplFn below because the tmplFn should already know what its stylesheets are.

(This is not true for static stylesheets but we'll cross that bridge when we get there.)

yield *__renderAttrs(attrs)
yield '>';
const tmplFn = ${isIdentOrRenderCall} ?? __fallbackTmpl;
yield* tmplFn(props, attrs, slotted, ${is.identifier}, instance, defaultStylesheets);
yield* tmplFn(props, attrs, slotted, ${is.identifier}, instance);
yield \`</\${tagName}>\`;
}
`;
Expand Down
11 changes: 9 additions & 2 deletions packages/@lwc/ssr-compiler/src/compile-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AriaPropNameToAttrNameMap } from '@lwc/shared';

import { replaceLwcImport } from './lwc-import';
import { catalogTmplImport } from './catalog-tmpls';
import { addStylesheetImports, catalogStaticStylesheets, catalogStyleImport } from './stylesheets';
import { catalogStaticStylesheets, catalogStyleImport } from './stylesheets';
import { addGenerateMarkupExport } from './generate-markup';

import type { Identifier as EsIdentifier, Program as EsProgram } from 'estree';
Expand Down Expand Up @@ -145,8 +145,15 @@ export default function compileJS(src: string, filename: string) {
};
}

if (state.cssExplicitImports || state.staticStylesheetIds) {
throw new Error(
`Unimplemented static stylesheets, but found:\n${[...state.cssExplicitImports!].join(
' \n'
)}`
);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved this error message from elsewhere to here.

addGenerateMarkupExport(ast, state, filename);
addStylesheetImports(ast, state, filename);

return {
code: generate(ast, {}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { is } from 'estree-toolkit';
import { generateScopeTokens } from '@lwc/template-compiler';
import { builders as b } from 'estree-toolkit/dist/builders';
import { esTemplate } from '../estemplate';
import type { BlockStatement, ExportNamedDeclaration, Program, VariableDeclaration } from 'estree';

function generateStylesheetScopeToken(filename: string) {
// FIXME: we should be getting the namespace/name from the config options,
// since these actually come from the component filename, not the template filename.
const split = filename.split('/');
const namespace = split.at(-3)!;
const baseName = split.at(-1)!;

const componentName = baseName.replace(/\.[^.]+$/, '');
const {
// FIXME: handle legacy scope token for older API versions
scopeToken,
} = generateScopeTokens(filename, namespace, componentName);

return scopeToken;
}

const bStylesheetTokenDeclaration = esTemplate<VariableDeclaration>`
const stylesheetScopeToken = '${is.literal}';
`;

const bAdditionalDeclarations = [
esTemplate<VariableDeclaration>`
const hasScopedStylesheets = defaultScopedStylesheets && defaultScopedStylesheets.length > 0;
`,
esTemplate<ExportNamedDeclaration>`
const stylesheetScopeTokenClass = hasScopedStylesheets ? \` class="\${stylesheetScopeToken}"\` : '';
`,
esTemplate<ExportNamedDeclaration>`
const stylesheetScopeTokenHostClass = hasScopedStylesheets ? \` class="\${stylesheetScopeToken}-host"\` : '';
`,
esTemplate<ExportNamedDeclaration>`
const stylesheetScopeTokenClassPrefix = hasScopedStylesheets ? (stylesheetScopeToken + ' ') : '';
`,
];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is probably a better way to do this, but I like the esTemplate utility and did not find a more succinct way to represent this.


// Scope tokens are associated with a given template. This is assigned here so that it can be used in `generateMarkup`.
const tmplAssignmentBlock = esTemplate<BlockStatement>`
${is.identifier}.stylesheetScopeTokenHostClass = stylesheetScopeTokenHostClass;
`;

export function addScopeTokenDeclarations(program: Program, filename: string) {
const scopeToken = generateStylesheetScopeToken(filename);

program.body.unshift(
bStylesheetTokenDeclaration(b.literal(scopeToken)),
...bAdditionalDeclarations.map((declaration) => declaration())
);

program.body.push(tmplAssignmentBlock(b.identifier('tmpl')));
}
29 changes: 13 additions & 16 deletions packages/@lwc/ssr-compiler/src/compile-js/stylesheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { builders as b, is } from 'estree-toolkit';
import { esTemplate } from '../estemplate';

import type { NodePath } from 'estree-toolkit';
import type { Program, ImportDeclaration } from 'estree';
import type { ImportDeclaration } from 'estree';
import type { ComponentMetaState } from './types';

const bDefaultStyleImport = esTemplate<ImportDeclaration>`
import defaultStylesheets from '${is.literal}';
`;

const bDefaultScopedStyleImport = esTemplate<ImportDeclaration>`
import defaultScopedStylesheets from '${is.literal}';
`;

export function catalogStyleImport(path: NodePath<ImportDeclaration>, state: ComponentMetaState) {
const specifier = path.node!.specifiers[0];

Expand All @@ -32,26 +36,19 @@ export function catalogStyleImport(path: NodePath<ImportDeclaration>, state: Com
state.cssExplicitImports.set(specifier.local.name, path.node!.source.value);
}

const componentNamePattern = /(?<componentName>[^/]+)\.[tj]s$/;

/**
* This adds implicit style imports to the compiled component artifact.
*/
export function addStylesheetImports(ast: Program, state: ComponentMetaState, filepath: string) {
const componentName = componentNamePattern.exec(filepath)?.groups?.componentName;
if (!componentName) {
throw new Error(`Could not determine component name from file path: ${filepath}`);
}

if (state.cssExplicitImports || state.staticStylesheetIds) {
throw new Error(
`Unimplemented static stylesheets, but found:\n${[...state.cssExplicitImports!].join(
' \n'
)}`
);
export function getStylesheetImports(filepath: string) {
const moduleName = /(?<moduleName>[^/]+)\.html$/.exec(filepath)?.groups?.moduleName;
if (!moduleName) {
throw new Error(`Could not determine module name from file path: ${filepath}`);
}

ast.body.unshift(bDefaultStyleImport(b.literal(`./${componentName}.css`)));
return [
bDefaultStyleImport(b.literal(`./${moduleName}.css`)),
bDefaultScopedStyleImport(b.literal(`./${moduleName}.scoped.css?scoped=true`)),
];
}

export function catalogStaticStylesheets(ids: string[], state: ComponentMetaState) {
Expand Down
44 changes: 36 additions & 8 deletions packages/@lwc/ssr-compiler/src/compile-template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Property as IrProperty,
} from '@lwc/template-compiler';
import type {
BinaryExpression,
BlockStatement as EsBlockStatement,
Expression as EsExpression,
Statement as EsStatement,
Expand All @@ -28,31 +29,47 @@ import type { Transformer } from './types';
const bYield = (expr: EsExpression) => b.expressionStatement(b.yieldExpression(expr));
const bConditionalLiveYield = esTemplateWithYield<EsBlockStatement>`
{
const prefix = (${/* isClass */ is.literal} && stylesheetScopeTokenClassPrefix) || '';
const attrOrPropValue = ${is.expression};
const valueType = typeof attrOrPropValue;
if (attrOrPropValue && (valueType === 'string' || valueType === 'boolean')) {
yield ' ' + ${is.literal};
if (valueType === 'string') {
yield '="' + htmlEscape(attrOrPropValue, true) + '"';
yield \`="\${prefix}\${htmlEscape(attrOrPropValue, true)}"\`;
}
}
}
`;

function yieldAttrOrPropLiteralValue(name: string, valueNode: IrLiteral): EsStatement[] {
const bStringLiteralYield = esTemplateWithYield<EsBlockStatement>`
{
const prefix = (${/* isClass */ is.literal} && stylesheetScopeTokenClassPrefix) || '';
yield ' ' + ${is.literal} + '="' + prefix + "${is.literal}" + '"'
}
`;

function yieldAttrOrPropLiteralValue(
name: string,
valueNode: IrLiteral,
isClass: boolean
): EsStatement[] {
const { value, type } = valueNode;
if (typeof value === 'string') {
const yieldedValue = name === 'style' ? cleanStyleAttrVal(value) : value;
return [bYield(b.literal(` ${name}="${yieldedValue}"`))];
return [bStringLiteralYield(b.literal(isClass), b.literal(name), b.literal(yieldedValue))];
} else if (typeof value === 'boolean') {
return [bYield(b.literal(` ${name}`))];
}
throw new Error(`Unknown attr/prop literal: ${type}`);
}

function yieldAttrOrPropLiveValue(name: string, value: IrExpression): EsStatement[] {
function yieldAttrOrPropLiveValue(
name: string,
value: IrExpression | BinaryExpression,
isClass: boolean
): EsStatement[] {
const instanceMemberRef = b.memberExpression(b.identifier('instance'), value as EsExpression);
return [bConditionalLiveYield(instanceMemberRef, b.literal(name))];
return [bConditionalLiveYield(b.literal(isClass), instanceMemberRef, b.literal(name))];
}

function reorderAttributes(
Expand Down Expand Up @@ -88,12 +105,21 @@ export const Element: Transformer<IrElement> = function Element(node, cxt): EsSt
node.properties
);

let hasClassAttribute = false;
const yieldAttrsAndProps = attrsAndProps.flatMap((attr) => {
const { name, value, type } = attr;

// For classes, these may need to be prefixed with the scope token
const isClass = type === 'Attribute' && name === 'class';
if (isClass) {
hasClassAttribute = true;
}

cxt.hoist(bImportHtmlEscape(), importHtmlEscapeKey);
if (attr.value.type === 'Literal') {
return yieldAttrOrPropLiteralValue(attr.name, attr.value);
if (value.type === 'Literal') {
return yieldAttrOrPropLiteralValue(name, value, isClass);
} else {
return yieldAttrOrPropLiveValue(attr.name, attr.value);
return yieldAttrOrPropLiveValue(name, value, isClass);
}
});

Expand All @@ -103,6 +129,8 @@ export const Element: Transformer<IrElement> = function Element(node, cxt): EsSt

return [
bYield(b.literal(`<${node.name}`)),
// If we haven't already prefixed the scope token to an existing class, add an explicit class here
...(hasClassAttribute ? [] : [bYield(b.identifier('stylesheetScopeTokenClass'))]),
...yieldAttrsAndProps,
bYield(b.literal(`>`)),
...irChildrenToEs(node.children, cxt),
Expand Down
38 changes: 24 additions & 14 deletions packages/@lwc/ssr-compiler/src/compile-template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { generate } from 'astring';
import { is, builders as b } from 'estree-toolkit';
import { parse } from '@lwc/template-compiler';
import { esTemplate } from '../estemplate';
import { templateIrToEsTree } from './ir-to-es';
import { getStylesheetImports } from '../compile-js/stylesheets';
import { addScopeTokenDeclarations } from '../compile-js/stylesheet-scope-token';
import { optimizeAdjacentYieldStmts } from './shared';

import { templateIrToEsTree } from './ir-to-es';
import type {
Node as EsNode,
Statement as EsStatement,
Expand All @@ -25,30 +26,34 @@ const bExportTemplate = esTemplate<
EsExportDefaultDeclaration,
[EsLiteral, EsStatement[], EsLiteral]
>`
export default async function* tmpl(props, attrs, slotted, Cmp, instance, stylesheets) {
export default async function* tmpl(props, attrs, slotted, Cmp, instance) {
if (!${isBool} && Cmp.renderMode !== 'light') {
yield \`<template shadowrootmode="open"\${Cmp.delegatesFocus ? ' shadowrootdelegatesfocus' : ''}>\`
}

for (const stylesheet of stylesheets ?? []) {
// TODO
const token = null;
const useActualHostSelector = true;
const useNativeDirPseudoclass = null;
yield '<style type="text/css">';
yield stylesheet(token, useActualHostSelector, useNativeDirPseudoclass);
yield '</style>';

if (defaultStylesheets || defaultScopedStylesheets) {
// Flatten all stylesheets infinitely and concatenate
const stylesheets = [defaultStylesheets, defaultScopedStylesheets].filter(Boolean).flat(Infinity);

for (const stylesheet of stylesheets) {
const token = stylesheet.$scoped$ ? stylesheetScopeToken : undefined;
const useActualHostSelector = !stylesheet.$scoped$ || Cmp.renderMode !== 'light';
const useNativeDirPseudoclass = true;
yield '<style' + stylesheetScopeTokenClass + ' type="text/css">';
yield stylesheet(token, useActualHostSelector, useNativeDirPseudoclass);
yield '</style>';
}
}

${is.statement};

if (!${isBool} && Cmp.renderMode !== 'light') {
yield '</template>'
yield '</template>';
}
}
`;

export default function compileTemplate(src: string, _filename: string) {
export default function compileTemplate(src: string, filename: string) {
const { root, warnings } = parse(src);
if (!root || warnings.length) {
for (const warning of warnings) {
Expand Down Expand Up @@ -79,6 +84,11 @@ export default function compileTemplate(src: string, _filename: string) {
];
const program = b.program(moduleBody, 'module');

addScopeTokenDeclarations(program, filename);

const stylesheetImports = getStylesheetImports(filename);
program.body.unshift(...stylesheetImports);

return {
code: generate(program, {}),
};
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/template-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { CustomRendererConfig, CustomRendererElementConfig } from './shared/rend
export { Config } from './config';
export { toPropertyName } from './shared/utils';
export { kebabcaseToCamelcase } from './shared/naming';
export { generateScopeTokens } from './scopeTokens';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are now exposing this from the template compiler... may the gods forgive us.


/**
* Parses HTML markup into an AST
Expand Down
11 changes: 9 additions & 2 deletions packages/@lwc/template-compiler/src/scopeTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ export type scopeTokens = {
cssScopeTokens: string[];
};

/**
* Generate the scope tokens for a given component. Note that this API is NOT stable and should be
* considered internal to the LWC framework.
* @param filename - full filename, e.g. `path/to/x/foo/foo.js`
* @param namespace - namespace, e.g. 'x' for `x/foo/foo.js`
* @param componentName - component name, e.g. 'foo' for `x/foo/foo.js`
*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a big warning here that this is not stable. I can't imagine why someone would use it, but you never know... 🤷

export function generateScopeTokens(
filename: string,
namespace: string | undefined,
name: string | undefined
componentName: string | undefined
): scopeTokens {
const uniqueToken = `${namespace}-${name}_${path.basename(filename, path.extname(filename))}`;
const uniqueToken = `${namespace}-${componentName}_${path.basename(filename, path.extname(filename))}`;

// This scope token is all lowercase so that it works correctly in case-sensitive namespaces (e.g. SVG).
// It is deliberately designed to discourage people from relying on it by appearing somewhat random.
Expand Down
Loading