-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for style injection in compiler (#302)
* feat: add style injection * test: add integration test for style injection * test: fix compiler snapshot tests
- Loading branch information
Showing
26 changed files
with
391 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/lwc-compiler/src/__tests__/fixtures/expected-prod_compat-mode.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/lwc-compiler/src/__tests__/fixtures/expected-styled-prod.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,51 @@ | ||
import * as path from 'path'; | ||
import compile from 'lwc-template-compiler'; | ||
|
||
export function getTemplateToken(filename, options) { | ||
const templateId = path.basename(filename, path.extname(filename)); | ||
return `${options.moduleNamespace}-${options.moduleName}_${templateId}`; | ||
} | ||
|
||
/** | ||
* Transforms a HTML template into module exporting a template function. | ||
* The transform also add a style import for the default stylesheet associated with | ||
* the template regardless if there is an actual style or not. | ||
*/ | ||
export default function(src, options) { | ||
const { filename, moduleName, moduleNamespace } = options; | ||
const { code: template, metadata, warnings } = compile(src, {}); | ||
function attachStyleToTemplate(src, { moduleName, moduleNamespace, filename }) { | ||
const templateFilename = path.basename(filename, path.extname(filename)); | ||
|
||
const fatalError = warnings.find(warning => warning.level === 'error'); | ||
if (fatalError) { | ||
throw new Error(fatalError.message); | ||
} | ||
// Use the component tagname and a unique style token to scope the compiled | ||
// styles to the component. | ||
const tagName = `${moduleNamespace}-${moduleName}`; | ||
const scopingToken = `${tagName}_${templateFilename}`; | ||
|
||
|
||
const token = getTemplateToken(filename, options); | ||
const cssName = path.basename(filename, path.extname(filename)) + '.css'; | ||
|
||
const code = [ | ||
`import style from './${cssName}'`, | ||
return [ | ||
`import stylesheet from './${templateFilename}.css'`, | ||
'', | ||
template, | ||
src, | ||
'', | ||
`if (style) {`, | ||
` const tagName = '${moduleNamespace}-${moduleName}';`, | ||
` const token = '${token}';`, | ||
|
||
// The compiler resolves the style import to undefined if the stylesheet | ||
// doesn't exists. | ||
`if (stylesheet) {`, | ||
|
||
// The engine picks the style token from the template during rendering to | ||
// add the token to all generated elements. | ||
` tmpl.token = '${scopingToken}';`, | ||
``, | ||
` tmpl.token = token;`, | ||
` tmpl.style = style(tagName, token);`, | ||
|
||
// Inject the component style in a new style tag the document head. | ||
` const style = document.createElement('style');`, | ||
` style.type = 'text/css';`, | ||
` style.dataset.token = '${scopingToken}'`, | ||
` style.textContent = stylesheet('${tagName}', '${scopingToken}');`, | ||
` document.head.appendChild(style);`, | ||
`}`, | ||
].join('\n'); | ||
} | ||
|
||
export default function(src, options) { | ||
// Transform HTML template to javascript | ||
const { code, metadata, warnings } = compile(src, {}); | ||
|
||
// Throw if a fatal error ocurred during the compilation | ||
const error = warnings.find(warning => warning.level === 'error'); | ||
if (error) { | ||
throw new Error(error.message); | ||
} | ||
|
||
return { code, metadata }; | ||
return { | ||
code: attachStyleToTemplate(code, options), | ||
metadata, | ||
}; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/lwc-integration/src/components/style/test-inject-style/child/child.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<template> | ||
<div id="child-div">Styles are not leaking</div> | ||
</template> |
3 changes: 3 additions & 0 deletions
3
packages/lwc-integration/src/components/style/test-inject-style/child/child.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Element } from 'engine'; | ||
|
||
export default class XChild extends Element {} |
10 changes: 10 additions & 0 deletions
10
...ages/lwc-integration/src/components/style/test-inject-style/inject-style/inject-style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
:host { | ||
display: block; | ||
width: 300px; | ||
height: 300px; | ||
background: red; | ||
} | ||
|
||
div { | ||
color: #00ff00; | ||
} |
8 changes: 8 additions & 0 deletions
8
...ges/lwc-integration/src/components/style/test-inject-style/inject-style/inject-style.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<template> | ||
<div id="parent-div">I am styled!</div> | ||
<span id="dimensions">{dimensions}</span> | ||
|
||
<hr> | ||
|
||
<x-child></x-child> | ||
</template> |
12 changes: 12 additions & 0 deletions
12
packages/lwc-integration/src/components/style/test-inject-style/inject-style/inject-style.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Element, track } from 'engine'; | ||
|
||
export default class InjectStyle extends Element { | ||
@track dimensions; | ||
|
||
renderedCallback() { | ||
if (this.dimensions === undefined) { | ||
const { width, height } = this.getBoundingClientRect(); | ||
this.dimensions = `${width}x${height}`; | ||
} | ||
} | ||
} |
Oops, something went wrong.