babel-plugin-injectcomponentcode
Advanced tools
+13
-5
| const pluginSyntaxJsx = require("@babel/plugin-syntax-jsx").default; | ||
| const defaultFileNameList = ["/page.tsx", "/page.jsx"]; | ||
| const { glob } = require("glob"); | ||
| module.exports = function ({ types, template }, options) { | ||
| const { importComponentFilePath, importComponentName, isImportDefault = true, needWrapperFileRelativePath = [] } = options ?? {}; | ||
| // 使用 glob 模式 匹配文件 | ||
| // 内置 ignore node_modules | ||
| module.exports = async function ({ types, template }, options) { | ||
| const ignorePathList = Array.isArray(options.globIgnorePath) ? ['node_modules/**',, ...options.globIgnorePath] : options.globIgnorePath; | ||
| const globMachedFileList = await glob(options.globMatchPath , { ignore: ignorePathList}) | ||
| const optionsFileNameSet = new Set(needWrapperFileRelativePath ? [...defaultFileNameList, ...needWrapperFileRelativePath] : needWrapperFileRelativePath); | ||
| const { importComponentFilePath, importComponentName, isImportDefault = true } = options ?? {}; | ||
| const isUseGlob = options.globIgnorePath && options.globMatchPath | ||
| const optionsFileNameSet = new Set(isUseGlob ? globMachedFileList : defaultFileNameList); | ||
| return { | ||
@@ -41,5 +48,6 @@ inherits: pluginSyntaxJsx, | ||
| if (isImportAstNode) { | ||
| const importedPackageName = itembodyChildren.get("source").toString(); | ||
| // const importedPackageName = itembodyChildren.get("source").toString(); | ||
| const importedPackageName = itembodyChildren.get("source").node.value; | ||
| if (importedPackageName === `'${importComponentFilePath}'`) { | ||
| if (importedPackageName === importComponentFilePath) { | ||
| const itemImportSpecifiersList = itembodyChildren.get("specifiers"); | ||
@@ -46,0 +54,0 @@ |
+6
-5
| { | ||
| "name": "babel-plugin-injectcomponentcode", | ||
| "version": "1.0.3", | ||
| "version": "2.0.0", | ||
| "description": "babel plugin react nextjs inject code", | ||
@@ -35,8 +35,9 @@ "private": false, | ||
| }, | ||
| "scripts": {}, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "@babel/plugin-syntax-jsx": "^7.21.4" | ||
| }, | ||
| "scripts": {} | ||
| } | ||
| "@babel/plugin-syntax-jsx": "^7.21.4", | ||
| "glob": "^11.0.0" | ||
| } | ||
| } |
+8
-3
@@ -13,3 +13,4 @@ # babel-plugin-injectcomponentcode | ||
| // babel.config.js | ||
| // 在 next | react 框架中 指定 jsx文件 进行 高阶组件行为 | ||
| // 在 react 框架中 指定 jsx文件 进行 高阶组件行为 | ||
| // 使用 glob 模式匹配 | ||
@@ -20,4 +21,6 @@ // options 配置 | ||
| // isImportDefault(可选参数) -> 是否默认导出(默认是 true) | ||
| // needWrapperFileRelativePath(可选参数) -> 需要被包裹的组件的相对路径 | ||
| // globMatchPath -> 需要被包裹的组件的相对路径 (参数规则同 glob) | ||
| // globIgnorePath -> 需要被忽略的组件的相对路径, 不会匹配 node_modules (参数规则同 glob) | ||
| // 如果没有传递 globMatchPath 和 globIgnorePath, 则会 只匹配 "/page.tsx" | "/page.jsx" 结尾的文件 | ||
| module.exports = { | ||
@@ -28,6 +31,8 @@ ... | ||
| [[ | ||
| injectcomponentcode, { | ||
| babelPluginInjectComponentCode, { | ||
| importComponentFilePath: '@/components/PageWrapper', | ||
| importComponentName: 'PageWrapper', | ||
| isImportDefault: true, | ||
| globIgnorePath: "src/page/Home/index.tsx", | ||
| globMatchPath: ["src/modules/KycRadioGroup/**/*.tsx"], // "src/page/**/*.tsx" | ||
| } | ||
@@ -34,0 +39,0 @@ ]] |
6072
15.31%76
10.14%61
8.93%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added