我有这个代码:
console.log(/\.js$/gi.test('src/public/app/cookieMsgs/cookieMsgs.js'));
const pathReplaces = {};
function getPathReplace(path) {
if (!pathReplaces[path]) {
pathReplaces[path] = new RegExp(`^${path}\/?`, 'gi');
}
return pathReplaces[path];
}
function copyFiles(files, path, dest, opts = {}) {
const pathReplace = getPathReplace(path);
const isProcessFileCfg = (opts.exclude !== undefined) || (opts.include === undefined) || (!opts.exclude && !opts.include);
const popts = opts.exclude || opts.include || [];
let fileParts;
let fileName;
let isProcessFile;
for (let file of files) {
try {
console.log(file);
isProcessFile = isProcessFileCfg;
for (let opt of popts) {
console.log(opt, file, isProcessFile);
if (opt.test(file)) {
isProcessFile = !isProcessFile;
console.log('Test result:', isProcessFile);
break;
}
}
if (isProcessFile) {
fileParts = file.replace(pathReplace, '').split('/');
fileName = fileParts.pop();
}
} catch (err) {
console.log(err);
}
}
}
function test() {
copyFiles([
'src/public/app/app.js',
'src/public/app/checkout/directCard.js',
'src/public/app/cookieMsgs/cookieMsgs.js',
], 'src/public', 'build', { exclude: [/\.js$/gi, /\.css$/gi, /\.html$/gi]});
}
test();

输出真的很奇怪,您可以see test in here复制粘贴babeljs.io中的代码:
true
src/public/app/app.js
/\.js$/gi src/public/app/app.js true
Test result: false
src/public/app/checkout/directCard.js
/\.js$/gi src/public/app/checkout/directCard.js true
Test result: false
src/public/app/cookieMsgs/cookieMsgs.js
/\.js$/gi src/public/app/cookieMsgs/cookieMsgs.js true
/\.css$/gi src/public/app/cookieMsgs/cookieMsgs.js true
/\.html$/gi src/public/app/cookieMsgs/cookieMsgs.js true
发生了什么事?我无法理解为什么/\.js$/gi.test('src/public/app/checkout/cookieMsgs.js')
通过了测试,但是在for (.. of ...)
内部完成时,它没有通过测试......