From 5061f56102dddc0ac02ec2e29e3077b51e5a2de3 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 31 Mar 2022 17:09:35 +0800 Subject: [PATCH] feat(stat): support uni-cloud-stat --- packages/uni-cli-shared/lib/index.js | 8 +++++--- packages/uni-cli-shared/lib/platform.js | 11 +++++++++-- .../packages/webpack-uni-nvue-loader/lib/main.js | 7 ++++--- packages/vue-cli-plugin-uni/lib/app-plus/index.js | 5 +++-- packages/vue-cli-plugin-uni/lib/configure-webpack.js | 3 --- packages/vue-cli-plugin-uni/lib/env.js | 11 ++++++----- packages/vue-cli-plugin-uni/lib/h5/index.js | 5 +++-- packages/vue-cli-plugin-uni/lib/mp/index.js | 5 +++-- 8 files changed, 33 insertions(+), 22 deletions(-) diff --git a/packages/uni-cli-shared/lib/index.js b/packages/uni-cli-shared/lib/index.js index 564a54b4ad..2e93a7b004 100644 --- a/packages/uni-cli-shared/lib/index.js +++ b/packages/uni-cli-shared/lib/index.js @@ -57,7 +57,8 @@ const { nvueJsPreprocessOptions, nvueCssPreprocessOptions, nvueHtmlPreprocessOptions, - getPlatformGlobal + getPlatformGlobal, + getPlatformStat } = require('./platform') module.exports = { @@ -106,5 +107,6 @@ module.exports = { nvueJsPreprocessOptions, nvueCssPreprocessOptions, nvueHtmlPreprocessOptions, - getPlatformGlobal -} + getPlatformGlobal, + getPlatformStat +} diff --git a/packages/uni-cli-shared/lib/platform.js b/packages/uni-cli-shared/lib/platform.js index a634bb879d..93d78e8396 100644 --- a/packages/uni-cli-shared/lib/platform.js +++ b/packages/uni-cli-shared/lib/platform.js @@ -29,8 +29,8 @@ const cdns = { 'mp-360': 7, 'mp-dingtalk': 8, 'mp-kuaishou': 9, - 'mp-lark': 10, - 'mp-jd': 11, + 'mp-lark': 10, + 'mp-jd': 11, 'mp-xhs': 12, 'quickapp-webview-huawei': 200, 'quickapp-webview-union': 201 @@ -159,6 +159,13 @@ module.exports = { getPlatformSass () { return SASS }, + getPlatformStat () { + if (!process.env.UNI_USING_STAT) { + return '' + } + return process.env.UNI_USING_STAT === '2' ? 'import \'@dcloudio/uni-stat/dist/uni-cloud-stat.es.js\';' + : 'import \'@dcloudio/uni-stat/dist/uni-stat.es.js\';' + }, getBabelParserOptions () { return { sourceType: 'module', diff --git a/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js b/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js index 04b64ea808..e13b03d7a8 100644 --- a/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js +++ b/packages/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/main.js @@ -12,7 +12,8 @@ const { } = require('vue-loader/lib/codegen/utils') const { - normalizePath + normalizePath, + getPlatformStat } = require('@dcloudio/uni-cli-shared') const appVuePath = path.resolve(process.env.UNI_INPUT_DIR, 'App.vue') @@ -47,7 +48,7 @@ module.exports = function (content, map) { const loaderContext = this - const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : '' + const statCode = getPlatformStat() if (this.resourceQuery) { const params = loaderUtils.parseQuery(this.resourceQuery) @@ -88,4 +89,4 @@ module.exports = function (content, map) { const automatorCode = process.env.UNI_AUTOMATOR_WS_ENDPOINT ? 'import \'@dcloudio/uni-app-plus/dist/automator\';' : '' return automatorCode + statCode + content -} +} 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 e98adb3e53..259c8dfc4a 100644 --- a/packages/vue-cli-plugin-uni/lib/app-plus/index.js +++ b/packages/vue-cli-plugin-uni/lib/app-plus/index.js @@ -2,7 +2,8 @@ const path = require('path') const webpack = require('webpack') const { - getMainEntry + getMainEntry, + getPlatformStat } = require('@dcloudio/uni-cli-shared') const vueLoader = require('@dcloudio/uni-cli-shared/lib/vue-loader') @@ -58,7 +59,7 @@ const v3 = { const isAppService = !!vueOptions.pluginOptions['uni-app-plus'].service const isAppView = !!vueOptions.pluginOptions['uni-app-plus'].view - const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : '' + const statCode = getPlatformStat() const beforeCode = 'import \'uni-pages\';' diff --git a/packages/vue-cli-plugin-uni/lib/configure-webpack.js b/packages/vue-cli-plugin-uni/lib/configure-webpack.js index c049c4bb2f..4f6376ffb5 100644 --- a/packages/vue-cli-plugin-uni/lib/configure-webpack.js +++ b/packages/vue-cli-plugin-uni/lib/configure-webpack.js @@ -323,9 +323,6 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt .UNI_INPUT_DIR), // css中的'@/static/logo.png'会被转换成'./@/static/logo.png'加载 vue$: getPlatformVue(vueOptions), 'uni-pages': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'), - '@dcloudio/uni-stat': process.env.UNI_USING_VUE3 ? require.resolve( - '@dcloudio/vue-cli-plugin-uni/packages/uni-stat') : require - .resolve('@dcloudio/uni-stat'), 'uni-stat-config': path.resolve(process.env.UNI_INPUT_DIR, 'pages.json') + '?' + JSON.stringify({ diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index da0d52d3b5..18f87041f2 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -50,7 +50,8 @@ process.env.UNI_USING_V3_SCOPED = true process.env.UNI_CLOUD_PROVIDER = JSON.stringify([]) // 导出到小程序插件 -process.env.UNI_MP_PLUGIN_EXPORT = JSON.stringify(Object.keys(platformOptions.plugins || {}).map(pluginName => platformOptions.plugins[pluginName].export)) +process.env.UNI_MP_PLUGIN_EXPORT = JSON.stringify(Object.keys(platformOptions.plugins || {}).map(pluginName => + platformOptions.plugins[pluginName].export)) const isH5 = !process.env.UNI_SUB_PLATFORM && process.env.UNI_PLATFORM === 'h5' const isProduction = process.env.NODE_ENV === 'production' @@ -308,7 +309,7 @@ if ( ) if (uniStatistics.enable === true) { - process.env.UNI_USING_STAT = true + process.env.UNI_USING_STAT = uniStatistics.version === '2' ? '2' : '1' if (!process.UNI_STAT_CONFIG.appid && process.env.NODE_ENV === 'production') { console.log() console.warn(uniI18n.__('pluginUni.uniStatisticsNoAppid', { @@ -399,9 +400,9 @@ moduleAlias.addAlias('vue-template-compiler', '@dcloudio/vue-cli-plugin-uni/pack moduleAlias.addAlias('@megalo/template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/@megalo/template-compiler') moduleAlias.addAlias('mpvue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/mpvue-template-compiler') // vue-loader -moduleAlias.addAlias('vue-loader', '@dcloudio/vue-cli-plugin-uni/packages/vue-loader') +moduleAlias.addAlias('vue-loader', '@dcloudio/vue-cli-plugin-uni/packages/vue-loader') // sass-loader -moduleAlias.addAlias('sass-loader', '@dcloudio/vue-cli-plugin-uni/packages/sass-loader') +moduleAlias.addAlias('sass-loader', '@dcloudio/vue-cli-plugin-uni/packages/sass-loader') if (process.env.UNI_USING_V3 && process.env.UNI_PLATFORM === 'app-plus') { moduleAlias.addAlias('./runtime/getUrl.js', '@dcloudio/vue-cli-plugin-uni/lib/app-plus/getUrl.js') @@ -501,4 +502,4 @@ runByHBuilderX && console.log(uniI18n.__('compiling')) module.exports = { manifestPlatformOptions: platformOptions -} +} diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index 82a26ba4f5..7edb91a587 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -4,7 +4,8 @@ const webpack = require('webpack') const { getMainEntry, - getH5Options + getH5Options, + getPlatformStat } = require('@dcloudio/uni-cli-shared') const { @@ -91,7 +92,7 @@ module.exports = { webpackConfig (webpackConfig) { let useBuiltIns = 'usage' - const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : '' + const statCode = getPlatformStat() try { const babelConfig = require(path.resolve(process.env.UNI_CLI_CONTEXT, 'babel.config.js')) diff --git a/packages/vue-cli-plugin-uni/lib/mp/index.js b/packages/vue-cli-plugin-uni/lib/mp/index.js index ec8c6eb964..a539e7a3c9 100644 --- a/packages/vue-cli-plugin-uni/lib/mp/index.js +++ b/packages/vue-cli-plugin-uni/lib/mp/index.js @@ -6,7 +6,8 @@ const { getMainEntry, normalizePath, getPlatformExts, - getPlatformCssnano + getPlatformCssnano, + getPlatformStat } = require('@dcloudio/uni-cli-shared') const WebpackUniAppPlugin = require('../../packages/webpack-uni-app-loader/plugin/index') @@ -162,7 +163,7 @@ module.exports = { parseEntry() - const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : '' + const statCode = getPlatformStat() let beforeCode = 'import \'uni-pages\';' -- GitLab