AKJ
AKJ

Reputation: 809

Webpack mode & loader issue

I am learning to set up my own webpack and I encountered a few curious issues.

Here is my 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',
    })
  ]
}

When i run the following command:

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

Here is the output in my console:

I:\sample>npm run dev

> [email protected] 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.

I have a few questions:

1) How come the code cannot seem to pick up that i specified port 3000 but instead it defaulted to port 8080?

2) I have set mode: 'development' but yet there is a warning given that i have been defaulted to 'production'

3) What loader am i missing that the code cannot understand my index.js?

Node packages:

Upvotes: 3

Views: 463

Answers (1)

Vencovsky
Vencovsky

Reputation: 31585

The reason why you are getting the error is because you forgot to add jsx to the loader

{            // \/ 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']
    }
  }
}

Edit:

You also need @babel/preset-react

Just run npm i @babel/preset-react and add it in the options.presets

Upvotes: 1

Related Questions