@Marat20
Начинающий Frontend-developer

Не работает Babel-loader?

Установил вебпак, установил babel, запускаю webpack-dev-server и в терминале пишет ошибку:

ERROR in ./src/index.jsx 6:13
Module parse failed: Unexpected token (6:13)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| let block = document.querySelector('#app')
| 
> let button = <button>Send</button>

webpack 5.37.1 compiled with 1 error in 191 ms
ℹ 「wdm」: Failed to compile.

webpack.config.js:

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

module.exports = {
    entry: './src/index.jsx',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['@babel/preset-env']
              }
            }
          }
        ]
    },
    plugins: [new HtmlWebpackPlugin()],
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 9000,
      },
}


package.json:

{
  "name": "first-react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack serve --mode development",
    "build": "webpack --mode development"
  },
  "browserslist": "> 0.25%, not dead",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Marat20/First-React.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Marat20/First-React/issues"
  },
  "homepage": "https://github.com/Marat20/First-React#readme",
  "devDependencies": {
    "@babel/core": "^7.14.3",
    "@babel/preset-env": "^7.14.2",
    "babel-loader": "^8.2.2",
    "html-webpack-plugin": "^5.3.1",
    "webpack": "^5.37.1",
    "webpack-cli": "^4.7.0",
    "webpack-dev-server": "^3.11.2"
  },
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}


Что можно с этим сделать?
  • Вопрос задан
  • 49 просмотров
Решения вопроса 1
module: {
        rules: [
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: [
                  '@babel/preset-env',
                  '@babel/preset-react' // установить этот пакет в dev-dependencies
                ]
              }
            }
          }
        ]
    },

Вообще лучше использовать .babelrc, пример:
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "corejs": 3,
        "modules": false,
        "useBuiltIns": "usage"
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-optional-chaining"
  ]
}

А в вебпаке просто:
module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы