Commit 256a9a69 authored by huanglu's avatar huanglu

feat: 添加骨架屏配置

parent 7e30d13c
......@@ -3,12 +3,15 @@
### 项目结构
```
├── README.md
├── babel.config.js
├── config
│   ├── index.js // 项目配置
│   ├── cos-config.js // 上传资源文件到COS
│   ├── index.js
│   ├── skeleton.config.js // 骨架屏配置
│   └── util.js
├── jsconfig.json
├── mock // 存放mock数据
├── mock
│   └── index.js
├── package-lock.json
├── package.json
......@@ -17,22 +20,22 @@
│   └── index.html
├── scripts
│   ├── skeleton-plugin.txt
│   └── skeleton.js // skeleton脚本
│   └── skeleton.js
├── src
│   ├── App.vue
│   ├── api
│   │   └── index.js
│   ├── assets
│   │   ├── images // 存放图片
│   │   ├── images
│   │   │   ├── index.js
│   │   │   ├── nocontent.png
│   │   │   ├── video_bg.png
│   │   │   └── video_play.png
│   │   └── styles // 全局样式文件
│   │   └── styles
│   │   ├── clear.scss
│   │   ├── common.scss
│   │   └── variable.scss
│   ├── components // 组件
│   ├── components
│   │   ├── error-nofity
│   │   │   ├── images
│   │   │   │   ├── close.png
......@@ -42,16 +45,19 @@
│   │   ├── index.js
│   │   └── video-item
│   │   └── index.vue
│   ├── constant // 常量文件夹目录
│   │   └── index.js // 统一通过该目录进行导出
│   ├── layout // 全局布局组件
│   ├── constant
│   │   └── index.js
│   ├── entry-skeleton.js // 骨架屏入口文件
│   ├── layout
│   │   └── normal-layout.vue
│   ├── main.js
│   ├── plugins // 插件
│   ├── plugins
│   │   ├── index.js
│   │   └── nativebrige.js
│   ├── router
│   │   └── index.js
│   ├── skeleton // 骨架屏目录
│   │   └── home-skeleton.vue
│   ├── store
│   │   ├── index.js
│   │   └── modules
......@@ -69,12 +75,12 @@
│   └── index
│   └── index.vue
├── vue.config.js
├── yarn-error.log
└── yarn.lock
```
### 配置路由的骨架屏
- yarn dev后,在浏览器 中, cmd|ctl + enter, 或者在控制台输入 toggleBar 打开骨架屏生成页面
- 骨架屏只能在 开发阶段生成,并放在 skeleton-pages 目录下
- config/skeleton.config.js 配置骨架屏
### mock
......
const { resolve } = require('./util');
module.exports = {
webpackConfig: {
entry: {
app: resolve('src/entry-skeleton.js')
}
},
minimize: true,
quiet: true,
router: {
mode: 'history',
routes: [
{
path: '/',
skeletonId: 'skeleton-home'
}
]
}
}
\ No newline at end of file
......@@ -13,8 +13,7 @@
"dev": "npm run serve:dev",
"stage": "npm run serve:stage",
"prod": "npm run serve:prod",
"lint": "vue-cli-service lint",
"postinstall": "node scripts/skeleton.js"
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^0.19.2",
......@@ -38,12 +37,12 @@
"eslint-plugin-vue": "^6.2.2",
"lint-staged": "^9.5.0",
"node-sass": "^4.14.1",
"page-skeleton-webpack-plugin": "^0.10.12",
"postcss-px-to-viewport": "^1.1.1",
"sass": "^1.26.8",
"sass-loader": "^8.0.2",
"validate-commit-msg": "^2.14.0",
"vconsole-webpack-plugin": "^1.5.1",
"vue-skeleton-webpack-plugin": "^1.2.2",
"vue-template-compiler": "^2.6.11",
"webpack-bundle-analyzer": "^3.6.1"
},
......
'use strict'
const merge = require('lodash/merge')
const webpack = require('webpack')
const optionsSchema = require('./config/optionsSchema.json')
const Server = require('./server')
const { addScriptTag, outputSkeletonScreen, snakeToCamel } = require('./util')
const { defaultOptions, staticPath } = require('./config/config')
const OptionsValidationError = require('./config/optionsValidationError')
const EVENT_LIST = process.env.NODE_ENV === 'production' ? ['watch-close', 'failed', 'done'] : ['watch-close', 'failed']
const PLUGIN_NAME = 'pageSkeletonWebpackPlugin'
function SkeletonPlugin(options = {}) {
const validationErrors = webpack.validateSchema(optionsSchema, options)
if (validationErrors.length) {
throw new OptionsValidationError(validationErrors)
}
this.options = merge({ staticPath }, defaultOptions, options)
this.server = null
this.originalHtml = ''
}
SkeletonPlugin.prototype.createServer = function () { // eslint-disable-line func-names
if (!this.server) {
const server = this.server = new Server(this.options) // eslint-disable-line no-multi-assign
server.listen().catch(err => server.log.warn(err))
}
}
SkeletonPlugin.prototype.insertScriptToClient = function (htmlPluginData) { // eslint-disable-line func-names
// at develop phase, insert the interface code
if (process.env.NODE_ENV !== 'production') {
const { port } = this.options
const clientEntry = `http://localhost:${port}/${staticPath}/index.bundle.js`
const oldHtml = htmlPluginData.html
htmlPluginData.html = addScriptTag(oldHtml, clientEntry, port)
}
}
SkeletonPlugin.prototype.outputSkeletonScreen = async function () { // eslint-disable-line func-names
try {
await outputSkeletonScreen(this.originalHtml, this.options, this.server.log.info)
} catch (err) {
this.server.log.warn(err.toString())
}
}
SkeletonPlugin.prototype.apply = function (compiler) { // eslint-disable-line func-names
if (compiler.hooks) {
compiler.hooks.entryOption.tap(PLUGIN_NAME, () => {
this.createServer()
})
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(PLUGIN_NAME, (htmlPluginData, callback) => {
this.insertScriptToClient(htmlPluginData)
callback(null, htmlPluginData)
})
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(PLUGIN_NAME, (htmlPluginData, callback) => {
this.originalHtml = htmlPluginData.html
callback(null, htmlPluginData)
})
})
compiler.hooks.afterEmit.tap(PLUGIN_NAME, async () => {
await this.outputSkeletonScreen()
})
EVENT_LIST.forEach((event) => {
compiler.hooks[snakeToCamel(event)].tap(PLUGIN_NAME, () => {
if (this.server) {
this.server.close()
}
})
})
} else {
compiler.plugin('entry-option', () => {
this.createServer()
})
compiler.plugin('compilation', (compilation) => {
compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, callback) => {
this.insertScriptToClient(htmlPluginData)
callback(null, htmlPluginData)
})
compilation.plugin('html-webpack-plugin-after-html-processing', (htmlPluginData, callback) => {
this.originalHtml = htmlPluginData.html
callback(null, htmlPluginData)
})
})
compiler.plugin('after-emit', async (compilation, done) => {
await this.outputSkeletonScreen()
done()
})
EVENT_LIST.forEach((event) => {
compiler.plugin(event, () => {
if (this.server) {
this.server.close()
}
})
})
}
}
module.exports = SkeletonPlugin
const fs = require('fs')
const path = require('path')
const changeSkeleton = () => {
const txt = fs.readFileSync(path.join(__dirname, './skeleton-plugin.txt'), {
encoding: 'utf8'
})
const targetFile = path.join(__dirname, '..', 'node_modules/page-skeleton-webpack-plugin/src/skeletonPlugin.js')
try {
fs.writeFileSync(targetFile, txt, {
encoding: 'utf8'
})
} catch (error) {
console.log('error: ', error)
}
}
console.log('changeSkeleton')
changeSkeleton()
\ No newline at end of file
......@@ -27,7 +27,6 @@ export default {
<style lang="scss">
.wrap {
color: $geekColor;
}
body {
width: 100vw;
......
......@@ -5,10 +5,8 @@ function install(Vue) {
const errNotify = new NotifyConstructor({
duration: 500,
})
const errorDiv = document.createElement('div')
errorDiv.setAttribute('id', 'error_wrap');
document.body.appendChild(errorDiv)
errNotify.$mount(errorDiv)
const notify = errNotify.$mount()
document.body.appendChild(notify.$el)
const errorNotify = {
show(message, duration) {
errNotify.showNotify(message, duration)
......
import Vue from 'vue'
import skeletonHome from '@/skeleton/home-skeleton';
export default new Vue({
components: {
skeletonHome
},
template: `
<div>
<skeletonHome id="skeleton-home" style="display:none"/>
</div>
`
});
\ No newline at end of file
<template>
<div class="home-skeleton">
<p>loading ....</p>
</div>
</template>
<script>
export default {
components: {},
data() {
return {};
},
methods: {
},
mounted() {
}
}
</script>
<style lang='scss' scoped>
.home-skeleton {
background: red;
height: 100vh;
width: 100vw;
color: black;
}
</style>
\ No newline at end of file
<template>
<div>sssss
<div class="wrap">
<span>sss</span>
<p> net : {{ mData }} </p>
<p> net : {{ mData }} </p>
<p> net : {{ mData }} </p>
......@@ -25,4 +26,7 @@ export default {
}
</script>
<style>
.wrap {
padding: 10px;
}
</style>
\ No newline at end of file
<template>
<div>
this is index path
<p>this is index path</p>
<button @click="home">home</button>
</div>
</template>
......
// vue.config.js
const { SkeletonPlugin } = require('page-skeleton-webpack-plugin')
const VconsolePlugin = require('vconsole-webpack-plugin')
const path = require('path');
const VueSkeletonPlugin = require('vue-skeleton-webpack-plugin')
const CosPlugin = require('cos-webpack');
const mock = require('./mock');
const config = require('./config');
const resolve = (dir) => path.join(__dirname, dir);
const skeletonConfig = require('./config/skeleton.config')
const isProd = ['production', 'prod'].includes(process.env.NODE_ENV)
......@@ -16,15 +14,11 @@ module.exports = {
secretId, secretKey, enable, bucket, region, path
} = config.cosConfig || {}
const plugins = [];
if (!isProd) {
plugins.push(
new SkeletonPlugin({
staticDir: resolve(config.outputDir || 'dist'),
pathname: resolve('skeleton-pages'),
routes: [...config.skeletonRoutes]
})
)
}
plugins.push(
new VueSkeletonPlugin({
...skeletonConfig
})
)
if (isProd && enable && process.env.NODE_ENV === 'production') {
plugins.push(
new CosPlugin({
......@@ -54,6 +48,7 @@ module.exports = {
}
},
css: {
extract: true,
loaderOptions: {
postcss: {
plugins: [
......
......@@ -2522,6 +2522,11 @@ commander@~2.1.0:
resolved "http://git.jksxb.cn:8081/repository/geek/commander/-/commander-2.1.0.tgz#d121bbae860d9992a3d517ba96f56588e47c6781"
integrity sha1-0SG7roYNmZKj1Re6lvVliOR8Z4E=
commander@~2.13.0:
version "2.13.0"
resolved "http://git.jksxb.cn:8081/repository/geek/commander/-/commander-2.13.0.tgz#6964bca67685df7c1f1430c584f07d7597885b9c"
integrity sha1-aWS8pnaF33wfFDDFhPB9dZeIW5w=
commander@~2.19.0:
version "2.19.0"
resolved "https://registry.npm.taobao.org/commander/download/commander-2.19.0.tgz?cache=0&sync_timestamp=1584148407069&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"
......@@ -5766,6 +5771,11 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"
lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "http://git.jksxb.cn:8081/repository/geek/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
integrity sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=
lodash.defaultsdeep@^4.6.1:
version "4.6.1"
resolved "https://registry.npm.taobao.org/lodash.defaultsdeep/download/lodash.defaultsdeep-4.6.1.tgz#512e9bd721d272d94e3d3a63653fa17516741ca6"
......@@ -5791,6 +5801,21 @@ lodash.memoize@^4.1.2:
resolved "https://registry.npm.taobao.org/lodash.memoize/download/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
lodash.template@^4.5.0:
version "4.5.0"
resolved "http://git.jksxb.cn:8081/repository/geek/lodash.template/-/lodash.template-4.5.0.tgz#f976195cf3f347d0d5f52483569fe8031ccce8ab"
integrity sha1-+XYZXPPzR9DV9SSDVp/oAxzM6Ks=
dependencies:
lodash._reinterpolate "^3.0.0"
lodash.templatesettings "^4.0.0"
lodash.templatesettings@^4.0.0:
version "4.2.0"
resolved "http://git.jksxb.cn:8081/repository/geek/lodash.templatesettings/-/lodash.templatesettings-4.2.0.tgz#e481310f049d3cf6d47e912ad09313b154f0fb33"
integrity sha1-5IExDwSdPPbUfpEq0JMTsVTw+zM=
dependencies:
lodash._reinterpolate "^3.0.0"
lodash.transform@^4.6.0:
version "4.6.0"
resolved "https://registry.npm.taobao.org/lodash.transform/download/lodash.transform-4.6.0.tgz#12306422f63324aed8483d3f38332b5f670547a0"
......@@ -6998,6 +7023,11 @@ path-to-regexp@0.1.7:
resolved "https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-0.1.7.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpath-to-regexp%2Fdownload%2Fpath-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
path-to-regexp@^2.1.0:
version "2.4.0"
resolved "http://git.jksxb.cn:8081/repository/geek/path-to-regexp/-/path-to-regexp-2.4.0.tgz#35ce7f333d5616f1c1e1bfe266c3aba2e5b2e704"
integrity sha1-Nc5/Mz1WFvHB4b/iZsOrouWy5wQ=
path-type@^1.0.0:
version "1.1.0"
resolved "http://git.jksxb.cn:8081/repository/geek/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
......@@ -8026,6 +8056,13 @@ resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.3.2, resolve@^1.8.
dependencies:
path-parse "^1.0.6"
resolve@^1.2.0:
version "1.17.0"
resolved "http://git.jksxb.cn:8081/repository/geek/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444"
integrity sha1-sllBtUloIxzC0bt2p5y38sC/hEQ=
dependencies:
path-parse "^1.0.6"
restore-cursor@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/restore-cursor/download/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"
......@@ -8506,6 +8543,11 @@ source-map-url@^0.4.0:
resolved "https://registry.npm.taobao.org/source-map-url/download/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3"
integrity sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=
source-map@0.5.6:
version "0.5.6"
resolved "http://git.jksxb.cn:8081/repository/geek/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412"
integrity sha1-dc449SvwczxafwwRjYEzSiu19BI=
source-map@^0.4.2:
version "0.4.4"
resolved "http://git.jksxb.cn:8081/repository/geek/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"
......@@ -9196,6 +9238,14 @@ typedarray@^0.0.6:
resolved "https://registry.npm.taobao.org/typedarray/download/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
uglify-es@^3.3.4:
version "3.3.9"
resolved "http://git.jksxb.cn:8081/repository/geek/uglify-es/-/uglify-es-3.3.9.tgz#0c1c4f0700bed8dbc124cdb304d2592ca203e677"
integrity sha1-DBxPBwC+2NvBJM2zBNJZLKID5nc=
dependencies:
commander "~2.13.0"
source-map "~0.6.1"
uglify-js@3.4.x:
version "3.4.10"
resolved "https://registry.npm.taobao.org/uglify-js/download/uglify-js-3.4.10.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fuglify-js%2Fdownload%2Fuglify-js-3.4.10.tgz#9ad9563d8eb3acdfb8d38597d2af1d815f6a755f"
......@@ -9502,6 +9552,31 @@ vue-router@^3.1.6:
resolved "https://registry.npm.taobao.org/vue-router/download/vue-router-3.1.6.tgz#45f5a3a3843e31702c061dd829393554e4328f89"
integrity sha1-RfWjo4Q+MXAsBh3YKTk1VOQyj4k=
vue-server-renderer@^2.3.4:
version "2.6.11"
resolved "http://git.jksxb.cn:8081/repository/geek/vue-server-renderer/-/vue-server-renderer-2.6.11.tgz#be8c9abc6aacc309828a755c021a05fc474b4bc3"
integrity sha1-voyavGqswwmCinVcAhoF/EdLS8M=
dependencies:
chalk "^1.1.3"
hash-sum "^1.0.2"
he "^1.1.0"
lodash.template "^4.5.0"
lodash.uniq "^4.5.0"
resolve "^1.2.0"
serialize-javascript "^2.1.2"
source-map "0.5.6"
vue-skeleton-webpack-plugin@^1.2.2:
version "1.2.2"
resolved "http://git.jksxb.cn:8081/repository/geek/vue-skeleton-webpack-plugin/-/vue-skeleton-webpack-plugin-1.2.2.tgz#4601ef0eb7e5446c4b30347b4a0ef414e6dc883e"
integrity sha1-RgHvDrflRGxLMDR7Sg70FObciD4=
dependencies:
loader-utils "^1.1.0"
path-to-regexp "^2.1.0"
uglify-es "^3.3.4"
vue-server-renderer "^2.3.4"
webpack-node-externals "^1.6.0"
vue-style-loader@^4.1.0, vue-style-loader@^4.1.2:
version "4.1.2"
resolved "https://registry.npm.taobao.org/vue-style-loader/download/vue-style-loader-4.1.2.tgz#dedf349806f25ceb4e64f3ad7c0a44fba735fcf8"
......@@ -9653,6 +9728,11 @@ webpack-merge@^4.2.2:
dependencies:
lodash "^4.17.15"
webpack-node-externals@^1.6.0:
version "1.7.2"
resolved "http://git.jksxb.cn:8081/repository/geek/webpack-node-externals/-/webpack-node-externals-1.7.2.tgz#6e1ee79ac67c070402ba700ef033a9b8d52ac4e3"
integrity sha1-bh7nmsZ8BwQCunAO8DOpuNUqxOM=
webpack-sources@^1.1.0, webpack-sources@^1.4.0, webpack-sources@^1.4.1, webpack-sources@^1.4.3:
version "1.4.3"
resolved "https://registry.npm.taobao.org/webpack-sources/download/webpack-sources-1.4.3.tgz?cache=0&sync_timestamp=1574264229907&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fwebpack-sources%2Fdownload%2Fwebpack-sources-1.4.3.tgz#eedd8ec0b928fbf1cbfe994e22d2d890f330a933"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment