From 5f6838dc5d8577b7337bf6ec5c2b5bc6d34d0162 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 15 Feb 2020 14:13:18 +0800 Subject: [PATCH] feat(v3): app-confusion.js --- .../vue-cli-plugin-uni/lib/app-plus/index.js | 4 +-- packages/vue-cli-plugin-uni/lib/env.js | 20 ++++++++++-- .../vue-cli-plugin-uni/lib/split-chunks.js | 31 ++++++++++++++++--- .../lib/platforms/app-plus/index.js | 10 ++++-- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/packages/vue-cli-plugin-uni/lib/app-plus/index.js b/packages/vue-cli-plugin-uni/lib/app-plus/index.js index ae0f243513..f9681b0ad4 100644 --- a/packages/vue-cli-plugin-uni/lib/app-plus/index.js +++ b/packages/vue-cli-plugin-uni/lib/app-plus/index.js @@ -73,12 +73,12 @@ const v3 = { webpackConfig.optimization.runtimeChunk = { name: 'app-config' } + webpackConfig.optimization.splitChunks = require('../split-chunks')() } else if (isAppView) { webpackConfig.optimization.runtimeChunk = false + webpackConfig.optimization.splitChunks = false } - webpackConfig.optimization.splitChunks = false - let devtool = false if (isAppService && process.env.NODE_ENV !== 'production') { diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 45b9dfd60b..c46a7884d3 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -14,7 +14,7 @@ if (process.env.UNI_CLOUD_SPACES) { process.UNI_CLOUD = spaces.length > 0 process.UNI_CLOUD_ALIYUN = !!spaces.find(space => space.clientSecret) if (spaces.length === 1) { - const space = spaces[0] + const space = spaces[0] console.log(`本项目的uniCloud使用的默认服务空间spaceId为:${space.id}`) if (space.clientSecret) { process.env.UNI_CLOUD_PROVIDER = JSON.stringify({ @@ -85,7 +85,8 @@ if (process.env.NODE_ENV === 'production') { // 发行模式,不启用 cache delete process.env.UNI_USING_CACHE } -const { +const { + normalizePath, isSupportSubPackages, runByHBuilderX, // isInHBuilderXAlpha, @@ -194,6 +195,21 @@ if (process.env.UNI_PLATFORM === 'app-plus') { process.env.UNI_USING_V8 = true process.env.UNI_OUTPUT_TMP_DIR = '' } + // v3 支持指定 js 混淆(仅发行模式) + if ( + process.env.NODE_ENV === 'production' && + process.env.UNI_USING_V3 + ) { + const resources = platformOptions.confusion && + platformOptions.confusion.resources + const resourcesKeys = resources && + Object.keys(resources).filter(filepath => path.extname(filepath) === '.js') + if (resourcesKeys && resourcesKeys.length) { + process.UNI_CONFUSION = resourcesKeys.map(filepath => + normalizePath(path.resolve(process.env.UNI_INPUT_DIR, filepath)) + ) + } + } } else { // 其他平台,待确认配置方案 if ( manifestJsonObj['app-plus'] && diff --git a/packages/vue-cli-plugin-uni/lib/split-chunks.js b/packages/vue-cli-plugin-uni/lib/split-chunks.js index 2feca11d2d..03226ded50 100644 --- a/packages/vue-cli-plugin-uni/lib/split-chunks.js +++ b/packages/vue-cli-plugin-uni/lib/split-chunks.js @@ -1,8 +1,33 @@ const path = require('path') module.exports = function getSplitChunks () { + const { + normalizePath + } = require('@dcloudio/uni-cli-shared') + if (process.env.UNI_USING_V3) { - return false + if (!process.UNI_CONFUSION) { // 无加密 + return false + } + return { + cacheGroups: { + vendor: { + minSize: 0, + minChunks: 1, + test: function (module) { + if (!module.resource) { + return false + } + if (process.UNI_CONFUSION.includes(normalizePath(module.resource))) { + return true + } + return false + }, + name: 'app-confusion', + chunks: 'all' + } + } + } } if (!process.env.UNI_USING_COMPONENTS) { return { @@ -16,10 +41,6 @@ module.exports = function getSplitChunks () { } } - const { - normalizePath - } = require('@dcloudio/uni-cli-shared') - const mainPath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, 'main.')) if (!process.env.UNI_OPT_SUBPACKAGES) { diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js index 478f18af32..70eb6f9e53 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js @@ -321,15 +321,19 @@ module.exports = function (pagesJson, userManifestJson) { // 检查原生混淆选项 const confusion = manifestJson.plus.confusion if (confusion && confusion.resources) { - const resources = {} + const resources = {} + const nvuePages = (appJson.nvue && appJson.nvue.pages) || {} for (const key in confusion.resources) { + if (path.extname(key) === '.js') { // 支持 js 混淆,过滤掉 + continue + } if (!/\.nvue$/.test(key)) { throw new Error(`原生混淆仅支持 nvue 页面,错误的页面路径:${key}`) } else { resources[key.replace(/\.nvue$/, '.js')] = confusion.resources[key] } - if (!Object.keys(appJson.nvue.pages).find(path => { - const subNVues = appJson.nvue.pages[path].window.subNVues || [] + if (!Object.keys(nvuePages).find(path => { + const subNVues = nvuePages[path].window.subNVues || [] return path.replace(/\.html$/, '.nvue') === key || subNVues.find(({ path }) => path === key.replace(/\.nvue$/, '')) -- GitLab