Skip to content

Commit

Permalink
Chore: Release 1.2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Maorey committed Apr 10, 2020
1 parent 1957ae2 commit 30f4d93
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 290 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SKIN_FIELD=s
SEARCH_FIELD=_target\d+

# 路由配置别名 (json [ [别名, 目录, 分组(cli参数)], ... ])
ROUTES=[["@iRoute", "src/pages/index/config/route", ["iFoo", "iBar"]], ["@oRoute", "src/pages/other/config/route", ["oFoo", "oBar"]]]
_ROUTES=[["@iRoute", "src/pages/index/config/route", ["iFoo", "iBar"]], ["@oRoute", "src/pages/other/config/route", ["oFoo", "oBar"]]]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ yarn vue-cli-service help # [命令] : 比如 yarn vue-cli-service help test:e2e
```

可配置和使用指定路由配置(请使用别名引用路由配置)进行开发/构建:
- 配置 [.env](.env) `ROUTES`, 示例:
- 配置 [.env](.env) `_ROUTES`, 示例:

```bash
ROUTES=[["@iRoute", "src/pages/index/config/route", ["iFoo", "iBar"]], ["@oRoute", "src/pages/other/config/route", ["oFoo", "oBar"]]]
_ROUTES=[["@iRoute", "src/pages/index/config/route", ["iFoo", "iBar"]], ["@oRoute", "src/pages/other/config/route", ["oFoo", "oBar"]]]
```
- `route` 目录结构
Expand Down
9 changes: 9 additions & 0 deletions build/banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** 为页面添加横幅
* @param ENV 环境变量
* @param config chainWebpack
*/
module.exports = ENV =>
`console.log('%cbuild: ${ENV.APP_VERSION}-${String(Date.now()).substr(
-11,
5
)}','background:green;color:white');` // build 信息(时间戳年~时)
42 changes: 41 additions & 1 deletion build/insertPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ module.exports = class {
* @param {Object} option 选项
* {
* runtime:String|Array<String>|RegExp 内联runtime(指定js文件)
* strict:Boolean 内联脚本是否严格模式
* defer:Boolean 脚本是否defer
* async:Boolean 脚本是否async (和defer只能有一个)
* skin:String 默认皮肤({skin}@*.css)
* noPreload:Boolean 是否去掉 preload
* noPrefetch:Boolean 是否去掉 prefetch
* banner:String|Function 添加横幅(比如版权注释/控制台打印信息等, 复用/新增<script>)
* }
*/
constructor(option = {}) {
Expand Down Expand Up @@ -110,6 +112,8 @@ module.exports = class {
/// preload & prefetch ///
this._L = option.noPreload && 'preload'
this._F = option.noPrefetch && 'prefetch'
this._B = option.banner
this._S = !option.strict && /['"]use\s+strict['"];?/gm
}

// https://webpack.docschina.org/api/plugins/
Expand All @@ -125,10 +129,10 @@ module.exports = class {
)
})
/// inline-manifest ///
const PLUGIN2 = 'inline-manifest'
const REG_RUNTIME = this._REG_RUNTIME
if (REG_RUNTIME) {
const MERGE = this._SA
const PLUGIN2 = 'inline-manifest'
compiler.hooks.emit.tap(PLUGIN2, compilation => {
for (const item of getAssetName(compilation.chunks, REG_RUNTIME)) {
delete compilation.assets[item.file]
Expand Down Expand Up @@ -198,6 +202,42 @@ module.exports = class {
)
})
}
/// banner ///
const BANNER = this._B
if (BANNER) {
const PLUGIN3 = 'banner'
const REG_STRICT = this._S
compiler.hooks.compilation.tap(PLUGIN3, compilation => {
compilation.hooks.htmlWebpackPluginAlterAssetTags &&
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(
PLUGIN3,
(data, cb) => {
let banner = typeof BANNER === 'string' ? BANNER : BANNER(data)
banner &&
['head', 'body'].forEach(section => {
for (const tag of data[section]) {
if (tag.innerHTML && tag.tagName === 'script') {
REG_STRICT &&
(tag.innerHTML = tag.innerHTML.replace(REG_STRICT, ''))
if (banner) {
tag.innerHTML = banner + tag.innerHTML
banner = 0
}
}
}
})
banner &&
data.body.unshift({
tagName: 'script',
closeTag: true,
innerHTML: banner,
attributes: { type: 'text/javascript' },
})
cb(null, data)
}
)
})
}
}

// 补充缺失的资源
Expand Down
1 change: 1 addition & 0 deletions build/production.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function plugin(config, ENV) {
defer: true,
skin: ENV.SKIN,
runtime: ['c_', 'r_'],
banner: require('./banner')(ENV, config),
},
])
// 文件 gzip 压缩 https://webpack.docschina.org/plugins/compression-webpack-plugin/
Expand Down
55 changes: 50 additions & 5 deletions build/updateJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,55 @@ const fs = require('fs')
const path = require('path')

