-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
songyu
committed
Mar 13, 2023
1 parent
e790289
commit 9cacd59
Showing
6 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
const createWxMpIndependentPlugins = require('./createIndependentPlugin') | ||
const RequireAsyncPlugin = require('./support-require-async/RequireAsyncPlugin') | ||
|
||
module.exports = function createWxMpPlugins () { | ||
if (process.env.UNI_PLATFORM === 'mp-weixin') { | ||
return [ | ||
...createWxMpIndependentPlugins(), | ||
new RequireAsyncPlugin() | ||
] | ||
} | ||
return [] | ||
} |
44 changes: 44 additions & 0 deletions
44
...ages/uni-mp-weixin/lib/support-require-async/RequireAsyncDependenciesBlockParserPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const AsyncDependenciesBlock = require('webpack/lib/AsyncDependenciesBlock') | ||
const RequireAsyncDependency = require('./RequireAsyncDependency') | ||
|
||
class RequireAsyncDependenciesBlock extends AsyncDependenciesBlock { | ||
constructor (request, range, groupOptions, module, loc, originModule) { | ||
super(groupOptions, module, loc, request) | ||
this.range = range | ||
const dep = new RequireAsyncDependency(request, originModule, this) | ||
dep.loc = loc | ||
this.addDependency(dep) | ||
} | ||
} | ||
|
||
module.exports = class RequireAsyncDependenciesBlockParserPlugin { | ||
constructor (options) { | ||
this.options = options | ||
} | ||
|
||
apply (parser) { | ||
parser.hooks.call | ||
.for('require.async').tap('RequireAsyncDependenciesBlockParserPlugin', expr => { | ||
const param = parser.evaluateExpression(expr.arguments[0]) | ||
|
||
const { options: importOptions } = parser.parseCommentOptions(expr.range) | ||
let chunkName = null | ||
if (importOptions && importOptions.webpackChunkName !== undefined) { | ||
chunkName = importOptions.webpackChunkName | ||
} | ||
const groupOptions = { name: chunkName } | ||
|
||
const depBlock = new RequireAsyncDependenciesBlock( | ||
param.string, | ||
expr.range, | ||
groupOptions, | ||
parser.state.module, | ||
expr.loc, | ||
parser.state.module | ||
) | ||
|
||
parser.state.current.addBlock(depBlock) | ||
return true | ||
}) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/uni-mp-weixin/lib/support-require-async/RequireAsyncDependency.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const path = require('path') | ||
const ImportDependency = require('webpack/lib/dependencies/ImportDependency') | ||
|
||
class RequireAsyncDependency extends ImportDependency { | ||
|
||
}; | ||
|
||
RequireAsyncDependency.Template = class ImportDependencyTemplate { | ||
apply (dep, source, runtime) { | ||
let content = runtime.moduleExports({ | ||
module: dep.module, | ||
request: dep.request | ||
}) | ||
|
||
// 目前只支持相对路径引用 | ||
const relativePath = path.relative(dep.originModule.context, dep.module.userRequest) | ||
// 利用 require.async 加载分包中的资源,取代web环境下的 __webpack_require__.e(...) | ||
content = `require.async("${relativePath}").then(function(){return ${content} })` | ||
source.replace(dep.block.range[0], dep.block.range[1] - 1, content) | ||
} | ||
} | ||
|
||
module.exports = RequireAsyncDependency |
29 changes: 29 additions & 0 deletions
29
packages/uni-mp-weixin/lib/support-require-async/RequireAsyncPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
const RequireAsyncDependenciesBlockParserPlugin = require('./RequireAsyncDependenciesBlockParserPlugin') | ||
const RequireAsyncDependency = require('./RequireAsyncDependency') | ||
|
||
class RequireAsyncPlugin { | ||
constructor(options) { | ||
this.options = options | ||
} | ||
|
||
apply(compiler) { | ||
const options = this.options | ||
compiler.hooks.compilation.tap('RequireAsyncPlugin', | ||
|
||
(compilation, { normalModuleFactory }) => { | ||
compilation.dependencyFactories.set(RequireAsyncDependency, normalModuleFactory) | ||
compilation.dependencyTemplates.set(RequireAsyncDependency, new RequireAsyncDependency.Template()) | ||
|
||
const handler = (parser, parserOptions) => { | ||
new RequireAsyncDependenciesBlockParserPlugin(options).apply(parser) | ||
} | ||
|
||
normalModuleFactory.hooks.parser.for('javascript/auto').tap('RequireAsyncDependenciesBlockParserPlugin', handler) | ||
normalModuleFactory.hooks.parser.for('javascript/dynamic').tap('RequireAsyncDependenciesBlockParserPlugin', handler) | ||
normalModuleFactory.hooks.parser.for('javascript/esm').tap('RequireAsyncDependenciesBlockParserPlugin', handler) | ||
} | ||
) | ||
} | ||
} | ||
module.exports = RequireAsyncPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters