Skip to content

Commit

Permalink
docs: update unminify docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Nov 26, 2023
1 parent 4ead6e1 commit 2a41c7c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
27 changes: 19 additions & 8 deletions packages/unminify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,19 @@ React Hooks:

```diff
- const th = createContext('light');
- const [e, f] = useState(0);
- const g = useRef(null);
+ const ThContext = createContext('light');

- const [e, f] = useState(0);
+ const [e, setE] = useState(0);

- const g = useRef(null);
+ const gRef = useRef(null);

- const [e, f] = o.useReducer(reducer, initialArg, init?);
+ const [eState, fDispatch] = o.useReducer(reducer, initialArg, init?);

- const Z = o.forwardRef((e, t) => { ... })
+ const Z = o.forwardRef((props, ref) => { ... })
```

### `un-iife`
Expand Down Expand Up @@ -418,6 +426,9 @@ Restore TypeScript enum syntax.
+ var Direction = {
+ Up: "UP",
+ Down: 2,

+ // reverse mapping
+ 2: "Down"
+ }
```

Expand All @@ -443,7 +454,7 @@ Converts object property accesses and array index accesses to destructuring.
+ console.log(t, n, r);
```

Inline reassigned variables.
Inline reassigned temp variables.

```diff
- const a = d;
Expand Down Expand Up @@ -502,10 +513,10 @@ Converts CommonJS's `require` and `module.exports` to ES6's `import` and `export
+ export default foo
```

Note: Please aware that CJS and ESM are not fully compatible, and this transformation is not perfect. We have a comprehensive test suite to ensure the correctness of this transformation, but there are still some edge cases that are not covered. Feel free to open an issue if you find any bugs.
Note: Please aware that CJS and ESM are not fully compatible, and this transformation is not perfect. We have a comprehensive test suite to ensure the correctness of the transformation, but there are still some edge cases that are not covered. Feel free to open an issue if you find any bugs.

Limitations:
- `require(dynamic)` is not supported as ESM does not support dynamic imports. Convert it to `await import()` is not appropriate as it require the whole execution context to be async.
- `require(dynamic)` is not supported as ESM does not support dynamic imports. Convert it to `await import()` is not appropriate as it require the whole execution context to be **async**.
- Some packages require `import * as name from 'package'` instead of `import name from 'package'`. We cannot detect this automatically, so you might need to fix it manually.
- Currently, it won't aware the exports format of other files generated by our `unpacker`. PRs are welcome.

Expand Down Expand Up @@ -565,7 +576,7 @@ Currently, this transformation only supports output from **TypeScript**.

And it does not handled control flow properly, as it needs graph analysis.

Please aware there are tons of edge cases that are not covered by this rule.
Please aware there are **tons of edge cases** that are not covered by this rule.

```diff
-function func() {
Expand Down Expand Up @@ -601,7 +612,7 @@ Please aware there are tons of edge cases that are not covered by this rule.

### `un-jsx`

Converts `React.createElement` to JSX.
Converts `React.createElement` and `jsxRuntime.jsx` back to JSX.

```diff
- React.createElement("div", { className: "title" }, "Hello World");
Expand All @@ -620,7 +631,7 @@ Pass `pragmaFrag` option to specify the JSX fragment pragma.
+ </>
```

It will also automatically guess the component name from the `displayName` property.
It will automatically guess the component name from the `displayName` property.

```diff
- var S = /*#__PURE__*/React.createElement("div", null);
Expand Down
18 changes: 14 additions & 4 deletions packages/unminify/src/transformations/smart-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,21 @@ const MINIFIED_IDENTIFIER_THRESHOLD = 2
* ->
* const uContext = o.createContext(u);
*
* const d = o.useRef(u);
* ->
* const uRef = o.useRef(u);
*
* const [e, f] = o.useState(0);
* ->
* const [e, SetE] = o.useState(0);
*
* const [e, f] = o.useReducer(reducer, initialArg, init?);
* ->
* const [eState, fDispatch] = o.useReducer(reducer, initialArg, init?);
*
* const Z = o.forwardRef((e, t) => { ... })
* ->
* const Z = o.forwardRef((props, ref) => { ... })
*/
export const transformAST: ASTTransformation = (context) => {
const { root, j } = context
Expand Down Expand Up @@ -264,11 +276,9 @@ function handleReactRename(j: JSCodeshift, root: Collection) {
})

/**
* const Z = o.forwardRef((e, t) => {
* })
* const Z = o.forwardRef((e, t) => { ... })
* ->
* const Z = o.forwardRef((props, ref) => {
* })
* const Z = o.forwardRef((props, ref) => { ... })
*
* @see https://react.dev/reference/react/forwardRef
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/unminify/src/transformations/un-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const DEFAULT_PRAGMA_FRAG_CANDIDATES = [
// ]

/**
* Converts `React.createElement` to JSX.
* Converts `React.createElement` and `jsxRuntime.jsx` back to JSX.
*/
export const transformAST: ASTTransformation<Params> = (context, params) => {
const { root, j } = context
Expand Down

0 comments on commit 2a41c7c

Please sign in to comment.