From 3f25dc85b6cc0f3359e3d530af92ff134e2b1bd1 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 19 Dec 2019 14:53:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20H5=E6=A8=A1=E6=9D=BF=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=20VUE=5FAPP=5FINDEX=5FCSS=5FHASH?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-cli-shared/lib/platform.js | 43 ++++++++++++++++++------- packages/vue-cli-plugin-uni/lib/env.js | 7 ++-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/packages/uni-cli-shared/lib/platform.js b/packages/uni-cli-shared/lib/platform.js index 81e66afd06..c165ee5a11 100644 --- a/packages/uni-cli-shared/lib/platform.js +++ b/packages/uni-cli-shared/lib/platform.js @@ -103,18 +103,39 @@ const PLATFORMS = { copyWebpackOptions ({ assetsDir }) { + const indexCssCopyOptions = [{ + from: require.resolve('@dcloudio/uni-h5/dist/index.css'), + to: assetsDir, + transform (content) { + if (process.env.NODE_ENV === 'production') { + return content + getShadowCss() + } + return content + } + }] + const VUE_APP_INDEX_CSS_HASH = process.env.VUE_APP_INDEX_CSS_HASH + if (VUE_APP_INDEX_CSS_HASH) { + const { + getH5Options + } = require('./manifest') + const { + template + } = getH5Options() + const to = path.join(assetsDir, `[name].${VUE_APP_INDEX_CSS_HASH}.[ext]`) + if (process.env.NODE_ENV === 'production') { + const templateContent = fs.readFileSync(template, { encoding: 'utf8' }) + if (/\bVUE_APP_INDEX_CSS_HASH\b/.test(templateContent)) { + indexCssCopyOptions[0].to = to + } + } else { + const indexCssCopyOption = Object.assign({}, indexCssCopyOptions[0]) + indexCssCopyOption.to = to + indexCssCopyOptions.push(indexCssCopyOption) + } + } return [ ...getStaticCopyOptions(assetsDir), - { - from: require.resolve('@dcloudio/uni-h5/dist/index.css'), - to: assetsDir, - transform (content) { - if (process.env.NODE_ENV === 'production') { - return content + getShadowCss() - } - return content - } - }, + ...indexCssCopyOptions, ...getCopyOptions(['hybrid/html']) ] } @@ -564,4 +585,4 @@ module.exports = { getPlatformSass () { return SASS } -} +} diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 57226787d6..86f2056312 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -1,6 +1,7 @@ const fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') +const loaderUtils = require('loader-utils') // 初始化环境变量 const defaultInputDir = '../../../../src' @@ -109,6 +110,8 @@ if (process.env.UNI_PLATFORM === 'h5') { process.env.UNI_OPT_PRELOAD = true } } + const buffer = fs.readFileSync(require.resolve('@dcloudio/uni-h5/dist/index.css')) + process.env.VUE_APP_INDEX_CSS_HASH = loaderUtils.getHashDigest(buffer, 'md5', 'hex', 8) } if (process.env.UNI_PLATFORM === 'mp-qq') { // QQ小程序 强制自定义组件模式 @@ -259,7 +262,7 @@ moduleAlias.addAlias('mpvue-template-compiler', '@dcloudio/vue-cli-plugin-uni/pa if (process.env.UNI_USING_V3 && process.env.UNI_PLATFORM === 'app-plus') { moduleAlias.addAlias('vue-style-loader', '@dcloudio/vue-cli-plugin-uni/packages/app-vue-style-loader') -} +} // vue cache if ( // 非 h5 ,非 v3,非 native @@ -320,4 +323,4 @@ runByHBuilderX && console.log(`正在编译中...`) module.exports = { manifestPlatformOptions: platformOptions -} +} -- GitLab