-
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.
- Loading branch information
1 parent
58712a8
commit a163544
Showing
6 changed files
with
124 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,45 @@ | ||
import * as babel from '@babel/core'; | ||
import { basename } from 'node:path'; | ||
import { Plugin } from 'vite'; | ||
import { astAddAutoNaming } from './babel/astAutoNaming'; | ||
import { parseModule } from 'magicast'; | ||
import { | ||
babelAstAddAutoNaming, | ||
BabelAstAddAutoNamingOptions, | ||
} from './babel/astAutoNaming'; | ||
import { transformAsync } from './babel/transform'; | ||
|
||
interface StatebuilderAutonamingOptions { | ||
transformStores: string[]; | ||
} | ||
export function autoKey(options: StatebuilderAutonamingOptions): Plugin { | ||
const { transformStores } = options; | ||
const findStoresTransformRegexp = new RegExp(transformStores.join('|')); | ||
return { | ||
name: 'statebuilder:autokey', | ||
transform(code, id, options) { | ||
if (!code.includes('statebuilder')) { | ||
async transform(code, id, options) { | ||
if ( | ||
!code.includes('statebuilder') || | ||
!findStoresTransformRegexp.test(code) | ||
) { | ||
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(); | ||
} | ||
const result = await transformAsync(id, code, [ | ||
[ | ||
babelAstAddAutoNaming, | ||
{ | ||
filterStores: (functionName) => | ||
transformStores.includes(functionName), | ||
} as BabelAstAddAutoNamingOptions, | ||
], | ||
]); | ||
|
||
if (!result) { | ||
return; | ||
} | ||
|
||
return { | ||
code: result.code ?? '', | ||
map: result.map, | ||
}; | ||
}, | ||
}; | ||
} |
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,27 @@ | ||
import * as babel from '@babel/core'; | ||
import { basename } from 'node:path'; | ||
|
||
export function transformAsync( | ||
moduleId: string, | ||
code: string, | ||
customPlugins: babel.TransformOptions['plugins'], | ||
) { | ||
const plugins: NonNullable< | ||
NonNullable<babel.TransformOptions['parserOpts']>['plugins'] | ||
> = ['jsx']; | ||
if (/\.[mc]?tsx?$/i.test(moduleId)) { | ||
plugins.push('typescript'); | ||
} | ||
return babel.transformAsync(code, { | ||
plugins: customPlugins, | ||
parserOpts: { | ||
plugins, | ||
}, | ||
filename: basename(moduleId), | ||
ast: false, | ||
sourceMaps: true, | ||
configFile: false, | ||
babelrc: false, | ||
sourceFileName: moduleId, | ||
}); | ||
} |
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