Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const deps = []
if (es6.async) {
console.log(' ', chalk.blue('has async -- convert to generator'), file.key)
result = asyncToGen(result).toString()
}
if (es6.fetch) {
console.log(' ', chalk.blue('has fetch -- add shim'), file.key)
deps.push(addDependency({ browser: 'whatwg-fetch', dependencies, result }))
// deps.push(addDependency({ node: 'node-fetch', dependencies, result }))
}
if (es6.generator) {
console.log(' ', chalk.blue('has generator -- add shim'), file.key)
es5 = regenerator.compile(result).code
deps.push(addDependency({ browser: 'regenerator-runtime/runtime', dependencies, result }))
}
if (es6.promise) {
console.log(' ', chalk.blue('has promise -- add shim'), file.key)
deps.push(addDependency({ browser: 'promise-polyfill', dependencies, result }))
}
Promise.all(deps).then(() => {
es5 = buble.transform(es5 || result).code
resolve({ result, dependencies, es5 })
}).catch(err => reject(err))
} else {
es5 = buble.transform(result).code
resolve({ result, dependencies, es5 })
}
const id = file.id.val.join('/')
if (es6.async) {
if (boy.__verbose__) console.log(' ', chalk.blue('has async - convert to generator'), id)
result = asyncToGen(result).toString()
}
if (es6.fetch) {
if (boy.__verbose__) console.log(' ', chalk.blue('has fetch - add shim'), id)
deps.push(addDependency({ file, browser: 'whatwg-fetch', dependencies, result }))
// deps.push(addDependency({ node: 'node-fetch', dependencies, result }))
}
if (es6.generator) {
if (boy.__verbose__) console.log(' ', chalk.blue('has generator - add shim'), id)
es5 = regenerator.compile(result).code
deps.push(addDependency({ file, browser: 'regenerator-runtime/runtime', dependencies, result }))
}
if (es6.promise && file.id.val.join('/') !== 'promise-polyfill/promise.js') {
if (boy.__verbose__) console.log(' ', chalk.blue('has promise -- add shim'), id)
deps.push(addDependency({ file, browser: 'promise-polyfill', dependencies, result }))
}
Promise.all(deps).then((results) => {
results.forEach((val) => {
dependencies.push(val)
})
es5 = buble.transform(es5 || result, bubleOptions).code
resolve({ result, dependencies, es5 })
}).catch(err => reject(err))
return;
}
// proceed with the transformation...
var res;
if (withSourceMap) {
var inputAst = recastParse(code, {
sourceFileName: id
});
var outputAst = regeneratorTransform(inputAst);
res = recastPrint(outputAst, {
sourceMapName: id
});
} else {
res = regeneratorCompile(code);
}
return {
code: res.code,
map: withSourceMap && res.map ? res.map : { mappings: '' }
};
}
};
function evalScript (name) {
var Module = require('module');
var path = require('path');
var cwd = process.cwd();
var module = new Module(name);
module.filename = path.join(cwd, name);
module.paths = Module._nodeModulePaths(cwd);
var script = process._eval;
// compile JS via facebook/regenerator
script = regenerator.compile(script, {
includeRuntime: 'object' !== typeof regeneratorRuntime
}).code;
if (!Module._contextLoad) {
var body = script;
script = 'global.__filename = ' + JSON.stringify(name) + ';\n' +
'global.exports = exports;\n' +
'global.module = module;\n' +
'global.__dirname = __dirname;\n' +
'global.require = require;\n' +
'return require("vm").runInThisContext(' +
JSON.stringify(body) + ', ' +
JSON.stringify(name) + ', true);\n';
}
var result = module._compile(script, name + '-wrapper');
function gnodeEval (code, context, file, fn) {
var err, result;
try {
// compile JS via facebook/regenerator
code = regenerator.compile(code, {
includeRuntime: 'object' !== typeof regeneratorRuntime
}).code;
} catch (e) {
// Treat regenerator errors as syntax errors in repl.
// A hack to have repl interpret certain js structures correctly.
e.name = 'SyntaxError'
err = e;
}
try {
if (this.useGlobal) {
result = vm.runInThisContext(code, file);
} else {
result = vm.runInContext(code, context, file);
}
} catch (e) {
err = e;
return;
}
// proceed with the transformation...
var res;
if (withSourceMap) {
var inputAst = recast.parse(code, {
sourceFileName: id
});
var outputAst = regenerator.transform(inputAst);
res = recast.print(outputAst, {
sourceMapName: id
});
} else {
res = regenerator.compile(code);
}
return {
code: res.code,
map: withSourceMap && res.map ? res.map : { mappings: '' }
};
}
};
process: function(src) {
src = es6defaultParams.compile(src).code;
// Regenerator has a bug when using backbone
if (src.indexOf("\"enable_regenerator\"") !== -1) {
src = regenerator.compile(src, {
includeRuntime: true
}).code;
}
src = ReactTools.transform(src, {
stripTypes: true,
harmony: true
});
return src;
}
};
module.exports = function(source) {
this.cacheable(true);
return regenerator.compile(source).code;
};
function compile(js, includeRuntime) {
includeRuntime = includeRuntime || false;
return regenerator.compile(js, { includeRuntime: includeRuntime }).code;
}