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

feat(compiler): return cssScopeTokens #3556

Merged
merged 1 commit into from
Jun 6, 2023
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
1 change: 1 addition & 0 deletions packages/@lwc/compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const { code } = transformSync(source, filename, options);
- `code` (string) - the compiled source code.
- `map` (object) - the generated source map.
- `warnings` (array, optional) - the array of diagnostic warnings, if any.
- `cssScopeTokens` (array, optional) - String tokens used for style scoping in synthetic shadow DOM and `*.scoped.css` (as either attributes or classes), if any.

### `transform` (deprecated)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ describe('transformSync', () => {
expect(code).not.toMatch('renderer: renderer');
});

it('should return scope tokens', () => {
const template = `
<template>
<div>Hello</div>
</template>
`;
const { cssScopeTokens } = transformSync(template, 'foo.html', TRANSFORMATION_OPTIONS);

expect(cssScopeTokens).toEqual(['x-foo_foo', 'x-foo_foo-host']);
});

describe('dynamic components', () => {
it('should allow dynamic components when enableDynamicComponents is set to true', () => {
const template = `
Expand Down
22 changes: 13 additions & 9 deletions packages/@lwc/compiler/src/transformers/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export default function templateTransform(
enableDynamicComponents,
experimentalDynamicDirective: deprecatedDynamicDirective,
instrumentation,
namespace,
name,
} = options;
const experimentalDynamicDirective =
deprecatedDynamicDirective ?? Boolean(experimentalDynamicComponent);
Expand Down Expand Up @@ -68,12 +70,20 @@ export default function templateTransform(
// thrown above. As for "Log" and "Fatal", they are currently unused.
const warnings = result.warnings.filter((_) => _.level === DiagnosticLevel.Warning);

const scopeToken = escapeScopeToken(
`${namespace}-${name}_${path.basename(filename, path.extname(filename))}`
);

// Rollup only cares about the mappings property on the map. Since producing a source map for
// the template doesn't make sense, the transform returns an empty mappings.
return {
code: serialize(result.code, filename, options),
code: serialize(result.code, filename, scopeToken),
map: { mappings: '' },
warnings,
cssScopeTokens: [
scopeToken,
`${scopeToken}-host`, // implicit scope token created by `makeHostToken()` in `@lwc/engine-core`
],
};
}

Expand All @@ -83,16 +93,10 @@ function escapeScopeToken(input: string) {
return input.replace(/@/g, '___at___').replace(/#/g, '___hash___');
}

function serialize(
code: string,
filename: string,
{ namespace, name }: NormalizedTransformOptions
): string {
function serialize(code: string, filename: string, scopeToken: string): string {
const cssRelPath = `./${path.basename(filename, path.extname(filename))}.css`;
const scopedCssRelPath = `./${path.basename(filename, path.extname(filename))}.scoped.css`;
const scopeToken = escapeScopeToken(
`${namespace}-${name}_${path.basename(filename, path.extname(filename))}`
);

let buffer = '';
buffer += `import { freezeTemplate } from "lwc";\n\n`;
buffer += `import _implicitStylesheets from "${cssRelPath}";\n\n`;
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/compiler/src/transformers/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface TransformResult {
code: string;
map: unknown;
warnings?: CompilerDiagnostic[];
cssScopeTokens?: string[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this optional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only the template compiler returns it, not the style compiler or babel compiler.

}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/engine-core/src/framework/stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type StylesheetFactory = (
export type TemplateStylesheetFactories = Array<StylesheetFactory | TemplateStylesheetFactories>;

function makeHostToken(token: string) {
// Note: if this ever changes, update the `cssScopeTokens` returned by `@lwc/compiler`
return `${token}-host`;
}

Expand Down