-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug Report] 使用vux-loader之后所有vue组件script标签内的代码都会执行两次 #65
Comments
我也发现了这个bug。。1.2.0无此bug。。1.2.1之后有这个bug |
@wych1987 摊手╮(╯▽╰)╭ |
很久之前就发现这个问题,还以为是我哪里配置不对 |
@wych1987 查看了下1.2.1的改动 d230eaf 问题寻找测试发现确实是添加的这段代码造成的问题 if (content.indexOf('export * from') !== -1) {
let loaders = content.split('!')
loaders = loaders.map(function (item) {
if (item === 'babel-loader') {
item += '!' + SCRIPT
}
return item
})
content = loaders.join('!')
} 可以看这段代码所在的函数 const _addScriptLoader = function (content, SCRIPT) {
// get script type
if (/type=script/.test(content)) {
// split loaders
var loaders = content.split('!')
loaders = loaders.map(function (item) {
if (/type=script/.test(item)) {
item = SCRIPT + '!' + item
}
return item
}).join('!')
content = loaders
} else if (/require\("!!babel-loader/.test(content)) {
content = content.replace('!!babel-loader!', `!!babel-loader!${SCRIPT}!`)
} else if (/import\s__vue_script__\sfrom\s"!!babel\-loader!\.\/(.*?)"/.test(content)) {
let loaders = content.split('!')
loaders = loaders.map(function (item) {
if (item === 'babel-loader') {
item += '!' + SCRIPT
}
return item
})
content = loaders.join('!')
}
if (content.indexOf('export * from') !== -1) {
let loaders = content.split('!')
loaders = loaders.map(function (item) {
if (item === 'babel-loader') {
item += '!' + SCRIPT
}
return item
})
content = loaders.join('!')
}
return content
} 可以看到前面判断条件中有一种情况是 content = content.replace('!!babel-loader!', `!!babel-loader!${SCRIPT}!`) 这种情况下,再跑后面添加的这段代码,会造成 问题测试可以打印这个函数的输入输出
输出为
明显看到 解决方案因为并不清楚当初作者添加这段代码的具体意义, 但问题的原因很清楚就是 if (content.indexOf('export * from') !== -1 && content.indexOf(SCRIPT) === -1) {
let loaders = content.split('!')
loaders = loaders.map(function (item) {
if (item === 'babel-loader') {
item += '!' + SCRIPT
}
return item
})
content = loaders.join('!')
} 添加了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
vux-loader version
1.2.9
OS/Browsers version
MacOS Chrome
Vue version
2.5.16
Code
Steps to reproduce
What is Expected?
vue组件script标签内的代码只会执行一次
What is actually happening?
vue组件script标签内的代码执行了二次
The text was updated successfully, but these errors were encountered: