最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

webpack中多文件打包配置的詳細(xì)流程

 更新時(shí)間:2023年11月20日 09:57:51   作者:周星星日記  
這篇文章主要介紹了webpack中多文件打包配置的詳細(xì)流程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

1.module,chunk,bundle的區(qū)別

  • moudle - 各個(gè)源碼文件,webpack中一切皆是模塊
  • chunk - 多模塊合并成的,如entryimport(), splitChunk
  • bundle - 最終的輸出文件

2.多文件打包配置

2.1 webpack.common.js

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { srcPath, distPath } = require('./paths')
module.exports = {
    entry: {
        index: path.join(srcPath, 'index.js'),
        other: path.join(srcPath, 'other.js')
    },
    module: {
        rules: [
          // ----- 同上文 ----
        ]
    },
    plugins: [
        // 多入口 - 生成 index.html
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'index.html'),
            filename: 'index.html',
            // chunks 表示該頁面要引用哪些 chunk (即上面的 index 和 other),默認(rèn)全部引用
            // chunks: ['index']  // 只引用 index.js
        }),
        // 多入口 - 生成 other.html
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'other.html'),
            filename: 'other.html',
            // chunks: ['other']  // 只引用 other.js
        })
    ]
}

上面的chunks配置,如果不配置chunks,那么打包出來的結(jié)果是默認(rèn)引入全部js

<body>
    <p>webpack demo</p>
    <input type="text"/>
	<script type="text/javascript" src="index.js"></script>
	<script type="text/javascript" src="other.js"></script>
</body>

如果配置了chunks,那么就只引入對應(yīng)的結(jié)果

<body>
    <p>webpack demo</p>
    <input type="text"/>
     <script type="text/javascript" src="index.js"></script>
</body>

2.2 webpack.prod.js

const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpackCommonConf = require('./webpack.common.js')
const { smart } = require('webpack-merge')
const { srcPath, distPath } = require('./paths')
module.exports = smart(webpackCommonConf, {
    mode: 'production',
    output: {
        // filename: 'bundle.[contentHash:8].js',  // 打包代碼時(shí),加上 hash 戳
        filename: '[name].[contentHash:8].js', // name 即多入口時(shí) entry 的 key
        path: distPath,
        // publicPath: 'http://cdn.abc.com'  // 修改所有靜態(tài)文件 url 的前綴(如 cdn 域名),這里暫時(shí)用不到
    },
    module: {
        rules: [
            //代碼重復(fù)
        ]
    },
    plugins: [
        new CleanWebpackPlugin(), // 會(huì)默認(rèn)清空 output.path 文件夾
        new webpack.DefinePlugin({
            // window.ENV = 'production'
            ENV: JSON.stringify('production')
        })
    ]
})

多入口時(shí),output出口的【name】變量會(huì)對應(yīng)到入口的變量名 

到此這篇關(guān)于webpack中多文件打包的文章就介紹到這了,更多相關(guān)webpack多文件打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

手机| 河北省| 旌德县| 都匀市| 嘉峪关市| 浦城县| 宜君县| 岫岩| 乌拉特后旗| 连南| 井陉县| 清涧县| 新营市| 明水县| 分宜县| 清流县| 台东县| 湖北省| 正镶白旗| 泾川县| 诏安县| 岳普湖县| 新郑市| 河曲县| 都匀市| 宁武县| 邵东县| 元朗区| 崇明县| 横山县| 阿城市| 乌恰县| 丹寨县| 天津市| 丰县| 宁明县| 清水县| 唐河县| 镇康县| 井陉县| 汪清县|