feat: implement build-time @store PostCSS directive
This commit is contained in:
34
plugins/postcss-storestyles/index.js
Normal file
34
plugins/postcss-storestyles/index.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @type {import('postcss').PluginCreator}
|
||||
*/
|
||||
module.exports = (opts = {}) => {
|
||||
return {
|
||||
postcssPlugin: 'postcss-storestyles',
|
||||
/*
|
||||
* Resolve `@store` blocks in `Once`, before Tailwind's own `Once`
|
||||
* compiler pass runs. PostCSS fires every plugin's `Once` handler
|
||||
* (in registration order) ahead of the node-visitor traversal, so
|
||||
* a plain `AtRule` visitor would run too late — Tailwind/lightningcss
|
||||
* would already have seen `@store` and flagged it as an unknown
|
||||
* at-rule. Registering this plugin before `@tailwindcss/postcss`
|
||||
* therefore guarantees `@store` is gone before Tailwind compiles.
|
||||
*/
|
||||
Once: (root) => {
|
||||
const storeCode = opts.storeCode || process.env.NEXT_PUBLIC_STORE_CODE;
|
||||
const shopSystem = String(storeCode).split('_').shift();
|
||||
root.walkAtRules('store', (atRule) => {
|
||||
if (
|
||||
atRule.params != shopSystem &&
|
||||
!atRule.params.startsWith(`${shopSystem}_`) &&
|
||||
atRule.params !== storeCode
|
||||
) {
|
||||
atRule.remove();
|
||||
} else {
|
||||
atRule.replaceWith(atRule.nodes);
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
module.exports.postcss = true;
|
||||
Reference in New Issue
Block a user