const REG_SPLIT = /\.(?!['"])/
function isEqual(x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y // isEqual(0, -0) => false
}

// eslint-disable-next-line no-self-compare
if (x !== x && y !== y) {
return true // isEqual(NaN, NaN) => true
}

let temp // 工具人
if (Array.isArray(x)) {
if (!Array.isArray(y) || (temp = x.length) !== y.length) {
return false
}

while (temp--) {
if (!isEqual(x[temp], y[temp])) {
return false
}
}

return true
}

if (typeof x === 'object') {
if (typeof y !== 'object') {
return false // 先比较Object.keys()长度不太划算
}

const KEYS = {}
for (temp in x) {
if (!isEqual(x[temp], y[temp])) {
return false
}
KEYS[temp] = 1
}

for (temp in y) {
if (!KEYS[temp]) {
return false
}
}

return true
}

return false
}

/** 读取/更新json文件
* @param {string} fileName 文件名(含路径)
Expand Down Expand Up @@ -42,11 +91,7 @@ module.exports = function(fileName, key, value) {
return current
}

if (
typeof value === 'object' && typeof current === 'object'
? JSON.stringify(current) !== JSON.stringify(value)
: current !== value
) {
if (!isEqual(current, value)) {
parent[k] = value
// 异步写
fs.writeFile(
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"axios": "^0.19.2",
"core-js": "^3.6.4",
"core-js": "^3.6.5",
"crypto-js": "3.3.0",
"d3": "^5.15.1",
"echarts": "^4.7.0",
Expand Down Expand Up @@ -60,7 +60,7 @@
"@vue/eslint-config-standard": "^5.1.2",
"@vue/eslint-config-typescript": "^5.0.2",
"@vue/test-utils": "^1.0.0-beta.33",
"alternate-css-extract-plugin": "^0.9.3",
"alternate-css-extract-plugin": "^0.9.4",
"compression-webpack-plugin": "^3.1.0",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.20.2",
Expand All @@ -70,14 +70,14 @@
"eslint-plugin-vue": "^6.2.2",
"fibers": "^4.0.2",
"hard-source-webpack-plugin": "^0.13.1",
"lint-staged": "^10.1.2",
"lint-staged": "^10.1.3",
"postcss-preset-env": "^6.7.0",
"regenerate": "^1.4.0",
"regjsgen": "^0.5.1",
"regjsparser": "^0.6.4",
"sass": "^1.26.3",
"sass-loader": "^8.0.2",
"stylelint": "^13.3.0",
"stylelint": "^13.3.1",
"stylelint-config-scss-maorey": "^1.1.1",
"stylelint-webpack-plugin": "^1.2.3",
"typescript": "~3.8.3",
Expand Down
4 changes: 0 additions & 4 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,4 @@
<!-- 自动插入打包脚本 -->
<%= isProd ? '</body>' : '</div></body>' %>

<script>
// 打印版本信息
console.log('%cbuild: <%= process.env.APP_VERSION %>','background:green;color:white')
</script>
</html>
4 changes: 0 additions & 4 deletions public/other.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,4 @@
<!-- 自动插入打包脚本 -->
<%= isProd ? '</body>' : '</div></body>' %>

<script>
// 打印版本信息
console.log('%cbuild: <%= process.env.APP_VERSION %>','background:green;color:white')
</script>
</html>
25 changes: 3 additions & 22 deletions src/pages/index/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import router from './router'
import store from './store'
import Vue from 'vue'
import App from './App'

import { dev } from '@/utils'
// import { throttle } from '@/utils/performance'
import './registerServiceWorker'

Expand Down Expand Up @@ -36,28 +38,7 @@ import '@/scss/transitions.scss?skin='
// submit(data) // 上传数据
// })

// 在浏览器开发工具的性能/时间线面板中启用Vue组件性能追踪 && 更友好的组件名(vue-devtool)
;(Vue.config.performance = process.env.NODE_ENV === 'development') &&
Vue.mixin({
beforeCreate() {
let options
options = this.$options
options ||
((options = this.$vnode) &&
(options = options.componentOptions) &&
(options = options.Ctor) &&
(options = (options as any).options))

// 匿名组件就不处理了 vue-devtool自己找 $vm0.$options.__file
if (options && options.__file && /^default/i.test(options.name)) {
const result = /(?:[\\/]([^\\/]+)[\\/])?([^\\/]+)(?:[\\/]index)?\.\w+/.exec(
options.__file
)
result && (options.name = result[1] + result[2])
}
},
})

dev(Vue)
// 防阻塞页面(defer的脚本已缓存时不会非阻塞执行bug:chromium#717979)
setTimeout(() => {
// new Vue({
Expand Down
25 changes: 3 additions & 22 deletions src/pages/other/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Vue from 'vue'
import router from './router'
import store from './store'
import App from './App'

import { dev } from '@/utils'
// import { throttle } from '@/utils/performance'
import './registerServiceWorker'

Expand Down Expand Up @@ -134,28 +136,7 @@ Vue.use(Scrollbar)
// submit(data) // 上传数据
// })

// 在浏览器开发工具的性能/时间线面板中启用Vue组件性能追踪 && 更友好的组件名(vue-devtool)
;(Vue.config.performance = process.env.NODE_ENV === 'development') &&
Vue.mixin({
beforeCreate() {
let options
options = this.$options
options ||
((options = this.$vnode) &&
(options = options.componentOptions) &&
(options = options.Ctor) &&
(options = (options as any).options))

// 匿名组件就不处理了 vue-devtool自己找 $vm0.$options.__file
if (options && options.__file && /^default/i.test(options.name)) {
const result = /(?:[\\/]([^\\/]+)[\\/])?([^\\/]+)(?:[\\/]index)?\.\w+/.exec(
options.__file
)
result && (options.name = result[1] + result[2])
}
},
})

dev(Vue)
// 防阻塞页面(defer的脚本已缓存时不会非阻塞执行bug:chromium#717979)
setTimeout(() => {
// new Vue({
Expand Down
29 changes: 29 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** 工具函数 */
import { VueConstructor } from 'vue'

/** 对象自身是否存在指定属性 (查找原型链请用 key in obj 判断)
* @test true
Expand Down Expand Up @@ -223,6 +224,33 @@ function isEqual(x?: any, y?: any): boolean {
return false
}

/** 开发环境处理
* @param Vue
*/
function dev(Vue: VueConstructor) {
// 在浏览器开发工具的性能/时间线面板中启用Vue组件性能追踪 && 更友好的组件名(vue-devtool)
;(Vue.config.performance = process.env.NODE_ENV === 'development') &&
Vue.mixin({
beforeCreate() {
let options
options = this.$options
options ||
((options = this.$vnode) &&
(options = options.componentOptions) &&
(options = options.Ctor) &&
(options = (options as any).options))

// 匿名组件就不处理了 vue-devtool自己找 $vm0.$options.__file
if (options && options.__file && /^default/i.test(options.name)) {
const result = /(?:[\\/]([^\\/]+)[\\/])?([^\\/]+)(?:[\\/]index)?\.\w+/.exec(
options.__file
)
result && (options.name = result[1] + result[2])
}
},
})
}

export {
hasOwnProperty,
getType,
Expand All @@ -239,4 +267,5 @@ export {
isArray,
isFn,
isEqual,
dev,
}
5 changes: 1 addition & 4 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ module.exports = {
let env // 工具人
/// 设置目录别名 已有: @ => src ///
try {
env = JSON.parse(ENV.ROUTES)
env = JSON.parse(ENV._ROUTES)
} catch (error) {}
delete ENV.ROUTES
require('./build/alias')(pages, config, ALIAS, env)

/// 环境变量 ///
env = {}
const prefix = 'process.env.'
const REG_ENV = /^[A-Z][A-Z_]*$/
// build信息(时间戳年-时)
ENV.APP_VERSION += '-' + String(Date.now()).substr(-11, 5)
for (const att in ENV) {
REG_ENV.test(att) && (env[prefix + att] = JSON.stringify(ENV[att]))
}
Expand Down
Loading

0 comments on commit 30f4d93

Please sign in to comment.