babel-plugin-injectcomponentcode
Advanced tools
+31
-16
@@ -9,9 +9,9 @@ const pluginSyntaxJsx = require("@babel/plugin-syntax-jsx").default; | ||
| module.exports = async function ({ types, template }, options) { | ||
| const { importComponentFilePath, importComponentName, isImportDefault = true, globMatchPath, globIgnorePath = [] } = options ?? {}; | ||
| const ignorePathList = Array.isArray(globIgnorePath) ? ['node_modules/**', ...globIgnorePath] : globIgnorePath; | ||
| const globMachedFileList = await glob(globMatchPath , { ignore: ignorePathList}) | ||
| const ignorePathList = Array.isArray(globIgnorePath) ? ["node_modules/**", ...globIgnorePath] : globIgnorePath; | ||
| const globMachedFileList = await glob(globMatchPath, { ignore: ignorePathList }); | ||
| const isUseGlob = globIgnorePath && globMatchPath | ||
| const isUseGlob = globIgnorePath && globMatchPath; | ||
@@ -33,2 +33,3 @@ const optionsFileNameSet = new Set(isUseGlob ? globMachedFileList : defaultFileNameList); | ||
| let bodyAstPath; | ||
| if (matchFileFlag) { | ||
@@ -38,6 +39,8 @@ let needBabelPluginImported = true; | ||
| const programAstPath = astPath.find((parentPath) => parentPath.isProgram()); | ||
| let oldExportNodeVal = ""; | ||
| let oldExportNode = ""; | ||
| let oldExportNodeFnVal = ""; | ||
| let oldExportNodeEmitExport = ""; | ||
| let oldExportAstNode = ""; | ||
| let isFnASt = false; | ||
| const bodyAstPath = programAstPath.get("body"); | ||
| bodyAstPath = programAstPath.get("body"); | ||
| for (const itembodyChildren of bodyAstPath) { | ||
@@ -48,4 +51,12 @@ let isImportAstNode = types.isImportDeclaration(itembodyChildren); | ||
| if (isExportAstNode) { | ||
| oldExportNodeVal = itembodyChildren.get("declaration").toString(); | ||
| oldExportNode = itembodyChildren; | ||
| oldExportNodeEmitExport = itembodyChildren.get("declaration"); | ||
| isFnASt = types.isFunctionDeclaration(oldExportNodeEmitExport); | ||
| if (isFnASt) { | ||
| oldExportNodeFnVal = itembodyChildren.get("declaration").get("id").toString(); | ||
| } else { | ||
| oldExportNodeFnVal = itembodyChildren.get("declaration").toString(); | ||
| } | ||
| oldExportAstNode = itembodyChildren; | ||
| } | ||
@@ -55,3 +66,3 @@ | ||
| // const importedPackageName = itembodyChildren.get("source").toString(); | ||
| const importedPackageName = itembodyChildren.get("source").node.value; | ||
| const importedPackageName = itembodyChildren.get("source").node.value; | ||
@@ -86,13 +97,17 @@ if (importedPackageName === importComponentFilePath) { | ||
| // wrapper | ||
| if (oldExportNodeVal) { | ||
| if (oldExportNodeFnVal) { | ||
| const newExportNodeTemplateFn = template("export default IMPORTFNPACKAGE(OLDNOE)"); | ||
| let replaceOldVal = oldExportNodeVal.replace(/\s*__source=\{{1}[^}]*\}}{1}\s*/g, " ").replace(/\s*__self={this}\s*/g, " "); | ||
| const newNode = newExportNodeTemplateFn({ | ||
| IMPORTFNPACKAGE: types.identifier(importComponentName), | ||
| OLDNOE: replaceOldVal, | ||
| OLDNOE: oldExportNodeFnVal, | ||
| }); | ||
| oldExportNode.replaceWith(newNode); | ||
| programAstPath.pushContainer("body", [newNode]); | ||
| if (isFnASt) { | ||
| oldExportAstNode.replaceWith(oldExportNodeEmitExport); | ||
| } else { | ||
| oldExportAstNode.remove(); | ||
| } | ||
| } | ||
@@ -99,0 +114,0 @@ } |
+1
-1
| { | ||
| "name": "babel-plugin-injectcomponentcode", | ||
| "version": "3.0.0", | ||
| "version": "3.0.1", | ||
| "description": "babel plugin react nextjs inject code", | ||
@@ -5,0 +5,0 @@ "private": false, |
6470
6.55%89
17.11%