详解Vue CLI3 多页应用实践和源码设计

对于一个网站来说,即需要h5页面也同时需要web页面,而h5和web页面共用很多代码,不做响应式,只能拆分两个页面来写,一个h5,一个web.用vue cli3怎么配置h5与web的应用呢?

解决思路:

首先,需要产生多页面应用,用webpack配置成多页面应用,一个h5一个web,这个网上已经有很多教程了,下面会再整理一次,接着把所有公共的代码提到多页面目录外面.

我们看一下官网给的 multi-page 的配置:需要在 vue.config.js 配置 pages,示例如下:

rush:js;"> pages: { index: { // page 的入口 entry: 'src/index/main.js',// 模板来源 template: 'public/index.html',// 在 dist/index.html 的输出 filename: 'index.html',// 当使用 title 选项时, // template 中的 title 标签需要是 <%= htmlWebpackPlugin.options.title %> title: 'Index Page',// 在这页面中包含的块,认情况下会包含 // 提取出来的通用 chunk 和 vendor chunk。 chunks: ['chunk-vendors','chunk-common','index'] },// 当使用只有入口的字符串格式时, // 模板会被推导为 `public/subpage.html` // 并且如果找不到的话,就回退到 `public/index.html`。 // 输出文件名会被推导为 `subpage.html`。 subpage: 'src/subpage/main.js' }

一个页面中就是一个对象,包含了如下配置:

首先,我们需要设计一下 src 目录下面放置 multi-page 的文件

看了很多多页项目,有 2 个方案:

  • 一种叫 pages 文件
  • 一种叫 views 或者其他名字的文件

大家自行选择或者定义就好了,这里我们选 pages

我们再看一下里面的文件

  • 入口文件文件名可以叫 main.js 或者 index.js
  • 模板文件:可以用统一的 'public/index.html',或者目录内放置一个自己的,取名 index.html
  • title:可以从一个文件里面取
rush:plain;"> src pages page1 index.html main.js App.vue page2 index.html main.js App.vue

下面就是通过函数生成 pages 的配置:

第一步:找到入口文件

可以用 glob

rush:js;"> const glob = require('glob')

pages 目录的位置,可以用相对路径,也可以用绝对路径

rush:js;"> const path = require('path') const PAGES_PATH = path.resolve(__dirname,'./src/pages')

定义一个 pages 对象:

rush:js;"> const pages = {}
{ // ... })

这里就是去设置对应几个 key 了,很多项目基本多是通过

/ 分隔符来对字符串进行数组话,然后简单地获取

但是熟悉 node.js path 模块的会如下处理:

rush:js;"> const pageName = path.basename(path.dirname(filepath))

往 pages 里面循环设置:

rush:js;"> pages[pageName] = { entry: filepath,filename: `${pageName}.html`,chunks: ['chunk-vendors',pageName] }

关于 template 稍微复杂一点,我们需要做判断,如果存在就用自定义的,如果不存在就用通用的

rush:js;"> const templatePath = path.dirname(filepath) + '/index.html'

然后通过 fs.existsSync 会判断自定义文件是否存在:

rush:js;"> if (!fs.existsSync(templatePath)) { // 入口如果不配置直接使用 templatePath = 'public/index.html' }

当然后面我们分享了源码之后,你就会发现你做了无用功

下面我们看一下源码实现部分:

每个版本的 cli-service 多有微小的改动

cli-service/lib/config/app.js 文件

定义了一个变量 multiPageConfig 获取 vue.config.js 取出来的 pages:

rush:js;"> const multiPageConfig = options.pages

清空一次 entry

rush:js;"> webpackConfig.entryPoints.clear()

通过 Object.keys 获取 keys,然后 forEach 循环

{ })

循环内部:

先定义要用的变量,从 multiPageConfig[name] 的每一个对象取:

rush:js;"> const { title,entry,template = `public/${name}.html`,filename = `${name}.html`,chunks } = normalizePageConfig(multiPageConfig[name])

normalizePageConfig 函数如下:

处理 subpage: 'src/subpage/main.js' 的情况

typeof c === 'string' ? { entry: c } : c

设置 entry

rush:js;"> webpackConfig.entry(name).add(api.resolve(entry))

hasDedicatedTemplate 是判断

用户传递的多页配置自定义模板路径是否存在:

rush:js;"> const fs = require('fs') const hasDedicatedTemplate = fs.existsSync(api.resolve(template))

templatePath 的处理细节:

htmlPath 路径是:

/Users/*/public/index.html

rush:js;"> const htmlPath = api.resolve('public/index.html')

defaultHtmlPath 路径是:

/Users/*/node_modules/@vue/cli-service/lib/config/index-default.html

rush:js;"> const defaultHtmlPath = path.resolve(__dirname,'index-default.html')

如果:

1、用户自定义的模板存在就直接给 templatePath 2、如果不存在,先取 public/index.html,再不行就取 node_modules 里面的

rush:js;"> const templatePath = hasDedicatedTemplate ? template : fs.existsSync(htmlPath) ? htmlPath : defaultHtmlPath

最终通过 html-webpack-plugin 插件生成指定名字的 html 文件到指定目录:

1、指定目录:

由 vue.config.js 中的 outputDir 来决定

rush:js;"> const outputDir = api.resolve(options.outputDir)

2、生成 webpack config 关于 html-webpack-plugin 的部分:

rush:js;"> const HTMLPlugin = require('html-webpack-plugin') webpackConfig .plugin(`html-${name}`) .use(HTMLPlugin,[pageHtmlOptions])

pageHtmlOptions 的处理细节:

传递给 html-webpack-plugin 插件的参数,这里认会设置 chunks 的,所以上面实战中配置也是无用功

rush:js;"> const pageHtmlOptions = Object.assign({},htmlOptions,{ chunks: chunks || ['chunk-vendors',name],template: templatePath,filename: ensureRelative(outputDir,filename),title })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

可以通过min-width属性来设置el-table-column的最小宽度。以...
yarn dev,当文件变动后,会自动重启。 yanr start不会自动重...
ref 用于创建一个对值的响应式引用。这个值可以是原始值(如...
通过修改 getWK005 函数来实现这一点。这里的 query 参数就是...
&lt;el-form-item label=&quot;入库类型&quot; ...
API 变动 样式类名变化: 一些组件的样式类名有所变动,可能需...