babel-loader 6.0.0 不能正常工作

babel-loader 6.0.0 doesn't work correct

我正在使用 webpack 这是我的

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/app',
  output: {
    path: path.join(__dirname, '/public'),
    filename: 'bundle.js'
  },
  watch: true,
  watchOptions: {
    aggregateTimeout: 100
  },
  devtool: 'source-map',
  module: {
    loaders: [{
      test: /\.js$/,
      loader: "babel-loader"
    }]
  }
}

这是我来自入口点的文件

app.js

import React from 'react'

什么都没有,只有简单的一行代码。 当我尝试从命令行 运行 webpack 时出现下一个错误

app.js Line 1: Unexpected token You may need an appropriate loader to handle this file type. | import React from 'react';

package.json

{
  "name": "a-b-c",
  "version": "1.0.0",
  "main": "server.js",
  "dependencies": {
    "babel-loader": "^6.0.0",
    "react": "^0.14.1",
    "webpack": "^1.12.2"
  },
  "devDependencies": {
    "babel-core": "^6.0.14"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description": ""
}

此外,当我将 app.js 文件中的 ES6 语法更改为 ES5 时,它运行良好。 有什么建议么? 提前致谢。

我知道了。问题出在 babel-loader 上,要解决此问题,您需要安装

npm install babel-loader babel-core babel-preset-es2015

并添加到您的 webpack.config.js 文件下一行

loader: 'babel?presets[]=es2015'

Source 条信息

谢谢