Bundle ES6 files without transpilation

2019-07-30 00:14发布

问题:

I'm probably missing something obvious, but I searched a lot before posting this and was, honestly, shocked to not find a solution. I want to bundle my ES6 files into a single module but, since ES6 is supported by the browser I use for testing, don't actually want to transpile anything. I can't figure out how to do it. I'm using webpack and saw a suggestion to simply omit the presets and plugins in my .babelrc file, but I get an error saying an object spread line has an unexpected token (the spread operator). How do I simply traverse the import/export paths to bundle the code but leave it as ES6? Huge thanks in advance!

回答1:

Use babel-preset-env

And, target transpilation for your specific browser. If that browser supports all ES6 features, then it won't be transpiled down to ES5.

E.g.

Your .babelrc could look like

{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 60
      }
    }]
  ]
}

You can also target multiple browsers so that your code works everywhere.

{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 60,
        "browsers": ["last 2 versions", "safari 7"]
      }
    }]
  ]
}