zoukankan      html  css  js  c++  java
  • webpack4

    本地安装:

    npm init -y

    cnpm install webpack webpack-cli webpack-dev-server --save-dev

    然后装一些所需要的loader和插件:

    如:

    const path = require("path");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    /*
        __dirname:当前文件夹的绝对路径
        path.join() 将第一个参数和第二个参数进行链接
    */
    const PATH = {
        main:path.join(__dirname,"./src/index.js"),
        build:path.join(__dirname,"./build")
    }
    //下面的代码都是 webpack的配置项
    module.exports = {
        entry:{
            //这里面的key值决定了下面name的名字叫什么
            main:PATH.main
        },
        output:{
            filename:"[name].js",//app.js
            path:PATH.build
        },
        mode:"development",
        //做模块的处理 用loader进行处理
        module: {
            rules:[
                {
                    test:/\.jsx?$/,
                    use:{
                        loader:"babel-loader",
                        options:{
                            presets:["@babel/env","@babel/react"]
                        }
                    }
                },
                {
                    test:/\.(css)$/,
                    use:[
                        {
                            loader: MiniCssExtractPlugin.loader,
                            options: {
                                //publicPath: "/"
                            }
                        },
                        {
                            loader: "css-loader",
                            options: {
                                importLoaders: 1,
                                minimize: process.env.NODE_ENV == "production"
                            }
                        },
                        {
                            loader: "style-loader"
                        }
                    ]
                },
                {
                    test: /\.(jpe?g|png|gif|svg)/,
                    use: {
                        loader: "url-loader",
                        options: {
                            limit: 1024,
                            outputPath: "images/",
                            name: "[name].[ext]"
                        }
                    }
                },
                {
                    test: /\.(eot|ttf|woff2?)/i,
                    use: {
                        loader: "url-loader",
                        options: {
                            outputPath: "fonts/",
                            name: "[name].[ext]"
                        }
                    }
                }
            ]
        },
        plugins:[
            new HtmlWebpackPlugin({
                filename:"index.html",
                template:"./index.html",
                hash: true
            }),
            new MiniCssExtractPlugin({
                filename: "css/[name].bundle.css",
                chunkFilename: "css/[name].chunk.css"
            })
        ],
        resolve: {
            extensions: [".js", ".jsx", ".json", ".css", ".html"]
        },
        devtool: "source-map",//该选项控制是否以及如何生成源映射。选择一种源映射样式,以增强调试过程。这些值可以显著地影响构建和重建速度。source-map之后你会发现你的 output 目录下多了一个 index.js.map 文件,此文件记录了sourceMap 行列信息如何映射源代码的信息
    
    devServer: {//访问本地静态文件 compress: false, //contentBase: "./public", port: 8080, proxy: { "/": "http://localhost:3000" } } }

      

  • 相关阅读:
    【UVA12093】Protecting Zonk (树形DP)
    【UVA1579】俄罗斯套娃 Matryoshka (动态规划)
    【UVA1371】Period (二分+DP)
    【UVA1379】Pitcher Rotation (贪心+DP)
    【UVA1633】禁止的回文串(状压DP)
    【POJ3358】
    【POJ2773】Happy 2006 欧几里德
    【POJ1284】Primitive Roots 欧拉函数
    【POJ2478】Farey Seque
    【POJ3243】拓展BSGS(附hash版)
  • 原文地址:https://www.cnblogs.com/wyryuebanwan/p/10334871.html
Copyright © 2011-2022 走看看