webpack+react多页面开发(二)-终极架构

webpack4+react16多页面架构

webpack在单页面打包上应用广泛,以create-react-app为首的脚手架众多,单页面打包通常指的是将业务js,css打包到同一个html文件中,整个项目只有一个html文件入口,但也有许多业务需要多个页面不同的入口,比如不同的h5活动,或者需要支持seo的官方网站,都需要多个不同的htmlwebpack-react-multi-page架构让你可以在多页面在项目开发中自动化打包新创建页面并保证每个页面都可以热更新 ,build打包后有清晰的文件层次结构。

概览

key value
名称 webpack+react多页面架构
描述 简单易用的多页面自动化开发架构
开发者 leinov
发布日期 2018-11-07
版本 2.0
仓库 github地址

特性

  • ??????????? 支持多页面同时热加载开发
  • ?? 自动识别新创建页面
  • ?? 每个页面生成个性化信息
  • ?? 分类打包
  • ?? 灵活扩展

安装&使用

// clone
git clone [email protected]:leinov/webpack-react-multi-page.git

// 安装依赖包
npm install

// 开发
npm run dev

// 编译打包
npm run build

// 启动生产页面
npm start

新创建页面在src下添加文件夹并创建pageinfo.json 然后npm run dev 即可

|-- src
    |-- index/
    |-- page2/
        |-- index.js
        |-- pageinfo.json

项目架构

技术使用

  • react16
  • webpack4
    • html-webpack-plugin 生成html文件
    • mini-css-extract-plugin css分离打包
    • uglifyjs-webpack-plugin js压缩
    • optimize-css-assets-webpack-plugin css压缩
  • es6
  • babel
  • node
    • opn 打开浏览器
    • compression 开启gzip压缩
    • express
    • fs
  • git

目录结构

|-- webpack-react-multi-pages //项目
    |-- dist //编译生产目录
        |-- index
            |-- index.css
            |-- index.js
        |-- about
            |-- about.css
            |-- about.js
        |-- images
        |-- index.html
        |-- about.html
    |-- node_modules //node包
    |-- src //开发目录
        |-- index //index页面打包入口
            |-- images/
            |-- js
                |-- app.js// 业务js
            |-- index.sass
            |-- index.js //页面js入口
        |-- about //about页面打包入口
            |-- images/
                |--js
                    |-- app.js// 业务js
            |-- about.sass
            |-- about.js //页面js入口
        |-- template.html // html模板
        |-- style.sass //公共sass
    |-- webpackConfig //在webpack中使用
        |-- getEntry.js //获取入口
        |-- getFilepath.js //src下需要打包页面文件夹
        |-- htmlconfig.js //每个页面html注入数据
    |-- package.json
    |-- .gitignore
    |-- webpack.config.js //webpack配置文件
    |-- www.js //生产启动程序

wiki

webpack打包单页面应用

webpack在单页面打包上应用广泛,以create-react-app为首的接触脚手架众多,单页面打包通常指的是将业务js,css打包到同一个html文件中,整个项目只有一个html文件入口

webpack单页面打包配置

webpack.config.js

module.exports = (env, argv) => ({
    entry: ".src/index.js",
    output: {
        path: path.join(__dirname, "dist"),
        filename: "bundle.js"
    },
    module: {
        rules: [
        ...
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: "首页",
            filename:"index.html",
            favicon:"",
            template: "./src/template.html",

        })
    ]
});

这样就可以在dist文件夹下打包出一个下面这样的文件

<!DOCTYPE html>
<html lang="en">
    <head>
    <title>首页</title>
    <body>
        <div id="root"></div>
        <script type="text/javascript" src="bundle.js"></script></body>
</html>

webpack多页面打包配置

webpack 的entry支持两种种格式

打包单个文件

module.exports = {
  entry: ‘.src/file.js‘,
  output: {
    path: path.resolve(__dirname, ‘dist‘),
    filename: ‘bundle.js‘
  }
};

这样就会在dist下打包出一个bundle.js

打包出多个文件

module.exports = {
  entry: {
    index:"./src/index.js",
    about:"./src/about.js"
  },
  output: {
    path: path.resolve(__dirname, ‘dist‘),
    filename: ‘[name].js‘ index.js,about.js这两个文件
  }
};

