From e4bcd7578aa4aff4c246e54d2ef87c21028de5f1 Mon Sep 17 00:00:00 2001 From: gh0sted Date: Mon, 29 Jun 2026 12:06:18 +0100 Subject: [PATCH] feat: implement build-time @store PostCSS directive --- plugins/postcss-storestyles/index.js | 34 ++++++++++++++++++++++++ plugins/postcss-storestyles/package.json | 18 +++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 plugins/postcss-storestyles/index.js create mode 100644 plugins/postcss-storestyles/package.json diff --git a/plugins/postcss-storestyles/index.js b/plugins/postcss-storestyles/index.js new file mode 100644 index 0000000..0224b84 --- /dev/null +++ b/plugins/postcss-storestyles/index.js @@ -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; diff --git a/plugins/postcss-storestyles/package.json b/plugins/postcss-storestyles/package.json new file mode 100644 index 0000000..e359644 --- /dev/null +++ b/plugins/postcss-storestyles/package.json @@ -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 ", + "engines": { + "node": ">=18.0.0" + }, + "peerDependencies": { + "postcss": "^8.4.27" + }, + "devDependencies": { + "eslint": "^8.47.0", + "postcss": "^8.4.27" + } +}