我有一个带有React 15.3,Webpack 4和Babel 7的function clone(obj) {
const replace = {};
let idx = 0;
const undefCache = [];
const replacer = (key, value) => {
let result;
if (value === undefined) {
result = '__undefined__';
} else if (typeof value === 'symbol' || typeof value === 'function') {
const keyIdx = `__replaced__${idx}`;
idx += 1;
replace[keyIdx] = [this, key]; // I understand mostly what's happening except for the line
result = keyIdx;
} else {
result = value;
}
return result;
};
function reviver(key, value) {
let result;
if (value === '__undefined__') {
undefCache.push([this, key]);// I understand mostly what's happening except for the line
} else if (replace[value] !== undefined) {
result = replace[value][0][key];
} else {
result = value;
}
return result;
}
const json = JSON.stringify(obj, replacer);
console.log(json);
const newObject = JSON.parse(json, reviver);
undefCache.forEach(el => {
const [o, key] = el;
o[key] = undefined;
});
return newObject;
}
const source = {
a: 2,
b: '2',
c: false,
g: [
{ a: { j: undefined }, func: () => {} },
{ a: 2, b: '2', c: false, g: [{ a: { j: undefined }, func: () => {} }] }
]
};
const targetOne = clone(source);
console.log(targetOne);
存储库。Webpack很有魅力,但是我们使用Nightwatch 0.9.20的E2E测试套件无法与新的client
一起编译包。
我们公司将babel版本从6升级到7。
在互联网上浮动的解决方案是将以下内容添加到@babel/register
文件中:
babel.config.js
在我们的实例中,此解决方案无法解决我们的问题。
我要提到的项目结构如下:
{
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "current"
}
}
]
],
"plugins": [
"add-module-exports",
]
}
我们的nightwatch.conf.js结构如下:
client // where nightwatch is run from the terminal
|-- tests // our E2E tests
|-- nightwatch.conf.js
|-- nighwatch_globals.js
|-- babel.config.js
api // a completely separate repository
|-- tests // we store factory definitions here as well
|-- db
|-- models // also a frequently referenced directory in our E2E tests
我们的require('@babel/register')(); // this has been upgraded from 'babel-register'
require('dotenv').config();
...
module.exports = {
// nightwatch configurations
"globals_path": "./nightwatch_globals.js",
// more nightwatch configurations
}
文件(正在调用错误的地方看起来像这样:
nightwatch_globals.js
我们的import fetch from 'isomorphic-fetch';
module.exports = {
reporter: function(results, cb) {
fetch('http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer').then(() => {
cb();
if (
(typeof(results.failed) === 'undefined' || results.failed === 0) &&
(typeof(results.error) === 'undefined' || results.error === 0)
) {
process.exit(0);
} else {
process.exit(1);
}
});
},
// give the db some time to finish ops from previous test before
// clearing db and starting the next test
before: function(done) {
require('./../eka-api/test/factories/index.js');
done();
},
beforeEach: function(done) {
setTimeout(function() {
done();
}, 5000);
},
};
文件如下:
babel.config.js
在客户端目录中,我在终端
module.exports = function(api) {
api.cache(true);
const presets = [
"@babel/preset-env",
"@babel/react",
"@babel/preset-flow",
];
const plugins = [
"@babel/plugin-syntax-flow",
"@babel/transform-flow-strip-types",
["@babel/plugin-proposal-decorators", {"legacy": true}],
// react hot loader must come before class properties plugin
"react-hot-loader/babel",
// class properties plugin must come after decorators
// if decorators has a 'legacy' attribute, class properties must be loose
["@babel/plugin-proposal-class-properties", {"loose" : true}],
"@babel/plugin-transform-runtime"
];
return {
presets,
plugins
};
};
中运行
这是持续存在的错误
node nightwatch.conf.js ; nightwatch
我的直觉告诉我,无法识别导入令牌的原因与Babel 7如何将代码编译范围缩小到/Users/myUser/Documents/api/test/factories/index.js:1
(function (exports, require, module, __filename, __dirname) { import bluebird from 'bluebird';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Module._compile (/Users/myUser/Documents/eka-client/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (module.js:580:10)
at Object.newLoader [as .js] (/Users/myUser/Documents/eka-client/node_modules/pirates/lib/index.js:104:7)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
有关。如您所见,错误来自api。由于nightwatch是客户端目录中的软件包,因此Babel不会编译api目录(该目录仍在Babel 6中)。但是,我想避免不得不重构整个客户端测试套件以使其独立于api文件。
我怀疑解决方案是为process.cwd()
找到一种从客户端编译api的方法,但是我不知道该怎么做。