webpack

webpack成神之路(三)

2019-05-12  本文已影响13人  AAA前端
  1. 现在我们来处理js模块,比如es6语法转换为es5语法。这里就需要babel-loader模块了。https://www.npmjs.com/package/babel-loader
  1. 现在我们使用es6里面的 class


    image.png
  1. 现在我们要给class 类添加 @log 装饰器


    image.png
  1. 现在我们在a.js中再定义一个类以及一个generator;让我们来看一下有什么需要改进的。


    image.png
{
  "presets": ["@babel/preset-env"],
  "plugins": [
    "@babel/plugin-transform-runtime",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ]
  ]
}
image.png

现在启动服务,控制台不报错了,而且打包后也可以看到多了很多代码,就是把很多需要用的代码提出来了。

  1. 现在我们在index.js中使用es7语法 image.png
  1. 现在我们可以给我们的js添加语法规范eslint;

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
    mode: 'development',  // development production
    optimization: {
        minimizer: [new UglifyJsPlugin(
            {
                cache: true,  // 启用cache缓存
                parallel: true, // 并发打包
                sourceMap: true, // 映射原代码地址
              }
        ), new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
    },
    entry: './src/index.js', 
    output: {
        filename: "index.[hash:8].js",
        path: path.resolve(__dirname, 'build')  
    },
    devServer: {
        port: 8080, 
        contentBase: './', 
    },
    plugins: [  
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            template: './index.html',  
            filename: 'index.html', 
            minify: {  
                removeAttributeQuotes: true,
                collapseWhitespace: true, 
            }
        }),
        new MiniCssExtractPlugin({
            filename: '[name].css',
            chunkFilename: '[id].css',
          }),
    ],
    module: { // 模块
        rules: [
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader','postcss-loader'] 
            },
            {
                test: /\.less$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader','postcss-loader', 'less-loader']
            },
            // {
            //     test: /\.js$/,
            //     exclude: /node_modules/,
            //     include: path.resolve(__dirname, 'src'),
            //     use:{
            //         loader: 'eslint-loader',
            //         options:{
            //             enforce: 'pre'  // pre匹配到的js先执行   post后执行 
            //         }
            //     }
            // },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                include: path.resolve(__dirname, 'src'),
                use: {
                  loader: 'babel-loader',
                //   options: {
                //     presets: ['@babel/preset-env'],
                //     "plugins": [
                //         ["@babel/plugin-proposal-decorators", { "legacy": true }],
                //         ["@babel/plugin-proposal-class-properties", { "loose" : true }]
                //       ]
                //   }
                }
              }

        ]
    }
}
{
  "presets": ["@babel/preset-env"],
  "plugins": [
    "@babel/plugin-transform-runtime",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ]
  ]
}
上一篇 下一篇

猜你喜欢

热点阅读