From 75e6f5a39a0c6215dc5bb16fe3fdb05c061aba24 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Mon, 29 Mar 2021 19:56:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=88=B0=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-mp-weixin/lib/uni.config.js | 5 +- packages/vue-cli-plugin-uni/commands/build.js | 56 ++++++++++++++++++- packages/vue-cli-plugin-uni/lib/mp/index.js | 13 ++++- .../lib/plugin/index-new.js | 35 +++++++++++- 4 files changed, 103 insertions(+), 6 deletions(-) diff --git a/packages/uni-mp-weixin/lib/uni.config.js b/packages/uni-mp-weixin/lib/uni.config.js index 8cf180bc6..74fb7c40e 100644 --- a/packages/uni-mp-weixin/lib/uni.config.js +++ b/packages/uni-mp-weixin/lib/uni.config.js @@ -27,7 +27,8 @@ module.exports = { 'theme.json', 'sitemap.json', 'ext.json', - 'custom-tab-bar' + 'custom-tab-bar', + 'plugin.json' ] const workers = platformOptions.workers workers && copyOptions.push(workers) @@ -52,4 +53,4 @@ module.exports = { }) return copyOptions } -} +} diff --git a/packages/vue-cli-plugin-uni/commands/build.js b/packages/vue-cli-plugin-uni/commands/build.js index 8d6319c4d..ac56d0922 100644 --- a/packages/vue-cli-plugin-uni/commands/build.js +++ b/packages/vue-cli-plugin-uni/commands/build.js @@ -1,4 +1,5 @@ const path = require('path') +const fs = require('fs') const { runByHBuilderX, @@ -43,8 +44,14 @@ module.exports = (api, options) => { process.env.UNI_SUBPACKGE = args.subpackage } - if (args.plugin && platforms.includes(process.env.UNI_PLATFORM)) { - process.env.UNI_MP_PLUGIN = args.plugin + if (args.plugin) { + if (process.env.UNI_PLATFORM === 'mp-weixin') { + process.env.UNI_MP_PLUGIN = args.plugin + analysisPluginDir() + } else { + console.error('编译到小程序插件只支持微信小程序') + process.exit(0) + } } require('./util').initAutomator(args) @@ -196,3 +203,48 @@ async function build (args, api, options) { module.exports.defaultModes = { 'uni-build': process.env.NODE_ENV } + +/** + * 编译到微信小程序插件 文件校验 + */ +function analysisPluginDir () { + // plugin.json 是否存在 + const pluginJsonName = 'plugin.json' + const pluginJsonPath = path.resolve(process.env.UNI_INPUT_DIR, pluginJsonName) + if (!fs.existsSync(pluginJsonPath)) { + console.error(`${pluginJsonName}文件不存在,请检查后重试`) + process.exit(0) + } + + const pluginJson = require(pluginJsonPath) + + // index.js 入口文件是否存在 + process.env.UNI_MP_PLUGIN_MAIN = pluginJson.main + const UNI_MP_PLUGIN_MAIN = process.env.UNI_MP_PLUGIN_MAIN + const mainFilePath = path.resolve(process.env.UNI_INPUT_DIR, UNI_MP_PLUGIN_MAIN) + if (UNI_MP_PLUGIN_MAIN && !fs.existsSync(mainFilePath)) { + console.error(`${UNI_MP_PLUGIN_MAIN}入口文件不存在,请检查后重试`) + process.exit(0) + } + + // 目前编译到小程序插件,需要在 pages.json 中配置页面,在main.js中引入使用一下组件,因此先不做一下校验 + // 配置的路径是否存在 + /* const pages = pluginJson.pages || {} + const publicComponents = pluginJson.publicComponents || {} + const allFilesPath = Object.values(pages).map(item => item + '.vue').concat(Object.values(publicComponents).map(item => item + '.vue')) + const inexistenceFiles = [] + if (allFilesPath.length) { + allFilesPath.forEach(pagePath => { + const curentPageAbsolutePath = path.resolve(process.env.UNI_INPUT_DIR, pagePath) + if (!fs.existsSync(curentPageAbsolutePath)) { + inexistenceFiles.push(curentPageAbsolutePath) + } + }) + } + if (inexistenceFiles.length) { + inexistenceFiles.forEach(path => { + console.error(`${path}文件不存在,请检查后重试`) + }) + process.exit(0) + } */ +} diff --git a/packages/vue-cli-plugin-uni/lib/mp/index.js b/packages/vue-cli-plugin-uni/lib/mp/index.js index c0d7c0e26..6f19496e6 100644 --- a/packages/vue-cli-plugin-uni/lib/mp/index.js +++ b/packages/vue-cli-plugin-uni/lib/mp/index.js @@ -156,7 +156,7 @@ module.exports = { const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : '' - const beforeCode = 'import \'uni-pages\';' + let beforeCode = 'import \'uni-pages\';' const plugins = [ new WebpackUniAppPlugin(), @@ -168,6 +168,17 @@ module.exports = { plugins.push(new PreprocessAssetsPlugin()) } + if (process.env.UNI_MP_PLUGIN) { + // 小程序插件入口使用 + // packages\webpack-uni-mp-loader\lib\plugin\index-new.js -> addMPPluginRequire + beforeCode += 'wx.__webpack_require__ = __webpack_require__;' + + const UNI_MP_PLUGIN_MAIN = process.env.UNI_MP_PLUGIN_MAIN + if (UNI_MP_PLUGIN_MAIN) { + process.UNI_ENTRY[UNI_MP_PLUGIN_MAIN.split('.')[0]] = path.resolve(process.env.UNI_INPUT_DIR, UNI_MP_PLUGIN_MAIN) + } + } + return { mode: process.env.NODE_ENV === 'production' ? 'production' : 'development', entry () { diff --git a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js index 4f5eb9af9..a41eb18ef 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js @@ -65,6 +65,37 @@ function addSubPackagesRequire (compilation) { }) } +function addMPPluginRequire (compilation) { + // 编译到小程序插件 特殊处理入口文件 + if (process.env.UNI_MP_PLUGIN) { + const assetsKeys = Object.keys(compilation.assets) + assetsKeys.forEach(name => { + if (name === process.env.UNI_MP_PLUGIN_MAIN) { + const modules = compilation.modules + + const mainFilePath = path.resolve(process.env.UNI_INPUT_DIR, process.env.UNI_MP_PLUGIN_MAIN).replace(/\\/g, '/') + + const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === mainFilePath).id + + const newlineIndex = compilation.assets[name].source().lastIndexOf('\n') + + const source = compilation.assets[name].source().substring(0, newlineIndex) + + `\nmodule.exports = wx.__webpack_require__(${uniModuleId});\n` + + compilation.assets[name].source().substring(newlineIndex + 1) + + compilation.assets[name] = { + size () { + return Buffer.byteLength(source, 'utf8') + }, + source () { + return source + } + } + } + }) + } +} + class WebpackUniMPPlugin { apply (compiler) { if (!process.env.UNI_USING_NATIVE && !process.env.UNI_USING_V3_NATIVE) { @@ -72,6 +103,8 @@ class WebpackUniMPPlugin { return new Promise((resolve, reject) => { addSubPackagesRequire(compilation) + addMPPluginRequire(compilation) + generateJson(compilation) // app.js,app.wxss @@ -110,4 +143,4 @@ class WebpackUniMPPlugin { } } -module.exports = WebpackUniMPPlugin +module.exports = WebpackUniMPPlugin -- GitLab