22、webpack import() 懶加載

import本身是懶加載模式,但是使用時(shí)有限制必須放在第一行 ,import()可以在任意位置使用

案例

src/index.js (src/public.js 自行創(chuàng)建本章忽略)

//
window.btnclick = () =>{
    import('./public').then((data)=>{
        console.log('data == ', data.default);
    })
}
document.write('<div onClick="window.btnclick()">點(diǎn)擊</div>')

webpack.config.js

//  webpack是node寫出來(lái)的, 需要node的寫法
let path = require('path');
// html-webpack-plugins 插件
let HtmlWebpackPlugins = require('html-webpack-plugin');
// 抽離css為單獨(dú)文件
let MiniCssExtractPlugin = require('mini-css-extract-plugin');
// 導(dǎo)入樣式壓縮
let OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin');
// 壓縮js
const TerserPlugin = require('terser-webpack-plugin')
// webpack相關(guān)配置
module.exports = {
    // 壓縮 model 必須是production才有效果
    optimization: {
        minimizer: [
            new TerserPlugin({}),
            new OptimizeCssPlugin()
        ]
    },
    // 開發(fā)服務(wù)的配置
    devServer: {
        // 端口,默認(rèn)8080
        port: '8099',
        // 啟動(dòng)壓縮
        //compress: true
    },
    // 模式,2種:生產(chǎn)模式(production)和開發(fā)模式(development)
    // 開發(fā)模式不壓縮打包后代碼,生產(chǎn)模式壓縮打包后代碼
    mode: 'production',
    // 入口,表示從哪里開始打包
    entry: './src/index.js',
    // 表示出口(輸出后文件相關(guān)配置)
    output: {
        // 哈希8位
        filename: 'main.[hash:8].js',
        // 輸出后的路徑(必須是一個(gè)絕對(duì)路徑)
        path: path.resolve(__dirname, '../dist')
    },
    // 插件配置
    plugins: [
        new HtmlWebpackPlugins({
            // 模板位置
            template: 'index.html',
            // 文件名
            filename: 'index.html'
        }),
        // 抽離css為單獨(dú)文件
        new MiniCssExtractPlugin({
            filename: 'css/main.css'
        })
    ],
    // 模塊處理
    module: {
        // loader
        rules: [{
                test: /\.html$/,
                use: 'html-withimg-loader'
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        // 打包少于1000kb用base64,否則使用file-loader產(chǎn)生文件
                        limit: 1000,
                        // 產(chǎn)出路徑
                        outputPath: 'img/',
                        // 只給圖片添加前綴,如果使用publicPath,outputPath無(wú)效
                        // publicPath: 'http://itssh.cn/'
                    }
                }
            },
            {
                test: /\.js$/, //匹配js文件
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            '@babel/preset-env'
                        ],
                        plugins: [
                            ["@babel/plugin-proposal-decorators", {
                                "legacy": true
                            }],
                            ["@babel/plugin-proposal-class-properties", {
                                "loose": true
                            }],
                            "@babel/plugin-transform-runtime"
                        ]
                    }
                }
            },
            {
                test: /\.css$/, //匹配css文件
                // css-load處理完 再 抽離,從后往前執(zhí)行l(wèi)oader
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
            },
            {
                test: /\.less$/, //匹配less文件
                // 從后向前執(zhí)行
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'less-loader']
            }
        ]
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容