All Projects → CJY0208 → webpack-multiple-pages

CJY0208 / webpack-multiple-pages

Licence: other
自由代码分割、react/vue共存、支持高清方案、代码自动校验与格式化

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects
Vue
7211 projects

Projects that are alternatives of or similar to webpack-multiple-pages

Workflow Reactjs
My workflow with ReactJS + Webpack 3+
Stars: ✭ 150 (+134.38%)
Mutual labels:  webpack3
Webpack Book
From apprentice to master (CC BY-NC-ND)
Stars: ✭ 2,372 (+3606.25%)
Mutual labels:  webpack3
vue-number-keyboard
vue-number-keyboard是基于VUE实现的数字键盘插件,当前支持整数、小数数字输入、乱序键盘,demo中给出了常用的验证码、金额数字示例。数字键盘的大小包括字体尺寸支持响应式。
Stars: ✭ 51 (-20.31%)
Mutual labels:  webpack3
Vue Multiple Page
vue + webpack 多页/单页 脚手架
Stars: ✭ 163 (+154.69%)
Mutual labels:  webpack3
React Redux
React+Redux 入门示例项目,完整的构建部署流程,可扫下方二维码在线查看
Stars: ✭ 198 (+209.38%)
Mutual labels:  webpack3
webpack-boilerplate
Webpack 4 boilerplate (babel, eslint, prettier, jest, sass, postcss, hmr, browsersync)
Stars: ✭ 33 (-48.44%)
Mutual labels:  webpack3
Svg Sprite Loader
Webpack loader for creating SVG sprites.
Stars: ✭ 1,822 (+2746.88%)
Mutual labels:  webpack3
mongo-images
Ever wonder how you can create a full stack reactive application that also saves images? Well look no further! We've got Spring Webflux, Reactive Mongo Streams with GridFS, and Angular5!
Stars: ✭ 12 (-81.25%)
Mutual labels:  webpack3
Eth Hot Wallet
Ethereum wallet with erc20 support / web wallet - built using react, web3, eth-lightwallet
Stars: ✭ 205 (+220.31%)
Mutual labels:  webpack3
electron-react
A simple and compact boilerplate for electron and react (redux, router)
Stars: ✭ 66 (+3.13%)
Mutual labels:  webpack3
React Blog
personal blog design by react
Stars: ✭ 170 (+165.63%)
Mutual labels:  webpack3
Webpack By Sample
Learn webpack by sample, each of the samples contains a readme.md file that indicates the purpose of the sample plus an step by step guide to reproduce it.
Stars: ✭ 190 (+196.88%)
Mutual labels:  webpack3
webpack-starter
A basic setup of webpack goodness. Packed with common fronted workflow to help ease development headache. Ready with development and production config. Feel free to fork and enhance. Have an Awesome frontend coding!
Stars: ✭ 22 (-65.62%)
Mutual labels:  webpack3
Weex Start Kit
A weex + vue template to build web/Android/iOS project
Stars: ✭ 155 (+142.19%)
Mutual labels:  webpack3
webpack-mpvue-startup
A template with webpack 3 + mpvue 1 setup for projects startup
Stars: ✭ 13 (-79.69%)
Mutual labels:  webpack3
Iceberg
Front-End Boilerplate built with React + Babel + Webpack + SASS
Stars: ✭ 144 (+125%)
Mutual labels:  webpack3
eruda-webpack-plugin
A webpack plugin of eruda to help you develop mobile app
Stars: ✭ 56 (-12.5%)
Mutual labels:  webpack3
path-replace-loader
Path replace loader for webpack
Stars: ✭ 14 (-78.12%)
Mutual labels:  webpack3
react-typescript
React16 + HMR + typescript + webpack + tslint + tests
Stars: ✭ 21 (-67.19%)
Mutual labels:  webpack3
webpack-entry-plus
Generate dynamic webpack bundle output names from known or unknown entry files
Stars: ✭ 24 (-62.5%)
Mutual labels:  webpack3

Webpack Multiple Pages

注:目前侧重于移动端支持,调整webpack配置后可支持PC端


要解决的问题

资源的拆分与缓存的利用

常见的SPA应用做法是:webpack会把所有公共代码打成一个大包,入口html都会引用整个大包

在单页应用中,上述方案通常只需做基本的优化工作(如代码分割),但在多页面情况下,对公共代码的依赖情况不尽相同、复杂多变(A页面引用react、redux,B页面引用vuex、vue,A/B页面都用了lodash、moment等)

对于未完全使用大包的入口来说,如果直接引入所有公共文件,意味着有可能引入无用的资源,浪费网络性能

