Skip to content

Commit

Permalink
Change preset config
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed May 16, 2023
1 parent ab82e74 commit d14ccd9
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 311 deletions.
157 changes: 84 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,88 +468,99 @@ Here's an example preset:
{
// This boolean is to choose if the compiler
// should optimize JSX or not. Defaults to `true`
"shouldOptimizeJSX": false,
// Required. This is used to construct a RegExp
// to match "components". This is done by testing
// either the component's function name or its
// variable name (if declared as a variable)
"componentFilter": {
"source": "^[A-Z]",
"flags": "",
},
// Optional, this has two purposes:
// 1. To match potential function that are hooks
// so that the compiler can also transform it.
// 2. Forces the hook call to follow hook rules,
// since normal function calls are memoized.
"hookFilter": {
"source": "^use[A-Z]",
"flags": "",
},
// This is used to construct the cache
// the information provided is going to be used
// as to where the hook is going to be imported.
memo: {
// Name of the hook
name: 'useMemo',
// What module source does it come from
source: 'react',
// Kind of import (named or default)
kind: 'named',
},
// This defines which hooks are special
// Take note that by default, hooks that match
// the hook filter already follows the hook rules,
// however this configuration is to optimize
// known hooks.
// There's 3 types of hooks it tracks:
// - memo: a hook that resembles `useMemo`
// - callback: a hook that resembles `useCallback`
// - effect: a hook that resembles `useEffect`
// - ref: a hook that resembles `useRef`
//
// The definition is almost the same as the `memo` config
// but you need to define the kind of the hook
hooks: [
{
type: 'ref',
name: 'useRef',
source: 'react',
kind: 'named',
},
{
type: 'memo',
name: 'useMemo',
source: 'react',
kind: 'named',
shouldOptimizeJSX: false,
filters: {
// Required. This is used to construct a RegExp
// to match "components". This is done by testing
// either the component's function name or its
// variable name (if declared as a variable)
component: {
"source": "^[A-Z]",
"flags": "",
},
{
type: 'callback',
name: 'useCallback',
source: 'react',
kind: 'named',
},
{
type: 'effect',
name: 'useEffect',
source: 'react',
kind: 'named',
// Optional, this has two purposes:
// 1. To match potential function that are hooks
// so that the compiler can also transform it.
// 2. Forces the hook call to follow hook rules,
// since normal function calls are memoized.
hook: {
"source": "^use[A-Z]",
"flags": "",
},
],
// This is used to match higher-order components.
// Same configuration as `memo` config.
hocs: [
{
name: 'forwardRef',
},
runtime: {
// This is used to construct the cache
// the information provided is going to be used
// as to where the hook is going to be imported.
useMemo: {
// Name of the hook
name: 'useMemo',
// What module source does it come from
source: 'react',
// Kind of import (named or default)
kind: 'named',
},
{
memo: {
name: 'memo',
source: 'react',
kind: 'named',
},
],
},
imports: {
// This defines which hooks are special
// Take note that by default, hooks that match
// the hook filter already follows the hook rules,
// however this configuration is to optimize
// known hooks.
// There's 3 types of hooks it tracks:
// - memo: a hook that resembles `useMemo`
// - callback: a hook that resembles `useCallback`
// - effect: a hook that resembles `useEffect`
// - ref: a hook that resembles `useRef`
//
// The definition is almost the same as the `memo` config
// but you need to define the kind of the hook
hooks: [
{
type: 'ref',
name: 'useRef',
source: 'react',
kind: 'named',
},
{
type: 'memo',
name: 'useMemo',
source: 'react',
kind: 'named',
},
{
type: 'callback',
name: 'useCallback',
source: 'react',
kind: 'named',
},
{
type: 'effect',
name: 'useEffect',
source: 'react',
kind: 'named',
},
],
// This is used to match higher-order components.
// Same configuration as `memo` config.
hocs: [
{
name: 'forwardRef',
source: 'react',
kind: 'named',
},
{
name: 'memo',
source: 'react',
kind: 'named',
},
],
}
}
```
Expand Down
157 changes: 84 additions & 73 deletions packages/forgetti/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,88 +468,99 @@ Here's an example preset:
{
// This boolean is to choose if the compiler
// should optimize JSX or not. Defaults to `true`
"shouldOptimizeJSX": false,
// Required. This is used to construct a RegExp
// to match "components". This is done by testing
// either the component's function name or its
// variable name (if declared as a variable)
"componentFilter": {
"source": "^[A-Z]",
"flags": "",
},
// Optional, this has two purposes:
// 1. To match potential function that are hooks
// so that the compiler can also transform it.
// 2. Forces the hook call to follow hook rules,
// since normal function calls are memoized.
"hookFilter": {
"source": "^use[A-Z]",
"flags": "",
},
// This is used to construct the cache
// the information provided is going to be used
// as to where the hook is going to be imported.
memo: {
// Name of the hook
name: 'useMemo',
// What module source does it come from
source: 'react',
// Kind of import (named or default)
kind: 'named',
},
// This defines which hooks are special
// Take note that by default, hooks that match
// the hook filter already follows the hook rules,
// however this configuration is to optimize
// known hooks.
// There's 3 types of hooks it tracks:
// - memo: a hook that resembles `useMemo`
// - callback: a hook that resembles `useCallback`
// - effect: a hook that resembles `useEffect`
// - ref: a hook that resembles `useRef`
//
// The definition is almost the same as the `memo` config
// but you need to define the kind of the hook
hooks: [
{
type: 'ref',
name: 'useRef',
source: 'react',
kind: 'named',
},
{
type: 'memo',
name: 'useMemo',
source: 'react',
kind: 'named',
shouldOptimizeJSX: false,
filters: {
// Required. This is used to construct a RegExp
// to match "components". This is done by testing
// either the component's function name or its
// variable name (if declared as a variable)
component: {
"source": "^[A-Z]",
"flags": "",
},
{
type: 'callback',
name: 'useCallback',
source: 'react',
kind: 'named',
},
{
type: 'effect',
name: 'useEffect',
source: 'react',
kind: 'named',
// Optional, this has two purposes:
// 1. To match potential function that are hooks
// so that the compiler can also transform it.
// 2. Forces the hook call to follow hook rules,
// since normal function calls are memoized.
hook: {
"source": "^use[A-Z]",
"flags": "",
},
],
// This is used to match higher-order components.
// Same configuration as `memo` config.
hocs: [
{
name: 'forwardRef',
},
runtime: {
// This is used to construct the cache
// the information provided is going to be used
// as to where the hook is going to be imported.
useMemo: {
// Name of the hook
name: 'useMemo',
// What module source does it come from
source: 'react',
// Kind of import (named or default)
kind: 'named',
},
{
memo: {
name: 'memo',
source: 'react',
kind: 'named',
},
],
},
imports: {
// This defines which hooks are special
// Take note that by default, hooks that match
// the hook filter already follows the hook rules,
// however this configuration is to optimize
// known hooks.
// There's 3 types of hooks it tracks:
// - memo: a hook that resembles `useMemo`
// - callback: a hook that resembles `useCallback`
// - effect: a hook that resembles `useEffect`
// - ref: a hook that resembles `useRef`
//
// The definition is almost the same as the `memo` config
// but you need to define the kind of the hook
hooks: [
{
type: 'ref',
name: 'useRef',
source: 'react',
kind: 'named',
},
{
type: 'memo',
name: 'useMemo',
source: 'react',
kind: 'named',
},
{
type: 'callback',
name: 'useCallback',
source: 'react',
kind: 'named',
},
{
type: 'effect',
name: 'useEffect',
source: 'react',
kind: 'named',
},
],
// This is used to match higher-order components.
// Same configuration as `memo` config.
hocs: [
{
name: 'forwardRef',
source: 'react',
kind: 'named',
},
{
name: 'memo',
source: 'react',
kind: 'named',
},
],
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions packages/forgetti/src/core/get-hook-call-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getHookCallType(
if (trueID) {
const binding = path.scope.getBindingIdentifier(trueID.name);
if (binding) {
const registration = ctx.registrations.hooks.get(binding);
const registration = ctx.registrations.named.hooks.get(binding);
if (registration) {
return registration.type;
}
Expand All @@ -41,7 +41,7 @@ export function getHookCallType(
if (obj) {
const binding = path.scope.getBindingIdentifier(obj.name);
if (binding) {
const registrations = ctx.registrations.hooksNamespaces.get(binding);
const registrations = ctx.registrations.namespace.hooks.get(binding);
if (registrations) {
let registration: typeof registrations[0];
for (let i = 0, len = registrations.length; i < len; i += 1) {
Expand Down
4 changes: 2 additions & 2 deletions packages/forgetti/src/core/get-import-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export default function getImportIdentifier(
definition: ImportRegistration,
): t.Identifier {
const target = `${definition.source}[${definition.name}]`;
const current = ctx.hooks.get(target);
const current = ctx.imports.get(target);
if (current) {
return current;
}
const newID = (definition.kind === 'named')
? addNamed(path, definition.name, definition.source)
: addDefault(path, definition.source);
ctx.hooks.set(target, newID);
ctx.imports.set(target, newID);
return newID;
}
2 changes: 1 addition & 1 deletion packages/forgetti/src/core/optimizer-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class OptimizerScope {
getImportIdentifier(
this.ctx,
this.path,
this.ctx.preset.memo,
this.ctx.preset.runtime.useMemo,
),
t.numericLiteral(this.indeces),
],
Expand Down
Loading

0 comments on commit d14ccd9

Please sign in to comment.