Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function async(presets, plugins, options) {
// Directly ask babel-preset-env whether we want to use transform-async
// based on currently configured targets. Only if that's the case we
// transform our async/await code. Otherwise we assume it works without
// any transpilation.
const requiresAsync = isPluginRequired(
getTargets(getEnvTargets(options)),
envPlugins["transform-async-to-generator"]
)
// Alternative to Babel Regenerator
// Implements the ES7 keywords async and await using syntax transformation
// to at Promises at compile-time, rather than using generators.
// https://github.com/babel/babel/pull/7076 (NEW: bundled plugin with Babel)
// https://www.npmjs.com/package/fast-async (OLD: separate Babel plugin)
if (requiresAsync) {
plugins.push(fastAsyncPlugin)
}
if (options.debug) {
console.log("- Async/Await Transpilation:", requiresAsync)
}
export default function async(presets, plugins, options) {
// Directly ask babel-preset-env whether we want to use transform-async
// based on currently configured targets. Only if that's the case we
// transform our async/await code. Otherwise we assume it works without
// any transpilation.
const requiresAsync = isPluginRequired(
getTargets(getEnvTargets(options)),
envPlugins["transform-async-to-generator"]
)
// Alternative to Babel Regenerator
// Implements the ES7 keywords async and await using syntax transformation
// to at Promises at compile-time, rather than using generators.
// https://github.com/babel/babel/pull/7076 (NEW: bundled plugin with Babel)
// https://www.npmjs.com/package/fast-async (OLD: separate Babel plugin)
if (requiresAsync) {
plugins.push(fastAsyncPlugin)
}
if (options.debug) {
console.log("- Async/Await Transpilation:", requiresAsync)
}
}
// This behavior reduces the amount a transpilations a bit when working in pure NodeJS environments
envTargets.node = "10.13.0"
}
} else {
throw new Error("Invalid transpile configuration!")
}
// We fill the browsers field if not defined with an empty array so that
// the browserslist config is properly ignored when transpilation if passed over.
if (envTargets.node != null && envTargets.browsers == null) {
envTargets.browsers = []
}
if (options.debug) {
console.log("- Computed Targets:", envTargets)
console.log("- Final Targets:", getTargets(envTargets))
}
return envTargets
}
isPluginRequired(pluginName) {
let targets = this._getTargets();
// if no targets are setup, assume that all plugins are required
if (!targets) { return true; }
const isPluginRequired = require('@babel/preset-env').isPluginRequired;
const pluginList = require('@babel/preset-env/data/plugins');
return isPluginRequired(targets, pluginList[pluginName]);
},
_getTargets() {
let targets = this.project && this.project.targets;
let parser = require('@babel/preset-env/lib/targets-parser').default;
if (typeof targets === 'object' && targets !== null) {
return parser(targets);
} else {
return targets;
}
},
toObject() {
return targetsParser({ browsers: this.toQuery() });
}
function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath }) {
const { isPluginRequired } = require('@babel/preset-env')
const builtInsList = require('core-js-compat/data')
const getTargets = require('@babel/preset-env/lib/targets-parser').default
const builtInTargets = getTargets(targets, {
ignoreBrowserslistConfig,
configPath
})
return includes.filter(item => {
return isPluginRequired(builtInTargets, builtInsList[item])
})
}
function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath, corejs }) {
const { isPluginRequired } = require('@babel/preset-env')
const builtInsList = require(coreJsMeta[corejs.version].builtIns)
const getTargets = require('@babel/preset-env/lib/targets-parser').default
const builtInTargets = getTargets(targets, {
ignoreBrowserslistConfig,
configPath
})
return includes.filter(item => isPluginRequired(builtInTargets, builtInsList[item]))
}
static parse(query) {
if (query === 'esmodules') {
let list = targetsParser({ esmodules: true });
query = Object.keys(list).map((browser) => `${browser} ${list[browser].replace(/(\.0)*$/, '')}`).join(', ');
} else if (query === 'node') {
let list = targetsParser({ node: true });
query = Object.keys(list).map((browser) => `${browser} ${list[browser].replace(/(\.0)*$/, '')}`).join(', ');
} else if (Array.isArray(query)) {
query = query.join(', ');
} else if (typeof query === 'object') {
query = Object.keys(query).map((browser) => `${browser} ${query[browser]}`).join(', ');
}
return new Targets(query);
}
async transpileAndMinify() {
this.log.info('transpiling and uglifying');
const settings = this.$.desktop.getSettings();
const options = 'uglifyOptions' in settings ? settings.uglifyOptions : {};
const uglifyingEnabled = 'uglify' in settings && !!settings.uglify;
const preset = presetEnv({ assertVersion: () => { } }, { targets: { node: '12' } });
const { data: files } = await this.$.utils.readDir(this.$.env.paths.desktopTmp.root);
files.forEach((file) => {
if (file.endsWith('.js')) {
let { code } = transformFileSync(file, {
presets: [preset]
});
let error;
if (settings.env === 'prod' && uglifyingEnabled) {
({ code, error } = uglify.minify(code, options));
}
if (error) {
throw new Error(error);
}
fs.writeFileSync(file, code);