上面在dist下打包出两个与entry属性名对应的js文件

将每个js挂载到相应的html文件上

这里我们需要用到html-webpack-plugin这个webpack插件,每添加一个页面就需要在plugins添加一个new HtmlWebpackPlugin({....})

const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = (env, argv) => ({
    entry: {
        index:"./src/index.js",
        about:"./src/about.js"
    },
    output: {
        path: path.resolve(__dirname, ‘dist‘),
        filename: ‘[name].js‘ index.js,about.js这两个文件
    }
    ....//其他配置
    plugins: [
        new HtmlWebpackPlugin(
            {
                filename:"index.html",//生成的index.html
                template: "./src/template.html",}) //模板
                chunks:["index"]
            }),
        new HtmlWebpackPlugin(
            {
                filename:"about.html",//生成的index.html
                template: "./src/template.html",}) //模板
                chunks:["index"]
            })
    ]
})

html-webpack-plugin 会通过 template.html 模板生成对应的filename名的html文件,并一并打包到output中对应的文件夹下,注意,在没有特殊配置的情况下所有打包的文件都是对应到output中 path 这个目录下,也包括html。这里的 chunks 需要注意,它是确定该html需要引入哪个js,如果没写的话,默认会引出所有打包的js,当然这不是我们想要的。

上面的配置最终可以在dist下打包出下面的文件结构

|-- dist
    |-- index.js
    |-- about.js
    |-- index.html //内挂载index.js
    |-- about.html //内挂载about.js

通过上面这样的配置,再加上devServer,我们已经可以实现多页面的配置开发了,但这样很不智能,因为你每增加一个页面,就要在wepback里面配置一次,会非常繁琐,所以我们来优化下,让我们只专注于开发页面,配置交给webpack自己.

webpack多页面配置优化

我们再看下src下面的文件结构

|-- src
    |-- index
        |-- app.js
        |-- index.scss
        |-- index.js
    |-- about
        |-- app.js
        |-- index.scss
        |-- index.js

src下面每个文件夹对应一个html页面的js业务,如果我们直接把文件夹对应入口js找到并把他们合并生成对应的entry,那是不是就不用手动写entry了呢,是的!

遍历文件目录

/* eslint-env node */

/**
 * @file: getFilePath.js  遍历文件目录
 * @author: leinov
 * @date: 2018-10-11
 */

const fs = require("fs");

/**
 * 【遍历某文件下的文件目录】
 *
 * @param {String} path 路径
 * @returns {Array} ["about","index"]
 */
module.exports = function getFilePath(path){
    let fileArr = [];
    let existpath = fs.existsSync(path); //是否存在目录
    if(existpath){
        let readdirSync = fs.readdirSync(path);  //获取目录下所有文件
        readdirSync.map((item)=>{
            let currentPath = path + "/" + item;
            let isDirector = fs.statSync(currentPath).isDirectory(); //判断是不是一个文件夹
            if(isDirector && item !== "component"){ // component目录下为组件 需要排除
                fileArr.push(item);
            }
        });
        return fileArr;
    }
};

比如在src下有index页面项目,about项目 遍历结果为["index","about"];

遍历生成打包入口数组

/* eslint-env node */
/**
 * @file: getEntry.js 获取entry文件入口
 * @author: leinov
 * @date: 2018-10-11
 * @update: 2018-11-04 优化入口方法 调用getFilePath
 */
const getFilePath = require("./getFilepath");
/**
 * 【获取entry文件入口】
 *
 * @param {String} path 引入根路径
 * @returns {Object} 返回的entry { "about/aoubt":"./src/about/about.js",...}
 */
module.exports = function getEnty(path){
    let entry = {};
    getFilePath(path).map((item)=>{
        /**
         * 下面输出格式为{"about/about":"./src/aobout/index.js"}
         * 这样目的是为了将js打包到对应的文件夹下
         */
        entry[`${item}/${item}`] = `${path}/${item}/index.js`;
    });
    return entry;
};

这里我们使用getFilepath获取的数组,在获取到每个目录下的js文件,组合成一个js入口文件的如下格式的对象。

{
    "index/index":"./src/index/index.js",
    "about/about":"./src/about/index.js"
}

