rollup-plugin-react-scoped-css 1.2.4 → 1.2.5
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/dist/css/scope-css.d.ts
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface ScopedCssResult {
|
|
2
|
+
code: string;
|
|
3
|
+
map: string | null;
|
|
4
|
+
}
|
|
5
|
+
export declare function scopeCss(css: string, filename: string, hash: string, source?: string): ScopedCssResult;
|
package/dist/css/scope-css.js
CHANGED
|
@@ -30,9 +30,9 @@ const isPseudoSelector = (item) => {
|
|
|
30
30
|
const isCombinator = (item) => {
|
|
31
31
|
return item?.data?.type === "Combinator";
|
|
32
32
|
};
|
|
33
|
-
export function scopeCss(css, filename, hash) {
|
|
33
|
+
export function scopeCss(css, filename, hash, source = filename) {
|
|
34
34
|
try {
|
|
35
|
-
const ast = parse(css);
|
|
35
|
+
const ast = parse(css, { positions: true, filename: source });
|
|
36
36
|
const selectorLists = getSelectors(ast.children);
|
|
37
37
|
const attributeSelector = {
|
|
38
38
|
type: "AttributeSelector",
|
|
@@ -75,10 +75,13 @@ export function scopeCss(css, filename, hash) {
|
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
});
|
|
78
|
-
|
|
78
|
+
const { css: scopedCss, map } = generate(ast, {
|
|
79
|
+
sourceMap: true,
|
|
80
|
+
});
|
|
81
|
+
return { code: scopedCss, map: map.toString() };
|
|
79
82
|
}
|
|
80
83
|
catch (e) {
|
|
81
84
|
console.log(`Failed scoping css of file: ${filename}\n\nError:\n`, e);
|
|
82
|
-
return css;
|
|
85
|
+
return { code: css, map: null };
|
|
83
86
|
}
|
|
84
87
|
}
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { generate } from "escodegen";
|
|
|
5
5
|
import { addHashAttributesToJsxTagsAst } from "./ast-program.js";
|
|
6
6
|
import { scopeCss } from "./css/scope-css.js";
|
|
7
7
|
import { getImportDeclarationsValue } from "./utils/getImportDeclarationsValue.js";
|
|
8
|
+
import { addLocationsToAst } from "./utils/addLocationsToAst.js";
|
|
8
9
|
const getFilenameFromPath = (filePath) => {
|
|
9
10
|
const parts = filePath.split("/");
|
|
10
11
|
return parts[parts.length - 1].split("?")[0];
|
|
@@ -81,8 +82,13 @@ export function reactScopedCssPlugin(optionsIn = {}) {
|
|
|
81
82
|
? `data-${hashPrefix}-${importerHash}`
|
|
82
83
|
: `data-${importerHash}`;
|
|
83
84
|
const newAst = addHashAttributesToJsxTagsAst(rootNode, scopedAttr);
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
addLocationsToAst(newAst, code, id);
|
|
86
|
+
const { code: newCode, map } = generate(newAst, {
|
|
87
|
+
sourceMap: id,
|
|
88
|
+
sourceContent: code,
|
|
89
|
+
sourceMapWithCode: true,
|
|
90
|
+
});
|
|
91
|
+
return { code: newCode, map: map.toString() };
|
|
86
92
|
}
|
|
87
93
|
}
|
|
88
94
|
else {
|
|
@@ -94,7 +100,7 @@ export function reactScopedCssPlugin(optionsIn = {}) {
|
|
|
94
100
|
const scopedAttr = options.hashPrefix
|
|
95
101
|
? `data-${options.hashPrefix}-${importerHash}`
|
|
96
102
|
: `data-${importerHash}`;
|
|
97
|
-
return scopeCss(code, getFilenameFromPath(id), scopedAttr);
|
|
103
|
+
return scopeCss(code, getFilenameFromPath(id), scopedAttr, id);
|
|
98
104
|
}
|
|
99
105
|
}
|
|
100
106
|
},
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escodegen generates source maps from the AST nodes' `loc` property, but
|
|
3
|
+
* Rollup's `this.parse()` only exposes `start`/`end` offsets. This walks the
|
|
4
|
+
* AST and adds equivalent `loc` information so a source map can be generated.
|
|
5
|
+
*/
|
|
6
|
+
export declare const addLocationsToAst: (ast: any, code: string, source: string) => any;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const getLineStarts = (code) => {
|
|
2
|
+
const lineStarts = [0];
|
|
3
|
+
for (let index = 0; index < code.length; index++) {
|
|
4
|
+
if (code.charCodeAt(index) === 10) {
|
|
5
|
+
lineStarts.push(index + 1);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return lineStarts;
|
|
9
|
+
};
|
|
10
|
+
const getPosition = (offset, lineStarts) => {
|
|
11
|
+
// Binary search for the last line start that is <= offset
|
|
12
|
+
let low = 0;
|
|
13
|
+
let high = lineStarts.length - 1;
|
|
14
|
+
while (low < high) {
|
|
15
|
+
const middle = Math.ceil((low + high) / 2);
|
|
16
|
+
if (lineStarts[middle] <= offset) {
|
|
17
|
+
low = middle;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
high = middle - 1;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return { line: low + 1, column: offset - lineStarts[low] };
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Escodegen generates source maps from the AST nodes' `loc` property, but
|
|
27
|
+
* Rollup's `this.parse()` only exposes `start`/`end` offsets. This walks the
|
|
28
|
+
* AST and adds equivalent `loc` information so a source map can be generated.
|
|
29
|
+
*/
|
|
30
|
+
export const addLocationsToAst = (ast, code, source) => {
|
|
31
|
+
const lineStarts = getLineStarts(code);
|
|
32
|
+
const visit = (node) => {
|
|
33
|
+
if (!node || typeof node !== "object") {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(node)) {
|
|
37
|
+
node.forEach(visit);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (typeof node.type === "string" &&
|
|
41
|
+
typeof node.start === "number" &&
|
|
42
|
+
typeof node.end === "number") {
|
|
43
|
+
const loc = {
|
|
44
|
+
start: getPosition(node.start, lineStarts),
|
|
45
|
+
end: getPosition(node.end, lineStarts),
|
|
46
|
+
source,
|
|
47
|
+
};
|
|
48
|
+
node.loc = loc;
|
|
49
|
+
}
|
|
50
|
+
for (const key of Object.keys(node)) {
|
|
51
|
+
if (key === "loc") {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const value = node[key];
|
|
55
|
+
if (value && typeof value === "object") {
|
|
56
|
+
visit(value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
visit(ast);
|
|
61
|
+
return ast;
|
|
62
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rollup-plugin-react-scoped-css",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "Bring scoped CSS to React with Vite & Rollup. Lightweight component-level style isolation similar to Vue and Angular.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -77,6 +77,7 @@
|
|
|
77
77
|
"jest": "30.2.0",
|
|
78
78
|
"rollup": "^4.59.0",
|
|
79
79
|
"semantic-release": "25.0.3",
|
|
80
|
+
"source-map-js": "1.2.1",
|
|
80
81
|
"typescript": "5.9.3"
|
|
81
82
|
}
|
|
82
83
|
}
|