提交 6e82d141 编写于 作者: D DCloud_LXH

fix(mp): 1. 修复 小程序插件编译时入口文件判断错误。移除使用 UNI_MP_PLUGIN 定义变量

上级 858a9f13
......@@ -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)
}
}
}
......
......@@ -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')
}
}
}
......@@ -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
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册