Skip to content

Commit 480fbb1

Browse files
committed
Fixed Backlist and Whitelist & Fixed Incorrect Content Finding & Added Experimental Feature
Experimental Feature: obfuscateForwardComponentJs
1 parent 8643ded commit 480fbb1

File tree

4 files changed

+295
-48
lines changed

4 files changed

+295
-48
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Project start on 30-10-2023
1111
This version is deeply inspired by [PostCSS-Obfuscator](https://github.com/n4j1Br4ch1D/postcss-obfuscator). Shout out to [n4j1Br4ch1D](https://github.com/n4j1Br4ch1D) for creating such a great package and thank you [tremor](https://github.com/tremorlabs) for sponsoring this project.
1212

1313
#### Changes:
14-
- Support partially obfuscation
14+
- Support basic partially obfuscation
1515
- Support TailwindCSS Dark Mode
1616
- New configuration file `next-css-obfuscator.config.cjs`
1717
- More configuration options
@@ -334,7 +334,9 @@ Your convertion table may be messed up. Try to delete the `classConversionJsonFo
334334
335335
## 🐛 Known Issues
336336
337-
- N/A
337+
- Partially obfuscation
338+
- Not work with complex component. (eg. A component with children components)
339+
- Reason: The obfuscation marker can't locate the correct code block to obfuscate.
338340
339341
## 💖 Sponsors
340342

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-css-obfuscator",
3-
"version": "2.0.4",
3+
"version": "2.0.6",
44
"description": "A temporary solution for using postcss-obfuscator in Next.js.",
55
"main": "dist/index.js",
66
"type": "commonjs",

src/utils.test.ts

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import css from "css";
33
import {
44
copyCssData, findContentBetweenMarker,
55
findHtmlTagContentsByClass, getFilenameFromPath,
6-
extractClassFromSelector
6+
extractClassFromSelector, searchForwardComponent
77
} from "./utils";
88

99
const testCss = `
@@ -453,3 +453,125 @@ describe("extractClassFromSelector", () => {
453453
});
454454

455455
});
456+
457+
//! ================================
458+
//! searchForwardComponent
459+
//! ================================
460+
461+
describe("searchForwardComponent", () => {
462+
463+
test("should return component name when jsx format is correct", () => {
464+
// Arrange
465+
const content = `const element = o.jsx(ComponentName, {data: dataValue, index: "date"});`;
466+
467+
// Act
468+
const result = searchForwardComponent(content);
469+
470+
// Assert
471+
expect(result).toEqual(["ComponentName"]);
472+
});
473+
474+
test("should return multiple component names for multiple matches", () => {
475+
// Arrange
476+
const content = `o.jsx(FirstComponent, props); o.jsx(SecondComponent, otherProps);`;
477+
478+
// Act
479+
const result = searchForwardComponent(content);
480+
481+
// Assert
482+
expect(result).toEqual(["FirstComponent", "SecondComponent"]);
483+
});
484+
485+
test("should return an empty array when no component name is found", () => {
486+
// Arrange
487+
const content = `o.jsx("h1", {data: dataValue, index: "date"});`;
488+
489+
// Act
490+
const result = searchForwardComponent(content);
491+
492+
// Assert
493+
expect(result).toEqual([]);
494+
});
495+
496+
test("should return an empty array when content is empty", () => {
497+
// Arrange
498+
const content = "";
499+
500+
// Act
501+
const result = searchForwardComponent(content);
502+
503+
// Assert
504+
expect(result).toEqual([]);
505+
});
506+
507+
test("should return an empty array when jsx is not used", () => {
508+
// Arrange
509+
const content = `const element = React.createElement("div", null, "Hello World");`;
510+
511+
// Act
512+
const result = searchForwardComponent(content);
513+
514+
// Assert
515+
expect(result).toEqual([]);
516+
});
517+
518+
test("should handle special characters in component names", () => {
519+
// Arrange
520+
const content = `o.jsx($Comp_1, props); o.jsx(_Comp$2, otherProps);`;
521+
522+
// Act
523+
const result = searchForwardComponent(content);
524+
525+
// Assert
526+
expect(result).toEqual(["$Comp_1", "_Comp$2"]);
527+
});
528+
529+
test("should not return component names when they are quoted", () => {
530+
// Arrange
531+
const content = `o.jsx("ComponentName", props); o.jsx('AnotherComponent', otherProps);`;
532+
533+
// Act
534+
const result = searchForwardComponent(content);
535+
536+
// Assert
537+
expect(result).toEqual([]);
538+
});
539+
540+
test("should return component names when they are followed by a brace", () => {
541+
// Arrange
542+
const content = `o.jsx(ComponentName, {props: true});`;
543+
544+
// Act
545+
const result = searchForwardComponent(content);
546+
547+
// Assert
548+
expect(result).toEqual(["ComponentName"]);
549+
});
550+
551+
test("should handle content with line breaks and multiple jsx calls", () => {
552+
// Arrange
553+
const content = `
554+
o.jsx(FirstComponent, {data: dataValue});
555+
o.jsx(SecondComponent, {index: "date"});
556+
o.jsx(ThirdComponent, {flag: true});
557+
`;
558+
559+
// Act
560+
const result = searchForwardComponent(content);
561+
562+
// Assert
563+
expect(result).toEqual(["FirstComponent", "SecondComponent", "ThirdComponent"]);
564+
});
565+
566+
test("should handle content with nested jsx calls", () => {
567+
// Arrange
568+
const content = `o.jsx(ParentComponent, {children: o.jsx(ChildComponent, {})})`;
569+
570+
// Act
571+
const result = searchForwardComponent(content);
572+
573+
// Assert
574+
expect(result).toEqual(["ParentComponent", "ChildComponent"]);
575+
});
576+
577+
});

0 commit comments

Comments
 (0)