Skip to content

Commit

Permalink
Merge branch 'master' into fix-2.9-i18n-web
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki authored Dec 11, 2023
2 parents 499f9bd + 4c788de commit 25c4af2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 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.9.7"
"version": "2.9.8"
}
81 changes: 46 additions & 35 deletions packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const EntryPlugin = require('webpack/lib/EntryPlugin')
const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
const FileSystemInfo = require('webpack/lib/FileSystemInfo')
const ImportDependency = require('webpack/lib/dependencies/ImportDependency')
const AsyncDependenciesBlock = require('webpack/lib/AsyncDependenciesBlock')
const normalize = require('./utils/normalize')
const toPosix = require('./utils/to-posix')
const addQuery = require('./utils/add-query')
Expand Down Expand Up @@ -325,36 +327,31 @@ class MpxWebpackPlugin {
compiler.options.resolve.plugins.push(packageEntryPlugin)
compiler.options.resolve.plugins.push(new FixDescriptionInfoPlugin())

let splitChunksPlugin
let splitChunksOptions

if (this.options.mode !== 'web') {
const optimization = compiler.options.optimization
optimization.runtimeChunk = {
name: (entrypoint) => {
for (const packageName in mpx.independentSubpackagesMap) {
if (hasOwn(mpx.independentSubpackagesMap, packageName) && isChunkInPackage(entrypoint.name, packageName)) {
return `${packageName}/bundle`
}
const optimization = compiler.options.optimization
optimization.runtimeChunk = {
name: (entrypoint) => {
for (const packageName in mpx.independentSubpackagesMap) {
if (hasOwn(mpx.independentSubpackagesMap, packageName) && isChunkInPackage(entrypoint.name, packageName)) {
return `${packageName}/bundle`
}
return 'bundle'
}
return 'bundle'
}
splitChunksOptions = Object.assign({
defaultSizeTypes: ['javascript', 'unknown'],
chunks: 'all',
usedExports: optimization.usedExports === true,
minChunks: 1,
minSize: 1000,
enforceSizeThreshold: Infinity,
maxAsyncRequests: 30,
maxInitialRequests: 30,
automaticNameDelimiter: '-'
}, optimization.splitChunks)
delete optimization.splitChunks
splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions)
splitChunksPlugin.apply(compiler)
}
const splitChunksOptions = Object.assign({
defaultSizeTypes: ['javascript', 'unknown'],
chunks: 'all',
usedExports: optimization.usedExports === true,
minChunks: 1,
minSize: 1000,
enforceSizeThreshold: Infinity,
maxAsyncRequests: 30,
maxInitialRequests: 30,
automaticNameDelimiter: '-'
}, optimization.splitChunks)
delete optimization.splitChunks
const splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions)
splitChunksPlugin.apply(compiler)

// 代理writeFile
if (this.options.writeMode === 'changed') {
Expand Down Expand Up @@ -635,7 +632,7 @@ class MpxWebpackPlugin {
useRelativePath: this.options.useRelativePath,
removedChunks: [],
forceProxyEventRules: this.options.forceProxyEventRules,
enableRequireAsync: this.options.mode === 'wx' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
supportRequireAsync: this.options.mode === 'wx' || this.options.mode === 'web' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
partialCompile: this.options.partialCompile,
collectDynamicEntryInfo: ({ resource, packageName, filename, entryType }) => {
const curInfo = mpx.dynamicEntryInfo[packageName] = mpx.dynamicEntryInfo[packageName] || {
Expand Down Expand Up @@ -1093,15 +1090,29 @@ class MpxWebpackPlugin {
// 删除root query
if (queryObj.root) request = addQuery(request, {}, false, ['root'])
// 目前仅wx和ali支持require.async,ali需要开启enableAliRequireAsync,其余平台使用CommonJsAsyncDependency进行模拟抹平
if (mpx.enableRequireAsync) {
const dep = new DynamicEntryDependency(request, 'export', '', tarRoot, '', context, range, {
isRequireAsync: true,
retryRequireAsync: !!this.options.retryRequireAsync
})
if (mpx.supportRequireAsync) {
if (mpx.mode === 'web') {
const depBlock = new AsyncDependenciesBlock(
{
name: tarRoot
},
expr.loc,
request
)
const dep = new ImportDependency(request, expr.range)
dep.loc = expr.loc
depBlock.addDependency(dep)
parser.state.current.addBlock(depBlock)
} else {
const dep = new DynamicEntryDependency(request, 'export', '', tarRoot, '', context, range, {
isRequireAsync: true,
retryRequireAsync: !!this.options.retryRequireAsync
})

parser.state.current.addPresentationalDependency(dep)
// 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
parser.state.current.addPresentationalDependency(dep)
// 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
}
} else {
const range = expr.range
const dep = new CommonJsAsyncDependency(request, range)
Expand Down
13 changes: 8 additions & 5 deletions packages/webpack-plugin/lib/json-compiler/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
const pathHash = mpx.pathHash
const getOutputPath = mpx.getOutputPath
const mode = mpx.mode
const enableRequireAsync = mpx.enableRequireAsync
const supportRequireAsync = mpx.supportRequireAsync
const asyncSubpackageRules = mpx.asyncSubpackageRules

const isUrlRequest = r => isUrlRequestRaw(r, root, externals)
Expand Down Expand Up @@ -51,15 +51,15 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
resolve(context, component, loaderContext, (err, resource, info) => {
if (err) return callback(err)
const { resourcePath, queryObj } = parseRequest(resource)
let placeholder = null
let placeholder = ''
if (queryObj.root) {
// 删除root query
resource = addQuery(resource, {}, false, ['root'])
// 目前只有微信支持分包异步化
if (enableRequireAsync) {
if (supportRequireAsync) {
tarRoot = queryObj.root
}
} else if (!queryObj.root && asyncSubpackageRules && enableRequireAsync) {
} else if (!queryObj.root && asyncSubpackageRules && supportRequireAsync) {
for (const item of asyncSubpackageRules) {
if (matchCondition(resourcePath, item)) {
tarRoot = item.root
Expand Down Expand Up @@ -97,7 +97,10 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
}

const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath)
callback(null, entry, tarRoot, placeholder)
callback(null, entry, {
tarRoot,
placeholder
})
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-plugin/lib/json-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ module.exports = function (content) {
const processComponents = (components, context, callback) => {
if (components) {
async.eachOf(components, (component, name, callback) => {
processComponent(component, context, { relativePath }, (err, entry, root, placeholder) => {
processComponent(component, context, { relativePath }, (err, entry, { tarRoot, placeholder } = {}) => {
if (err === RESOLVE_IGNORED_ERR) {
delete components[name]
return callback()
}
if (err) return callback(err)
components[name] = entry
if (root) {
if (tarRoot) {
if (placeholder) {
placeholder = normalizePlaceholder(placeholder)
if (placeholder.resource) {
Expand Down
7 changes: 4 additions & 3 deletions packages/webpack-plugin/lib/web/processJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ module.exports = function (json, {
customGetDynamicEntry (resource, type, outputPath, packageRoot) {
return {
resource,
outputPath: toPosix(path.join(packageRoot, outputPath)),
// 输出web时组件outputPath不需要拼接packageRoot
outputPath: type === 'page' ? toPosix(path.join(packageRoot, outputPath)) : outputPath,
packageRoot
}
}
Expand Down Expand Up @@ -297,7 +298,7 @@ module.exports = function (json, {
const processComponents = (components, context, callback) => {
if (components) {
async.eachOf(components, (component, name, callback) => {
processComponent(component, context, {}, (err, { resource, outputPath } = {}) => {
processComponent(component, context, {}, (err, { resource, outputPath } = {}, { tarRoot } = {}) => {
if (err) return callback(err === RESOLVE_IGNORED_ERR ? null : err)
const { resourcePath, queryObj } = parseRequest(resource)
componentsMap[resourcePath] = outputPath
Expand All @@ -307,7 +308,7 @@ module.exports = function (json, {
isComponent: true,
outputPath
}),
async: queryObj.async
async: queryObj.async || tarRoot
}
callback()
})
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.9.7",
"version": "2.9.8",
"description": "mpx compile core",
"keywords": [
"mpx"
Expand Down

0 comments on commit 25c4af2

Please sign in to comment.