-
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.
fix: plugin-react 跳过node_modules下的编译导致未标识Island组件
- Loading branch information
Showing
4 changed files
with
141 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
import { TransformOptions, transformAsync } from '@babel/core' | ||
import babelPluginIsland from '../babel-plugin-island' | ||
import type { Plugin } from 'vite' | ||
|
||
const ISLAND_FILE_REGEX = (name) => name.endsWith('x') // island组件可能存在的文件.jsx | .tsx | .mdx | ||
const ISLAND_CODE_REGEX = /(?<= )__island/ | ||
|
||
export function pluginTransformIslands(): Plugin { | ||
return { | ||
name: 'vite-plugin-transform-islands', | ||
apply: 'build', | ||
enforce: 'pre', // 需要最先处理一次 | ||
async transform(code, id) { | ||
if (ISLAND_FILE_REGEX(id) && ISLAND_CODE_REGEX.test(code)) { | ||
const res = await transformIslands(code, id) | ||
return res | ||
} | ||
}, | ||
} | ||
} | ||
|
||
async function transformIslands( | ||
code: string, | ||
filename: string | ||
): Promise<string> { | ||
const babelOptions: TransformOptions = { | ||
filename, | ||
presets: ['@babel/preset-typescript'], | ||
plugins: [babelPluginIsland], | ||
} | ||
return transformAsync(code, babelOptions) | ||
} |
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