webpack转换es6语法(babel)

使用webpack转换es6语法(使用babel)

相关插件

  • 模块babel-loader
  • @babel/preset-env
  • @babel/plugin-proposal-decorators
  • @babel/plugin-proposal-class-properties

webpack.config.js

let path = require('path');
let HTMLWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    mode: 'development',  // 模式 模式有两种  production development
    entry: './src/index.js', // 入口
    output: {
        filename: "bundle.[hash:8].js", // 打包后的文件名
        path: path.resolve(__dirname, 'dist'), // 路径必须是绝对路径
    },
    plugins: [  // 放着所有的webpack插件
        new HTMLWebpackPlugin({
            template: "./src/index.html",
            filename: "index.html",
            minify: {
                removeAttributeQuotes: true,
                collapseWhitespace: true,
            },
            hash: true
        }),
        new MiniCssExtractPlugin({
            filename: 'main.css',
        })
    ],
    module: {   // 模块
        // loader
        rules: [
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            '@babel/preset-env'
                        ],
                        plugins: [
                            ['@babel/plugin-proposal-decorators', {legacy: true}],
                            ['@babel/plugin-proposal-class-properties', {loose: true}],
                        ]
                    }
                }
            }
        ]
    }
};

你可能感兴趣的:(webpack系列)