-
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.
add stateprovider directive babel plugin
- Loading branch information
1 parent
eb02b7d
commit c271d45
Showing
9 changed files
with
1,487 additions
and
1,852 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,5 @@ | |
"~/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["./**/*.ts"], | ||
"exclude": ["dist", "node_modules"] | ||
} |
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
82 changes: 82 additions & 0 deletions
82
packages/state/vite/internal/replaceStateProviderDirective.ts
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,82 @@ | ||
import * as t from '@babel/types'; | ||
import type * as babel from '@babel/core'; | ||
|
||
export function babelReplaceStateProviderDirective(): babel.PluginObj<any> { | ||
return { | ||
name: 'statebuilder:stateprovider-directive', | ||
visitor: { | ||
Program(path) { | ||
let hasStateProviderDirective = false; | ||
let hasStateProviderImport = false; | ||
|
||
path.traverse({ | ||
ImportDeclaration(importPath) { | ||
if ( | ||
importPath.node.source.value === 'statebuilder' && | ||
importPath.node.specifiers.some( | ||
(specifier) => | ||
'imported' in specifier && | ||
'name' in specifier.imported && | ||
specifier.imported.name === 'StateProvider', | ||
) | ||
) { | ||
hasStateProviderImport = true; | ||
importPath.stop(); | ||
} | ||
}, | ||
FunctionDeclaration(path) { | ||
const bodyDirectives = path.node.body.directives || []; | ||
const stateProviderDirective = bodyDirectives.findIndex( | ||
(directive) => directive.value.value === 'use stateprovider', | ||
); | ||
if (stateProviderDirective !== -1) { | ||
hasStateProviderDirective = true; | ||
const originalName = path.node.id!.name; | ||
const wrappedName = `$Original${originalName}`; | ||
path.node.id = t.identifier(wrappedName); | ||
path.node.body.directives = path.node.body.directives.filter( | ||
(directive) => directive.value.value !== 'use stateprovider', | ||
); | ||
const wrappedComponent = t.functionDeclaration( | ||
t.identifier(originalName), | ||
[], | ||
t.blockStatement([ | ||
t.returnStatement( | ||
t.jsxElement( | ||
t.jsxOpeningElement(t.jsxIdentifier('StateProvider'), []), | ||
t.jsxClosingElement(t.jsxIdentifier('StateProvider')), | ||
[ | ||
t.jsxElement( | ||
t.jsxOpeningElement(t.jsxIdentifier(wrappedName), []), | ||
t.jsxClosingElement(t.jsxIdentifier(wrappedName)), | ||
[], | ||
true, | ||
), | ||
], | ||
true, | ||
), | ||
), | ||
]), | ||
); | ||
|
||
path.insertAfter(t.exportNamedDeclaration(wrappedComponent)); | ||
} | ||
}, | ||
}); | ||
|
||
if (hasStateProviderDirective && !hasStateProviderImport) { | ||
const importDeclaration = t.importDeclaration( | ||
[ | ||
t.importSpecifier( | ||
t.identifier('StateProvider'), | ||
t.identifier('StateProvider'), | ||
), | ||
], | ||
t.stringLiteral('statebuilder'), | ||
); | ||
path.unshiftContainer('body', importDeclaration); | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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,46 @@ | ||
import { Plugin } from 'vite'; | ||
import * as magicast from 'magicast'; | ||
import { Program, traverseFast } from '@babel/types'; | ||
import * as babel from '@babel/core'; | ||
import { basename } from 'node:path'; | ||
import * as t from '@babel/types'; | ||
import { babelReplaceStateProviderDirective } from './internal/replaceStateProviderDirective'; | ||
|
||
export function stateProviderDirective(): Plugin { | ||
return { | ||
name: 'statebuilder:stateprovider-directive', | ||
enforce: 'pre', | ||
async transform(code, id, options) { | ||
if (code.indexOf('use stateprovider') === -1) { | ||
return; | ||
} | ||
const plugins: NonNullable< | ||
NonNullable<babel.TransformOptions['parserOpts']>['plugins'] | ||
> = ['jsx']; | ||
if (/\.[mc]?tsx?$/i.test(id)) { | ||
plugins.push('typescript'); | ||
} | ||
|
||
const result = await babel.transformAsync(code, { | ||
plugins: [[babelReplaceStateProviderDirective]], | ||
parserOpts: { | ||
plugins, | ||
}, | ||
filename: basename(id), | ||
ast: false, | ||
sourceMaps: true, | ||
configFile: false, | ||
babelrc: false, | ||
sourceFileName: id, | ||
}); | ||
|
||
if (result) { | ||
console.log(result.code); | ||
return { | ||
code: result.code || '', | ||
map: result.map, | ||
}; | ||
} | ||
}, | ||
}; | ||
} |
Oops, something went wrong.