Webpack模式和加载器问题

时间:2019-06-12 13:54:08

标签: javascript reactjs webpack-dev-server webpack-4

我正在学习设置自己的Webpack,遇到了一些奇怪的问题。

这是我的webpack.development.config.js:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.export = {
  entry: './src/index.js',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, './dist'),
    publicPath: ''
  },
  mode: 'development',
  devServer: {
    contentBase: path.resolve(__dirname, './dist'),
    index: 'index.html',
    port: 3000
  },
  modules: {
    rules: [
      {
        test: /\.(png|jpg)$/,
        use: ['file-loader']
      },
      {
        test: /\.(css)$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(js|jsx)$/,
        exclude: '/node_modules/',
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/env', '@babel/preset-react'],
            plugins: ['transform-class-properties']
          }
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      description: 'Sample code',
    })
  ]
}

当我运行以下命令时:

webpack-dev-server --config ./webpack.development.config.js --hot

这是我的控制台中的输出:

I:\sample>npm run dev

> sample@0.0.0 dev I:\datum_gui
> webpack-dev-server --config ./webpack.development.config.js --hot

i 「wds」: Project is running at http://localhost:8080/

WARNING in configuration
The 'mode' option has not been set, [...]

ERROR in ./src/index.js 23:4
Module parse failed: Unexpected token (23:4)
You may need an appropriate loader to handle this file type.
| const render = (Component) => {
|   ReactDOM.render(
>     <AppContainer>
|       <CookiesProvider>
|         <Provider store={store}>
 @ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src main[2]
i 「wdm」: Failed to compile.

我有几个问题:

1)为什么代码似乎无法识别我指定的端口3000,却默认为端口8080?

2)我已将模式设置为:“开发”,但由于我已默认设置为“生产”,因此存在警告

3)我缺少什么装载程序,因为代码无法理解我的index.js?

节点包:

  • “ babel-loader”:“ ^ 8.0.5”,
  • “ webpack”:“ ^ 4.33.0”,
  • “ webpack-cli”:“ ^ 3.2.3”,
  • “ webpack-dev-server”:“ ^ 3.1.14”,
  • “ babel-plugin-transform-class-properties”:“ ^ 6.24.1”,
  • “ @ babel / preset-env”:“ 1.4.0”,
  • “ @ babel /预设反应”:“ 7.0.0”,

2 个答案:

答案 0 :(得分:1)

出现错误的原因是因为您忘记将jsx添加到加载程序中

{            // \/ forgot to add jsx here
  test: /\.(js|jsx)$/,
  exclude: '/node_modules/',
  use: {
    loader: 'babel-loader',
    options: {                   // also need @babel/preset-react
      presets: ['@babel/env', '@babel/preset-react'],
      plugins: ['transform-class-properties']
    }
  }
}

编辑:

您还需要@babel/preset-react

只需运行npm i @babel/preset-react并将其添加到options.presets

答案 1 :(得分:0)

我的webpack.config.js看起来像这样

{
  test: /\.(js|jsx)$/,
  exclude: /node_modules/,
  loader: ["babel-loader"]
}

我需要创建.babelrc文件

{
  "presets": ["es2015", "react"],
  "plugins": [
    "transform-es2015-destructuring",
    "transform-object-rest-spread",
    ["transform-class-properties", { "spec": true }]
  ]
}

然后我的packge.json中将需要这些包

"babel-cli": "6.26.0",
"babel-core": "6.26.3",
"babel-eslint": "8.2.6",
"babel-loader": "7.1.5",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-es2015-destructuring": "6.23.0",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-0": "6.24.1",