在webpack中使用getEntry

const getEntry = require("./webpackConfig/getEntry");
const entry = getEntry();

module.exports = (env, argv) => ({
    entry: entry,
})

这样我们就自动获取到了entry

html-webpack-plugin自动配置

因为每个页面都需要配置一个html,而且每个页面的标题,关键字,描述等信息可能不同,所以我们在每个页面文件夹下创建一个pageinfo.json,通过fs模块获取到json里信息再遍历到对应得html-webpack-plugin中生成一个html插件数组。

index/pageinfo.json 生成index.html页面信息

{
    "title":"首页",
     "keywords":"webpack多页面"
}

about/pageinfo.json 生成about.html页面信息供

{
    "title":"关于页面",
    "keywords":"webpack多页面关于页面"
}

通过fs遍历读取并生成HtmlWebpackPlugin数组供webpack使用

遍历html插件数组

/**
 * @file htmlconfig.js  页面html配置
 * @author:leinov
 * @date: 2018-10-09
 * @update: 2018-11-05
 * @use: 动态配置html页面,获取src下每个文件下的pageinfo.json内容,解析到HtmlWebpackPlugin中
 */

const fs = require("fs");
const HtmlWebpackPlugin = require("html-webpack-plugin");//生成html文件
const getFilePath = require("./getFilepath");
let htmlArr = [];

getFilePath("./src").map((item)=>{
    let infoJson ={},infoData={};
    try{
        // 读取pageinfo.json文件内容,如果在页面目录下没有找到pageinfo.json 捕获异常
        infoJson = fs.readFileSync(`src/${item}/pageinfo.json`,"utf-8");//
        infoData = JSON.parse(infoJson);
    }catch(err){
        infoData = {};
    }
    htmlArr.push(new HtmlWebpackPlugin({
        title:infoData.title ? infoData.title : "webpack,react多页面架构",
        meta:{
            keywords: infoData.keywords ? infoData.keywords : "webpack,react,github",
            description:infoData.description ? infoData.description : "这是一个webpack,react多页面架构"
        },
        chunks:[`${item}/${item}`], //引入的js
        template: "./src/template.html",
        filename : item == "index" ? "index.html" : `${item}/index.html`, //html位置
        minify:{//压缩html
            collapseWhitespace: true,
            preserveLineBreaks: true
        },
    }));
});

module.exports = htmlArr;

wbpack终极配置

const path = require("path");
const getEntry = require("./webpackConfig/getEntry"); //入口配置
const entry = getEntry("./src");
const htmlArr =require("./webpackConfig/htmlConfig");// html配置

module.exports = (env, argv) => ({
    entry: entry
    output: {
        path: path.resolve(__dirname, ‘dist‘),
        filename: ‘[name].js‘
    }
    ....//其他配置
    devServer: {
    port: 3100,
    open: true,
    },
    plugins: [
        ...htmlArr
    ]
})

这样一个自动化完整的多页面架构配置就完成了,如果我们要新创建一个页面

    1. 在src下创建一个文件目录
    1. 在新创建的文件目录下添加index.js(必须,因为是webpack打包入口文件)
    1. 在新创建文件夹下添加pageinfo.json(非必须) 供html插件使用
    1. npm run dev开发

      完整代码参考项目code

版本

版本 日期 分支 备注
2.0 2018-11-08 master 优化html插件自动化
1.0 2018-10-07 version1.0 第一版

原文地址:https://www.cnblogs.com/leinov/p/9932443.html

时间: 2024-07-29 19:46:35

webpack+react多页面开发(二)-终极架构的相关文章

webpack+react+redux+es6开发模式

一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入门教程   redux middleware 详解   Redux研究 React 入门实例教程 webpack学习demo NPM 使用介绍 三.工程搭建 之前有写过 webpack+react+es6开发模式 ,文章里介绍了一些简单的配置,欢迎访问. 1.可以npm init, 创建一个新的工程

Webpack之“多页面开发”最佳实战

前言:相信之前看过这篇文章,前端构建工具之“Webpack”的朋友,对于Webpack有了一定的了解.那么今天就跟大家分享下:如何利用webpack,来进行多页面项目实战开发. 一.项目初始化安装 1.先安装node.js 和 webpack 第一步:如果没有安装node的朋友,可以去node中文官网下载.安装好后,打开cmd工具,输入: 1  node - v // 如果有显示内容则证明安装成功(这是看我们node版本的指令) 如下图: 第二步: 全局安装webpack 1  npm  ins

