-
Notifications
You must be signed in to change notification settings - Fork 393
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): add disableSyntheticShadowSupport option #3229
Changes from 1 commit
6663a93
8767b4e
ed6f983
8d12bf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ export default { | |
- `experimentalDynamicComponent` (type: `DynamicComponentConfig`, default: `null`) - The configuration to pass to `@lwc/compiler`. | ||
- `enableLwcSpread` (type: `boolean`, default: `false`) - The configuration to pass to the `@lwc/template-compiler`. | ||
- `enableScopedSlots` (type: `boolean`, default: `false`) - The configuration to pass to `@lwc/template-compiler` to enable scoped slots feature. | ||
- `disableSyntheticShadowSupport` (type: `boolean`, default: `false`) - Set to true if synthetic shadow DOM support is not needed, which can result in smaller output. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could have put this boolean on the In this PR, though, I've only implemented the optimization for the style compiler. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:host(.foo) :dir(rtl) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"disableSyntheticShadowSupport": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function stylesheet() { | ||
var token; | ||
var useActualHostSelector = true; | ||
var useNativeDirPseudoclass = true; | ||
var shadowSelector = token ? ("[" + token + "]") : ""; | ||
var hostSelector = token ? ("[" + token + "-host]") : ""; | ||
return ((useActualHostSelector ? (useNativeDirPseudoclass ? '' : '[dir="rtl"]') + " :host(.foo) " + (useNativeDirPseudoclass ? ':dir(rtl)' : '') + " {}" : (useNativeDirPseudoclass ? '' : '[dir="rtl"]') + " " + hostSelector + ".foo " + (useNativeDirPseudoclass ? ':dir(rtl)' : '') + " {}")); | ||
/*LWC compiler vX.X.X*/ | ||
} | ||
export default [stylesheet]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ const HOST_SELECTOR_IDENTIFIER = 'hostSelector'; | |
const SHADOW_SELECTOR_IDENTIFIER = 'shadowSelector'; | ||
const USE_ACTUAL_HOST_SELECTOR = 'useActualHostSelector'; | ||
const USE_NATIVE_DIR_PSEUDOCLASS = 'useNativeDirPseudoclass'; | ||
const TOKEN = 'token'; | ||
const STYLESHEET_IDENTIFIER = 'stylesheet'; | ||
const VAR_RESOLVER_IDENTIFIER = 'varResolver'; | ||
|
||
|
@@ -50,6 +51,8 @@ export default function serialize(result: Result, config: Config): string { | |
); | ||
const useVarResolver = messages.some(isVarFunctionMessage); | ||
const importedStylesheets = messages.filter(isImportMessage).map((message) => message.id); | ||
const disableSyntheticShadow = Boolean(config.disableSyntheticShadowSupport); | ||
const scoped = Boolean(config.scoped); | ||
|
||
let buffer = ''; | ||
|
||
|
@@ -71,21 +74,33 @@ export default function serialize(result: Result, config: Config): string { | |
|
||
if (serializedStyle) { | ||
// inline function | ||
buffer += `function stylesheet(token, ${USE_ACTUAL_HOST_SELECTOR}, ${USE_NATIVE_DIR_PSEUDOCLASS}) {\n`; | ||
if (disableSyntheticShadow && !scoped) { | ||
// If synthetic shadow DOM support is disabled and this is not a scoped stylesheet, then the | ||
// function signature will always be: | ||
// stylesheet(token = undefined, useActualHostSelector = true, useNativeDirPseudoclass = true) | ||
// This means that we can just have a function that takes no arguments and returns a string, | ||
// reducing the bundle size when minified. | ||
buffer += `function ${STYLESHEET_IDENTIFIER}() {\n`; | ||
buffer += ` var ${TOKEN};\n`; // undefined | ||
buffer += ` var ${USE_ACTUAL_HOST_SELECTOR} = true;\n`; | ||
buffer += ` var ${USE_NATIVE_DIR_PSEUDOCLASS} = true;\n`; | ||
} else { | ||
buffer += `function ${STYLESHEET_IDENTIFIER}(${TOKEN}, ${USE_ACTUAL_HOST_SELECTOR}, ${USE_NATIVE_DIR_PSEUDOCLASS}) {\n`; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguably we could do something much fancier by modifying |
||
// For scoped stylesheets, we use classes, but for synthetic shadow DOM, we use attributes | ||
if (config.scoped) { | ||
buffer += ` var ${SHADOW_SELECTOR_IDENTIFIER} = token ? ("." + token) : "";\n`; | ||
buffer += ` var ${HOST_SELECTOR_IDENTIFIER} = token ? ("." + token + "-host") : "";\n`; | ||
if (scoped) { | ||
buffer += ` var ${SHADOW_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("." + ${TOKEN}) : "";\n`; | ||
buffer += ` var ${HOST_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("." + ${TOKEN} + "-host") : "";\n`; | ||
} else { | ||
buffer += ` var ${SHADOW_SELECTOR_IDENTIFIER} = token ? ("[" + token + "]") : "";\n`; | ||
buffer += ` var ${HOST_SELECTOR_IDENTIFIER} = token ? ("[" + token + "-host]") : "";\n`; | ||
buffer += ` var ${SHADOW_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("[" + ${TOKEN} + "]") : "";\n`; | ||
buffer += ` var ${HOST_SELECTOR_IDENTIFIER} = ${TOKEN} ? ("[" + ${TOKEN} + "-host]") : "";\n`; | ||
} | ||
buffer += ` return ${serializedStyle};\n`; | ||
buffer += ` /*${LWC_VERSION_COMMENT}*/\n`; | ||
buffer += `}\n`; | ||
if (config.scoped) { | ||
if (scoped) { | ||
// Mark the stylesheet as scoped so that we can distinguish it later at runtime | ||
buffer += `stylesheet.${KEY__SCOPED_CSS} = true;\n`; | ||
buffer += `${STYLESHEET_IDENTIFIER}.${KEY__SCOPED_CSS} = true;\n`; | ||
} | ||
|
||
// add import at the end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified manually that this Karma test works with the config passed in.
In the above screenshot, some of the functions still have their arguments, because they are scoped (
stylesheet.$scoped$ = true
).