Webpack 没有将 ES6 转换为 ES5

Webpack not converting ES6 to ES5

我是 Webpack 的新手。我想我做错了。我想使用 babel 将 ES6 函数转换为 ES5 函数。所以我做了一些研究,发现了 babel-loader。但是,我不确定自己在做什么。

我 运行 npm install babel-loader --save-dev 它被添加到我的 package.json

// package.json

{
  "name": "kanban",
  "version": "1.0.0",
  "description": "kanban",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.3.21",
    "babel-loader": "^6.2.0",
    "html-webpack-plugin": "^1.7.0",
    "json-loader": "^0.5.4",
    "webpack": "^1.12.9"
  }
}

// webpack.config.js

var path = require('path');
var HtmlwebpackPlugin =  require('html-webpack-plugin');

const PATHS = {
  app: path.join(__dirname, 'app'),
  build: path.join(__dirname, 'build')
};

module.exports = {
  entry: PATHS.app,
  output: {
    path: PATHS.build,
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlwebpackPlugin({
      title: 'Kanban app'
    })
  ],
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader' }
    ]
  }
};

// app/index.js - 我刚刚在 ES6 语法中添加了一些 运行dom 无用的函数。我希望我会在我的 bundle.js 文件中看到 ES5 格式,但它没有改变。 bundle.js

中仍然是 ES6 语法
var component = require('./component');
var app = document.createElement('div');
document.body.appendChild('app');
app.appendChild(component());

let myJson = {
  prop: 'myProp'
};

let fives = [];
nums = [1, 2, 5, 15, 25, 32];

// Statement bodies
nums.forEach(function (v) {
  if (v % 5 === 0) {
    fives.push(v);
  }
}, this);

console.log(fives);

let sum = (a, b) => a + b; 

// app/component.js

module.exports = function() {
  var element = document.createElement('h1');
  element.innerHTML = 'hello world';
  return element;
};

如果你想将 ES6 编译成 ES5,你需要安装 Babel ES2015 preset

npm install babel-preset-es2015

那么您需要启用这个预设。启用此 ES6 到 ES5 编译的一种方法是使用 babel-loader 查询字符串:

  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader?presets[]=es2015'
      }
    ]
  }

或查询选项:

  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015']
        }
      }
    ]
  }

对于 webpack 3,模块选项应该类似于

module: {
  rules: [
    {
       use: {
          loader:'babel-loader',
          options: { presets: ['es2015'] }
       },
       test: /\.js$/,
       exclude: /node_modules/
    }
  ]
},

这仍然需要 babel-loaderbabel-preset-es2015

我遇到了同样的问题。 React官方的回答是将这个配置添加到Webpack中。

{
  "presets": ["es2015", "react", "stage-0"],
  "plugins": ["react-hot-loader/babel"]
}

这里是link:https://github.com/facebook/react/issues/8379

babel-preset-es2015 已弃用。尝试:

npm install -D babel-loader @babel/core @babel/preset-env webpack

然后在你的 webpack.config.js 中:

{
  test: /\.m?js$/,
  exclude: /(node_modules|bower_components)/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  }
}

more info

您还应该在 webpack.config.js

中添加 target: "es5"
...

module.exports = {
  ...
  module: {
    target: "es5", // add this
    loaders: [
      { test: /\.js$/, loader: 'babel-loader' }
    ]
  }
};

详情见