webpack 3.X学习之多页面打包

做者网站原文:http://hawkzz.com/blog/blog/1514542087911html

简介

咱们开发不可能只写一个页面,每次都要写不少页面,这时为了开发效率,咱们使用前端自动化工具webpack,那么webpack是如何打包页面的呢?又是如何打包多页面的呢?前端

单页面打包

咱们知道要打包单页面的方法,很简单,配置入口,和html插件,webpack

const HtmlWebpackPlugin = require('html-webpack-plugin');

const config = {
    entry:{
        index:'./src/index.js'
    },
    output:{
        path: path.join(__dirname, 'dist'),
        filename: 'js/index.js'
    }
    ...
    plugins:[
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html',
            hash: true,
            minify: {
                removeAttributeQuotes:true,
                removeComments: true,
                collapseWhitespace: true,
                removeScriptTypeAttributes:true,
                removeStyleLinkTypeAttributes:true
            }
        })
    ]
}

上面的配置就是打包一个单页面的代码,具体配置项的意思请参考HTMLWebpackPlugin;程序员

如何打包多页面

在学了webpack以后,个人感觉是我会配置webpack了,也能运行了,可是学习的过程当中都是单页面的,那么多页是如何打包的呢?其实多页面的打包和单页面的打包的原理是同样的,只是多配置几个对应的入口,和出口,以及HtmlWebpackPlugin对象;固然你彻底能够像下面这样:web

const config = {
    entry:{
        index:'./src/index.js',
        info:'./src/index.js'
    },
    output:{
        path: path.join(__dirname, 'dist'),
        filename: 'js/[name].js'
    }
    ...
    plugins:[
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html',
            chunks:['index'],
            hash: true,
            minify: {
                removeAttributeQuotes:true,
                removeComments: true,
                collapseWhitespace: true,
                removeScriptTypeAttributes:true,
                removeStyleLinkTypeAttributes:true
            }
        }),
        new HtmlWebpackPlugin({
            filename: 'info.html',
            template: './src/info.html',
            hash: true,
            chunks:['info'],
            minify: {
                removeAttributeQuotes:true,
                removeComments: true,
                collapseWhitespace: true,
                removeScriptTypeAttributes:true,
                removeStyleLinkTypeAttributes:true
            }
        })
    ]
}

细心的你确定发现我改变了几个地方,一是,把output.filename的‘js/index.js’变成了‘js/[name].js’,这是由于咱们是多页面,每一个页面对应相应的js这样方便管理,二是,在HtmlWebpackPlugin对象中添加了chunks这个属性,chunk属性是让你选择对应的js模块;函数

上面这种写法固然是没有问题,这是只有两个页面无所谓,那么有十个甚至更多呢,咱们一直作着重复的事,这不是咱们程序员的风格,咱们就是为了可以偷懒才作程序员的不是吗?(固然还有高工资(#^.^#)),接下来咱们来抽离这些重复的事;工具

首先,咱们经过Node的glob对象,来获取咱们存在的html或js;学习

/**
*
* @param {string}  globPath  文件的路径
* @returns entries
*/
function getView(globPath,flag){
    let files = glob.sync(globPath);

    let entries = {},
    entry, dirname, basename, pathname, extname;

    files.forEach(item => {
        entry = item;
        dirname = path.dirname(entry);//当前目录
        extname = path.extname(entry);//后缀
        basename = path.basename(entry, extname);//文件名
        pathname = path.join(dirname, basename);//文件路径
        if (extname === '.html') {
            entries[pathname] = './' + entry;
        } else if (extname === '.js') {
            entries[basename] = entry;
        }
    });

    return entries;
}

既然,咱们已经有了getView()函数,能够获取html和js文件,那么咱们就能够肯定有多少个入口,和多少个页面;网站

let entriesObj = getView('./src/js/*.js');

let config = {
    entry:entriesObj,
    ...
}

上面咱们就配置好了入口,不须要咱们手动添加了,有多少js就有多少入口;ui

let pages = Object.keys(getView('./src/*html'));

pages.forEach(pathname => {
    let htmlname = pathname.split('src\\')[1];
    let conf = {
        filename: `${htmlname}.html`,
        template: `${pathname}.html`,
        hash: true,
        chunks:[htmlname],
        minify: {
            removeAttributeQuotes:true,
            removeComments: true,
            collapseWhitespace: true,
            removeScriptTypeAttributes:true,
            removeStyleLinkTypeAttributes:true
        }
    }

    config.plugins.push(new HtmlWebpackPlugin(conf));
});

最后,咱们获取HTML页面,和添加对应页面的HTMLWebpackPlugin对象;

后记

经过以上的三个步骤,就能够配置好一个能够打包多页面的webpack工具;本人的水平比较有限,在书写的过程当中,可能有不少说的比较模糊,请多多包涵,也请大神拍砖,多多指教

相关文章
相关标签/搜索