0

我已经准备了很多关于这个主题的问题和文章,但似乎没有任何效果。我的错误是这样的:

SyntaxError: import declarations may only appear at top level of a module

这是我无法正确转译 es6 代码的结果。这是我的 webpack 和 .babelrc:

网络包:

var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: __dirname + '/app/index.html',
  filename: 'index.html',
  inject: 'body'
});
var path = require('path')
var webpack = require('webpack');

module.exports = {
  devtool: 'source-map',
  entry: [
    'webpack-hot-middleware/client?reload=true',
    path.join(__dirname, './app/index.js')
  ],
  module: {
    loaders: [{
      test: [/\.js$/,/\.jsx$/],
      loader: "babel-loader",
      include: [
         path.resolve(__dirname, "node_modules/flash-notification-react-redux"),
         path.resolve(__dirname, "./app")
      ],
    },{
      test: /\.(css|scss)$/,
      loader: ExtractTextPlugin.extract('css!sass')
    },{
      test: /\.(png|jpg)$/,
      loader: 'url-loader?limit=8192'
    }]
  },
  output: {
    filename: 'index.bundle.js',
    path: __dirname + '/dist',
    publicPath: '/'
  },
  plugins: [
    HTMLWebpackPluginConfig,
    new ExtractTextPlugin('public/style.css', {
      allChunks: true
    }),
    new webpack.HotModuleReplacementPlugin(),
  ]
}

.babelrc

{
  presets: [ 'es2015', 'stage-0', 'react'],
  plugins: ['transform-decorators-legacy']
}

我尝试了各种方法(例如使用 'babel-preset-es2015',以及 stage-0 添加),但似乎没有任何效果。

此代码在我的一个组件中:

import GrowlerComponent from 'flash-notification-react-redux'

在 中转换为 this index.bundle.js,这实际上引发了错误:

import GrowlerReducer from "./src/reducer/growler.reducer";
4

0 回答 0