美文网首页
webpack学习六(多页面通用打包方案)

webpack学习六(多页面通用打包方案)

作者: 梦行乌托邦 | 来源:发表于2020-07-13 12:23 被阅读0次

MPA

多页应用,我们之前的学习都是针对SPA单页应用

修改/src/index.js为

console.log('index page');

在src目录下新增list.js、detail.js,内容类似index.js
修改webpack.config.js

entry: { 
        index: './src/index.js', 
        list: './src/list.js',
        detail: './src/detail.js'
    },

输出的文件名直接对应源文件名就行了

output: {
        path: path.resolve(__dirname, './dist'), // 输出到哪里,必须是绝对路径
        filename: '[name].js'
    },

HtmlWebpackPlugin这里配置三个页面,但用同一个模板

plugins: [ // 插件,作用于webpack构建的整个生命周期
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'index.html'
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'list.html'
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'detail.html'
        }),
                ...
]

执行打包命令(npm run test),完后查看dist目录


img1

生成了三个html三个js,但是每个html文件中都引入了三个js文件?而我们的目的是各自的html引入各自的js!
我们可以在HtmlWebpackPlugin构造函数中新增一个字段“chunks”指定需要引入的js
修改webpack.config.js

plugins: [ // 插件,作用于webpack构建的整个生命周期
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'index.html',
            chunks: ['index']
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'list.html',
            chunks: ['list']
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'detail.html',
            chunks: ['detail']
        }),
                ...
]

重新打包,这样每个html都引入了指定的一个js入口文件

以上解决方案,过于繁琐,每增加一个页面都需要增加配置,现在我们来学习一下怎么做成一个通用方案?
调整目录结构


img2

安装glob来匹配路径

npm i glob -D

在项目根目录下新建webpack.mpa.js

const glob = require('glob');
const path = require('path');

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

const setMPA = () => {
    const entry = {};
    const htmlWebpackPlugins = [];
    
    const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
    
    entryFiles.map((item, index) => {
        const entryFile = item;
        const match = entryFile.match(/src\/(.*)\/index\.js$/);
        console.log(match);
        const pageName = match && match[1];
        entry[pageName] = entryFile;
        htmlWebpackPlugins.push(new HtmlWebpackPlugin({
            title: pageName,
            template: path.join(__dirname, `src/${pageName}/index.html`),
            filename: `${pageName}.html`,
            chunks: [pageName],
            inject: true // true或body会放到页面底部,false或head则相反
        }));
    });
    
    return {
        entry,
        htmlWebpackPlugins
    };
};

const {entry, htmlWebpackPlugins} = setMPA();

module.exports = {
    mode: 'development',
    entry,
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js'
    },
    plugins: [
        ...htmlWebpackPlugins
    ],
        ...
}

这里主要列出多页面打包配置的区别部分
然后修改package.json,增加打包命令:

"scripts": {
    "test": "webpack",
    "dev": "webpack --config webpack.dev.config.js",
    "server": "webpack-dev-server",
    "mpa": "webpack --config webpack.mpa.js"
  },

执行打包命令

npm run mpa

查看生成的dist


img3

你会发现一个问题,html文件的title标签是空的?为什么呢?因为我们还没有写入<%= htmlWebpackPlugin.options.title %>
修改/src/index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title><%= htmlWebpackPlugin.options.title %></title>
    </head>
    <body>
    </body>
</html>

重新打包,再看dist目录下的list.html,title标签之间加了“list”

相关文章

  • webpack学习六(多页面通用打包方案)

    MPA 多页应用,我们之前的学习都是针对SPA单页应用 修改/src/index.js为 在src目录下新增lis...

  • webpack 多页面打包通用方案

    20200810 ? 今天发布一个webpack多页面打包的通用方案 原理: 配置多个入口、多个 (出口依赖的)模...

  • webpack多页面打包通用方案

    1.构建约定: 所有入口文件按模块划分,并且都放在 src 下 不同的模块放到不同的模块文件夹下,每个模块的入口文...

  • webpack多页面应用打包通用方案

    多页面应用(MPA)概念 每次页面跳转的时候,后台服务器都会返回一个新的html文档,这种类型的网站也就是多页网站...

  • Webpack 多页面打包

    功能 打包编译JS 压缩合并css 图片打包处理 rem手机适配 postcss 多页面导航生成 模块热替换 开发...

  • webpack 打包传统页面 示例

    起因 尝试将使用 webpack 进行打包传统页面、jquery。 解决方案 使用 cdn 方式 示例代码 web...

  • vue cli3备忘

    多页面或者多项目配置projects.js 开启gzip先安装打包插件compression-webpack-pl...

  • [webpack]多页面打包工具

    根据公司需求,要做对SEO(Secrch enginner Optimization)友好的网站,SPA(Sing...

  • webpack 多页面打包配置(23)

    获取全套webpack 4.x教程,请访问瓦力博客 之前的配置是单页面应用,就是只有一个html文件。多页面应用就...

  • Webpack多页面ts隔离打包

    由于我们的项目都是把公共的模块抽出来进行开发,例如菜单和头部。现有个项目的需求是,里面有个独立的页面需要不使用这些...

网友评论

      本文标题:webpack学习六(多页面通用打包方案)

      本文链接:https://www.haomeiwen.com/subject/bzkacktx.html