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;
|
||||||
18
plugins/postcss-storestyles/package.json
Normal file
18
plugins/postcss-storestyles/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user