From 6e82d141565af75aec627c815abe1529ec24fed9 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 11 Jan 2022 17:41:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(mp):=201.=20=E4=BF=AE=E5=A4=8D=20=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=8F=92=E4=BB=B6=E7=BC=96=E8=AF=91=E6=97=B6?= =?UTF-8?q?=E5=85=A5=E5=8F=A3=E6=96=87=E4=BB=B6=E5=88=A4=E6=96=AD=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E3=80=82=E7=A7=BB=E9=99=A4=E4=BD=BF=E7=94=A8=20UNI=5F?= =?UTF-8?q?MP=5FPLUGIN=20=E5=AE=9A=E4=B9=89=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/vue-cli-plugin-uni/commands/build.js | 26 ++++++++++--------- packages/vue-cli-plugin-uni/lib/mp/index.js | 13 +++++----- .../lib/plugin/index-new.js | 18 ++++++------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/packages/vue-cli-plugin-uni/commands/build.js b/packages/vue-cli-plugin-uni/commands/build.js index a390d3224d..bf66955831 100644 --- a/packages/vue-cli-plugin-uni/commands/build.js +++ b/packages/vue-cli-plugin-uni/commands/build.js @@ -34,7 +34,7 @@ module.exports = (api, options) => { '--auto-host': 'specify automator host', '--auto-port': 'specify automator port', '--subpackage': 'specify subpackage', - '--plugin': 'specify plugin', + '--plugin': 'specify mp plugin', '--manifest': 'build manifest.json' } }, async (args) => { @@ -244,17 +244,19 @@ function analysisPluginDir () { const pluginJson = parseJson(fs.readFileSync(pluginJsonPath, 'utf-8'), true) // main 入口文件是否存在 - 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.pathExistsSync(mainFilePath)) { - console.log() - console.error(uniI18n.__('pluginUni.entryDileNoExistsCheckAfterRetry', { - 0: UNI_MP_PLUGIN_MAIN - })) - console.log() - process.exit(0) + if (pluginJson.main) { + 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.pathExistsSync(mainFilePath)) { + console.log() + console.error(uniI18n.__('pluginUni.entryDileNoExistsCheckAfterRetry', { + 0: UNI_MP_PLUGIN_MAIN + })) + console.log() + 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 d37a80a2b4..905b89a32e 100644 --- a/packages/vue-cli-plugin-uni/lib/mp/index.js +++ b/packages/vue-cli-plugin-uni/lib/mp/index.js @@ -178,11 +178,12 @@ module.exports = { { const globalEnv = process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'wx'; - [].concat(process.env.UNI_MP_PLUGIN ? process.env.UNI_MP_PLUGIN_MAIN : JSON.parse(process.env - .UNI_MP_PLUGIN_EXPORT)) - .forEach(fileName => addToUniEntry(fileName)) - beforeCode += - `${globalEnv}.__webpack_require_${(process.env.UNI_MP_PLUGIN || 'UNI_MP_PLUGIN').replace(/-/g, '_')}__ = __webpack_require__;` + [].concat( + process.env.UNI_MP_PLUGIN + ? process.env.UNI_MP_PLUGIN_MAIN + : JSON.parse(process.env.UNI_MP_PLUGIN_EXPORT) + ).forEach(fileName => addToUniEntry(fileName)) + beforeCode += `${globalEnv}.__webpack_require_UNI_MP_PLUGIN__ = __webpack_require__;` } const alias = { // 仅 mp-weixin @@ -319,4 +320,4 @@ module.exports = { webpackConfig.plugins.delete('preload') webpackConfig.plugins.delete('prefetch') } -} +} 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 ddc26facbe..1859383995 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/index-new.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/index-new.js @@ -76,22 +76,22 @@ function addMPPluginRequire (compilation) { const needProcess = process.env.UNI_MP_PLUGIN ? name === UNI_MP_PLUGIN_MAIN : UNI_MP_PLUGIN_EXPORT.includes(name) if (needProcess) { const modules = compilation.modules - + const orignalSource = compilation.assets[name].source() + const globalEnv = process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'wx'; const filePath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, name)) - const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === filePath).id + const newlineIndex = orignalSource.lastIndexOf('\n') - const newlineIndex = compilation.assets[name].source().lastIndexOf('\n') - - const source = compilation.assets[name].source().substring(0, newlineIndex) + - `\nmodule.exports = ${process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'wx'}.__webpack_require_${(process.env.UNI_MP_PLUGIN || 'UNI_MP_PLUGIN').replace(/-/g, '_')}__('${uniModuleId}');\n` + - compilation.assets[name].source().substring(newlineIndex + 1) + const source = + orignalSource.substring(0, newlineIndex) + + `\nmodule.exports = ${globalEnv}.__webpack_require_UNI_MP_PLUGIN__('${uniModuleId}');\n` + + orignalSource.substring(newlineIndex + 1) compilation.assets[name] = { - size () { + size() { return Buffer.byteLength(source, 'utf8') }, - source () { + source() { return source } } -- GitLab