Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/didi/mpx into fix_web_com…
Browse files Browse the repository at this point in the history
…pile
  • Loading branch information
anotherso1a committed Sep 27, 2023
2 parents e0c61ed + 4cbd364 commit 824e04d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "2.8.47"
"version": "2.8.48"
}
26 changes: 17 additions & 9 deletions packages/core/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ type ArrayType<T extends any[]> = T extends Array<infer R> ? R : never;
// Mpx types
type Data = object | (() => object)

type PropType = StringConstructor | NumberConstructor | BooleanConstructor | ObjectConstructor | ArrayConstructor | null
type PropConstructor<T = any> = {
new (...args: any[]): T & {};
} | {
(): T;
}

interface PropOpt {
type: PropType
optionalTypes?: Array<PropType>
value?: any
export type PropType<T> = PropConstructor<T>

observer? (value: any, old: any, changedPath: string): void
type FullPropType<T> = {
type: PropType<T>;
value?: T;
optionalTypes?: PropType<T>[];
}

interface Properties {
[key: string]: WechatMiniprogram.Component.AllProperty
[key: string]: WechatMiniprogram.Component.AllProperty | PropType<any> | FullPropType<any>
}

interface Methods {
Expand Down Expand Up @@ -78,8 +82,12 @@ type PropValueType<Def> = Def extends {
}
? T
: Def extends (...args: any[]) => infer T
? T
: any;
? T
: Def extends FullPropType<infer T>
? T
: Def extends PropType<infer T>
? T
: any;

type GetPropsType<T> = {
readonly [K in keyof T]: PropValueType<T[K]>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpxjs/core",
"version": "2.8.47",
"version": "2.8.48",
"description": "mpx runtime core",
"keywords": [
"miniprogram",
Expand Down
2 changes: 1 addition & 1 deletion packages/size-report/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpxjs/size-report",
"version": "2.8.47",
"version": "2.8.48",
"description": "mpx size report plugin",
"main": "src/index.js",
"scripts": {
Expand Down
42 changes: 32 additions & 10 deletions packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class MpxWebpackPlugin {
options.asyncSubpackageRules = options.asyncSubpackageRules || null
options.retryRequireAsync = options.retryRequireAsync || false
options.enableAliRequireAsync = options.enableAliRequireAsync || false
options.optimizeSize = options.optimizeSize || false
this.options = options
// Hack for buildDependencies
const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
Expand Down Expand Up @@ -294,6 +295,14 @@ class MpxWebpackPlugin {
warnings.push(`webpack options: MpxWebpackPlugin accept options.output.filename to be ${outputFilename} only, custom options.output.filename will be ignored!`)
}
compiler.options.output.filename = compiler.options.output.chunkFilename = outputFilename
if (this.options.optimizeSize) {
compiler.options.optimization.chunkIds = 'total-size'
compiler.options.optimization.moduleIds = 'natural'
compiler.options.optimization.mangleExports = 'size'
compiler.options.output.globalObject = 'g'
// todo chunkLoadingGlobal不具备项目唯一性,在多构建产物混编时可能存在问题,尤其在支付宝使用全局对象传递的情况下
compiler.options.output.chunkLoadingGlobal = 'c'
}
}

if (!compiler.options.node || !compiler.options.node.global) {
Expand Down Expand Up @@ -387,6 +396,7 @@ class MpxWebpackPlugin {
const query = parseQuery(obj.query || '?')
return query.isPage && !query.type
}

// new PartialCompilePlugin(this.options.partialCompile).apply(compiler)
compiler.resolverFactory.hooks.resolver.intercept({
factory: (type, hook) => {
Expand Down Expand Up @@ -685,7 +695,15 @@ class MpxWebpackPlugin {
mpx.extractedFilesCache.set(resource, file)
return file
},
recordResourceMap: ({ resourcePath, resourceType, outputPath, packageRoot = '', recordOnly, warn, error }) => {
recordResourceMap: ({
resourcePath,
resourceType,
outputPath,
packageRoot = '',
recordOnly,
warn,
error
}) => {
const packageName = packageRoot || 'main'
const resourceMap = mpx[`${resourceType}sMap`] || mpx.otherResourcesMap
const currentResourceMap = resourceMap.main ? resourceMap[packageName] = resourceMap[packageName] || {} : resourceMap
Expand Down Expand Up @@ -1297,6 +1315,8 @@ class MpxWebpackPlugin {
chunkLoadingGlobal
} = compilation.outputOptions

const chunkLoadingGlobalStr = JSON.stringify(chunkLoadingGlobal)

function getTargetFile (file) {
let targetFile = file
const queryStringIdx = targetFile.indexOf('?')
Expand All @@ -1316,7 +1336,7 @@ class MpxWebpackPlugin {

const originalSource = compilation.assets[chunkFile]
const source = new ConcatSource()
source.add(`\nvar ${globalObject} = ${globalObject} || {};\n\n`)
source.add(`\nvar ${globalObject} = {};\n`)

relativeChunks.forEach((relativeChunk, index) => {
const relativeChunkFile = relativeChunk.files.values().next().value
Expand All @@ -1333,27 +1353,28 @@ class MpxWebpackPlugin {
if (compilation.options.entry[chunk.name]) {
// 在rootChunk中挂载jsonpCallback
source.add('// process ali subpackages runtime in root chunk\n' +
'var context = (function() { return this })() || Function("return this")();\n\n')
source.add(`context[${JSON.stringify(chunkLoadingGlobal)}] = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}] = require("${relativePath}");\n`)
'var context = (function() { return this })() || Function("return this")();\n')
source.add(`context[${chunkLoadingGlobalStr}] = ${globalObject}[${chunkLoadingGlobalStr}] = require("${relativePath}");\n`)
} else {
// 其余chunk中通过context全局传递runtime
source.add('// process ali subpackages runtime in other chunk\n' +
'var context = (function() { return this })() || Function("return this")();\n\n')
source.add(`${globalObject}[${JSON.stringify(chunkLoadingGlobal)}] = context[${JSON.stringify(chunkLoadingGlobal)}];\n`)
'var context = (function() { return this })() || Function("return this")();\n')
source.add(`${globalObject}[${chunkLoadingGlobalStr}] = context[${chunkLoadingGlobalStr}];\n`)
}
} else {
source.add(`${globalObject}[${JSON.stringify(chunkLoadingGlobal)}] = require("${relativePath}");\n`)
source.add(`${globalObject}[${chunkLoadingGlobalStr}] = require("${relativePath}");\n`)
}
} else {
source.add(`require("${relativePath}");\n`)
}
})

if (isRuntime) {
source.add('var context = (function() { return this })() || Function("return this")();\n')
source.add(`
if (mpx.mode === 'ali' || mpx.mode === 'qq') {
source.add(`
// Fix babel runtime in some quirky environment like ali & qq dev.
try {
var context = (function() { return this })() || Function("return this")();
if(!context.console){
context.console = console;
context.setInterval = setInterval;
Expand Down Expand Up @@ -1394,8 +1415,9 @@ try {
}
} catch(e){
}\n`)
}
source.add(originalSource)
source.add(`\nmodule.exports = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}];\n`)
source.add(`\nmodule.exports = ${globalObject}[${chunkLoadingGlobalStr}];\n`)
} else {
source.add(originalSource)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpxjs/webpack-plugin",
"version": "2.8.47",
"version": "2.8.48",
"description": "mpx compile core",
"keywords": [
"mpx"
Expand Down

0 comments on commit 824e04d

Please sign in to comment.