【问题标题】:Object spread plugin not working in webpack对象传播插件在 webpack 中不起作用
【发布时间】:2017-09-30 00:51:05
【问题描述】:

我试图在我的应用程序中使用对象扩展运算符。这些是我的文件。

webpack.config.js

const webpack = require('webpack')
const path = require('path');

const BUILD_DIR = path.resolve(__dirname + '/build')
const APP_DIR = path.resolve(__dirname + '/app')

module.exports = {
  entry: APP_DIR + '/index.jsx',
  output: {
    filename: 'bundle.js',
    path: BUILD_DIR,
    publicPath: '/',
  },

  devServer: {
      inline: true,
      contentBase: BUILD_DIR,
      port: 3333,
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      }
    ]
  },
  resolve: {

      extensions: [".js", ".json", ".jsx", ".css"],
  }
};

.babelrc(在项目根文件夹中)

{
  "presets": ["es2015", "react"],
  "plugins": ["transform-object-rest-spread"]
}

package.json

{
  "name": "shoop",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "dependencies": {
    "webpack": "^2.4.1"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-plugin-transform-object-rest-spread": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack-dev-server": "^2.4.5"
  },
  "scripts": {
    "dev": "webpack-dev-server",
    "compile": "webpack"
  },
  "author": "",
  "license": "ISC"
}

当我尝试运行下面的代码时,我收到“意外令牌”消息。:

index.jsx

 let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
    console.log(x); // 1
    console.log(y); // 2
    console.log(z); // { a: 3, b: 4 }

【问题讨论】:

  • 尝试将test: /\.js$/改为test: /\.jsx?$/
  • 它现在可以工作了,谢谢。我要多注意正则表达式
  • @juliobetta 您可能应该将此添加为答案,以便 OP 可以接受它,其他有相同问题的人可以找到此解决方案:)

标签: javascript reactjs webpack-dev-server ecmascript-next


【解决方案1】:

将test: /\.js$/ 更改为test: /\.jsx?$/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 2019-02-12
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2017-01-30
    相关资源
    最近更新 更多