2017-05-03 25 views
0

編譯這裏是我的減速器Redux的傳播運營商不與的WebPack 2

const serviceSelected = (state = {} , action) => { 
    switch(action.type) { 
     case 'ADD_SERVICE': 
      return { ...state, serviceSelected : action.service } 
     default: 
     return state; 
    } 
} 
export default serviceSelected; 

我已經通過NPM加載"babel-plugin-transform-object-rest-spread" : "6.23.0"

我的WebPack的conf是

'use strict'; 

const webpack = require('webpack'); 
const webpackMerge = require('webpack-merge'); 
const path = require('path'); 
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
const baseConfig = require('./webpack.config.js'); 

const environment = process.env.NODE_ENV || 'development'; 
const isProduction = environment === 'production'; 

module.exports = webpackMerge(baseConfig, { 
    entry: { 
    app: [path.resolve(__dirname, 'client', 'scripts', 'app', 'index.js')], 
    pl: [path.resolve(__dirname, 'client', 'scripts', 'pl.js')] 
    }, 
    resolve: { 
    alias: { 
     scripts: path.resolve(__dirname, 'client', 'scripts'), 
     views: path.resolve(__dirname, 'client', 'views') 
    } 
    }, 
    module: { 
    rules: [{ 
     test: /\.js$/, 
     include: [ 
     path.resolve(__dirname, 'client', 'scripts'), 
     path.resolve(__dirname, 'client', 'components'), 
     ], 
     use: [{ 
     loader: 'ng-annotate-loader' 
     }, { 
     loader: 'babel-loader', 
     options: { 
      presets: ['es2015', 'react'], 
      plugins: [require('babel-plugin-transform-object-rest-spread')], 
      cacheDirectory: path.resolve(__dirname, '.tmp', '.babel-cache') 
     } 
     }] 
    }, { 
     test: /\.html$/, 
     include: [ 
     path.resolve(__dirname, 'client', 'views'), 
     path.resolve(__dirname, 'client', 'components'), 
     ], 
     use: 'raw-loader' 
    }, { 
     test: /\.css$/, 
     use: ExtractTextPlugin.extract({ 
     fallback: 'style-loader', 
     use: [{ 
      loader: 'css-loader', 
      options: { 
      minimize: false, 
      modules: true, 
      localIdentName: '[name]__[local]___[hash:base64:5]' 
      } 
     }] 
     }) 
    }] 
    }, 
    plugins: [ 
    new webpack.DefinePlugin({ 
     process: { 
     env: { 
      NODE_ENV: JSON.stringify(environment), 
      ZAP_WIDGET_URL: JSON.stringify(`${isProduction ? 'https://widget.meso.com' : 'http://localhost:8080'}/embed.js`) 
     } 
     } 
    }), 
    new webpack.DllReferencePlugin({ 
     context: __dirname, 
     manifest: path.resolve(__dirname, '.tmp', 'scripts', 'vendor-manifest.json'), 
     name: 'FXO_VENDOR' 
    }), 
    new ExtractTextPlugin('../stage/components.css'), 
    ] 
}); 

在此的conf一半時,你將會看到

plugins: [require('babel-plugin-transform-object-rest-spread')], 

我也嘗試添加一個.babelrc文件,

{ 
    "plugins": ["babel-plugin-transform-object-rest-spread"] 
} 

我得到這個錯誤

Unexpected token (6:19) 
You may need an appropriate loader to handle this file type. 
|  switch(action.type) { 
|  case 'ADD_SERVICE': 
|   return { ...state, serviceSelected : action.service } 

如果我改變減速是

const serviceSelected = (state = [] , action) => { 
    switch(action.type) { 
     case 'ADD_SERVICE': 

     return [ 
       ...state, { 
        serviceSelected : action.service 
       } 
      ] 

     default: 
     return state; 
    } 
} 
export default serviceSelected; 

我沒有得到任何編譯錯誤

+0

我們使用它只是罰款,但'plugins'是的WebPack 1 –

+0

下包含在'query'對象presets'和'我也有它的WebPack 1.工作,但我們已經移到Webpack 2 – Rory

回答

-1

作爲臨時解決方案我在我的減速器

case 'ADD_SERVICE': 
      return Object.assign(state, { serviceSelected : action.service}); 

用這個,但如果有人能回答我原來的職位,我會很高興威猛

+0

這是爲什麼被拒絕投票?正如我所說,由於Webpack問題,這是一個臨時解決方案。請問小學生請提出解決方案嗎? – Rory

0

我的臨時修復被某人投下。

這是我最終使用的。

case 'ADD_SERVICE': 
      let [last] = [...state].reverse(); 
      let newIndex = 1; 
      if(last != undefined){ 
       newIndex = last.index; 
       newIndex++; 
      } 

      return [ 
       ...state, { 
       index: newIndex, 
       selectedService: action.service 
       } 
      ]