且大包中如果存在频繁变动的业务代码,将大大降低缓存的可用性。例如:大包中1%的代码发生了变动,缓存会失效,用户需要重新下载剩余99%未变动的代码

针对上述问题,解决的思路是:

  • 把大包有计划地拆成多个小包(如可配置地单独打包react相关资源、helpers资源)
  • 每个页面按自己的需求有针对性地引用

入口资源按需引用

在解决上述问题的过程中遇到这样的问题:无法直接实现 每个页面按自己的需求有针对性地引用

webpack中无法直接得知每个资源之间的引用关系,且html-webpack-plugin为各入口生成html时,需要手动配置插件的chunks属性来声明入口的依赖资源,不够智能

所以写了这个插件build/utils/auto-inject-plugin,来实现提取依赖关系并将相关资源自动注入html的功能(目前只针对于当前方案)

如何配置多入口

遇到过两种声明入口的方式

  1. 使用配置表来指定入口

    优点:无

    缺点:每次新增入口都需要配置,十分麻烦

  2. 约定一个目录存放所有入口

    优点:约定大于配置,消除了配置成本

    缺点:文件夹不好整理,如果将来页面很多,约定目录展开后看着眼睛会疼

目前采用的是上述第二种方法的升级版:以约定好的入口命名方式 <name> @[alias] 来实现自动采集,避免了上述存在的问题


特性介绍

  1. 自由的代码分割,在build/entries.js文件中配置代码分割

    • 多页面(project),每个页面一个js
    • 不同第三方库(lib)可定制化打包成某个js
    • 支持多个公共业务库(vendor),每个库一个js
    • 使用了AutoDllPlugin功能加快编译速度,但dll文件可能会对lib文件依赖造成影响,未了解其中原理需慎用
  2. 稳定的模块版本控制,各模块改动时,尽可能不影响其他模块的hash值,保证对资源缓存的最大利用

  3. 支持多框架共存(目前内置了reactvue),如A页面用react,B页面用vue,并引入了常用的工具,如下

  4. eslint代码校验,prettier格式化(commit时自动格式化)

  5. 内置了babel-polyfill垫片库,支持es各种新特性(装饰器、async/await等)

  6. 样式支持(抽取成独立的css文件)

    • 业务样式支持scss、sass、css
    • 第三方样式支持css、less
    • 支持cssModule,需要使用.m.css、.m.scss、.m.sass文件后缀
  7. 引入postcss,启用以下插件

  8. 集成了部分常用工具:lodashlodash/fpaxiosdate-fnsmd5fastclick

  9. 高清方案方面,flexible.js官方推荐使用viewport方案代替rem,参考《如何在Vue项目中使用vw实现移动端适配》,目前脚手架中已经默认启用了此方案,如使用px单位会自动转为vw,设计稿尺寸为720px

  10. 默认采用根目录下的template.html作为各入口html模板,可在入口目录下使用index.html文件自定义html,需要注意保留资源的动态注入模板


业务入口约定

  • 业务代码存放在src目录中,每个入口文件夹名称必须符合 <name> @[alias] 格式,入口文件必须为index.js

  • src/project中为多页面入口代码

  • src/vendor中为业务公共部分代码


开发构建启动方式

  1. 使用以下命令进行开发构建
npm start
  1. 启动服务器后,浏览器打开localhost:10001/wmp/任意页面名 来进行浏览

    例如 localhost:10001/wmp/reduxCouterDemo


编译方式

  1. 使用以下命令进行开发构建
npm run build
  1. 若要启动dist服务器,使用以下命令,服务器默认运行在10000端口
npm run server:dist

开发时构建的提速说明

  1. start命令中包含了dll命令,作用是尽可能将使用到的第三方资源打包成静态包,加快开发构建的速度

    所以在第三方资源不变动的情况下,start命令只需执行一次,后续开发构建的启动直接使用以下命令

    npm run dev

    如果明确得知第三方资源发生了变动,需再执行一次start命令(不执行也可,不过dev速度会受到影响)

  2. 我们在开发多页应用时,一般来说不会同时关注所有页面的开发工作,一次只会负责一个或数个页面的开发

    针对这个特性,我们在开发构建时可以指定(或忽略)部分入口以加快构建速度,具体操作如下

    在根目录下创建名为__entry.config.js的文件(此文件被git忽略),文件内容示例为:

    module.exports = {
      include: [
        /demo/
      ],
      exclude: [
        /vue/
      ]
    }

    include为指定包含的入口,exclude为需要排除的入口,每个项的内容为匹配入口路径的正则表达式

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].