@mpxjs/webpack-plugin 2.10.14-beta.2 → 2.10.14-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const Dependency = require('webpack/lib/Dependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency')
|
|
4
|
+
const { RetryRuntimeGlobal } = require('../retry-runtime-module')
|
|
5
|
+
|
|
6
|
+
class ImportDependency extends ModuleDependency {
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} request the request
|
|
9
|
+
* @param {[number, number]} range expression range
|
|
10
|
+
* @param {string[][]=} referencedExports list of referenced exports
|
|
11
|
+
*/
|
|
12
|
+
constructor (request, range, referencedExports, extraOptions) {
|
|
13
|
+
super(request)
|
|
14
|
+
this.range = range
|
|
15
|
+
this.referencedExports = referencedExports
|
|
16
|
+
this.extraOptions = extraOptions
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get type () {
|
|
20
|
+
return 'import()'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get category () {
|
|
24
|
+
return 'esm'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Returns list of exports referenced by this dependency
|
|
29
|
+
* @param {ModuleGraph} moduleGraph module graph
|
|
30
|
+
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|
31
|
+
* @returns {(string[] | ReferencedExport)[]} referenced exports
|
|
32
|
+
*/
|
|
33
|
+
getReferencedExports (moduleGraph, runtime) {
|
|
34
|
+
return this.referencedExports
|
|
35
|
+
? this.referencedExports.map((e) => ({
|
|
36
|
+
name: e,
|
|
37
|
+
canMangle: false
|
|
38
|
+
}))
|
|
39
|
+
: Dependency.EXPORTS_OBJECT_REFERENCED
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
serialize (context) {
|
|
43
|
+
context.write(this.range)
|
|
44
|
+
context.write(this.referencedExports)
|
|
45
|
+
context.write(this.extraOptions)
|
|
46
|
+
super.serialize(context)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
deserialize (context) {
|
|
50
|
+
this.range = context.read()
|
|
51
|
+
this.referencedExports = context.read()
|
|
52
|
+
this.extraOptions = context.read()
|
|
53
|
+
super.deserialize(context)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
makeSerializable(ImportDependency, '@mpxjs/webpack-plugin/lib/dependencies/ImportDependency')
|
|
58
|
+
|
|
59
|
+
ImportDependency.Template = class ImportDependencyTemplate extends (
|
|
60
|
+
ModuleDependency.Template
|
|
61
|
+
) {
|
|
62
|
+
/**
|
|
63
|
+
* @param {Dependency} dependency the dependency for which the template should be applied
|
|
64
|
+
* @param {ReplaceSource} source the current replace source which can be modified
|
|
65
|
+
* @param {DependencyTemplateContext} templateContext the context object
|
|
66
|
+
* @returns {void}
|
|
67
|
+
*/
|
|
68
|
+
apply (
|
|
69
|
+
dependency,
|
|
70
|
+
source,
|
|
71
|
+
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
|
|
72
|
+
) {
|
|
73
|
+
const dep = /** @type {ImportDependency} */ (dependency)
|
|
74
|
+
const block = /** @type {AsyncDependenciesBlock} */ (
|
|
75
|
+
moduleGraph.getParentBlock(dep)
|
|
76
|
+
)
|
|
77
|
+
let content = runtimeTemplate.moduleNamespacePromise({
|
|
78
|
+
chunkGraph,
|
|
79
|
+
block: block,
|
|
80
|
+
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
|
81
|
+
request: dep.request,
|
|
82
|
+
strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
|
|
83
|
+
message: 'import()',
|
|
84
|
+
runtimeRequirements
|
|
85
|
+
})
|
|
86
|
+
// replace fakeType by 9 to fix require.async to commonjs2 module like 'module.exports = function(){...}'
|
|
87
|
+
content = content.replace(/(__webpack_require__\.t\.bind\(.+,\s*)(\d+)(\s*\))/, (_, p1, p2, p3) => {
|
|
88
|
+
return p1 + '9' + p3
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
// require.async 的场景且配置了重试次数才注入 RetryRuntimeModule
|
|
92
|
+
const extraOptions = dep.extraOptions || {}
|
|
93
|
+
if (extraOptions.isRequireAsync && extraOptions.retryRequireAsync && extraOptions.retryRequireAsync.times > 0) {
|
|
94
|
+
runtimeRequirements.add(RetryRuntimeGlobal)
|
|
95
|
+
content = `${RetryRuntimeGlobal}(function() { return ${content} }, ${extraOptions.retryRequireAsync.times}, ${extraOptions.retryRequireAsync.interval})`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
source.replace(dep.range[0], dep.range[1] - 1, content)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = ImportDependency
|
package/lib/index.js
CHANGED
|
@@ -14,8 +14,7 @@ const EntryPlugin = require('webpack/lib/EntryPlugin')
|
|
|
14
14
|
const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
|
|
15
15
|
const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
|
|
16
16
|
const FileSystemInfo = require('webpack/lib/FileSystemInfo')
|
|
17
|
-
const ImportDependency = require('
|
|
18
|
-
const ImportDependencyTemplate = require('./dependencies/ImportDependencyTemplate')
|
|
17
|
+
const ImportDependency = require('./dependencies/ImportDependency')
|
|
19
18
|
const AsyncDependenciesBlock = require('webpack/lib/AsyncDependenciesBlock')
|
|
20
19
|
const ProvidePlugin = require('webpack/lib/ProvidePlugin')
|
|
21
20
|
const normalize = require('./utils/normalize')
|
|
@@ -696,7 +695,8 @@ class MpxWebpackPlugin {
|
|
|
696
695
|
compilation.dependencyFactories.set(RequireExternalDependency, new NullFactory())
|
|
697
696
|
compilation.dependencyTemplates.set(RequireExternalDependency, new RequireExternalDependency.Template())
|
|
698
697
|
|
|
699
|
-
compilation.
|
|
698
|
+
compilation.dependencyFactories.set(ImportDependency, normalModuleFactory)
|
|
699
|
+
compilation.dependencyTemplates.set(ImportDependency, new ImportDependency.Template())
|
|
700
700
|
})
|
|
701
701
|
|
|
702
702
|
compiler.hooks.thisCompilation.tap('MpxWebpackPlugin', (compilation, { normalModuleFactory }) => {
|
|
@@ -1451,10 +1451,6 @@ class MpxWebpackPlugin {
|
|
|
1451
1451
|
if (mpx.supportRequireAsync) {
|
|
1452
1452
|
if (isWeb(mpx.mode) || isReact(mpx.mode)) {
|
|
1453
1453
|
if (isReact(mpx.mode)) tarRoot = transSubpackage(mpx.transSubpackageRules, tarRoot)
|
|
1454
|
-
request = addQuery(request, {
|
|
1455
|
-
isRequireAsync: true,
|
|
1456
|
-
retryRequireAsync: JSON.stringify(this.options.retryRequireAsync)
|
|
1457
|
-
})
|
|
1458
1454
|
const depBlock = new AsyncDependenciesBlock(
|
|
1459
1455
|
{
|
|
1460
1456
|
name: tarRoot + '/index'
|
|
@@ -1462,7 +1458,10 @@ class MpxWebpackPlugin {
|
|
|
1462
1458
|
expr.loc,
|
|
1463
1459
|
request
|
|
1464
1460
|
)
|
|
1465
|
-
const dep = new ImportDependency(request, expr.range
|
|
1461
|
+
const dep = new ImportDependency(request, expr.range, undefined, {
|
|
1462
|
+
isRequireAsync: true,
|
|
1463
|
+
retryRequireAsync: this.options.retryRequireAsync
|
|
1464
|
+
})
|
|
1466
1465
|
dep.loc = expr.loc
|
|
1467
1466
|
depBlock.addDependency(dep)
|
|
1468
1467
|
parser.state.current.addBlock(depBlock)
|
package/package.json
CHANGED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency')
|
|
2
|
-
const { RetryRuntimeGlobal } = require('../retry-runtime-module')
|
|
3
|
-
const parseRequest = require('../utils/parse-request')
|
|
4
|
-
|
|
5
|
-
class ImportDependencyTemplate extends (
|
|
6
|
-
ModuleDependency.Template
|
|
7
|
-
) {
|
|
8
|
-
/**
|
|
9
|
-
* @param {Dependency} dependency the dependency for which the template should be applied
|
|
10
|
-
* @param {ReplaceSource} source the current replace source which can be modified
|
|
11
|
-
* @param {DependencyTemplateContext} templateContext the context object
|
|
12
|
-
* @returns {void}
|
|
13
|
-
*/
|
|
14
|
-
apply (
|
|
15
|
-
dependency,
|
|
16
|
-
source,
|
|
17
|
-
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
|
|
18
|
-
) {
|
|
19
|
-
const dep = /** @type {ImportDependency} */ (dependency)
|
|
20
|
-
const block = /** @type {AsyncDependenciesBlock} */ (
|
|
21
|
-
moduleGraph.getParentBlock(dep)
|
|
22
|
-
)
|
|
23
|
-
let content = runtimeTemplate.moduleNamespacePromise({
|
|
24
|
-
chunkGraph,
|
|
25
|
-
block: block,
|
|
26
|
-
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
|
27
|
-
request: dep.request,
|
|
28
|
-
strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
|
|
29
|
-
message: 'import()',
|
|
30
|
-
runtimeRequirements
|
|
31
|
-
})
|
|
32
|
-
// replace fakeType by 9 to fix require.async to commonjs2 module like 'module.exports = function(){...}'
|
|
33
|
-
content = content.replace(/(__webpack_require__\.t\.bind\(.+,\s*)(\d+)(\s*\))/, (_, p1, p2, p3) => {
|
|
34
|
-
return p1 + '9' + p3
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
const { queryObj } = parseRequest(dep.request)
|
|
38
|
-
const retryRequireAsync = queryObj.retryRequireAsync && JSON.parse(queryObj.retryRequireAsync)
|
|
39
|
-
|
|
40
|
-
// require.async 的场景且配置了重试次数才注入 RetryRuntimeModule
|
|
41
|
-
if (queryObj.isRequireAsync && retryRequireAsync && retryRequireAsync.times > 0) {
|
|
42
|
-
runtimeRequirements.add(RetryRuntimeGlobal)
|
|
43
|
-
content = `${RetryRuntimeGlobal}(function() { return ${content} }, ${retryRequireAsync.times}, ${retryRequireAsync.interval})`
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
source.replace(dep.range[0], dep.range[1] - 1, content)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
module.exports = ImportDependencyTemplate
|