-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom store key and add auto key feature via plugin
- Loading branch information
1 parent
b563c3d
commit b86c154
Showing
10 changed files
with
293 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"paths": { | ||
"~/*": [ | ||
"./src/*" | ||
] | ||
"~/*": ["./src/*"] | ||
} | ||
}, | ||
"include": [ | ||
"./src", | ||
"./test" | ||
] | ||
"include": ["./src", "./test"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Plugin } from 'vite'; | ||
import { astAddAutoNaming } from './internal/astAutoNaming'; | ||
import { parseModule } from 'magicast'; | ||
|
||
interface StatebuilderAutonamingOptions { | ||
transformStores: string[]; | ||
} | ||
export function autoKey(options: StatebuilderAutonamingOptions): Plugin { | ||
const { transformStores } = options; | ||
return { | ||
name: 'statebuilder:autokey', | ||
transform(code, id, options) { | ||
if (!code.includes('statebuilder')) { | ||
return; | ||
} | ||
const findStoresTransformRegexp = new RegExp(transformStores.join('|')); | ||
if (findStoresTransformRegexp.test(code)) { | ||
const module = parseModule(code); | ||
const result = astAddAutoNaming(module.$ast, (functionName) => | ||
transformStores.includes(functionName), | ||
); | ||
if (result) { | ||
return module.generate(); | ||
} | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as t from '@babel/types'; | ||
|
||
export function astAddAutoNaming( | ||
program: t.Node, | ||
filterStores: (name: string) => boolean, | ||
): boolean { | ||
if (program.type !== 'Program') { | ||
return false; | ||
} | ||
let modify = false; | ||
for (const statement of program.body) { | ||
t.traverse(statement, (node, ancestors) => { | ||
if (t.isCallExpression(node)) { | ||
if (t.isIdentifier(node.callee) && filterStores(node.callee.name)) { | ||
ancestorsLoop: { | ||
let variableName: string | null = null; | ||
for (const ancestor of ancestors) { | ||
if ( | ||
t.isVariableDeclarator(ancestor.node) && | ||
t.isIdentifier(ancestor.node.id) | ||
) { | ||
variableName = ancestor.node.id.name; | ||
modify = true; | ||
const storeOptions = t.objectExpression([ | ||
createNameProperty(variableName!), | ||
]); | ||
node.arguments.push(storeOptions); | ||
break ancestorsLoop; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return modify; | ||
} | ||
|
||
function createNameProperty(variableName: string) { | ||
return t.objectProperty(t.identifier('key'), t.stringLiteral(variableName)); | ||
} |
Oops, something went wrong.