From f1a7d6219450df50208f82e64e81b934fccf760f Mon Sep 17 00:00:00 2001 From: pekonchan Date: Fri, 30 Jun 2023 17:58:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9console=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=A8=A1=E6=9D=BF=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=8E=92=E9=99=A4=E6=94=B6=E9=9B=86=EF=BC=9B?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A=E5=BF=BD=E7=95=A5=E6=94=B6?= =?UTF-8?q?=E9=9B=86=E8=BD=AC=E5=8C=96=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ loader/index.js | 48 +++++++++++++++++++++++++++++++++++++++++------- package.json | 2 +- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c73ca15..4610bfe 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,16 @@ options: { loader更多配置请查阅 [loader配置表](https://github.com/pekonchan/i18n-auto-webpack#loader) +> 如果你出现首次运行该工具收集的词条是正常的,但是后续再次收集发现少了,那么可能是受限于构建脚手架或者webpack某个版本的影响,对编译结果进行了缓存,导致无法收集。你可尝试禁用webpack的cache-loader缓存看看,如下所示 + +``` + // 禁用缓存 + // webpack-chain写法 +config.module.rule('vue').uses.delete('cache-loader'); +config.module.rule('js').uses.delete('cache-loader'); +config.module.rule('ts').uses.delete('cache-loader'); +``` + #### 注意事项 `i18n-auto-webpack/loader`预期接收的代码是`Javascript`内容,它的工作原理是对传递进来的是`Javascript`代码解析成`AST`,然后分析`AST`查找提取中文等系列操作后再转回去`Javascript`代码。 diff --git a/loader/index.js b/loader/index.js index b38208c..0ccd46a 100644 --- a/loader/index.js +++ b/loader/index.js @@ -45,6 +45,34 @@ module.exports = function i18nTransform (code) { sourceType: 'unambiguous' }) + function isInConsole (path) { + const { type: parentType, callee: parentCallee } = path.parent + if (parentType === 'CallExpression' && parentCallee.type === 'MemberExpression') { + const parentCalleeObject = parentCallee.object + if (parentCalleeObject.type === 'Identifier' && parentCalleeObject.name === 'console') { + return true + } + } + return false + } + function findCommentExclude(path) { + //If from TemplateLiteral to StringLiteral + if (!path.node.loc) { + return false + } + const startLine = path.node.loc.start.line + const leadingComments = path.node.leadingComments + const check = (commentList) => { + if (commentList && commentList.length) { + const end = commentList.some(comment => { + return comment.type === 'CommentBlock' && comment.value.trim() === 'no-i18n-auto' && comment.loc.start.line === startLine + }) + return end + } + } + return (check(leadingComments) || check(ast.comments)) + } + const visitor = { // Finds if the user's dependency is in the import declaration ImportDeclaration (path) { @@ -129,15 +157,15 @@ module.exports = function i18nTransform (code) { }) }, StringLiteral (path) { - const { type: parentType, callee: parentCallee } = path.parent - if (parentType === 'ImportDeclaration') { + if (path.parent.type === 'ImportDeclaration') { return } - if (parentType === 'CallExpression' && parentCallee.type === 'MemberExpression') { - const parentCalleeObject = parentCallee.object - if (parentCalleeObject.type === 'Identifier' && parentCalleeObject.name === 'console') { - return - } + if (findCommentExclude(path)) { + return + } + + if (isInConsole(path)) { + return } if (path.node.type === 'StringLiteral') { const val = path.node.value @@ -159,6 +187,12 @@ module.exports = function i18nTransform (code) { } }, TemplateLiteral (path) { + if (findCommentExclude(path)) { + return + } + if (isInConsole(path)) { + return + } const hasWord = path.node.quasis.some(item => globalSetting.localePattern.test(item.value.raw)) if (!hasWord) { return diff --git a/package.json b/package.json index c821b1f..e0faf74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "i18n-auto-webpack", - "version": "0.3.0", + "version": "0.4.0", "description": "This is a tools to help you work i18n automatically in webpack. It includs two part: plugin and loader", "main": "index.js", "scripts": {