使用 webpack + react + redux + es6 开发组件化前端项目

因为最近在工作中尝试了 webpack.react.redux.es6 技术栈,所以总结出了一套 boilerplate,以便下次做项目时可以快速开始,并进行持续优化. 项目结构规划 每个模块相关的 css.img.js 文件都放在一起,比较直观,删除模块时也会方便许多.测试文件也同样放在一起,哪些模块有没有写测试,哪些测试应该一起随模块删除,一目了然. build |-- webpack.config.js # 公共配置 |-- webpack.dev.js # 开发配置 |-- webpac

webpack+react+antd 单页面应用实例

webpack+react+antd 单页面应用实例 React框架已经火了好长一段时间了,再不学就out了! 对React还没有了解的同学可以看看我之前的一篇文章,可以快速简单的认识一下React.React入门最好的实例-TodoList 自己从开始接触react一窍不通,到慢慢的似懂非懂,通过各种途径学习也有一阵了.学习过程中还会接触到很多新的东西,比如ES6.webpack,过程艰辛谁人懂,见坑填坑慢慢来.今天把学习过程过滤了一下,只说项目实际需要用的东西,总结了一套能看到的东西,分享给

webpack+react+es6开发模式

一.前言 实习了两个月,把在公司用到的前端开发模式做个简单的整理.公司里前端开发模式webpack+react+redux+es6,这里去掉了redux. webpack, react, redux等学习网址:http://www.cnblogs.com/hujunzheng/p/5405780.html 二.简单的步骤条组件 1.通过react自定义的组件进行模拟   注:只是用了react,用到相关react的js请到 https://github.com/hjzgg/webpack-rea

注册绑定页面及微信二维码登陆页面开发项目总结

乐帝来到新的实习单位,也许是之前面试或者在爱奇艺实习的履历,很快被项目组"委以重任".而不是老套路刚入职,先在架构师那培训两周,专心钻研框架,不问具体业务.乐帝只有几天看框架的时间,当被分配给框架页面时,还是不能得心应手,正如同事所说,学习还得按部就班,写写例子,看代码是不行的.    目前这家公司类似<走出软件作坊>作者阿朱所在行业,是面向中大型企业,提供人才管理解决方案的软件公司,时髦的词叫SAAS.这类公司层次要比外包公司高,却还有很多外包公司的特点,不像互联网公司有

Webpack + React全栈工程架构项目实战精讲

详情请交流  QQ  709639943 01.Webpack + React全栈工程架构项目实战精讲 02.跨平台混编框架 MUI 仿豆瓣电影 APP 03.Node.js入门到企业Web开发中的应用 04.Python3 全网最热的Python3入门+进阶 比自学更快上手实际开发 05.Java秒杀系统方案优化 高性能高并发实战 06.Java深入微服务原理改造房产销售平台 07.快速上手Linux 玩转典型应用 08.全面系统讲解CSS 工作应用+面试一步搞定 09.Java Spring

webpack4+react多页面架构

webpack在单页面打包上应用广泛,以create-react-app为首的脚手架众多,单页面打包通常是将业务js,css打包到同一个html文件中,整个项目只有一个html文件入口,但也有许多业务需要多个页面不同的入口,比如不同的h5活动,或者需要支持seo的官方网站,都需要多个不同的html,webpack-react-multi-page架构让你可以实现多页面架构,在项目开发中保证每个页面都可以热更新并且打包后有清晰的文件层次结构. Github地址 项目架构 技术使用 react16

React Native移动开发实战-3-实现页面间的数据传递

React Native使用props来实现页面间数据传递和通信.在React Native中,有两种方式可以存储和传递数据:props(属性)以及state(状态),其中: props通常是在父组件中指定的,而且一经指定,在被指定的组件的生命周期中则不再改变. state通常是用于存储需要改变的数据,并且当state数据发生更新时,React Native会刷新界面. 了解了props与state的区别之后,读者应该知道,要将首页的数据传递到下一个页面,需要使用props.所以,修改home.