rollup-plugin-react-scoped-css 0.4.1 → 0.6.0
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/.eslintrc.yml +29 -0
- package/.github/workflows/pull-request.yml +20 -0
- package/.github/workflows/release.yml +30 -0
- package/.releaserc +13 -0
- package/.vscode/settings.json +8 -0
- package/README.md +55 -7
- package/babel.config.js +6 -2
- package/dist/ast-iterator.d.ts +1 -0
- package/dist/ast-iterator.js +114 -0
- package/dist/ast-transformer.d.ts +1 -0
- package/dist/ast-transformer.js +50 -0
- package/dist/ast.utils.d.ts +4 -0
- package/dist/ast.utils.js +49 -108
- package/dist/index.d.ts +2 -2
- package/dist/index.js +24 -23
- package/dist/parsers/classic-parser.d.ts +3 -2
- package/dist/parsers/classic-parser.js +87 -24
- package/dist/parsers/jsx-parser.model.d.ts +0 -3
- package/dist/parsers/new-parser.d.ts +3 -2
- package/dist/parsers/new-parser.js +86 -23
- package/package.json +18 -3
- package/src/__mocks__/{compiled-jsx-with-attr.js → classic-compiled-jsx-with-attr.js} +0 -0
- package/src/__mocks__/{compiled-jsx.js → classic-compiled-jsx.js} +0 -0
- package/src/__mocks__/new-compiled-jsx-with-attr.js +101 -0
- package/src/__mocks__/new-compiled-jsx.js +64 -0
- package/src/__mocks__/sample-code-file.js +4 -0
- package/src/ast-iterator.spec.ts +22 -0
- package/src/ast-iterator.ts +32 -0
- package/src/ast-transformer.spec.ts +23 -0
- package/src/ast-transformer.ts +28 -0
- package/src/ast.utils.spec.ts +59 -20
- package/src/ast.utils.ts +41 -99
- package/src/index.ts +77 -57
- package/src/parsers/classic-parser.ts +72 -0
- package/src/parsers/jsx-parser.model.ts +10 -0
- package/src/parsers/new-parser.ts +74 -0
- package/tsconfig.json +2 -1
- package/dist/parsers/base-parser.d.ts +0 -15
- package/dist/parsers/base-parser.js +0 -109
- package/dist/parsers/jsx-parser.interface.d.ts +0 -6
- package/dist/parsers/jsx-parser.interface.js +0 -2
package/.eslintrc.yml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
env:
|
|
2
|
+
browser: true
|
|
3
|
+
es2021: true
|
|
4
|
+
extends:
|
|
5
|
+
- eslint:recommended
|
|
6
|
+
- plugin:@typescript-eslint/eslint-recommended
|
|
7
|
+
- plugin:@typescript-eslint/recommended
|
|
8
|
+
- plugin:react/recommended
|
|
9
|
+
- plugin:prettier/recommended
|
|
10
|
+
- plugin:react/jsx-runtime
|
|
11
|
+
parser: '@typescript-eslint/parser'
|
|
12
|
+
parserOptions:
|
|
13
|
+
ecmaFeatures:
|
|
14
|
+
jsx: true
|
|
15
|
+
ecmaVersion: "latest"
|
|
16
|
+
sourceType: module
|
|
17
|
+
plugins:
|
|
18
|
+
- react
|
|
19
|
+
- '@typescript-eslint'
|
|
20
|
+
settings:
|
|
21
|
+
react:
|
|
22
|
+
version: "detect"
|
|
23
|
+
ignorePatterns:
|
|
24
|
+
- "**/node_modules/**"
|
|
25
|
+
- "**/__mocks__/**/*.js"
|
|
26
|
+
- "**/dist/**"
|
|
27
|
+
rules: {
|
|
28
|
+
"react/jsx-filename-extension": [1, { "extensions": [".jsx", ".tsx"] }]
|
|
29
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Pull Request Workflow
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
- pull_request
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
build:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v2
|
|
12
|
+
- name: Use Node.js 16.x
|
|
13
|
+
uses: actions/setup-node@v2
|
|
14
|
+
with:
|
|
15
|
+
node-version: 16.x
|
|
16
|
+
cache: 'npm'
|
|
17
|
+
- run: npm ci
|
|
18
|
+
- run: npm run build --if-present
|
|
19
|
+
- run: npm test
|
|
20
|
+
- run: npm run lint
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Release Workflow
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- 'main'
|
|
7
|
+
paths-ignore:
|
|
8
|
+
- '.github/**'
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
release:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v2
|
|
16
|
+
- name: Use Node.js 16.x
|
|
17
|
+
uses: actions/setup-node@v2
|
|
18
|
+
with:
|
|
19
|
+
node-version: 16.x
|
|
20
|
+
registry-url: 'https://registry.npmjs.org'
|
|
21
|
+
cache: 'npm'
|
|
22
|
+
- run: npm ci
|
|
23
|
+
- run: npm run build --if-present
|
|
24
|
+
- name: Release
|
|
25
|
+
env:
|
|
26
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
27
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
28
|
+
run: npx semantic-release
|
|
29
|
+
|
|
30
|
+
|
package/.releaserc
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"branches": ["main", "next"],
|
|
3
|
+
"plugins": [
|
|
4
|
+
"@semantic-release/commit-analyzer",
|
|
5
|
+
"@semantic-release/release-notes-generator",
|
|
6
|
+
"@semantic-release/npm",
|
|
7
|
+
["@semantic-release/git", {
|
|
8
|
+
"assets": ["package.json"],
|
|
9
|
+
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
|
10
|
+
}],
|
|
11
|
+
"@semantic-release/github"
|
|
12
|
+
]
|
|
13
|
+
}
|
package/README.md
CHANGED
|
@@ -1,20 +1,68 @@
|
|
|
1
1
|
# rollup-plugin-react-scoped-css
|
|
2
2
|
|
|
3
|
-
**IMPORTANT: This plugin is still in its early phase, use at your own risk, but feel free to contribute.**
|
|
4
|
-
|
|
5
3
|
## Motivations
|
|
6
4
|
While using react in a professional context, I realized that it was lacking the scopped css feature that I learned to love from Vue and Angular. After some reasearch I came across good plugins, but sadly were not compatible with vite and/or rollup. Thus, I decided to create this plugin which was greatly inspired by the amazing work of [gaoxiaoliangz](https://github.com/gaoxiaoliangz) with his [react-scoped-css plugin](https://github.com/gaoxiaoliangz/react-scoped-css).
|
|
7
5
|
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```jsx
|
|
9
|
+
// Component.jsx
|
|
10
|
+
import './Component.scoped.scss'
|
|
11
|
+
|
|
12
|
+
export default function Sub() {
|
|
13
|
+
return (
|
|
14
|
+
<div className="wrap">
|
|
15
|
+
<h1>My Component</h1>
|
|
16
|
+
</div>
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```scss
|
|
22
|
+
// Component.scoped.scss
|
|
23
|
+
.wrap {
|
|
24
|
+
width: 500px;
|
|
25
|
+
h1 { color: red; }
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
And just like that the styles will be scoped to the component.
|
|
29
|
+
|
|
30
|
+
## Technicality
|
|
31
|
+
since this plugin uses the engine of vue to scope the css to the components, the following sections come straight from the [vue documentation](https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors).
|
|
32
|
+
|
|
33
|
+
### Deep selector
|
|
34
|
+
If you want a selector in scoped styles to be "deep", i.e. affecting child components, you can use the >>> combinator:
|
|
35
|
+
```css
|
|
36
|
+
.a >>> .b { /* ... */ }
|
|
37
|
+
```
|
|
38
|
+
The above will be compiled into:
|
|
39
|
+
```css
|
|
40
|
+
.a[data-v-f3f3eg9] .b { /* ... */ }
|
|
41
|
+
```
|
|
42
|
+
Some pre-processors, such as Sass, may not be able to parse >>> properly. In those cases you can use the /deep/ or ::v-deep combinator instead - both are aliases for >>> and work exactly the same. Based on the example above these two expressions will be compiled to the same output:
|
|
43
|
+
```scss
|
|
44
|
+
.a::v-deep .b { /* ... */ }
|
|
45
|
+
/* or */
|
|
46
|
+
.a /deep/ .b { /* ... */ }
|
|
47
|
+
```
|
|
48
|
+
### Dynamically Generated Content
|
|
49
|
+
DOM content created with dangerouslySetInnerHTML are not affected by scoped styles, but you can still style them using deep selectors.
|
|
50
|
+
|
|
51
|
+
### Also Keep in Mind
|
|
52
|
+
Scoped styles do not eliminate the need for classes. Due to the way browsers render various CSS selectors, p { color: red } will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in .example { color: red }, then you virtually eliminate that performance hit.
|
|
53
|
+
|
|
54
|
+
Be careful with descendant selectors in recursive components! For a CSS rule with the selector .a .b, if the element that matches .a contains a recursive child component, then all .b in that child component will be matched by the rule.
|
|
55
|
+
|
|
8
56
|
## How to install
|
|
9
57
|
|
|
10
58
|
```sh
|
|
11
|
-
npm i rollup-plugin-react-scoped-css
|
|
59
|
+
$ npm i rollup-plugin-react-scoped-css
|
|
12
60
|
```
|
|
13
61
|
|
|
14
62
|
### Simple Configuration
|
|
15
63
|
|
|
16
64
|
in vite:
|
|
17
|
-
```
|
|
65
|
+
```js
|
|
18
66
|
// vite.config.js
|
|
19
67
|
import { defineConfig } from 'vite'
|
|
20
68
|
import reactRefresh from '@vitejs/plugin-react-refresh'
|
|
@@ -27,7 +75,7 @@ export default defineConfig({
|
|
|
27
75
|
```
|
|
28
76
|
|
|
29
77
|
in rollup:
|
|
30
|
-
```
|
|
78
|
+
```js
|
|
31
79
|
// rollup.config.js
|
|
32
80
|
import reactScopedCssPlugin from 'rollup-plugin-react-scoped-css';
|
|
33
81
|
|
|
@@ -40,7 +88,7 @@ export default {
|
|
|
40
88
|
|
|
41
89
|
### Customizing the plugin
|
|
42
90
|
There are a few options available to customize how the plugin works
|
|
43
|
-
```
|
|
91
|
+
```ts
|
|
44
92
|
{
|
|
45
93
|
/**
|
|
46
94
|
* Which files should be included and parsed by the plugin
|
|
@@ -84,7 +132,7 @@ There are a few options available to customize how the plugin works
|
|
|
84
132
|
### With other rollup plugins
|
|
85
133
|
Since this plugin works in two parts, you might need to expose the first part, then add any other plugin, and then expose the second part of the plugin. This part is automatically handled with vite thanks to the enforce attribute.
|
|
86
134
|
|
|
87
|
-
```
|
|
135
|
+
```js
|
|
88
136
|
const reactScopedPlugins = reactScopedCssPlugin()
|
|
89
137
|
export default {
|
|
90
138
|
//...
|
package/babel.config.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// eslint-disable-next-line no-undef
|
|
1
2
|
module.exports = {
|
|
2
|
-
presets: [
|
|
3
|
-
}
|
|
3
|
+
presets: [
|
|
4
|
+
["@babel/preset-env", { targets: { node: "current" } }],
|
|
5
|
+
"@babel/preset-typescript",
|
|
6
|
+
],
|
|
7
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const astIterator: (ast: any) => IterableIterator<any>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (_) try {
|
|
9
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var __values = (this && this.__values) || function(o) {
|
|
30
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
31
|
+
if (m) return m.call(o);
|
|
32
|
+
if (o && typeof o.length === "number") return {
|
|
33
|
+
next: function () {
|
|
34
|
+
if (o && i >= o.length) o = void 0;
|
|
35
|
+
return { value: o && o[i++], done: !o };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.astIterator = void 0;
|
|
42
|
+
var isNode = function (obj) {
|
|
43
|
+
var _a, _b, _c;
|
|
44
|
+
return (typeof obj === "object" &&
|
|
45
|
+
typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.end) === "number" &&
|
|
46
|
+
typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.start) === "number" &&
|
|
47
|
+
typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.type) === "string");
|
|
48
|
+
};
|
|
49
|
+
var astIterator = function (ast) {
|
|
50
|
+
var _a, _b, key, _c, _d, _i, nodeIndex, e_1_1;
|
|
51
|
+
var e_1, _e;
|
|
52
|
+
return __generator(this, function (_f) {
|
|
53
|
+
switch (_f.label) {
|
|
54
|
+
case 0:
|
|
55
|
+
if (!ast) {
|
|
56
|
+
return [2 /*return*/];
|
|
57
|
+
}
|
|
58
|
+
_f.label = 1;
|
|
59
|
+
case 1:
|
|
60
|
+
_f.trys.push([1, 11, 12, 13]);
|
|
61
|
+
_a = __values(Object.keys(ast)), _b = _a.next();
|
|
62
|
+
_f.label = 2;
|
|
63
|
+
case 2:
|
|
64
|
+
if (!!_b.done) return [3 /*break*/, 10];
|
|
65
|
+
key = _b.value;
|
|
66
|
+
// Skip useless keys
|
|
67
|
+
if (["end", "start", "type"].includes(key)) {
|
|
68
|
+
return [3 /*break*/, 9];
|
|
69
|
+
}
|
|
70
|
+
if (!Array.isArray(ast[key])) return [3 /*break*/, 7];
|
|
71
|
+
_c = [];
|
|
72
|
+
for (_d in ast[key])
|
|
73
|
+
_c.push(_d);
|
|
74
|
+
_i = 0;
|
|
75
|
+
_f.label = 3;
|
|
76
|
+
case 3:
|
|
77
|
+
if (!(_i < _c.length)) return [3 /*break*/, 6];
|
|
78
|
+
nodeIndex = _c[_i];
|
|
79
|
+
return [5 /*yield**/, __values((0, exports.astIterator)(ast[key][nodeIndex]))];
|
|
80
|
+
case 4:
|
|
81
|
+
_f.sent();
|
|
82
|
+
_f.label = 5;
|
|
83
|
+
case 5:
|
|
84
|
+
_i++;
|
|
85
|
+
return [3 /*break*/, 3];
|
|
86
|
+
case 6: return [3 /*break*/, 9];
|
|
87
|
+
case 7:
|
|
88
|
+
if (!isNode(ast[key])) return [3 /*break*/, 9];
|
|
89
|
+
return [5 /*yield**/, __values((0, exports.astIterator)(ast[key]))];
|
|
90
|
+
case 8:
|
|
91
|
+
_f.sent();
|
|
92
|
+
_f.label = 9;
|
|
93
|
+
case 9:
|
|
94
|
+
_b = _a.next();
|
|
95
|
+
return [3 /*break*/, 2];
|
|
96
|
+
case 10: return [3 /*break*/, 13];
|
|
97
|
+
case 11:
|
|
98
|
+
e_1_1 = _f.sent();
|
|
99
|
+
e_1 = { error: e_1_1 };
|
|
100
|
+
return [3 /*break*/, 13];
|
|
101
|
+
case 12:
|
|
102
|
+
try {
|
|
103
|
+
if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
|
|
104
|
+
}
|
|
105
|
+
finally { if (e_1) throw e_1.error; }
|
|
106
|
+
return [7 /*endfinally*/];
|
|
107
|
+
case 13: return [4 /*yield*/, ast];
|
|
108
|
+
case 14:
|
|
109
|
+
_f.sent();
|
|
110
|
+
return [2 /*return*/];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
exports.astIterator = astIterator;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const astTransformer: (ast: any, callback: (ast: any) => any) => any;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.astTransformer = void 0;
|
|
15
|
+
var isNode = function (obj) {
|
|
16
|
+
var _a, _b, _c;
|
|
17
|
+
return (typeof obj === "object" &&
|
|
18
|
+
typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.end) === "number" &&
|
|
19
|
+
typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.start) === "number" &&
|
|
20
|
+
typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.type) === "string");
|
|
21
|
+
};
|
|
22
|
+
var astTransformer = function (ast, callback) {
|
|
23
|
+
var e_1, _a;
|
|
24
|
+
try {
|
|
25
|
+
for (var _b = __values(Object.keys(ast)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
26
|
+
var key = _c.value;
|
|
27
|
+
if (["end", "start", "type"].includes(key)) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(ast[key])) {
|
|
31
|
+
for (var nodeIndex in ast[key]) {
|
|
32
|
+
ast[key][nodeIndex] = (0, exports.astTransformer)(ast[key][nodeIndex], callback);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else if (isNode(ast[key])) {
|
|
36
|
+
ast[key] = (0, exports.astTransformer)(ast[key], callback);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
41
|
+
finally {
|
|
42
|
+
try {
|
|
43
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
44
|
+
}
|
|
45
|
+
finally { if (e_1) throw e_1.error; }
|
|
46
|
+
}
|
|
47
|
+
var result = callback(ast);
|
|
48
|
+
return typeof result !== "undefined" ? result : ast;
|
|
49
|
+
};
|
|
50
|
+
exports.astTransformer = astTransformer;
|
package/dist/ast.utils.d.ts
CHANGED
package/dist/ast.utils.js
CHANGED
|
@@ -1,122 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
var __values = (this && this.__values) || function(o) {
|
|
3
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
+
if (m) return m.call(o);
|
|
5
|
+
if (o && typeof o.length === "number") return {
|
|
6
|
+
next: function () {
|
|
7
|
+
if (o && i >= o.length) o = void 0;
|
|
8
|
+
return { value: o && o[i++], done: !o };
|
|
8
9
|
}
|
|
9
|
-
return t;
|
|
10
10
|
};
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
14
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
15
|
-
to[j] = from[i];
|
|
16
|
-
return to;
|
|
11
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
17
12
|
};
|
|
18
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
14
|
exports.addHashAttributesToJsxTagsAst = void 0;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
15
|
+
var ast_iterator_1 = require("./ast-iterator");
|
|
16
|
+
var ast_transformer_1 = require("./ast-transformer");
|
|
17
|
+
var classic_parser_1 = require("./parsers/classic-parser");
|
|
18
|
+
var jsx_parser_model_1 = require("./parsers/jsx-parser.model");
|
|
19
|
+
var new_parser_1 = require("./parsers/new-parser");
|
|
20
|
+
global.implementation = null;
|
|
21
|
+
var classicParser = new classic_parser_1.ClassicJsxParser();
|
|
22
|
+
var newParser = new new_parser_1.NewJsxParser();
|
|
23
|
+
var parser = classicParser;
|
|
24
|
+
var findImplementation = function (program) {
|
|
25
|
+
var e_1, _a;
|
|
26
|
+
var implementation = jsx_parser_model_1.ParserImplementations.classic;
|
|
27
|
+
try {
|
|
28
|
+
for (var _b = __values((0, ast_iterator_1.astIterator)(program)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
29
|
+
var node = _c.value;
|
|
30
|
+
if (node.type !== "ImportDeclaration") {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (node.specifiers.some(function (n) {
|
|
34
|
+
return ["_jsxDEV", "jsxRuntime"].includes(n.local.name);
|
|
35
|
+
}) ||
|
|
36
|
+
["react/jsx-dev-runtime", "react/jsx-runtime"].includes(node.source.value)) {
|
|
37
|
+
return jsx_parser_model_1.ParserImplementations.new;
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
}
|
|
41
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
42
|
+
finally {
|
|
43
|
+
try {
|
|
44
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
40
45
|
}
|
|
46
|
+
finally { if (e_1) throw e_1.error; }
|
|
41
47
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
function isNode(obj) {
|
|
46
|
-
var _a, _b, _c;
|
|
47
|
-
return typeof obj === 'object' &&
|
|
48
|
-
typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.end) === 'number' &&
|
|
49
|
-
typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.start) === 'number' &&
|
|
50
|
-
typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.type) === 'string';
|
|
51
|
-
}
|
|
52
|
-
function isElementWithOldJsxTransform(node) {
|
|
53
|
-
var _a, _b, _c, _d;
|
|
54
|
-
return (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
|
|
55
|
-
((_b = (_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.object) === null || _b === void 0 ? void 0 : _b.name) === 'React' &&
|
|
56
|
-
((_d = (_c = node === null || node === void 0 ? void 0 : node.callee) === null || _c === void 0 ? void 0 : _c.property) === null || _d === void 0 ? void 0 : _d.name) === 'createElement';
|
|
57
|
-
}
|
|
58
|
-
function isElementWithNewJsxTransform(node) {
|
|
59
|
-
var _a;
|
|
60
|
-
return (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
|
|
61
|
-
['_jsxDEV', '_jsx', '_jsxs'].includes((_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.name);
|
|
62
|
-
}
|
|
63
|
-
function isFragmentWithOldJsxTransform(node) {
|
|
64
|
-
var _a, _b, _c, _d, _e, _f;
|
|
65
|
-
return ((_c = (_b = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.object) === null || _c === void 0 ? void 0 : _c.name) === "React" &&
|
|
66
|
-
((_f = (_e = (_d = node === null || node === void 0 ? void 0 : node.arguments) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.property) === null || _f === void 0 ? void 0 : _f.name) === "Fragment";
|
|
67
|
-
}
|
|
68
|
-
function isFragmentWithNewJsxTransform(node) {
|
|
69
|
-
var _a, _b, _c, _d;
|
|
70
|
-
return ((_b = (_a = node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.type) === "Identifier" &&
|
|
71
|
-
((_d = (_c = node.arguments) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.name) === "_Fragment";
|
|
72
|
-
}
|
|
73
|
-
function isFragment(node) {
|
|
74
|
-
return isFragmentWithOldJsxTransform(node) ||
|
|
75
|
-
isFragmentWithNewJsxTransform(node);
|
|
76
|
-
}
|
|
77
|
-
function isReactElement(node) {
|
|
78
|
-
return isElementWithOldJsxTransform(node) ||
|
|
79
|
-
isElementWithNewJsxTransform(node);
|
|
80
|
-
}
|
|
81
|
-
function createAttributeNode(attr) {
|
|
82
|
-
return {
|
|
83
|
-
type: 'Property',
|
|
84
|
-
method: false,
|
|
85
|
-
shorthand: false,
|
|
86
|
-
computed: false,
|
|
87
|
-
key: {
|
|
88
|
-
type: 'Identifier',
|
|
89
|
-
name: "\"" + attr + "\""
|
|
90
|
-
},
|
|
91
|
-
value: {
|
|
92
|
-
type: 'Literal',
|
|
93
|
-
value: true,
|
|
94
|
-
raw: "true"
|
|
95
|
-
},
|
|
96
|
-
kind: 'init'
|
|
97
|
-
};
|
|
98
|
-
}
|
|
48
|
+
return implementation;
|
|
49
|
+
};
|
|
99
50
|
function addHashAttributesToJsxTagsAst(program, attr) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
return {
|
|
113
|
-
type: 'ObjectExpression',
|
|
114
|
-
properties: [createAttributeNode(attr)]
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return v;
|
|
119
|
-
}) });
|
|
51
|
+
// Once in the program, we can determine which parser to use
|
|
52
|
+
if (global.implementation === null) {
|
|
53
|
+
global.implementation = findImplementation(program);
|
|
54
|
+
if (global.implementation === jsx_parser_model_1.ParserImplementations.new) {
|
|
55
|
+
parser = newParser;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return (0, ast_transformer_1.astTransformer)(program, function (node) {
|
|
59
|
+
if (parser.isNodeReactElement(node) && !parser.isNodeReactFragment(node)) {
|
|
60
|
+
return parser.extendNodeWithAttributes(node, attr);
|
|
120
61
|
}
|
|
121
62
|
});
|
|
122
63
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FilterPattern } from
|
|
2
|
-
import type { Plugin } from
|
|
1
|
+
import { FilterPattern } from "@rollup/pluginutils";
|
|
2
|
+
import type { Plugin } from "rollup";
|
|
3
3
|
export interface ReactScopedCssPluginOptions {
|
|
4
4
|
/**
|
|
5
5
|
* Which files should be included and parsed by the plugin
|