Skip to content

Commit

Permalink
Merge pull request #1205 from didi/fix-ali-root-view-moduleid
Browse files Browse the repository at this point in the history
Fix ali root view moduleid
  • Loading branch information
hiyuki authored Nov 20, 2024
2 parents 6bad0b0 + bdbe6e5 commit 45cbd6b
Show file tree
Hide file tree
Showing 27 changed files with 291 additions and 214 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const NullDependency = require('webpack/lib/dependencies/NullDependency')
const makeSerializable = require('webpack/lib/util/makeSerializable')
const addQuery = require('../utils/add-query')

class RecordGlobalComponentsDependency extends NullDependency {
constructor (usingComponents, context) {
constructor (globalComponents, globalComponentsInfo, context) {
super()
this.usingComponents = usingComponents
this.globalComponents = globalComponents
this.globalComponentsInfo = globalComponentsInfo
this.context = context
}

Expand All @@ -15,26 +15,25 @@ class RecordGlobalComponentsDependency extends NullDependency {

mpxAction (module, compilation, callback) {
const mpx = compilation.__mpx__
const { usingComponents, context } = this
Object.keys(usingComponents).forEach((key) => {
const request = usingComponents[key]
mpx.usingComponents[key] = addQuery(request, {
context
})
})
const { globalComponents, globalComponentsInfo } = this

mpx.globalComponents = globalComponents
mpx.globalComponentsInfo = globalComponentsInfo
return callback()
}

serialize (context) {
const { write } = context
write(this.usingComponents)
write(this.globalComponents)
write(this.globalComponentsInfo)
write(this.context)
super.serialize(context)
}

deserialize (context) {
const { read } = context
this.usingComponents = read()
this.globalComponents = read()
this.globalComponentsInfo = read()
this.context = read()
super.deserialize(context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RecordRuntimeInfoDependency extends NullDependency {
template: {},
json: {},
style: [],
moduleId: '_' + mpx.pathHash(this.resourcePath)
moduleId: mpx.getModuleId(this.resourcePath)
}

const infoConfig = componentInfo[this.blockType]
Expand Down
15 changes: 13 additions & 2 deletions packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const async = require('async')
const { parseQuery } = require('loader-utils')
const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource')
const emitFile = require('./utils/emit-file')
const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE, MPX_APP_MODULE_ID } = require('./utils/const')
const isEmptyObject = require('./utils/is-empty-object')
const DynamicPlugin = require('./resolver/DynamicPlugin')
const { isReact, isWeb } = require('./utils/env')
Expand Down Expand Up @@ -179,6 +179,7 @@ class MpxWebpackPlugin {
include: () => true
}
options.customOutputPath = options.customOutputPath || null
options.customComponentModuleId = options.customComponentModuleId || null
options.nativeConfig = Object.assign({
cssLangs: ['css', 'less', 'stylus', 'scss', 'sass']
}, options.nativeConfig)
Expand Down Expand Up @@ -679,7 +680,8 @@ class MpxWebpackPlugin {
assetsModulesMap: new Map(),
// 记录与asset相关联的ast,用于体积分析和esCheck,避免重复parse
assetsASTsMap: new Map(),
usingComponents: {},
globalComponents: {},
globalComponentsInfo: {},
// todo es6 map读写性能高于object,之后会逐步替换
wxsAssetsCache: new Map(),
addEntryPromiseMap: new Map(),
Expand Down Expand Up @@ -745,6 +747,15 @@ class MpxWebpackPlugin {
compilation.addEntry(compiler.context, dep, { name }, callback)
return dep
},
getModuleId: (filePath, isApp = false) => {
if (isApp) return MPX_APP_MODULE_ID
const customComponentModuleId = this.options.customComponentModuleId
if (typeof customComponentModuleId === 'function') {
const customModuleId = customComponentModuleId(filePath)
if (customModuleId) return customModuleId
}
return '_' + mpx.pathHash(filePath)
},
getEntryNode: (module, type) => {
const entryNodeModulesMap = mpx.entryNodeModulesMap
let entryNode = entryNodeModulesMap.get(module)
Expand Down
13 changes: 2 additions & 11 deletions packages/webpack-plugin/lib/json-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const addQuery = require('../utils/add-query')
const getJSONContent = require('../utils/get-json-content')
const createHelpers = require('../helpers')
const createJSONHelper = require('./helper')
const RecordGlobalComponentsDependency = require('../dependencies/RecordGlobalComponentsDependency')
const RecordIndependentDependency = require('../dependencies/RecordIndependentDependency')
const RecordRuntimeInfoDependency = require('../dependencies/RecordRuntimeInfoDependency')
const { MPX_DISABLE_EXTRACTOR_CACHE, RESOLVE_IGNORED_ERR, JSON_JS_EXT } = require('../utils/const')
Expand Down Expand Up @@ -208,8 +207,8 @@ module.exports = function (content) {
warn: emitWarning,
error: emitError,
data: {
// polyfill global usingComponents & record globalComponents
globalComponents: mpx.usingComponents
// polyfill global usingComponents
globalComponents: mpx.globalComponents
}
}
if (!isApp) {
Expand All @@ -222,14 +221,6 @@ module.exports = function (content) {
rulesRunner(json)
}

if (isApp) {
Object.assign(mpx.usingComponents, json.usingComponents)
// 在 rulesRunner 运行后保存全局注册组件
// todo 其余地方在使用mpx.usingComponents时存在缓存问题,要规避该问题需要在所有使用mpx.usingComponents的loader中添加app resourcePath作为fileDependency,但对于缓存有效率影响巨大
// todo 需要考虑一种精准控制缓存的方式,仅在全局组件发生变更时才使相关使用方的缓存失效,例如按需在相关模块上动态添加request query?
this._module.addPresentationalDependency(new RecordGlobalComponentsDependency(mpx.usingComponents, this.context))
}

const processComponents = (components, context, callback) => {
if (components) {
async.eachOf(components, (component, name, callback) => {
Expand Down
69 changes: 24 additions & 45 deletions packages/webpack-plugin/lib/loader.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
const JSON5 = require('json5')
const parseComponent = require('./parser')
const createHelpers = require('./helpers')
const parseRequest = require('./utils/parse-request')
const { matchCondition } = require('./utils/match-condition')
const addQuery = require('./utils/add-query')
const async = require('async')
const getJSONContent = require('./utils/get-json-content')
const normalize = require('./utils/normalize')
const getEntryName = require('./utils/get-entry-name')
const AppEntryDependency = require('./dependencies/AppEntryDependency')
const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter')
const { MPX_APP_MODULE_ID } = require('./utils/const')
const { isReact } = require('./utils/env')
const preProcessJson = require('./utils/pre-process-json')
const path = require('path')
const processWeb = require('./web')
const processReact = require('./react')
const getRulesRunner = require('./platform')
const genMpxCustomElement = require('./runtime-render/gen-mpx-custom-element')

module.exports = function (content) {
Expand Down Expand Up @@ -89,7 +86,7 @@ module.exports = function (content) {
const loaderContext = this
const isProduction = this.minimize || process.env.NODE_ENV === 'production'
const filePath = this.resourcePath
const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : '_' + mpx.pathHash(filePath)
const moduleId = mpx.getModuleId(resourcePath, ctorType === 'app')

const parts = parseComponent(content, {
filePath,
Expand All @@ -106,55 +103,36 @@ module.exports = function (content) {

async.waterfall([
(callback) => {
getJSONContent(parts.json || {}, null, loaderContext, (err, content) => {
preProcessJson({
json: parts.json || {},
srcMode,
emitWarning,
emitError,
ctorType,
resourcePath,
loaderContext
}, (err, jsonInfo) => {
if (err) return callback(err)
if (parts.json) parts.json.content = content
callback()
callback(null, jsonInfo)
})
},
(callback) => {
(jsonInfo, callback) => {
const {
componentPlaceholder,
componentGenerics,
usingComponentsInfo,
jsonContent
} = jsonInfo
const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
const templateAttrs = parts.template && parts.template.attrs
const hasComment = templateAttrs && templateAttrs.comments
const isNative = false

let usingComponents = [].concat(Object.keys(mpx.usingComponents))
let componentPlaceholder = []
let componentGenerics = {}

if (parts.json && parts.json.content) {
const rulesRunnerOptions = {
mode,
srcMode,
type: 'json',
waterfall: true,
warn: emitWarning,
error: emitError
}
if (ctorType !== 'app') {
rulesRunnerOptions.mainKey = pagesMap[resourcePath] ? 'page' : 'component'
}
const rulesRunner = getRulesRunner(rulesRunnerOptions)
try {
const ret = JSON5.parse(parts.json.content)
if (rulesRunner) rulesRunner(ret)
if (ret.usingComponents) {
usingComponents = usingComponents.concat(Object.keys(ret.usingComponents))
}
if (ret.componentPlaceholder) {
componentPlaceholder = componentPlaceholder.concat(Object.values(ret.componentPlaceholder))
}
if (ret.componentGenerics) {
componentGenerics = Object.assign({}, ret.componentGenerics)
}
} catch (e) {
return callback(e)
}
}
// 处理mode为web时输出vue格式文件
if (mode === 'web') {
return processWeb({
parts,
jsonContent,
loaderContext,
pagesMap,
componentsMap,
Expand All @@ -166,7 +144,7 @@ module.exports = function (content) {
hasScoped,
hasComment,
isNative,
usingComponents,
usingComponentsInfo: JSON.stringify(usingComponentsInfo),
componentGenerics,
autoScope,
callback
Expand All @@ -176,6 +154,7 @@ module.exports = function (content) {
if (isReact(mode)) {
return processReact({
parts,
jsonContent,
loaderContext,
pagesMap,
componentsMap,
Expand All @@ -187,7 +166,7 @@ module.exports = function (content) {
hasScoped,
hasComment,
isNative,
usingComponents,
usingComponentsInfo: JSON.stringify(usingComponentsInfo),
componentGenerics,
autoScope,
callback
Expand Down Expand Up @@ -255,7 +234,7 @@ module.exports = function (content) {
isNative,
ctorType,
moduleId,
usingComponents,
usingComponentsInfo: JSON.stringify(usingComponentsInfo),
componentPlaceholder
// 添加babel处理渲染函数中可能包含的...展开运算符
// 由于...运算符应用范围极小以及babel成本极高,先关闭此特性后续看情况打开
Expand Down
Loading

0 comments on commit 45cbd6b

Please sign in to comment.