@mpxjs/webpack-plugin 2.6.102 → 2.7.0-beta.1
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.
- package/lib/dependencies/AppEntryDependency.js +56 -0
- package/lib/dependencies/CommonJsVariableDependency.js +74 -0
- package/lib/dependencies/DynamicEntryDependency.js +127 -0
- package/lib/dependencies/FlagPluginDependency.js +23 -0
- package/lib/dependencies/InjectDependency.js +43 -0
- package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
- package/lib/dependencies/RecordStaticResourceDependency.js +47 -0
- package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
- package/lib/dependencies/ResolveDependency.js +83 -0
- package/lib/extractor.js +72 -179
- package/lib/file-loader.js +7 -19
- package/lib/helpers.js +41 -330
- package/lib/index.js +472 -356
- package/lib/json-compiler/helper.js +152 -0
- package/lib/json-compiler/index.js +148 -407
- package/lib/json-compiler/plugin.js +134 -0
- package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
- package/lib/loader.js +76 -171
- package/lib/native-loader.js +40 -70
- package/lib/record-loader.js +11 -0
- package/lib/{path-loader.js → resolve-loader.js} +0 -0
- package/lib/runtime/i18n.wxs +3 -3
- package/lib/selector.js +10 -7
- package/lib/style-compiler/index.js +20 -12
- package/lib/style-compiler/plugins/trans-special.js +21 -0
- package/lib/template-compiler/compiler.js +44 -176
- package/lib/template-compiler/index.js +47 -128
- package/lib/url-loader.js +11 -29
- package/lib/utils/add-query.js +1 -1
- package/lib/utils/const.js +5 -0
- package/lib/utils/emit-file.js +10 -0
- package/lib/utils/get-entry-name.js +13 -0
- package/lib/utils/is-url-request.js +10 -1
- package/lib/utils/normalize.js +0 -13
- package/lib/utils/parse-request.js +3 -3
- package/lib/utils/set.js +47 -0
- package/lib/utils/stringify-loaders-resource.js +25 -0
- package/lib/utils/stringify-query.js +4 -0
- package/lib/web/processScript.js +3 -3
- package/lib/web/processTemplate.js +2 -0
- package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
- package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
- package/lib/wxs/WxsParserPlugin.js +2 -2
- package/lib/wxs/WxsPlugin.js +4 -8
- package/lib/wxs/WxsTemplatePlugin.js +46 -92
- package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +0 -0
- package/lib/wxs/{wxs-loader.js → loader.js} +33 -38
- package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +0 -0
- package/lib/wxss/loader.js +31 -43
- package/lib/wxss/localsLoader.js +1 -5
- package/package.json +4 -8
- package/lib/content-loader.js +0 -13
- package/lib/dependency/ChildCompileDependency.js +0 -24
- package/lib/dependency/InjectDependency.js +0 -26
- package/lib/dependency/RemovedModuleDependency.js +0 -23
- package/lib/dependency/ResolveDependency.js +0 -49
- package/lib/plugin-loader.js +0 -287
- package/lib/utils/try-require.js +0 -16
- package/lib/wxss/getImportPrefix.js +0 -30
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
|
|
4
|
+
class AppEntryDependency extends NullDependency {
|
|
5
|
+
constructor (resourcePath, name) {
|
|
6
|
+
super()
|
|
7
|
+
this.resourcePath = resourcePath
|
|
8
|
+
this.name = name
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get type () {
|
|
12
|
+
return 'mpx app entry'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
mpxAction (module, compilation, callback) {
|
|
16
|
+
const mpx = compilation.__mpx__
|
|
17
|
+
const moduleGraph = compilation.moduleGraph
|
|
18
|
+
|
|
19
|
+
if (mpx.appInfo.name) {
|
|
20
|
+
const issuer = moduleGraph.getIssuer(module)
|
|
21
|
+
const err = new Error(issuer
|
|
22
|
+
? `[json compiler]:Mpx单次构建中只能存在一个App,当前组件/页面[${module.resource}]通过[${issuer.resource}]非法引入,引用的资源将被忽略,请确保组件/页面资源通过usingComponents/pages配置引入!`
|
|
23
|
+
: `[json compiler]:Mpx单次构建中只能存在一个App,请检查当前entry中的资源[${module.resource}]是否为组件/页面,通过添加?component/page查询字符串显式声明该资源是组件/页面!`)
|
|
24
|
+
return callback(err)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mpx.appInfo = {
|
|
28
|
+
resourcePath: this.resourcePath,
|
|
29
|
+
name: this.name
|
|
30
|
+
}
|
|
31
|
+
return callback()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
serialize (context) {
|
|
35
|
+
const { write } = context
|
|
36
|
+
write(this.resourcePath)
|
|
37
|
+
write(this.name)
|
|
38
|
+
super.serialize(context)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
deserialize (context) {
|
|
42
|
+
const { read } = context
|
|
43
|
+
this.resourcePath = read()
|
|
44
|
+
this.name = read()
|
|
45
|
+
super.deserialize(context)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
AppEntryDependency.Template = class AppEntryDependencyTemplate {
|
|
50
|
+
apply () {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
makeSerializable(AppEntryDependency, '@mpxjs/webpack-plugin/lib/dependencies/AppEntryDependency')
|
|
55
|
+
|
|
56
|
+
module.exports = AppEntryDependency
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
|
|
4
|
+
class CommonJsVariableDependency extends ModuleDependency {
|
|
5
|
+
constructor (request, name) {
|
|
6
|
+
super(request)
|
|
7
|
+
this.name = name
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
serialize (context) {
|
|
11
|
+
const { write } = context
|
|
12
|
+
write(this.name)
|
|
13
|
+
super.serialize(context)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
deserialize (context) {
|
|
17
|
+
const { read } = context
|
|
18
|
+
this.name = read()
|
|
19
|
+
super.deserialize(context)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
updateHash (hash, context) {
|
|
23
|
+
hash.update(this.request)
|
|
24
|
+
hash.update(this.name)
|
|
25
|
+
super.updateHash(hash, context)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get type () {
|
|
29
|
+
return 'mpx cjs variable'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get category () {
|
|
33
|
+
return 'commonjs'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
CommonJsVariableDependency.Template = class CommonJsVariableDependencyTemplate extends (
|
|
38
|
+
ModuleDependency.Template
|
|
39
|
+
) {
|
|
40
|
+
apply (
|
|
41
|
+
dep,
|
|
42
|
+
source,
|
|
43
|
+
{
|
|
44
|
+
module,
|
|
45
|
+
runtimeTemplate,
|
|
46
|
+
moduleGraph,
|
|
47
|
+
chunkGraph,
|
|
48
|
+
runtimeRequirements,
|
|
49
|
+
runtime,
|
|
50
|
+
initFragments
|
|
51
|
+
}
|
|
52
|
+
) {
|
|
53
|
+
if (!dep.name) return
|
|
54
|
+
const importedModule = moduleGraph.getModule(dep)
|
|
55
|
+
let requireExpr = runtimeTemplate.moduleExports({
|
|
56
|
+
module: importedModule,
|
|
57
|
+
chunkGraph,
|
|
58
|
+
request: dep.request,
|
|
59
|
+
weak: dep.weak,
|
|
60
|
+
runtimeRequirements
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
requireExpr = `/* mpx cjs variable */ var ${dep.name} = ` + requireExpr
|
|
64
|
+
|
|
65
|
+
source.insert(0, requireExpr)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
makeSerializable(
|
|
70
|
+
CommonJsVariableDependency,
|
|
71
|
+
'@mpxjs/webpack-plugin/lib/dependencies/CommonJsVariableDependency'
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
module.exports = CommonJsVariableDependency
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
const path = require('path')
|
|
4
|
+
const addQuery = require('../utils/add-query')
|
|
5
|
+
const toPosix = require('../utils/to-posix')
|
|
6
|
+
|
|
7
|
+
class DynamicEntryDependency extends NullDependency {
|
|
8
|
+
constructor (resource, entryType, outputPath = '', packageRoot = '', relativePath = '', range) {
|
|
9
|
+
super()
|
|
10
|
+
this.resource = resource
|
|
11
|
+
this.entryType = entryType
|
|
12
|
+
this.outputPath = outputPath
|
|
13
|
+
this.packageRoot = packageRoot
|
|
14
|
+
this.relativePath = relativePath
|
|
15
|
+
this.range = range
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get type () {
|
|
19
|
+
return 'mpx dynamic entry'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get key () {
|
|
23
|
+
return `${this.resource}_${this.entryType}_${this.outputPath}_${this.packageRoot}_${this.relativePath}_${this.range[0]}_${this.range[1]}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
addEntry (compilation, callback) {
|
|
27
|
+
const mpx = compilation.__mpx__
|
|
28
|
+
const publicPath = compilation.outputOptions.publicPath || ''
|
|
29
|
+
let { resource, entryType, outputPath, relativePath } = this
|
|
30
|
+
|
|
31
|
+
const { packageRoot, outputPath: filename, alreadyOutputed } = mpx.getPackageInfo({
|
|
32
|
+
resource,
|
|
33
|
+
outputPath,
|
|
34
|
+
resourceType: entryType,
|
|
35
|
+
warn (e) {
|
|
36
|
+
compilation.warnings.push(e)
|
|
37
|
+
},
|
|
38
|
+
error (e) {
|
|
39
|
+
compilation.errors.push(e)
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
let resultPath = publicPath + filename
|
|
44
|
+
if (relativePath) {
|
|
45
|
+
resultPath = toPosix(path.relative(relativePath, resultPath))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (alreadyOutputed) return callback(null, { resultPath })
|
|
49
|
+
|
|
50
|
+
if (packageRoot) {
|
|
51
|
+
resource = addQuery(resource, { packageRoot })
|
|
52
|
+
}
|
|
53
|
+
mpx.addEntry(resource, filename, (err, entryModule) => {
|
|
54
|
+
if (err) return callback(err)
|
|
55
|
+
if (entryType === 'export') {
|
|
56
|
+
mpx.exportModules.add(entryModule)
|
|
57
|
+
}
|
|
58
|
+
// todo entry的父子关系可以在这里建立
|
|
59
|
+
return callback(null, {
|
|
60
|
+
resultPath,
|
|
61
|
+
entryModule
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
mpxAction (module, compilation, callback) {
|
|
67
|
+
const mpx = compilation.__mpx__
|
|
68
|
+
const { packageRoot } = this
|
|
69
|
+
// 分包构建在需要在主包构建完成后在finishMake中处理,返回的资源路径先用key来占位,在合成extractedAssets时再进行最终替换
|
|
70
|
+
if (packageRoot && mpx.currentPackageRoot !== packageRoot) {
|
|
71
|
+
mpx.subpackagesEntriesMap[packageRoot] = mpx.subpackagesEntriesMap[packageRoot] || []
|
|
72
|
+
mpx.subpackagesEntriesMap[packageRoot].push(this)
|
|
73
|
+
callback()
|
|
74
|
+
} else {
|
|
75
|
+
this.addEntry(compilation, (err, { resultPath }) => {
|
|
76
|
+
if (err) return callback(err)
|
|
77
|
+
this.resultPath = resultPath
|
|
78
|
+
callback()
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// hash会影响最终的codeGenerateResult是否走缓存,由于该dep中resultPath是动态变更的,需要将其更新到hash中,避免错误使用缓存
|
|
84
|
+
updateHash (hash, context) {
|
|
85
|
+
const { resultPath } = this
|
|
86
|
+
if (resultPath) hash.update(resultPath)
|
|
87
|
+
super.updateHash(hash, context)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
serialize (context) {
|
|
91
|
+
const { write } = context
|
|
92
|
+
write(this.resource)
|
|
93
|
+
write(this.entryType)
|
|
94
|
+
write(this.outputPath)
|
|
95
|
+
write(this.packageRoot)
|
|
96
|
+
write(this.relativePath)
|
|
97
|
+
write(this.range)
|
|
98
|
+
super.serialize(context)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
deserialize (context) {
|
|
102
|
+
const { read } = context
|
|
103
|
+
this.resource = read()
|
|
104
|
+
this.entryType = read()
|
|
105
|
+
this.outputPath = read()
|
|
106
|
+
this.packageRoot = read()
|
|
107
|
+
this.relativePath = read()
|
|
108
|
+
this.range = read()
|
|
109
|
+
super.deserialize(context)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
DynamicEntryDependency.Template = class DynamicEntryDependencyTemplate {
|
|
114
|
+
apply (dep, source) {
|
|
115
|
+
const { resultPath, range, key } = dep
|
|
116
|
+
if (resultPath) {
|
|
117
|
+
source.replace(range[0], range[1] - 1, JSON.stringify(resultPath))
|
|
118
|
+
} else {
|
|
119
|
+
const replaceRange = `mpx_replace_path_${key}`
|
|
120
|
+
source.replace(range[0], range[1] - 1, JSON.stringify(replaceRange))
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
makeSerializable(DynamicEntryDependency, '@mpxjs/webpack-plugin/lib/dependencies/DynamicEntryDependency')
|
|
126
|
+
|
|
127
|
+
module.exports = DynamicEntryDependency
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
|
|
4
|
+
class FlagPluginDependency extends NullDependency {
|
|
5
|
+
get type () {
|
|
6
|
+
return 'mpx flag plugin'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
mpxAction (module, compilation, callback) {
|
|
10
|
+
const mpx = compilation.__mpx__
|
|
11
|
+
mpx.isPluginMode = true
|
|
12
|
+
return callback()
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
FlagPluginDependency.Template = class FlagPluginDependencyTemplate {
|
|
17
|
+
apply () {
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
makeSerializable(FlagPluginDependency, '@mpxjs/webpack-plugin/lib/dependencies/FlagPluginDependency')
|
|
22
|
+
|
|
23
|
+
module.exports = FlagPluginDependency
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
|
|
4
|
+
class InjectDependency extends NullDependency {
|
|
5
|
+
constructor (options = {}) {
|
|
6
|
+
super()
|
|
7
|
+
this.content = options.content
|
|
8
|
+
this.index = options.index || 0
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get type () {
|
|
12
|
+
return 'mpx inject'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
updateHash (hash, context) {
|
|
16
|
+
hash.update(this.content)
|
|
17
|
+
super.updateHash(hash, context)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
serialize (context) {
|
|
21
|
+
const { write } = context
|
|
22
|
+
write(this.content)
|
|
23
|
+
write(this.index)
|
|
24
|
+
super.serialize(context)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
deserialize (context) {
|
|
28
|
+
const { read } = context
|
|
29
|
+
this.content = read()
|
|
30
|
+
this.index = read()
|
|
31
|
+
super.deserialize(context)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
InjectDependency.Template = class InjectDependencyTemplate {
|
|
36
|
+
apply (dep, source) {
|
|
37
|
+
source.insert(dep.index, '/* mpx inject */ ' + dep.content)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
makeSerializable(InjectDependency, '@mpxjs/webpack-plugin/lib/dependencies/InjectDependency')
|
|
42
|
+
|
|
43
|
+
module.exports = InjectDependency
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
const addQuery = require('../utils/add-query')
|
|
4
|
+
|
|
5
|
+
class RecordGlobalComponentsDependency extends NullDependency {
|
|
6
|
+
constructor (usingComponents, context) {
|
|
7
|
+
super()
|
|
8
|
+
this.usingComponents = usingComponents
|
|
9
|
+
this.context = context
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get type () {
|
|
13
|
+
return 'mpx record global components'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
mpxAction (module, compilation, callback) {
|
|
17
|
+
const mpx = compilation.__mpx__
|
|
18
|
+
const { usingComponents, context } = this
|
|
19
|
+
Object.keys(usingComponents).forEach((key) => {
|
|
20
|
+
const request = usingComponents[key]
|
|
21
|
+
mpx.usingComponents[key] = addQuery(request, {
|
|
22
|
+
context
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
return callback()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
serialize (context) {
|
|
29
|
+
const { write } = context
|
|
30
|
+
write(this.usingComponents)
|
|
31
|
+
write(this.context)
|
|
32
|
+
super.serialize(context)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
deserialize (context) {
|
|
36
|
+
const { read } = context
|
|
37
|
+
this.usingComponents = read()
|
|
38
|
+
this.context = read()
|
|
39
|
+
super.deserialize(context)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
RecordGlobalComponentsDependency.Template = class RecordGlobalComponentsDependencyTemplate {
|
|
44
|
+
apply () {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
makeSerializable(RecordGlobalComponentsDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordGlobalComponentsDependency')
|
|
49
|
+
|
|
50
|
+
module.exports = RecordGlobalComponentsDependency
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
3
|
+
|
|
4
|
+
class RecordStaticResourceDependency extends NullDependency {
|
|
5
|
+
constructor (resourcePath, outputPath, packageRoot = '') {
|
|
6
|
+
super()
|
|
7
|
+
this.resourcePath = resourcePath
|
|
8
|
+
this.outputPath = outputPath
|
|
9
|
+
this.packageRoot = packageRoot
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get type () {
|
|
13
|
+
return 'mpx record static resource'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
mpxAction (module, compilation, callback) {
|
|
17
|
+
const mpx = compilation.__mpx__
|
|
18
|
+
const packageName = this.packageRoot || 'main'
|
|
19
|
+
mpx.staticResourcesMap[packageName][this.resourcePath] = this.outputPath
|
|
20
|
+
return callback()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
serialize (context) {
|
|
24
|
+
const { write } = context
|
|
25
|
+
write(this.resourcePath)
|
|
26
|
+
write(this.outputPath)
|
|
27
|
+
write(this.packageRoot)
|
|
28
|
+
super.serialize(context)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
deserialize (context) {
|
|
32
|
+
const { read } = context
|
|
33
|
+
this.resourcePath = read()
|
|
34
|
+
this.outputPath = read()
|
|
35
|
+
this.packageRoot = read()
|
|
36
|
+
super.deserialize(context)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
RecordStaticResourceDependency.Template = class RecordStaticResourceDependencyTemplate {
|
|
41
|
+
apply () {
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
makeSerializable(RecordStaticResourceDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordStaticResourceDependency')
|
|
46
|
+
|
|
47
|
+
module.exports = RecordStaticResourceDependency
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
2
3
|
|
|
3
4
|
class ReplaceDependency extends NullDependency {
|
|
4
5
|
constructor (replacement, range) {
|
|
@@ -11,9 +12,23 @@ class ReplaceDependency extends NullDependency {
|
|
|
11
12
|
return 'mpx replace'
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
updateHash (hash) {
|
|
15
|
-
super.updateHash(hash)
|
|
15
|
+
updateHash (hash, context) {
|
|
16
16
|
hash.update(this.replacement)
|
|
17
|
+
super.updateHash(hash, context)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
serialize (context) {
|
|
21
|
+
const { write } = context
|
|
22
|
+
write(this.replacement)
|
|
23
|
+
write(this.range)
|
|
24
|
+
super.serialize(context)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
deserialize (context) {
|
|
28
|
+
const { read } = context
|
|
29
|
+
this.replacement = read()
|
|
30
|
+
this.range = read()
|
|
31
|
+
super.deserialize(context)
|
|
17
32
|
}
|
|
18
33
|
}
|
|
19
34
|
|
|
@@ -23,4 +38,6 @@ ReplaceDependency.Template = class ReplaceDependencyTemplate {
|
|
|
23
38
|
}
|
|
24
39
|
}
|
|
25
40
|
|
|
41
|
+
makeSerializable(ReplaceDependency, '@mpxjs/webpack-plugin/lib/dependencies/ReplaceDependency')
|
|
42
|
+
|
|
26
43
|
module.exports = ReplaceDependency
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const NullDependency = require('webpack/lib/dependencies/NullDependency')
|
|
2
|
+
const parseRequest = require('../utils/parse-request')
|
|
3
|
+
const makeSerializable = require('webpack/lib/util/makeSerializable')
|
|
4
|
+
|
|
5
|
+
class ResolveDependency extends NullDependency {
|
|
6
|
+
constructor (resource, packageName, issuerResource, range) {
|
|
7
|
+
super()
|
|
8
|
+
this.resource = resource
|
|
9
|
+
this.packageName = packageName
|
|
10
|
+
this.issuerResource = issuerResource
|
|
11
|
+
this.range = range
|
|
12
|
+
this.compilation = null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get type () {
|
|
16
|
+
return 'mpx resolve'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
mpxAction (module, compilation, callback) {
|
|
20
|
+
this.compilation = compilation
|
|
21
|
+
return callback()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getResolved () {
|
|
25
|
+
const { resource, packageName, compilation } = this
|
|
26
|
+
if (!compilation) return ''
|
|
27
|
+
const mpx = compilation.__mpx__
|
|
28
|
+
if (!mpx) return ''
|
|
29
|
+
const { pagesMap, componentsMap, staticResourcesMap } = mpx
|
|
30
|
+
const { resourcePath } = parseRequest(resource)
|
|
31
|
+
const currentComponentsMap = componentsMap[packageName]
|
|
32
|
+
const mainComponentsMap = componentsMap.main
|
|
33
|
+
const currentStaticResourcesMap = staticResourcesMap[packageName]
|
|
34
|
+
const mainStaticResourcesMap = staticResourcesMap.main
|
|
35
|
+
return pagesMap[resourcePath] || currentComponentsMap[resourcePath] || mainComponentsMap[resourcePath] || currentStaticResourcesMap[resourcePath] || mainStaticResourcesMap[resourcePath] || ''
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// resolved可能会动态变更,需用此更新hash
|
|
39
|
+
updateHash (hash, context) {
|
|
40
|
+
const resolved = this.getResolved()
|
|
41
|
+
if (resolved) hash.update(resolved)
|
|
42
|
+
super.updateHash(hash, context)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
serialize (context) {
|
|
46
|
+
const { write } = context
|
|
47
|
+
write(this.resource)
|
|
48
|
+
write(this.packageName)
|
|
49
|
+
write(this.issuerResource)
|
|
50
|
+
write(this.range)
|
|
51
|
+
super.serialize(context)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
deserialize (context) {
|
|
55
|
+
const { read } = context
|
|
56
|
+
this.resource = read()
|
|
57
|
+
this.packageName = read()
|
|
58
|
+
this.issuerResource = read()
|
|
59
|
+
this.range = read()
|
|
60
|
+
super.deserialize(context)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
ResolveDependency.Template = class ResolveDependencyTemplate {
|
|
65
|
+
apply (dep, source) {
|
|
66
|
+
const content = this.getContent(dep)
|
|
67
|
+
source.replace(dep.range[0], dep.range[1] - 1, content)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getContent (dep) {
|
|
71
|
+
const { resource, issuerResource, compilation } = dep
|
|
72
|
+
const publicPath = compilation.outputOptions.publicPath || ''
|
|
73
|
+
const resolved = dep.getResolved()
|
|
74
|
+
if (!resolved) {
|
|
75
|
+
compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
|
|
76
|
+
}
|
|
77
|
+
return JSON.stringify(publicPath + resolved)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
makeSerializable(ResolveDependency, '@mpxjs/webpack-plugin/lib/dependencies/ResolveDependency')
|
|
82
|
+
|
|
83
|
+
module.exports = ResolveDependency
|