diff --git a/packages/uni-cli-shared/lib/cache.js b/packages/uni-cli-shared/lib/cache.js index 2e3fb63cc136c5d9d4e1bf5f340deac358daa3e6..23c2adcfce874fc366620431365754e55bfe2bfa 100644 --- a/packages/uni-cli-shared/lib/cache.js +++ b/packages/uni-cli-shared/lib/cache.js @@ -104,12 +104,12 @@ function updateComponentJson (name, jsonObj, usingComponents = true, type = 'Com } function updateUsingGlobalComponents (name, usingGlobalComponents) { - const manifestConfig = process.UNI_MANIFEST; - const weixinConfig = manifestConfig['mp-weixin']; - const independentSwitch = !!weixinConfig.independent; + const manifestConfig = process.UNI_MANIFEST + const weixinConfig = manifestConfig['mp-weixin'] + const independentSwitch = !!weixinConfig.independent if (!independentSwitch && supportGlobalUsingComponents) { - return; + return } const oldJsonStr = getJsonFile(name) diff --git a/packages/uni-cli-shared/lib/pages.js b/packages/uni-cli-shared/lib/pages.js index 402bf357237be26cafb42ef38f1ecf02b9f964e5..29d15f3bfc39e684c60096660d8e002dc8347641 100644 --- a/packages/uni-cli-shared/lib/pages.js +++ b/packages/uni-cli-shared/lib/pages.js @@ -209,20 +209,19 @@ function parseEntry (pagesJson) { process.UNI_ENTRY = { 'common/main': mainJsPath } - const manifestConfig = process.UNI_MANIFEST; - const weixinConfig = manifestConfig['mp-weixin'] || {}; - const independentSwitch = !!weixinConfig.independent; + const manifestConfig = process.UNI_MANIFEST + const weixinConfig = manifestConfig['mp-weixin'] || {} + const independentSwitch = !!weixinConfig.independent if (independentSwitch) { Object.values(process.UNI_SUBPACKAGES).forEach(({ root, independent = false }) => { if (root && independent) { - const pkgRootMainJsKey = `${root}/common/main`; + const pkgRootMainJsKey = `${root}/common/main` // const pkgRootMainJsPath = `${process.env.UNI_INPUT_DIR}/${root}/main.js`; - process.UNI_ENTRY[pkgRootMainJsKey] = mainJsPath; + process.UNI_ENTRY[pkgRootMainJsKey] = mainJsPath } - }); + }) } - process.UNI_SUB_PACKAGES_ROOT = {} process.UNI_NVUE_ENTRY = {} diff --git a/packages/uni-mp-weixin/lib/createIndependentPlugin.js b/packages/uni-mp-weixin/lib/createIndependentPlugin.js index be2e9cf485844b109d96fe7e9d5af9ed45bbfe12..7b39b1d4a6ec7a60b63ac48e7dcffa9b4e0ad3cc 100644 --- a/packages/uni-mp-weixin/lib/createIndependentPlugin.js +++ b/packages/uni-mp-weixin/lib/createIndependentPlugin.js @@ -1,37 +1,37 @@ -const AddShareAbilityToRuntimePlugin = require('./independent-plugins/add-share-ability-to-runtime-plugin'); -const GenerateIndepndentEntryPlugin = require('./independent-plugins/generate-indepndent-entry-plugin'); -const InjectEntryJsToIndependentPlugin = require('./independent-plugins/inject-entry-to-independent-plugin'); -const InjectMainCssToIndependentCssPlugin = require('./independent-plugins/inject-main-css-to-independent-plugin'); -const RunDefaultAppPlugin = require('./independent-plugins/run-default-app-plugin'); -const SplitIndependentChunksPlugin = require('./independent-plugins/split-independent-chunks-plugin'); -const ModifyUniAppWebpackConfigPlugin = require('./independent-plugins/modify-uniapp-webpack-config-Plugin'); -const AddWxMpRuntimePlugin = require('./independent-plugins/add-weixin-mp-runtime-plugin'); -const AppInterceptorPlugin = require('./independent-plugins/app-interceptor-plugin'); +const AddShareAbilityToRuntimePlugin = require('./independent-plugins/add-share-ability-to-runtime-plugin') +const GenerateIndepndentEntryPlugin = require('./independent-plugins/generate-indepndent-entry-plugin') +const InjectEntryJsToIndependentPlugin = require('./independent-plugins/inject-entry-to-independent-plugin') +const InjectMainCssToIndependentCssPlugin = require('./independent-plugins/inject-main-css-to-independent-plugin') +const RunDefaultAppPlugin = require('./independent-plugins/run-default-app-plugin') +const SplitIndependentChunksPlugin = require('./independent-plugins/split-independent-chunks-plugin') +const ModifyUniAppWebpackConfigPlugin = require('./independent-plugins/modify-uniapp-webpack-config-Plugin') +const AddWxMpRuntimePlugin = require('./independent-plugins/add-weixin-mp-runtime-plugin') +const AppInterceptorPlugin = require('./independent-plugins/app-interceptor-plugin') module.exports = function createIndependentPlugins () { - const manifestConfig = process.UNI_MANIFEST; - const weixinConfig = manifestConfig['mp-weixin']; - const independentSwitch = !!weixinConfig.independent; - if (!independentSwitch) return []; + const manifestConfig = process.UNI_MANIFEST + const weixinConfig = manifestConfig['mp-weixin'] + const independentSwitch = !!weixinConfig.independent + if (!independentSwitch) return [] - // 支持构造微信小程序的独立分包 - const independentPlugins = [ - new SplitIndependentChunksPlugin(), - new ModifyUniAppWebpackConfigPlugin(), // 修改 webpack配置 - new AddShareAbilityToRuntimePlugin(), // 保证独立分包和主包使用的相同的runtime.js - new GenerateIndepndentEntryPlugin(), // 生成独立分包执行入口文件(代替app.js - new InjectEntryJsToIndependentPlugin(), // 为独立分包注入执行入口 - new RunDefaultAppPlugin(), // 确保app.js中的App()被执行一次 - // 独立分包中 App,getApp 调用拦截 - new AddWxMpRuntimePlugin(), - new AppInterceptorPlugin() - ]; + // 支持构造微信小程序的独立分包 + const independentPlugins = [ + new SplitIndependentChunksPlugin(), + new ModifyUniAppWebpackConfigPlugin(), // 修改 webpack配置 + new AddShareAbilityToRuntimePlugin(), // 保证独立分包和主包使用的相同的runtime.js + new GenerateIndepndentEntryPlugin(), // 生成独立分包执行入口文件(代替app.js + new InjectEntryJsToIndependentPlugin(), // 为独立分包注入执行入口 + new RunDefaultAppPlugin(), // 确保app.js中的App()被执行一次 + // 独立分包中 App,getApp 调用拦截 + new AddWxMpRuntimePlugin(), + new AppInterceptorPlugin() + ] - const insertAppCssToIndependentSwitch = !!weixinConfig.insertAppCssToIndependent; - if (insertAppCssToIndependentSwitch) { - // 需要在 cacheSet 后面 - independentPlugins.push(new InjectMainCssToIndependentCssPlugin()); // 目前只对页面注入了,组件未注入 - } + const insertAppCssToIndependentSwitch = !!weixinConfig.insertAppCssToIndependent + if (insertAppCssToIndependentSwitch) { + // 需要在 cacheSet 后面 + independentPlugins.push(new InjectMainCssToIndependentCssPlugin()) // 目前只对页面注入了,组件未注入 + } - return independentPlugins; -}; \ No newline at end of file + return independentPlugins +} diff --git a/packages/uni-mp-weixin/lib/uni.config.js b/packages/uni-mp-weixin/lib/uni.config.js index 669027976ceb3d63ff4aa59a61bfefee3253c2a0..bf79e12547059843aaede1727157aba2ac29d12e 100644 --- a/packages/uni-mp-weixin/lib/uni.config.js +++ b/packages/uni-mp-weixin/lib/uni.config.js @@ -42,18 +42,18 @@ module.exports = { const workers = platformOptions.workers workers && copyOptions.push(workers) - const manifestConfig = process.UNI_MANIFEST; - const weixinConfig = manifestConfig['mp-weixin'] || {}; - const copyWxComponentsOnDemandSwitch = !!weixinConfig.copyWxComponentsOnDemand; // 默认值false + const manifestConfig = process.UNI_MANIFEST + const weixinConfig = manifestConfig['mp-weixin'] || {} + const copyWxComponentsOnDemandSwitch = !!weixinConfig.copyWxComponentsOnDemand // 默认值false if (!copyWxComponentsOnDemandSwitch) { - const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, COMPONENTS_DIR_NAME); + const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, COMPONENTS_DIR_NAME) if (fs.existsSync(wxcomponentsDir)) { copyOptions.push({ from: wxcomponentsDir, to: COMPONENTS_DIR_NAME, ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤 - }); + }) } } global.uniModules.forEach(module => { diff --git a/packages/vue-cli-plugin-uni/lib/mp/index.js b/packages/vue-cli-plugin-uni/lib/mp/index.js index 7b386fa75f3c49443463f899f861c043077847e2..5ccddb64a411b82ade9174b20a0db3b23e294b2b 100644 --- a/packages/vue-cli-plugin-uni/lib/mp/index.js +++ b/packages/vue-cli-plugin-uni/lib/mp/index.js @@ -23,7 +23,7 @@ function createUniMPPlugin () { return new WebpackUniMPPlugin() } -const createWxMpIndependentPlugins = require('@dcloudio/uni-mp-weixin/lib/createIndependentPlugin'); +const createWxMpIndependentPlugins = require('@dcloudio/uni-mp-weixin/lib/createIndependentPlugin') function getProvides () { const uniPath = require('@dcloudio/uni-cli-shared/lib/platform').getMPRuntimePath() diff --git a/packages/vue-cli-plugin-uni/lib/split-chunks.js b/packages/vue-cli-plugin-uni/lib/split-chunks.js index a7b62499d445322cc3bc11aacb27631f3e2fa3b6..26ab731de576c672dd716dcb65fb2f98baead039 100644 --- a/packages/vue-cli-plugin-uni/lib/split-chunks.js +++ b/packages/vue-cli-plugin-uni/lib/split-chunks.js @@ -4,12 +4,12 @@ const { normalizePath } = require('@dcloudio/uni-cli-shared') -const subPkgsInfo = Object.values(process.UNI_SUBPACKAGES); -const normalFilter = ({ independent }) => !independent; -const independentFilter = ({ independent }) => independent; -const map2Root = ({ root }) => root + '/'; -const normalSubPackageRoots = subPkgsInfo.filter(normalFilter).map(map2Root); -const independentSubpackageRoots = subPkgsInfo.filter(independentFilter).map(map2Root); +const subPkgsInfo = Object.values(process.UNI_SUBPACKAGES) +const normalFilter = ({ independent }) => !independent +const independentFilter = ({ independent }) => independent +const map2Root = ({ root }) => root + '/' +const normalSubPackageRoots = subPkgsInfo.filter(normalFilter).map(map2Root) +const independentSubpackageRoots = subPkgsInfo.filter(independentFilter).map(map2Root) function createCacheGroups () { const cacheGroups = {} @@ -187,9 +187,9 @@ module.exports = function getSplitChunks () { } // 独立分包除外 - const independentRoot = independentSubpackageRoots.find(root => resource.indexOf(root) >= 0); + const independentRoot = independentSubpackageRoots.find(root => resource.indexOf(root) >= 0) if (!independentRoot) { - return true; + return true } } } else { diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js index 7e5180cb3a07948a71283c1328428e55ee4afa9c..667f256f55215e806b2ccd5f46a6c00bd60dfc2f 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js @@ -105,11 +105,11 @@ module.exports = function generateComponent (compilation, jsonpFunction = 'webpa } const origSource = assets[name].source() - + if (isVueComponent) { componentChunkNameMap[name] = moduleId } else if (isVueOuterComponent) { - const startIndex = name.indexOf(vueOuterComponentSting) + vueOuterComponentSting.length + 1; + const startIndex = name.indexOf(vueOuterComponentSting) + vueOuterComponentSting.length + 1 const rightOriginalComponentName = name.substring(startIndex) moduleId = componentChunkNameMap[rightOriginalComponentName] } diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js index 8d109fa705c0d1744a61ebcc3352d69f8eeb4586..f0d330ded6c12d76aa1ec0512597ebce477eb0e9 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js @@ -26,7 +26,7 @@ const mpBaiduDynamicLibs = [ 'dynamicLib://myDynamicLib/vrvideo' ] -const AnalyzeDependency = require('@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/index'); +const AnalyzeDependency = require('@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/index') function analyzeUsingComponents () { if (!process.env.UNI_OPT_SUBPACKAGES) { @@ -112,11 +112,11 @@ function normalizeUsingComponents (file, usingComponents) { return usingComponents } -const cacheFileMap = new Map(); +const cacheFileMap = new Map() module.exports = function generateJson (compilation) { analyzeUsingComponents() - const emitFileMap = new Map([...cacheFileMap]); + const emitFileMap = new Map([...cacheFileMap]) const jsonFileMap = getChangedJsonFileMap() for (const name of jsonFileMap.keys()) { const jsonObj = JSON.parse(jsonFileMap.get(name)) @@ -214,24 +214,23 @@ module.exports = function generateJson (compilation) { jsonObj.usingComponents = normalizeUsingComponents(name, jsonObj.usingComponents) } - emitFileMap.set(name, jsonObj); - cacheFileMap.set(name, JSON.parse(JSON.stringify(jsonObj))); // 做一次拷贝,emitFileMap中内容在后面会被修改 + emitFileMap.set(name, jsonObj) + cacheFileMap.set(name, JSON.parse(JSON.stringify(jsonObj))) // 做一次拷贝,emitFileMap中内容在后面会被修改 } - // 组件依赖分析 - (new AnalyzeDependency()).init(emitFileMap, compilation); - - for (const [name, jsonObj] of emitFileMap) { - if (name === 'app.json') { // 删除manifest.json携带的配置项 - delete jsonObj.insertAppCssToIndependent; - delete jsonObj.independent; - delete jsonObj.copyWxComponentsOnDemand; - } else { // 删除用于临时记录的属性 - delete jsonObj.usingGlobalComponents; - } - emit(name, jsonObj, compilation); + (new AnalyzeDependency()).init(emitFileMap, compilation) + + for (const [name, jsonObj] of emitFileMap) { + if (name === 'app.json') { // 删除manifest.json携带的配置项 + delete jsonObj.insertAppCssToIndependent + delete jsonObj.independent + delete jsonObj.copyWxComponentsOnDemand + } else { // 删除用于临时记录的属性 + delete jsonObj.usingGlobalComponents } + emit(name, jsonObj, compilation) + } if (process.env.UNI_USING_CACHE && jsonFileMap.size) { setTimeout(() => { @@ -242,42 +241,41 @@ module.exports = function generateJson (compilation) { function emit (name, jsonObj, compilation) { if (jsonObj.usingComponents) { - jsonObj.usingComponents = Object.assign({}, jsonObj.usingComponents); + jsonObj.usingComponents = Object.assign({}, jsonObj.usingComponents) } const source = JSON.stringify(jsonObj, null, 2) - const jsFile = name.replace('.json', '.js') - if ( - ![ - 'app.js', - 'manifest.js', - 'mini.project.js', - 'quickapp.config.js', - 'project.config.js', - 'project.swan.js' - ].includes( - jsFile) && + const jsFile = name.replace('.json', '.js') + if ( + ![ + 'app.js', + 'manifest.js', + 'mini.project.js', + 'quickapp.config.js', + 'project.config.js', + 'project.swan.js' + ].includes( + jsFile) && !compilation.assets[jsFile] - ) { - const jsFileAsset = { - size () { - return Buffer.byteLength(EMPTY_COMPONENT, 'utf8') - }, - source () { - return EMPTY_COMPONENT - } - } - compilation.assets[jsFile] = jsFileAsset - } - const jsonAsset = { + ) { + const jsFileAsset = { size () { - return Buffer.byteLength(source, 'utf8') + return Buffer.byteLength(EMPTY_COMPONENT, 'utf8') }, source () { - return source + return EMPTY_COMPONENT } } + compilation.assets[jsFile] = jsFileAsset + } + const jsonAsset = { + size () { + return Buffer.byteLength(source, 'utf8') + }, + source () { + return source + } + } compilation.assets[name] = jsonAsset } -