Skip to content

Commit

Permalink
feat(plugin-react): separate hook context from Babel options
Browse files Browse the repository at this point in the history
  • Loading branch information
cyco130 committed May 20, 2022
1 parent c3e8c5d commit 7d3fcad
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export type BabelOptions = Omit<
* an `api.reactBabel` method.
*/
export interface ReactBabelOptions extends BabelOptions {
ssr?: boolean
file: string
plugins: Extract<BabelOptions['plugins'], any[]>
presets: Extract<BabelOptions['presets'], any[]>
overrides: Extract<BabelOptions['overrides'], any[]>
Expand All @@ -72,10 +70,13 @@ export interface ReactBabelOptions extends BabelOptions {
}

type ReactBabelHook = (
options: ReactBabelOptions,
babelConfig: ReactBabelOptions,
context: ReactBabelHookContext,
config: ResolvedConfig
) => void

type ReactBabelHookContext = { ssr: boolean; file: string }

declare module 'vite' {
export interface Plugin {
api?: {
Expand All @@ -95,7 +96,10 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
let projectRoot = process.cwd()
let skipFastRefresh = opts.fastRefresh === false
let skipReactImport = false
let runPluginOverrides = (options: ReactBabelOptions) => false
let runPluginOverrides = (
options: ReactBabelOptions,
context: ReactBabelHookContext
) => false
let staticBabelOptions: ReactBabelOptions | undefined

const useAutomaticRuntime = opts.jsxRuntime !== 'classic'
Expand Down Expand Up @@ -142,14 +146,14 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
)
})

runPluginOverrides = (babelOptions) => {
runPluginOverrides = (babelOptions, context) => {
const hooks = config.plugins
.map((plugin) => plugin.api?.reactBabel)
.filter(Boolean) as ReactBabelHook[]

if (hooks.length > 0) {
return (runPluginOverrides = (babelOptions) => {
hooks.forEach((hook) => hook(babelOptions, config))
hooks.forEach((hook) => hook(babelOptions, context, config))
return true
})(babelOptions)
}
Expand All @@ -172,19 +176,19 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
const isProjectFile =
!isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/'))

let reactBabelOptions = staticBabelOptions
let babelOptions = staticBabelOptions
if (typeof opts.babel === 'function') {
const rawOptions = opts.babel(id, { ssr })
reactBabelOptions = createBabelOptions(id, rawOptions, ssr)
runPluginOverrides(reactBabelOptions)
} else if (!reactBabelOptions) {
reactBabelOptions = createBabelOptions(id, opts.babel, ssr)
if (!runPluginOverrides(reactBabelOptions)) {
staticBabelOptions = reactBabelOptions
babelOptions = createBabelOptions(rawOptions)
runPluginOverrides(babelOptions, { ssr, file: id })
} else if (!babelOptions) {
babelOptions = createBabelOptions(opts.babel)
if (!runPluginOverrides(babelOptions, { ssr, file: id })) {
staticBabelOptions = babelOptions
}
}

const plugins = isProjectFile ? [...reactBabelOptions.plugins] : []
const plugins = isProjectFile ? [...babelOptions.plugins] : []

let useFastRefresh = false
if (!skipFastRefresh && !ssr && !isNodeModules) {
Expand Down Expand Up @@ -251,15 +255,15 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
// module, including node_modules and linked packages.
const shouldSkip =
!plugins.length &&
!reactBabelOptions.configFile &&
!(isProjectFile && reactBabelOptions.babelrc)
!babelOptions.configFile &&
!(isProjectFile && babelOptions.babelrc)

if (shouldSkip) {
return // Avoid parsing if no plugins exist.
}

const parserPlugins: typeof reactBabelOptions.parserOpts.plugins = [
...reactBabelOptions.parserOpts.plugins,
const parserPlugins: typeof babelOptions.parserOpts.plugins = [
...babelOptions.parserOpts.plugins,
'importMeta',
// This plugin is applied before esbuild transforms the code,
// so we need to enable some stage 3 syntax that is supported in
Expand All @@ -283,7 +287,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
: babel.transformAsync.bind(babel, code)

const isReasonReact = extension.endsWith('.bs.js')
const { ssr: _ssr, file: _file, ...babelOptions } = reactBabelOptions
const result = await transformAsync({
...babelOptions,
ast: !isReasonReact,
Expand Down Expand Up @@ -392,14 +395,8 @@ function loadPlugin(path: string): Promise<any> {
return import(path).then((module) => module.default || module)
}

function createBabelOptions(
file: string,
rawOptions?: BabelOptions,
ssr?: boolean
) {
function createBabelOptions(rawOptions?: BabelOptions) {
const babelOptions = {
ssr,
file,
babelrc: false,
configFile: false,
...rawOptions
Expand Down

0 comments on commit 7d3fcad

Please sign in to comment.