Skip to content

Commit

Permalink
Merge pull request #1506 from didi/fix-event-component
Browse files Browse the repository at this point in the history
fix: 单独构建组件事件、tranRpxFn 配置不生效等问题
  • Loading branch information
hiyuki authored Jun 18, 2024
2 parents eb96476 + 22592e7 commit 66af39e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
18 changes: 13 additions & 5 deletions packages/webpack-plugin/lib/runtime/components/web/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ function MpxEvent (layer) {
this.addListener()
}

if (isBrowser) {
document.addEventListener('DOMContentLoaded', function () {
// eslint-disable-next-line no-new
new MpxEvent(document.body)
}, false)
export function createEvent () {
if (isBrowser && !global.__mpxCreatedEvent) {
global.__mpxCreatedEvent = true
if (document.readyState === 'complete' || document.readyState === 'interactive') {
// eslint-disable-next-line no-new
new MpxEvent(document.body)
} else {
document.addEventListener('DOMContentLoaded', function () {
// eslint-disable-next-line no-new
new MpxEvent(document.body)
}, false)
}
}
}
3 changes: 3 additions & 0 deletions packages/webpack-plugin/lib/runtime/optionProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { hasOwn, isEmptyObject } from './utils'
import { isBrowser } from './env'
import transRpxStyle from './transRpxStyle'
import animation from './animation'
import { createEvent } from './components/web/event'

createEvent()

export function processComponentOption (
{
Expand Down
4 changes: 1 addition & 3 deletions packages/webpack-plugin/lib/web/processMainScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const addQuery = require('../utils/add-query')
const normalize = require('../utils/normalize')
const optionProcessorPath = normalize.lib('runtime/optionProcessor')
const eventPath = normalize.lib('runtime/components/web/event')

const {
buildComponentsMap,
buildPagesMap,
Expand Down Expand Up @@ -66,8 +66,6 @@ module.exports = function (script, {
globalTabBar
})

output += `\n require(${stringifyRequest(loaderContext, eventPath)})\n`

output += `\n var App = require(${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}).default\n`

output += `
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-plugin/lib/web/processScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (script, {
wxsModuleMap,
localComponentsMap
}, callback) {
const { projectRoot, appInfo } = loaderContext.getMpx()
const { projectRoot, appInfo, webConfig } = loaderContext.getMpx()

const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)

Expand Down Expand Up @@ -73,7 +73,7 @@ module.exports = function (script, {
})
}

content += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction })
content += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction, webConfig, hasApp })
content += getRequireScript({ ctorType, script, loaderContext })
content += `
export default processComponentOption({
Expand Down
8 changes: 7 additions & 1 deletion packages/webpack-plugin/lib/web/script-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function buildGlobalParams ({
jsonConfig,
webConfig,
isMain,
globalTabBar
globalTabBar,
hasApp
}) {
let content = ''
if (isMain) {
Expand Down Expand Up @@ -175,6 +176,11 @@ function buildGlobalParams ({
if (globalTabBar) {
content += globalTabBar
}
} else if (!hasApp) {
content += `
global.__mpxGenericsMap = global.__mpxGenericsMap || {}
global.__mpxOptionsMap = global.__mpxOptionsMap || {}
global.__mpxTransRpxFn = ${webConfig.transRpxFn} \n`
}
content += ` global.currentModuleId = ${JSON.stringify(moduleId)}\n`
content += ` global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
Expand Down

0 comments on commit 66af39e

Please sign in to comment.