rollup-plugin-react-scoped-css 0.3.0 → 0.5.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.
Files changed (35) hide show
  1. package/.github/workflows/pull-request.yml +19 -0
  2. package/.github/workflows/release.yml +30 -0
  3. package/.releaserc +13 -0
  4. package/README.md +35 -7
  5. package/dist/ast-iterator.d.ts +1 -0
  6. package/dist/ast-iterator.js +114 -0
  7. package/dist/ast-transformer.d.ts +1 -0
  8. package/dist/ast-transformer.js +50 -0
  9. package/dist/ast.utils.d.ts +4 -0
  10. package/dist/ast.utils.js +46 -89
  11. package/dist/index.d.ts +27 -1
  12. package/dist/index.js +21 -11
  13. package/dist/parsers/classic-parser.d.ts +7 -0
  14. package/dist/parsers/classic-parser.js +95 -0
  15. package/dist/parsers/jsx-parser.model.d.ts +9 -0
  16. package/dist/parsers/jsx-parser.model.js +8 -0
  17. package/dist/parsers/new-parser.d.ts +7 -0
  18. package/dist/parsers/new-parser.js +94 -0
  19. package/package.json +14 -3
  20. package/src/__mocks__/{compiled-jsx-with-attr.js → classic-compiled-jsx-with-attr.js} +0 -0
  21. package/src/__mocks__/{compiled-jsx.js → classic-compiled-jsx.js} +0 -0
  22. package/src/__mocks__/new-compiled-jsx-with-attr.js +101 -0
  23. package/src/__mocks__/new-compiled-jsx.js +64 -0
  24. package/src/__mocks__/sample-code-file.js +4 -0
  25. package/src/ast-iterator.spec.ts +19 -0
  26. package/src/ast-iterator.ts +27 -0
  27. package/src/ast-transformer.spec.ts +20 -0
  28. package/src/ast-transformer.ts +24 -0
  29. package/src/ast.utils.spec.ts +36 -12
  30. package/src/ast.utils.ts +33 -79
  31. package/src/index.ts +52 -8
  32. package/src/parsers/classic-parser.ts +58 -0
  33. package/src/parsers/jsx-parser.model.ts +10 -0
  34. package/src/parsers/new-parser.ts +58 -0
  35. package/tsconfig.json +2 -1
@@ -0,0 +1,19 @@
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
@@ -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,10 +1,33 @@
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
+
29
+ And just like that the styles will be scoped to the component.
30
+
8
31
  ## How to install
9
32
 
10
33
  ```sh
@@ -56,6 +79,7 @@ There are a few options available to customize how the plugin works
56
79
 
57
80
  /**
58
81
  * If you want to customize the stylesheet file pattern
82
+ * if undefined or '' is passed, all files will be evaluated
59
83
  * Default: 'scoped'
60
84
  */
61
85
  styleFileSuffix?: string
@@ -70,12 +94,18 @@ There are a few options available to customize how the plugin works
70
94
  * If you want to customize the preprocessors
71
95
  * Default: ['scss', 'css', 'sass', 'less']
72
96
  */
73
- preProcessors?: string[]
97
+ styleFileExtensions?: string[]
98
+
99
+ /**
100
+ * If you have jsx in other file extensions
101
+ * Default: ['jsx', 'tsx']
102
+ */
103
+ jsxFileExtensions?: string[]
74
104
  }
75
105
  ```
76
106
 
77
107
  ### With other rollup plugins
78
- Since this plugin works in two parts, you might need to expose the first part, then add any styling plugin, and then expose the second part of the plugin. This part is automatically handled with vite thanks to the enforce attribute.
108
+ 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.
79
109
 
80
110
  ```
81
111
  const reactScopedPlugins = reactScopedCssPlugin()
@@ -86,6 +116,4 @@ export default {
86
116
  ```
87
117
 
88
118
  ## Contributing
89
- Anyone is free to open a PR and contribute to this project... I, by no means, am the best, and I am sure someone smarter than me can bring a lot to this project.
90
-
91
- The only rules is: Don't be a dick
119
+ Anyone is free to open a PR and contribute to this project... just be civilized!
@@ -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: Function) => 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;
@@ -1 +1,5 @@
1
+ import { ParserImplementations } from "./parsers/jsx-parser.model";
2
+ declare global {
3
+ var implementation: null | ParserImplementations;
4
+ }
1
5
  export declare function addHashAttributesToJsxTagsAst(program: any, attr: string): any;
