Skip to content

Commit

Permalink
feat: 对console函数使用模板字符串进行排除收集;增加注释忽略收集转化能力
Browse files Browse the repository at this point in the history
  • Loading branch information
pekonchan committed Jun 30, 2023
1 parent 2ffa756 commit f1a7d62
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`代码。

Expand Down
48 changes: 41 additions & 7 deletions loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit f1a7d62

Please sign in to comment.