【问题标题】:Unexpected Token @ when using babel decorator使用 babel 装饰器时出现意外的 Token @
【发布时间】:2017-09-15 11:14:06
【问题描述】:

我目前正在构建一个 react 项目,并已开始使用 connect 合并 redux。我正在使用装饰器来引用它,即

const mapStateToProps = (state) => {
    return {
        active: state.sortOrder
    }
};

@connect(mapStateToProps)
export default class SortFilter extends Component {
    //component code here
}

SyntaxError: /Sort.js: Unexpected token (10:0) @connect(mapStateToProps)

这是我的 webpack 配置,其中包含 babel-transform-decorators 和 stage-0 预设(因为这似乎是其他人的解决方案)。

const PATH = require('path');

const webpack = require("webpack");

const ROOT = '../../../';

const APP_FOLDER = PATH.resolve(__dirname, ROOT, 'app/');
const APP_ENTRY_FILE = PATH.resolve(__dirname, ROOT, APP_FOLDER, 'client.js');

const BUILD_FOLDER = PATH.resolve(__dirname, ROOT, 'app/public/js/');
const PUBLIC_PATH = '/js/';

const BUILD_FILE = 'app.js';

const ESLINT_CONFIG_FILE = PATH.resolve(__dirname, ROOT, 'tools/build/config/eslint-config.json');

var webpackConfig = {
entry: {
    app: APP_ENTRY_FILE
},
output: {
    path: BUILD_FOLDER,
    publicPath: PUBLIC_PATH,
    filename: BUILD_FILE
},
devtool: 'inline-source-map',
debug: true,    
bail: true,
eslint: {
    configFile: ESLINT_CONFIG_FILE
},
module: {
    preLoaders: [
        {
            test: /\.js$/,
            include: [
                APP_FOLDER
            ],
            loader: 'eslint-loader'
        }
    ],
    loaders: [
        {
            test: /\.js$/,
            include: [
                APP_FOLDER
            ],
            loader: 'babel',
            query: {
                compact: false,
                cacheDirectory: true,
                presets: ['es2015', 'react', 'stage-0'],
                plugins: ['transform-decorators-legacy']
            }
        }
    ]        
},
externals: {
    'axios': 'axios',
    'react': 'React',
    'react-router': 'ReactRouter',
    'history': 'History',
    'react-dom': 'ReactDOM'
},
plugins: [
    new webpack.NoErrorsPlugin()
]
};

module.exports = webpackConfig;

任何帮助解决这个问题将不胜感激。

【问题讨论】:

    标签: javascript node.js webpack redux babeljs


    【解决方案1】:

    你需要安装babel-plugin-transform-decorators:

    npm install babel-plugin-transform-decorators-legacy --save-dev
    

    然后添加.babelrc:

    "plugins": ["transform-decorators-legacy"]
    

    【讨论】:

      【解决方案2】:

      您是否尝试过将 babel 配置移动到项目根目录下的 .babelrc 文件中(移除 webpack 上的 babel 配置)?

      文件是这样的:

      {
          "presets": [ "es2015", "react" ],
          "plugins": [
              "transform-decorators-legacy"
          ]
      }
      

      【讨论】:

      • 我已经尝试过了,然后又遇到了另一个错误。TypeError: Cannot read property 'error' of undefined at OptionManager.mergeOptions (/node_modules/babel-core/lib/transformation/file/options/ option-manager.js:126:28)
      • 你使用的是什么版本的babel-core?
      • 您使用的是babel 版本>= 6 吗?
      猜你喜欢
      • 2017-04-02
      • 2018-09-24
      • 2023-03-09
      • 1970-01-01
      • 2017-06-23
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多