How to use @babel/preset-env - 10 common examples

To help you get started, we’ve selected a few @babel/preset-env examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sebastian-software / babel-preset-edge / src / parts / async.js View on Github external
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)
  }
github sebastian-software / babel-preset-edge / src / parts / async.js View on Github external
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)
  }
}
github sebastian-software / babel-preset-edge / src / getEnvTargets.js View on Github external
// 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
}
github babel / ember-cli-babel / index.js View on Github external
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]);
  },
github babel / ember-cli-babel / index.js View on Github external
_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;
    }
  },
github chialab / rna-cli / lib / Targets.js View on Github external
toObject() {
        return targetsParser({ browsers: this.toQuery() });
    }
github vuejs / vue-cli / packages / @vue / babel-preset-app / index.js View on Github external
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])
  })
}
github nuxt / nuxt.js / packages / babel-preset-app / src / index.js View on Github external
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]))
}
github chialab / rna-cli / lib / Targets.js View on Github external
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);
    }
github wojtkowiak / meteor-desktop / lib / electronApp.js View on Github external
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);