postcss-safe-namespace 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Matt Pocock 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # postcss-safe-namespace
2
+
3
+ A PostCSS plugin that namespaces CSS selectors by adding a prefix, designed to prevent style conflicts in microfrontend and multi app environments.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install postcss-safe-namespace
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import postcss from "postcss";
15
+ import safeNamespace from "postcss-safe-namespace";
16
+
17
+ postcss([
18
+ safeNamespace({
19
+ prefix: ".my-prefix",
20
+ }),
21
+ ]);
22
+ ```
23
+
24
+ Or via vite:
25
+
26
+ ```js
27
+ // vite.config.js
28
+ import { defineConfig } from "vite";
29
+ import safeNamespace from "postcss-safe-namespace";
30
+
31
+ export default defineConfig({
32
+ css: {
33
+ postcss: {
34
+ plugins: [
35
+ safeNamespace({
36
+ prefix: ".my-prefix",
37
+ }),
38
+ ],
39
+ },
40
+ },
41
+ });
42
+ ```
43
+
44
+ ### Options
45
+
46
+ - `prefix`: The prefix to add to CSS selectors
47
+
48
+ ## Example
49
+
50
+ **Input:**
51
+
52
+ ```css
53
+ .button {
54
+ color: blue;
55
+ }
56
+
57
+ #header {
58
+ background: red;
59
+ }
60
+ ```
61
+
62
+ **Output:**
63
+
64
+ ```css
65
+ .my-prefix .button {
66
+ color: blue;
67
+ }
68
+
69
+ .my-prefix #header {
70
+ background: red;
71
+ }
72
+ ```
73
+
74
+ ## Why build another one?
75
+
76
+ Yes, there are already plugins that do this.
77
+
78
+ Our app that runs inside a application shell alongside other apps. We needed to make sure our styles only affected our app, not the others.
79
+
80
+ We tried existing plugins, but ran into problems:
81
+
82
+ - Some didn't handle nested CSS (we use Vite and Tailwind)
83
+ - Others broke CSS modules
84
+ - We needed to run separate set of plugins for production builds
85
+
86
+ So I made this one.
87
+
88
+ ### But wait, don't CSS modules solve this?
89
+
90
+ They do! We also use Tailwind, which adds base styles. There's no "nice" way to scope those base selectors with CSS modules alone.
91
+
92
+ ### Isn't that against the point of Tailwind?
93
+
94
+ Kind of, yeah. Tailwind classes are consistent everywhere. Different apps in our shell can use slightly different CSS variable values, which can cause the styles to clash again.
95
+
96
+ ## References
97
+
98
+ - https://postcss.org/docs/writing-a-postcss-plugin
99
+ - https://www.totaltypescript.com/how-to-create-an-npm-package
package/dist/index.cjs ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ default: () => safeNamespace_default
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/safeNamespace.ts
38
+ var import_postcss = require("postcss");
39
+ var import_postcss_selector_parser = __toESM(require("postcss-selector-parser"), 1);
40
+ var safeNamespace = (opts = {}) => {
41
+ const prefixSelector = opts.prefix || "";
42
+ return {
43
+ postcssPlugin: "safe-namespace",
44
+ Rule(rule) {
45
+ if (rule.parent && rule.parent.type === "rule") {
46
+ return;
47
+ }
48
+ if (rule.parent && rule.parent.type === "atrule") {
49
+ const atRule = rule.parent;
50
+ if (atRule.name === "keyframes") {
51
+ return;
52
+ }
53
+ }
54
+ rule.selector = (0, import_postcss_selector_parser.default)((selectors) => {
55
+ selectors.each((selector) => {
56
+ const firstNode = selector.first;
57
+ if (firstNode.value === ":root") {
58
+ firstNode.replaceWith(import_postcss_selector_parser.default.string({ value: prefixSelector }));
59
+ } else if (firstNode.value === "html") {
60
+ firstNode.replaceWith(import_postcss_selector_parser.default.string({ value: prefixSelector }));
61
+ } else {
62
+ selector.prepend(import_postcss_selector_parser.default.combinator({ value: " " }));
63
+ selector.prepend(import_postcss_selector_parser.default.string({ value: prefixSelector }));
64
+ }
65
+ });
66
+ }).processSync(rule.selector);
67
+ }
68
+ };
69
+ };
70
+ safeNamespace.postcss = true;
71
+ var safeNamespace_default = safeNamespace;
@@ -0,0 +1,8 @@
1
+ import { PluginCreator } from 'postcss';
2
+
3
+ interface PrefixOptions {
4
+ prefix?: string;
5
+ }
6
+ declare const safeNamespace: PluginCreator<PrefixOptions>;
7
+
8
+ export { safeNamespace as default };
@@ -0,0 +1,8 @@
1
+ import { PluginCreator } from 'postcss';
2
+
3
+ interface PrefixOptions {
4
+ prefix?: string;
5
+ }
6
+ declare const safeNamespace: PluginCreator<PrefixOptions>;
7
+
8
+ export { safeNamespace as default };
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ // src/safeNamespace.ts
2
+ import "postcss";
3
+ import parser from "postcss-selector-parser";
4
+ var safeNamespace = (opts = {}) => {
5
+ const prefixSelector = opts.prefix || "";
6
+ return {
7
+ postcssPlugin: "safe-namespace",
8
+ Rule(rule) {
9
+ if (rule.parent && rule.parent.type === "rule") {
10
+ return;
11
+ }
12
+ if (rule.parent && rule.parent.type === "atrule") {
13
+ const atRule = rule.parent;
14
+ if (atRule.name === "keyframes") {
15
+ return;
16
+ }
17
+ }
18
+ rule.selector = parser((selectors) => {
19
+ selectors.each((selector) => {
20
+ const firstNode = selector.first;
21
+ if (firstNode.value === ":root") {
22
+ firstNode.replaceWith(parser.string({ value: prefixSelector }));
23
+ } else if (firstNode.value === "html") {
24
+ firstNode.replaceWith(parser.string({ value: prefixSelector }));
25
+ } else {
26
+ selector.prepend(parser.combinator({ value: " " }));
27
+ selector.prepend(parser.string({ value: prefixSelector }));
28
+ }
29
+ });
30
+ }).processSync(rule.selector);
31
+ }
32
+ };
33
+ };
34
+ safeNamespace.postcss = true;
35
+ var safeNamespace_default = safeNamespace;
36
+ export {
37
+ safeNamespace_default as default
38
+ };
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "postcss-safe-namespace",
3
+ "version": "1.0.1",
4
+ "description": "A PostCSS plugin to safely namespace CSS selectors by adding a prefix.",
5
+ "keywords": [],
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.cjs"
12
+ }
13
+ },
14
+ "homepage": "https://github.com/hendrikkao1/postcss-safe-namespace",
15
+ "bugs": {
16
+ "url": "https://github.com/hendrikkao1/postcss-safe-namespace/issues"
17
+ },
18
+ "author": "Hendrik Käo",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/hendrikkao1/postcss-safe-namespace.git"
22
+ },
23
+ "license": "MIT",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "type": "module",
28
+ "devDependencies": {
29
+ "@changesets/cli": "^2.29.8",
30
+ "@types/node": "^25.0.3",
31
+ "postcss": "^8.5.6",
32
+ "prettier": "^3.7.4",
33
+ "tsup": "^8.5.1",
34
+ "typescript": "^5.9.3",
35
+ "vitest": "^3.2.4"
36
+ },
37
+ "scripts": {
38
+ "build": "tsup",
39
+ "ci": "npm run build && npm run format:check && npm run lint && npm run test",
40
+ "lint": "tsc",
41
+ "test": "vitest run",
42
+ "test:watch": "vitest --watch",
43
+ "format": "prettier --write .",
44
+ "format:check": "prettier --check .",
45
+ "local-release": "npm run ci && changeset version && changeset publish"
46
+ },
47
+ "dependencies": {
48
+ "postcss-selector-parser": "^7.1.1"
49
+ },
50
+ "peerDependencies": {
51
+ "postcss": "^8.5.6"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
56
+ }