feat: implement build-time @store PostCSS directive

This commit is contained in:
2026-06-29 12:06:18 +01:00
parent 1c9e946464
commit e4bcd7578a
2 changed files with 52 additions and 0 deletions

View 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;

View File

@@ -0,0 +1,18 @@
{
"name": "postcss-storestyles",
"version": "0.0.1",
"private": true,
"description": "PostCSS plugin store styles related to multitenant systems using tailwindcss",
"keywords": [],
"author": "Benjamin Wegener <hello@ghosted.studio>",
"engines": {
"node": ">=18.0.0"
},
"peerDependencies": {
"postcss": "^8.4.27"
},
"devDependencies": {
"eslint": "^8.47.0",
"postcss": "^8.4.27"
}
}