
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
@springload/metatemplate
Advanced tools
MetaTemplate is a web template generator that can take a single template definition and output...
This is particularly useful for Design Systems and Pattern Libraries where a single template definition could be converted into multiple formats.
"#336699"
and replace it with variable named theme-color-background
that will be replaced in situ with references to CSS Variables of --theme-color-background
, and those will also be Scss/CSS variables that you can define at ./scss/_settings.scss
.The input format to generate these is standard CSS, and almost standard HTML called MetaHTML.
Input: MetaHTML and standard CSS.
Output: React JS, React TS, React JS with Styled Components, React TS with Styled Components, Mustache/Handlebars, Vue, Twig, Angular, Sass (SCSS), HTML, CSS, SilverStripe Components.
These next two examples come from FlexBoxGrid.com and we've chosen two components with different complexities.
Input: MetaHTML and standard CSS.
Output: React JS, React TS, React JS with Styled Components, React TS with Styled Components, Mustache/Handlebars, Sass (SCSS), Vue, Twig, Angular, HTML CSS, SilverStripe Components.
Input: MetaHTML and standard CSS.
Output: React JS, React TS, React JS with Styled Components, React TS with Styled Components, Mustache/Handlebars, Sass (SCSS), Vue, Twig, Angular, HTML, CSS, SilverStripe.
npm i @springload/metatemplate
or yarn add @springload/metatemplate
.
children
values (childNodes) you could just nest other components instead. Maybe we don't need this.:not(.class)
and probably other features too, so check the output formats yourself.JSDOM
or Puppeteer
to parse HTML/CSS which mimics a browser environment inside Node.js. The JSDOM developers themselves note that it's possible to escape their sandbox when given malicious input, so don't use untrusted input, ya dingus.TypeScript types are provided.
async function makeTemplates(template, formatIds, options) => {}
The purpose of this function is to return templates in a variety of formats.
It's an async function that takes a Template
object, an optional array of template format ids (["react-ts", "angular"]
etc...). If the 2nd argument isn't provided a default list of template format ids is used instead. options
is an Object shaped like { async: true, dom: "jsdom", log: false }
. The async
configures the internal processing of templates as either syncronous or asyncronous (the default). The dom
can be either jsdom
(the default) or puppeteer
. Finally, log
just makes MetaTemplate console.log
a few more details about the conversion, like a verbose mode.
Returns a promise that resolves to a { metaTemplates, disposeAll }
.
metaTemplates
is an array of { templateFormat, files }
where
files
is an Object that represents a file archive, with Object keys as paths and values as strings of the templates. ie, { 'scss/button.scss': 'scss file data', 'mustache/button.mustache': 'mustache template data' }
.templateFormat
is a string of the template format (react-js
or scss
or react-ts-styled-components
etc).disposeAll
is a function to dispose of shared resources and after you finish using MetaTemplate. disposeAll
should be called to avoid memory/CPU waste. This should be called regardless of your configuration, however this is most important for people using Puppeteer rather than JSDOM (JSDOM is the default). Why disposeAll
? Well if MetaTemplate automatically cleaned up and called disposeAll
internally before returning your templates then it would be slower at batch processing, which is what MetaTemplate is typically used for. Perhaps this behaviour should be in options
.The 1st argument Template
Object looks like,
{ id, html, css, cssVariables }
id
is a required string that is used as the TemplateId
. This is your arbitrary but unique name for this template so use something meaningful. ie, FlexColumn.html
is a required string of MetaHTML.css
is a required string of standard CSS.cssVariables
is an optional Array of Objects shaped like { id, defaultValue, nameMatch, valueMatch, valueSubstringMatch }
,
id
is a required string for the variable name that will be made in Scss Variables and CSS Variables.defaultValue
: is a required string of your preferred default value for this variable.nameMatch
, valueMatch
, and valueSubstringMatch
are all optional strings, and you would choose one of them to match the CSS that you want to insert a variable at. If you want to replace a substring ie, "#000000" with "theme-color-dark" then you might write [{ id: 'theme-color-black', defaultValue: '#000000', valueSubstringMatch: '#000000' }]
to match that substring and replace it with variables. Currently there's no way of not outputting CSS Variables.The reason why we need to use non-standard HTML is to know which parts should be configurable, as variables.
MetaHTML is standard HTML with two types of template variables:
Those variables in attribute values:
{{ variableName }}
eg <span class="{{ class }}">
?
after the variable name to make it optional<span class="{{ class }}{{ otherClass }}">
{{ variableName: option1 | option2 }}
eg <span class="{{ color: class-red | class-blue }}">
{{ variableName: option1 as Option1 | option2 as Option2 }}
eg <span class="{{ color: class-red as Red | class-blue as Blue }}">
Those variables that are childNodes between elements:
<mt-variable key="variableName">default value</mt-variable>
eg if you want a component variable named "children" in an <h1>
you'd write <h1><mt-variable key="children">placeholder</mt-variable></h1>
There is also template if
support as <mt-if key="isShown">thing to show</mt-if>
.
async function makeIndexImports( files, cssVariables ) => {}
The purpose of this function is to provide "index" definitions for each format. The exact details vary by format, but the Sass (Scss) makes an index.scss
file with lots of @import "thing.scss"
, and a _settings.scss
for the Scss variables (hence the cssVariables
argument). The JavaScript/TypeScript templates have lazy-loaded imports in index.js
or index.ts
.
The files
Object is a required variable that represents a file archive, with Object keys as paths and values as strings of the file data. Typically you'd want to return the files
object returned by the default export.
cssVariables
is an optional Object with the same shape as the cssVariables
argument given to the default export.
Returns a files
Object that represents a file archive, but now it has index files for each format (ie, "scss/index.scss"
) to assist with importing files for that format.
async function makeUsage( code, templates, formatIds ) => {}
The purpose of this function is to convert a single code example (eg, in documentation) into examples from a variety of template formats.
code
is a code example,
jsxToUsageCode
for a slightly easier way of generating code
.It's datastructure is an Array of TemplateConfig
Objects shaped like,
{ templateId, variables }
Where templateId
is a string of the TemplateId
(same as the default export... your arbitrary id for the template), and variables
is an Object keyed by variableName
s whose values can be strings or other TemplateConfig Objects. ie, [{ templateId: 'H1', variables: { children: 'Hello' } }]
or [{ templateId: 'H1', variables: { children: [{ templateId: 'A', variables: { href: 'https://html5zombo.com/', children: 'Click me' } }] } }]
.
templates
is a required Object keyed by strings of TemplateId
s whose values are Template
Objects (like the default export). This must include every templateId
referenced in code
.
formatIds
is an optional array of strings of the formats to use, with a complete list of formatIds as the default.
async function jsxToUsageCode( jsx ) => {}
A convenience function that parses a string of React JSX and returns a code
variable for the makeUsage function.
'<H1><A href="https://html5zombo.com/">Click me</A></H1>'
will return [{ templateId: 'H1', variables: { children: [{ templateId: 'A', variables: { href: 'https://html5zombo.com/', children: 'Click me' } }] } }]
FAQs
MetaTemplate
We found that @springload/metatemplate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.