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": {