From 82e48e64676e41edce04cca3b83c3325d87e2fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BA=9A=E7=90=AA?= Date: Thu, 23 Jun 2022 19:16:28 +0800 Subject: [PATCH] refactor: getPlatformUniCloud --- packages/uni-cli-shared/lib/index.js | 12 +++++++----- packages/uni-cli-shared/lib/platform.js | 9 ++++++++- packages/vue-cli-plugin-uni/lib/app-plus/index.js | 12 +++++------- packages/vue-cli-plugin-uni/lib/h5/index.js | 13 ++++++------- packages/vue-cli-plugin-uni/lib/mp/index.js | 11 ++++------- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/packages/uni-cli-shared/lib/index.js b/packages/uni-cli-shared/lib/index.js index 24c74bc03..70528402f 100644 --- a/packages/uni-cli-shared/lib/index.js +++ b/packages/uni-cli-shared/lib/index.js @@ -58,8 +58,9 @@ const { nvueCssPreprocessOptions, nvueHtmlPreprocessOptions, getPlatformGlobal, - getPlatformStat, - getPlatformPush + getPlatformStat, + getPlatformPush, + getPlatformUniCloud } = require('./platform') module.exports = { @@ -109,6 +110,7 @@ module.exports = { nvueCssPreprocessOptions, nvueHtmlPreprocessOptions, getPlatformGlobal, - getPlatformStat, - getPlatformPush -} + getPlatformStat, + getPlatformPush, + getPlatformUniCloud +} diff --git a/packages/uni-cli-shared/lib/platform.js b/packages/uni-cli-shared/lib/platform.js index cb7908cec..5ae62aab6 100644 --- a/packages/uni-cli-shared/lib/platform.js +++ b/packages/uni-cli-shared/lib/platform.js @@ -175,6 +175,13 @@ module.exports = { } return '' }, + getPlatformUniCloud () { + if (JSON.parse(process.env.UNI_CLOUD_PROVIDER || '[]').length) { + const uniCloudLibPath = '@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js' + return `import '${uniCloudLibPath}';` + } + return '' + }, getBabelParserOptions () { return { sourceType: 'module', @@ -192,4 +199,4 @@ module.exports = { ] } } -} +} 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 af7a9cfb9..99193bbe0 100644 --- a/packages/vue-cli-plugin-uni/lib/app-plus/index.js +++ b/packages/vue-cli-plugin-uni/lib/app-plus/index.js @@ -4,7 +4,8 @@ const webpack = require('webpack') const { getMainEntry, getPlatformStat, - getPlatformPush + getPlatformPush, + getPlatformUniCloud } = require('@dcloudio/uni-cli-shared') const vueLoader = require('@dcloudio/uni-cli-shared/lib/vue-loader') @@ -62,12 +63,9 @@ const v3 = { const statCode = getPlatformStat() const pushCode = getPlatformPush() + const uniCloudCode = getPlatformUniCloud() - let beforeCode = 'import \'uni-pages\';' - if (JSON.parse(process.env.UNI_CLOUD_PROVIDER || '[]').length) { - const uniCloudLibPath = '@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js' - beforeCode += `import '${uniCloudLibPath}';` - } + const beforeCode = 'import \'uni-pages\';' if (!webpackConfig.optimization) { webpackConfig.optimization = {} } @@ -159,7 +157,7 @@ const v3 = { options: { compiler: vueLoader.compiler, before: [ - beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + + beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + uniCloudCode + getGlobalUsingComponentsCode() ] } diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index 5c85d0352..e6ec19eb1 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -6,7 +6,8 @@ const { getMainEntry, getH5Options, getPlatformStat, - getPlatformPush + getPlatformPush, + getPlatformUniCloud } = require('@dcloudio/uni-cli-shared') const { @@ -95,17 +96,15 @@ module.exports = { const statCode = getPlatformStat() const pushCode = getPlatformPush() + const uniCloudCode = getPlatformUniCloud() try { const babelConfig = require(path.resolve(process.env.UNI_CLI_CONTEXT, 'babel.config.js')) useBuiltIns = babelConfig.presets[0][1].useBuiltIns } catch (e) {} - let beforeCode = (useBuiltIns === 'entry' ? 'import \'@babel/polyfill\';' : '') + + const beforeCode = (useBuiltIns === 'entry' ? 'import \'@babel/polyfill\';' : '') + `import 'uni-pages';import 'uni-${process.env.UNI_PLATFORM}';` - if (JSON.parse(process.env.UNI_CLOUD_PROVIDER || '[]').length) { - const uniCloudLibPath = '@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js' - beforeCode += `import '${uniCloudLibPath}';` - } + return { resolve: { extensions: ['.nvue'], @@ -121,7 +120,7 @@ module.exports = { loader: path.resolve(__dirname, '../../packages/wrap-loader'), options: { before: [ - beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + + beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + uniCloudCode + getGlobalUsingComponentsCode() ] } diff --git a/packages/vue-cli-plugin-uni/lib/mp/index.js b/packages/vue-cli-plugin-uni/lib/mp/index.js index 726abde7d..a88e10ca5 100644 --- a/packages/vue-cli-plugin-uni/lib/mp/index.js +++ b/packages/vue-cli-plugin-uni/lib/mp/index.js @@ -8,7 +8,8 @@ const { getPlatformExts, getPlatformCssnano, getPlatformStat, - getPlatformPush + getPlatformPush, + getPlatformUniCloud } = require('@dcloudio/uni-cli-shared') const WebpackUniAppPlugin = require('../../packages/webpack-uni-app-loader/plugin/index') @@ -168,6 +169,7 @@ module.exports = { const statCode = getPlatformStat() const pushCode = getPlatformPush() + const uniCloudCode = getPlatformUniCloud() let beforeCode = 'import \'uni-pages\';' @@ -194,11 +196,6 @@ module.exports = { ${globalEnv}.__webpack_require_UNI_MP_PLUGIN__ = __webpack_require__;` } - if (JSON.parse(process.env.UNI_CLOUD_PROVIDER || '[]').length) { - const uniCloudLibPath = '@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js' - beforeCode += `import '${uniCloudLibPath}';` - } - const alias = { // 仅 mp-weixin 'mpvue-page-factory': require.resolve( '@dcloudio/vue-cli-plugin-uni/packages/mpvue-page-factory') @@ -244,7 +241,7 @@ ${globalEnv}.__webpack_require_UNI_MP_PLUGIN__ = __webpack_require__;` loader: path.resolve(__dirname, '../../packages/wrap-loader'), options: { before: [ - beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + beforeCode + require('../util').getAutomatorCode() + statCode + pushCode + uniCloudCode ] } }, { -- GitLab