package/dist/ast.utils.js CHANGED
@@ -1,103 +1,60 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
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
- return __assign.apply(this, arguments);
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
- function isNode(obj) {
21
- var _a, _b, _c;
22
- return typeof obj === 'object' &&
23
- typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.end) === 'number' &&
24
- typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.start) === 'number' &&
25
- typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.type) === 'string';
26
- }
27
- function isReactCreateElementCallExpression(node) {
28
- var _a, _b, _c, _d, _e;
29
- return ( // with React.createElement
30
- (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
31
- ((_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' &&
32
- ((_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') || ( // with babel jsx runtime
33
- (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
34
- ((_e = node === null || node === void 0 ? void 0 : node.callee) === null || _e === void 0 ? void 0 : _e.name) === '_jsxDEV');
35
- }
36
- function isFragmentCallExpression(node) {
37
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
38
- return ( // with React.createElement
39
- ((_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" &&
40
- ((_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") || ( // with babel jsx runtime
41
- ((_h = (_g = node.arguments) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.type) === "Identifier" &&
42
- ((_k = (_j = node.arguments) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.name) === "_Fragment");
43
- }
44
- function traverse(ast, callback) {
45
- for (var _i = 0, _a = Object.keys(ast); _i < _a.length; _i++) {
46
- var key = _a[_i];
47
- if (['end', 'start', 'type'].includes(key)) {
48
- continue;
49
- }
50
- if (Array.isArray(ast[key])) {
51
- for (var nodeIndex in ast[key]) {
52
- ast[key][nodeIndex] = traverse(ast[key][nodeIndex], callback);
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) { return n.local.name === 'jsxRuntime'; }) || node.source.value === 'react/jsx-runtime') {
34
+ return jsx_parser_model_1.ParserImplementations.new;
53
35
  }
54
36
  }
55
- else if (isNode(ast[key])) {
56
- ast[key] = traverse(ast[key], callback);
37
+ }
38
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
39
+ finally {
40
+ try {
41
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
57
42
  }
43
+ finally { if (e_1) throw e_1.error; }
58
44
  }
59
- var result = callback(ast);
60
- return typeof result !== 'undefined' ? result : ast;
61
- }
62
- function createAttributeNode(attr) {
63
- return {
64
- type: 'Property',
65
- method: false,
66
- shorthand: false,
67
- computed: false,
68
- key: {
69
- type: 'Identifier',
70
- name: "\"" + attr + "\""
71
- },
72
- value: {
73
- type: 'Literal',
74
- value: true,
75
- raw: "true"
76
- },
77
- kind: 'init'
78
- };
79
- }
45
+ return implementation;
46
+ };
80
47
  function addHashAttributesToJsxTagsAst(program, attr) {
81
- return traverse(program, function (node) {
82
- var _a;
83
- if (isReactCreateElementCallExpression(node) && !isFragmentCallExpression(node)) {
84
- var arg_1 = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[1];
85
- return __assign(__assign({}, node), { arguments: node === null || node === void 0 ? void 0 : node.arguments.map(function (v, i) {
86
- if (i === 1) {
87
- if (v.type === 'ObjectExpression') {
88
- return __assign(__assign({}, v), { properties: __spreadArray(__spreadArray([], arg_1.properties), [
89
- createAttributeNode(attr)
90
- ]) });
91
- }
92
- else {
93
- return {
94
- type: 'ObjectExpression',
95
- properties: [createAttributeNode(attr)]
96
- };
97
- }
98
- }
99
- return v;
100
- }) });
48
+ // Once in the program, we can determine which parser to use
49
+ if (global.implementation === null) {
50
+ global.implementation = findImplementation(program);
51
+ if (global.implementation === jsx_parser_model_1.ParserImplementations.new) {
52
+ parser = newParser;
53
+ }
54
+ }
55
+ return (0, ast_transformer_1.astTransformer)(program, function (node) {
56
+ if (parser.isNodeReactElement(node) && !parser.isNodeReactFragment(node)) {
57
+ return parser.extendNodeWithAttributes(node, attr);
101
58
  }
102
59
  });
103
60
  }
package/dist/index.d.ts CHANGED
@@ -1,11 +1,37 @@
1
1
  import { FilterPattern } from '@rollup/pluginutils';
2
2
  import type { Plugin } from 'rollup';
3
3
  export interface ReactScopedCssPluginOptions {
4
+ /**
5
+ * Which files should be included and parsed by the plugin
6
+ * Default: undefined
7
+ */
4
8
  include?: FilterPattern;
9
+ /**
10
+ * Which files should be exluded and that should not be parsed by the plugin
11
+ * Default: undefined
12
+ */
5
13
  exclude?: FilterPattern;
14
+ /**
15
+ * If you want to customize the stylesheet file pattern
16
+ * if undefined or '' is passed, all files will be evaluated
17
+ * Default: 'scoped'
18
+ */
6
19
  styleFileSuffix?: string;
20
+ /**
21
+ * If you want to customize the attribute prefix that is added to the jsx elements
22
+ * Default: 'v'
23
+ */
7
24
  hashPrefix?: string;
8
- preProcessors?: string[];
25
+ /**
26
+ * If you want to customize the stylesheet extensions
27
+ * Default: ['scss', 'css', 'sass', 'less']
28
+ */
29
+ styleFileExtensions?: string[];
30
+ /**
31
+ * If you have jsx in other file extensions
32
+ * Default: ['jsx', 'tsx']
33
+ */
34
+ jsxFileExtensions?: string[];
9
35
  }
10
36
  export interface VitePartialPlugin extends Plugin {
11
37
  enforce?: "pre" | "post";
package/dist/index.js CHANGED
@@ -47,11 +47,11 @@ var getHashFromPath = function (filePath) {
47
47
  };
48
48
  var generateHash = function (input, seed) {
49
49
  if (seed === void 0) { seed = 0; }
50
- var hashNum = js_xxhash_1.xxHash32(Buffer.from(input, 'utf8'), seed);
50
+ var hashNum = (0, js_xxhash_1.xxHash32)(Buffer.from(input, 'utf8'), seed);
51
51
  return hashNum.toString(16);
52
52
  };
53
53
  var addAttributesToCss = function (src, fileName, hash) {
54
- var code = component_compiler_utils_1.compileStyle({
54
+ var code = (0, component_compiler_utils_1.compileStyle)({
55
55
  source: src,
56
56
  filename: fileName,
57
57
  id: hash,
@@ -62,11 +62,21 @@ var addAttributesToCss = function (src, fileName, hash) {
62
62
  };
63
63
  function reactScopedCssPlugin(optionsIn) {
64
64
  if (optionsIn === void 0) { optionsIn = {}; }
65
- var options = __assign({ styleFileSuffix: 'scoped', hashPrefix: 'v', preProcessors: ['scss', 'css', 'sass', 'less'] }, optionsIn);
66
- var filter = pluginutils_1.createFilter(options.include, options.exclude);
67
- var scopedCssRegex = new RegExp("." + options.styleFileSuffix + ".(" + options.preProcessors.join('|') + ")$");
68
- var scopedCssInFileRegex = new RegExp("." + options.styleFileSuffix + ".(" + options.preProcessors.join('|') + ")(\"|')");
69
- var jsxRegex = /\.(tsx|jsx)$/;
65
+ var options = __assign({ hashPrefix: 'v', styleFileSuffix: 'scoped', styleFileExtensions: ['scss', 'css', 'sass', 'less'], jsxFileExtensions: ['jsx', 'tsx'] }, optionsIn);
66
+ if (!options.styleFileExtensions || !options.styleFileExtensions.length) {
67
+ throw new Error('You need to provide at least one style file extension');
68
+ }
69
+ if (!options.jsxFileExtensions || !options.jsxFileExtensions.length) {
70
+ throw new Error('You need to provide at least one jsx file extension');
71
+ }
72
+ var filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
73
+ var scopedCssRegex = options.styleFileSuffix
74
+ ? new RegExp(".".concat(options.styleFileSuffix, ".(").concat(options.styleFileExtensions.join('|'), ")$"))
75
+ : new RegExp(".(".concat(options.styleFileExtensions.join('|'), ")$"));
76
+ var scopedCssInFileRegex = options.styleFileSuffix
77
+ ? new RegExp(".".concat(options.styleFileSuffix, ".(").concat(options.styleFileExtensions.join('|'), ")(\"|')"))
78
+ : new RegExp(".(".concat(options.styleFileExtensions.join('|'), ")(\"|')"));
79
+ var jsxRegex = new RegExp(".(".concat(options.jsxFileExtensions.join('|'), ")$"));
70
80
  return [
71
81
  {
72
82
  name: 'rollup-plugin-react-scoped-css:pre',
@@ -76,7 +86,7 @@ function reactScopedCssPlugin(optionsIn) {
76
86
  }
77
87
  if (scopedCssRegex.test(source) && jsxRegex.test(importer)) {
78
88
  var importerHash = generateHash(importer);
79
- var url = path.resolve(path.dirname(importer), source + "?scope=" + importerHash);
89
+ var url = path.resolve(path.dirname(importer), "".concat(source, "?scope=").concat(importerHash));
80
90
  return url;
81
91
  }
82
92
  },
@@ -91,12 +101,12 @@ function reactScopedCssPlugin(optionsIn) {
91
101
  if (scopedCssInFileRegex.test(code)) {
92
102
  var importerHash = generateHash(id);
93
103
  var program = this.parse(code);
94
- var newAst = ast_utils_1.addHashAttributesToJsxTagsAst(program, "data-" + options.hashPrefix + "-" + importerHash);
95
- return escodegen_1.generate(newAst);
104
+ var newAst = (0, ast_utils_1.addHashAttributesToJsxTagsAst)(program, "data-".concat(options.hashPrefix, "-").concat(importerHash));
105
+ return (0, escodegen_1.generate)(newAst);
96
106
  }
97
107
  if (scopedCssRegex.test(getFilenameFromPath(id))) {
98
108
  var importerHash = getHashFromPath(id);
99
- return addAttributesToCss(code, getFilenameFromPath(id), "data-" + options.hashPrefix + "-" + importerHash);
109
+ return addAttributesToCss(code, getFilenameFromPath(id), "data-".concat(options.hashPrefix, "-").concat(importerHash));
100
110
  }
101
111
  }
102
112
  }
@@ -0,0 +1,7 @@
1
+ import { JsxParser } from "./jsx-parser.model";
2
+ export declare class ClassicJsxParser implements JsxParser {
3
+ isNodeReactFragment(node: any): boolean;
4
+ isNodeReactElement(node: any): boolean;
5
+ extendNodeWithAttributes(node: any, attr: string): any;
6
+ private createAttributeNode;
7
+ }