-
Notifications
You must be signed in to change notification settings - Fork 35
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
chore(atomic): add SVG transformer #4867
base: master
Are you sure you want to change the base?
Conversation
Pull Request ReportPR Title✅ Title follows the conventional commit spec. Live demo linksBundle Size
SSR Progress
Detailed logssearch : buildInteractiveResultsearch : buildInteractiveInstantResult search : buildInteractiveRecentResult search : buildInteractiveCitation search : buildGeneratedAnswer recommendation : missing SSR support case-assist : missing SSR support insight : missing SSR support commerce : missing SSR support |
before: [svgTransformer], | ||
}; | ||
|
||
return program.emit( |
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.
Creates a CompilerHost with our custom transformer
createStringLiteral(svgContent) | ||
), | ||
], | ||
NodeFlags.Const |
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.
Asking to make the assignation statement a const.
const variable = ...
variableName, | ||
bindingName, | ||
exclamationToken, | ||
createStringLiteral(svgContent) |
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.
converting the SVG content into a string literal for the right hand side of the assignation
export default function svgTransformer(context) { | ||
const {factory} = context; | ||
|
||
function visit(node) { |
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.
Recursive method that visits node and their children following import statements
const {factory} = context; | ||
|
||
function visit(node) { | ||
if (isImportDeclaration(node)) { |
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.
if the statement is something like
import something from "something"
const svgContent = readFileSync(svgPath, 'utf8'); | ||
const variableName = node.importClause?.name?.escapedText; | ||
|
||
return createStatement(factory, svgContent, variableName); |
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.
After getting the variable name from the import and the content from the svg file, we are ready to craft the new statement
Update the Lit build process to be support SVG imports
Explanation
We are essentially creating a compiler which takes a list of TypeScript files (Lit components and dependencies) and compiles them to their corresponding JavaScript keeping the same file structure as in
/src
.Typescript doc
To that compiler, I have added a SVG custom transformer.
Example
The following typescript file
was transpiled to
With the SVG transformer, we have
Pseudo-code
program.emit
function).svg
import statements.createVariableStatement
typescript built-in method. Ensure to right-hand side of the assignation is the content of the .svg filehttps://coveord.atlassian.net/browse/KIT-3865