This is important as postcss will transform your CSS before typescript
transpiles to JS (which is what you want to happen).
Usage with Vite or Rollup
If you use vite or rollup, you can simply use the rollupPostCSSLit plugin:
// rollup.config.js// IMPORTANT: since we're still CommonJS, use a default importimport postcssLit from 'postcss-lit';export default { plugins: [ postcssLit.rollupPostCSSLit({ // process only files in the `src` directory globInclude: 'src/**/*.{js,ts}', // exclude files with `-legacy.js` in their name globExclude: '**/*-legacy.js' }) ]}
The plugin will transform your CSS from template literals inside JS/TS
files using PostCSS. It will also automatically use local PostCSS config files.
You can use the optional globInclude and globExclude plugin options
to specify which files should be processed by PostCSS.
Usage with stylelint
In your .stylelintrc.json (or other stylelint config file):
{ "customSyntax": "postcss-lit"}
Or with the CLI:
stylelint --custom-syntax postcss-lit
Usage with vscode-stylelint
In order to make the
vscode-stylelint
extension work with this syntax correctly, you must configure it
to validate JS and/or TypeScript files.
You must specify all
tailwind directives
you intend to use in your CSS, otherwise their replacement CSS will be
incorrectly appended to the end of the document.
For example, in the code above, @tailwind base and @tailwind utilities
were specified to make text-xs available. Without them, the code would not
build.
Tailwind with webpack
See the same advice as with postcss standalone, here.
Disable specific templates
You can use postcss-lit-disable-next-line to disable a particular template
from being processed:
And a few unsupported examples (though some may work, they are not
officially supported):
// Part of a selector bound incss` .foo, ${expr} { color: hotpink; }`;// Part of a value bound incss` .foo { color: hot${expr}; }`;// Part of a property bound incss` .foo { col${expr}: hotpink; }`;
In cases we fail to parse, we will raise a warning in the console and skip
the template (i.e. leave it untouched and won't process it).
You can then use a // postcss-lit-disable-next-line comment to silence the
warning.
Custom babel options
You may customise the babel options via your package.json: