diff --git a/packages/uni-cli-shared/lib/index.js b/packages/uni-cli-shared/lib/index.js index 24c74bc03112a9d5c866689c9185ae37aea1fb15..70528402fce0618c423d0ab5aa22d3adca296a45 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 cb7908cecff42b63b633e23cb71689fa55c5ecd0..5ae62aab661b0392b4805d8a754fb238ec5532a3 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 af7a9cfb979f2436a77010d61f3fd6e993b0d616..99193bbe04ace95defe0d9e74a7a4838e7c86464 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 5c85d03528d5a6f1acda65b8682222ca4f11e175..e6ec19eb19c5e1247e72298257b5fb7381c5de55 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 726abde7db5c09299f0830840472dd92dc09a7c6..a88e10ca536ba6e2a4de6290db97c3682e54e56a 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 ] } }, {