@mpxjs/webpack-plugin 2.6.115 → 2.7.0-alpha

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.
Files changed (129) hide show
  1. package/LICENSE +433 -0
  2. package/README.md +1 -1
  3. package/lib/config.js +14 -0
  4. package/lib/dependencies/AddEntryDependency.js +24 -0
  5. package/lib/dependencies/AppEntryDependency.js +58 -0
  6. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  7. package/lib/dependencies/CommonJsVariableDependency.js +81 -0
  8. package/lib/dependencies/DynamicEntryDependency.js +171 -0
  9. package/lib/dependencies/FlagPluginDependency.js +24 -0
  10. package/lib/dependencies/InjectDependency.js +43 -0
  11. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  12. package/lib/dependencies/RecordIndependentDependency.js +44 -0
  13. package/lib/dependencies/RecordResourceMapDependency.js +62 -0
  14. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  15. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  16. package/lib/dependencies/ResolveDependency.js +88 -0
  17. package/lib/extractor.js +82 -178
  18. package/lib/file-loader.js +7 -19
  19. package/lib/helpers.js +39 -334
  20. package/lib/independent-loader.js +52 -0
  21. package/lib/index.js +889 -525
  22. package/lib/json-compiler/helper.js +156 -0
  23. package/lib/json-compiler/index.js +245 -451
  24. package/lib/json-compiler/plugin.js +150 -0
  25. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  26. package/lib/loader.js +178 -241
  27. package/lib/native-loader.js +71 -133
  28. package/lib/parser.js +1 -2
  29. package/lib/partial-compile/index.js +35 -0
  30. package/lib/platform/json/wx/index.js +1 -1
  31. package/lib/platform/template/normalize-component-rules.js +2 -3
  32. package/lib/platform/template/wx/component-config/button.js +14 -2
  33. package/lib/platform/template/wx/component-config/image.js +4 -0
  34. package/lib/platform/template/wx/component-config/input.js +4 -0
  35. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  36. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  37. package/lib/platform/template/wx/component-config/switch.js +4 -0
  38. package/lib/platform/template/wx/component-config/text.js +4 -0
  39. package/lib/platform/template/wx/component-config/textarea.js +5 -0
  40. package/lib/platform/template/wx/component-config/view.js +4 -0
  41. package/lib/platform/template/wx/index.js +149 -3
  42. package/lib/record-loader.js +11 -0
  43. package/lib/resolve-loader.js +6 -0
  44. package/lib/resolver/AddEnvPlugin.js +4 -3
  45. package/lib/resolver/AddModePlugin.js +4 -3
  46. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  47. package/lib/resolver/PackageEntryPlugin.js +23 -36
  48. package/lib/runtime/base.styl +5 -0
  49. package/lib/runtime/components/tenon/getInnerListeners.js +317 -0
  50. package/lib/runtime/components/tenon/tenon-button.vue +305 -0
  51. package/lib/runtime/components/tenon/tenon-image.vue +61 -0
  52. package/lib/runtime/components/tenon/tenon-input.vue +99 -0
  53. package/lib/runtime/components/tenon/tenon-rich-text.vue +21 -0
  54. package/lib/runtime/components/tenon/tenon-scroll-view.vue +124 -0
  55. package/lib/runtime/components/tenon/tenon-switch.vue +91 -0
  56. package/lib/runtime/components/tenon/tenon-text-area.vue +64 -0
  57. package/lib/runtime/components/tenon/tenon-text.vue +64 -0
  58. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  59. package/lib/runtime/components/tenon/util.js +44 -0
  60. package/lib/runtime/components/web/getInnerListeners.js +1 -3
  61. package/lib/runtime/components/web/mpx-image.vue +20 -5
  62. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  63. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  64. package/lib/runtime/i18n.wxs +31 -11
  65. package/lib/runtime/optionProcessor.js +48 -3
  66. package/lib/runtime/optionProcessor.tenon.js +386 -0
  67. package/lib/selector.js +29 -10
  68. package/lib/style-compiler/index.js +16 -24
  69. package/lib/style-compiler/load-postcss-config.js +3 -1
  70. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  71. package/lib/style-compiler/plugins/hm.js +20 -0
  72. package/lib/style-compiler/plugins/rpx.js +43 -37
  73. package/lib/style-compiler/plugins/scope-id.js +79 -72
  74. package/lib/style-compiler/plugins/trans-special.js +25 -18
  75. package/lib/style-compiler/plugins/trim.js +13 -7
  76. package/lib/style-compiler/plugins/vw.js +22 -16
  77. package/lib/template-compiler/compiler.js +106 -199
  78. package/lib/template-compiler/index.js +52 -139
  79. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  80. package/lib/tenon/index.js +105 -0
  81. package/lib/tenon/processJSON.js +356 -0
  82. package/lib/tenon/processScript.js +261 -0
  83. package/lib/tenon/processStyles.js +21 -0
  84. package/lib/tenon/processTemplate.js +133 -0
  85. package/lib/url-loader.js +11 -29
  86. package/lib/utils/add-query.js +1 -1
  87. package/lib/utils/const.js +10 -0
  88. package/lib/utils/emit-file.js +10 -0
  89. package/lib/utils/eval-json-js.js +31 -0
  90. package/lib/utils/get-entry-name.js +13 -0
  91. package/lib/utils/get-json-content.js +42 -0
  92. package/lib/utils/get-relative-path.js +25 -0
  93. package/lib/utils/is-url-request.js +10 -1
  94. package/lib/utils/match-condition.js +4 -1
  95. package/lib/utils/normalize.js +4 -15
  96. package/lib/utils/parse-request.js +3 -3
  97. package/lib/utils/resolve.js +13 -0
  98. package/lib/utils/set.js +47 -0
  99. package/lib/utils/stringify-loaders-resource.js +25 -0
  100. package/lib/utils/stringify-query.js +4 -0
  101. package/lib/web/processJSON.js +113 -144
  102. package/lib/web/processScript.js +47 -34
  103. package/lib/web/processTemplate.js +57 -40
  104. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  105. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  106. package/lib/wxs/WxsParserPlugin.js +2 -2
  107. package/lib/wxs/WxsPlugin.js +4 -8
  108. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  109. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  110. package/lib/wxs/loader.js +142 -0
  111. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  112. package/lib/wxss/loader.js +31 -43
  113. package/lib/wxss/localsLoader.js +1 -5
  114. package/lib/wxss/processCss.js +107 -103
  115. package/package.json +21 -18
  116. package/lib/built-in-loader.js +0 -49
  117. package/lib/content-loader.js +0 -13
  118. package/lib/dependency/ChildCompileDependency.js +0 -24
  119. package/lib/dependency/InjectDependency.js +0 -26
  120. package/lib/dependency/RemovedModuleDependency.js +0 -23
  121. package/lib/dependency/ResolveDependency.js +0 -49
  122. package/lib/path-loader.js +0 -3
  123. package/lib/plugin-loader.js +0 -287
  124. package/lib/staticConfig.js +0 -4
  125. package/lib/utils/get-main-compilation.js +0 -6
  126. package/lib/utils/read-json-for-src.js +0 -34
  127. package/lib/utils/try-require.js +0 -16
  128. package/lib/wxs/wxs-loader.js +0 -117
  129. package/lib/wxss/getImportPrefix.js +0 -30
@@ -0,0 +1,171 @@
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
+ const async = require('async')
7
+ const parseRequest = require('../utils/parse-request')
8
+ const { MPX_CURRENT_CHUNK } = require('../utils/const')
9
+
10
+ class DynamicEntryDependency extends NullDependency {
11
+ constructor (request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', range) {
12
+ super()
13
+ this.request = request
14
+ this.entryType = entryType
15
+ this.outputPath = outputPath
16
+ this.packageRoot = packageRoot
17
+ this.relativePath = relativePath
18
+ this.context = context
19
+ this.range = range
20
+ }
21
+
22
+ get type () {
23
+ return 'mpx dynamic entry'
24
+ }
25
+
26
+ get key () {
27
+ const { request, entryType, outputPath, packageRoot, relativePath, context, range } = this
28
+ return toPosix([request, entryType, outputPath, packageRoot, relativePath, context, ...range].join('|'))
29
+ }
30
+
31
+ addEntry (compilation, callback) {
32
+ const mpx = compilation.__mpx__
33
+ let { request, entryType, outputPath, relativePath, context, originEntryNode, publicPath, resolver } = this
34
+
35
+ async.waterfall([
36
+ (callback) => {
37
+ if (context && resolver) {
38
+ resolver.resolve({}, context, request, {}, (err, resource) => {
39
+ callback(err, resource)
40
+ })
41
+ } else {
42
+ callback(null, request)
43
+ }
44
+ },
45
+ (resource, callback) => {
46
+ if (!outputPath) {
47
+ const { resourcePath } = parseRequest(resource)
48
+ outputPath = mpx.getOutputPath(resourcePath, entryType)
49
+ }
50
+
51
+ const { packageRoot, outputPath: filename, alreadyOutputted } = mpx.getPackageInfo({
52
+ resource,
53
+ outputPath,
54
+ resourceType: entryType,
55
+ warn (e) {
56
+ compilation.warnings.push(e)
57
+ },
58
+ error (e) {
59
+ compilation.errors.push(e)
60
+ }
61
+ })
62
+
63
+ let resultPath = publicPath + filename
64
+ if (relativePath && relativePath !== MPX_CURRENT_CHUNK) {
65
+ resultPath = toPosix(path.relative(relativePath, resultPath))
66
+ }
67
+
68
+ // export类型的resultPath需要添加.js后缀
69
+ if (entryType === 'export') resultPath += '.js'
70
+
71
+ if (alreadyOutputted) return callback(null, { resultPath })
72
+
73
+ // 对于常规js模块不应添加packageRoot避免冗余
74
+ if (packageRoot && entryType !== 'export') {
75
+ resource = addQuery(resource, { packageRoot }, true)
76
+ }
77
+
78
+ mpx.addEntry(resource, filename, (err, entryModule) => {
79
+ if (err) return callback(err)
80
+ if (entryType === 'export') {
81
+ mpx.exportModules.add(entryModule)
82
+ }
83
+ originEntryNode.addChild(mpx.getEntryNode(entryModule, entryType))
84
+ return callback(null, {
85
+ resultPath,
86
+ entryModule
87
+ })
88
+ })
89
+ }
90
+ ], callback)
91
+ }
92
+
93
+ mpxAction (module, compilation, callback) {
94
+ const mpx = compilation.__mpx__
95
+ const { packageRoot, context } = this
96
+ this.originEntryNode = mpx.getEntryNode(module)
97
+ this.publicPath = compilation.outputOptions.publicPath || ''
98
+ if (context) this.resolver = compilation.resolverFactory.get('normal', module.resolveOptions)
99
+ // 分包构建在需要在主包构建完成后在finishMake中处理,返回的资源路径先用key来占位,在合成extractedAssets时再进行最终替换
100
+ if (packageRoot && mpx.currentPackageRoot !== packageRoot) {
101
+ mpx.subpackagesEntriesMap[packageRoot] = mpx.subpackagesEntriesMap[packageRoot] || []
102
+ mpx.subpackagesEntriesMap[packageRoot].push(this)
103
+ callback()
104
+ } else {
105
+ this.addEntry(compilation, (err, { resultPath }) => {
106
+ if (err) return callback(err)
107
+ this.resultPath = resultPath
108
+ callback()
109
+ })
110
+ }
111
+ }
112
+
113
+ // hash会影响最终的codeGenerateResult是否走缓存,由于该dep中resultPath是动态变更的,需要将其更新到hash中,避免错误使用缓存
114
+ updateHash (hash, context) {
115
+ const { resultPath, relativePath } = this
116
+ if (resultPath) hash.update(resultPath)
117
+ // relativePath为MPX_CURRENT_CHUNK时,插入随机hash使当前module的codeGeneration cache失效,从而执行dep.apply动态获取当前module所属的chunk路径
118
+ if (relativePath === MPX_CURRENT_CHUNK) hash.update('' + (+new Date()) + Math.random())
119
+ super.updateHash(hash, context)
120
+ }
121
+
122
+ serialize (context) {
123
+ const { write } = context
124
+ write(this.request)
125
+ write(this.entryType)
126
+ write(this.outputPath)
127
+ write(this.packageRoot)
128
+ write(this.relativePath)
129
+ write(this.context)
130
+ write(this.range)
131
+ super.serialize(context)
132
+ }
133
+
134
+ deserialize (context) {
135
+ const { read } = context
136
+ this.request = read()
137
+ this.entryType = read()
138
+ this.outputPath = read()
139
+ this.packageRoot = read()
140
+ this.relativePath = read()
141
+ this.context = read()
142
+ this.range = read()
143
+ super.deserialize(context)
144
+ }
145
+ }
146
+
147
+ DynamicEntryDependency.Template = class DynamicEntryDependencyTemplate {
148
+ apply (dep, source, {
149
+ module,
150
+ chunkGraph
151
+ }) {
152
+ let { resultPath, range, key, outputPath, relativePath, publicPath } = dep
153
+ if (outputPath === 'custom-tab-bar/index') {
154
+ // replace with true for custom-tab-bar
155
+ source.replace(range[0], range[1] - 1, 'true')
156
+ } else if (resultPath) {
157
+ if (relativePath === MPX_CURRENT_CHUNK) {
158
+ relativePath = publicPath + path.dirname(chunkGraph.getModuleChunks(module)[0].name)
159
+ resultPath = toPosix(path.relative(relativePath, resultPath))
160
+ }
161
+ source.replace(range[0], range[1] - 1, JSON.stringify(resultPath))
162
+ } else {
163
+ const replaceRange = `mpx_replace_path_${key}`
164
+ source.replace(range[0], range[1] - 1, JSON.stringify(replaceRange))
165
+ }
166
+ }
167
+ }
168
+
169
+ makeSerializable(DynamicEntryDependency, '@mpxjs/webpack-plugin/lib/dependencies/DynamicEntryDependency')
170
+
171
+ module.exports = DynamicEntryDependency
@@ -0,0 +1,24 @@
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
+ mpx.getEntryNode(module, 'plugin')
13
+ return callback()
14
+ }
15
+ }
16
+
17
+ FlagPluginDependency.Template = class FlagPluginDependencyTemplate {
18
+ apply () {
19
+ }
20
+ }
21
+
22
+ makeSerializable(FlagPluginDependency, '@mpxjs/webpack-plugin/lib/dependencies/FlagPluginDependency')
23
+
24
+ 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,44 @@
1
+ const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+
4
+ class RecordIndependentDependency extends NullDependency {
5
+ constructor (root, request) {
6
+ super()
7
+ this.root = root
8
+ this.request = request
9
+ }
10
+
11
+ get type () {
12
+ return 'mpx record independent'
13
+ }
14
+
15
+ mpxAction (module, compilation, callback) {
16
+ const mpx = compilation.__mpx__
17
+ const { root, request } = this
18
+ mpx.independentSubpackagesMap[root] = request
19
+ return callback()
20
+ }
21
+
22
+ serialize (context) {
23
+ const { write } = context
24
+ write(this.root)
25
+ write(this.request)
26
+ super.serialize(context)
27
+ }
28
+
29
+ deserialize (context) {
30
+ const { read } = context
31
+ this.root = read()
32
+ this.request = read()
33
+ super.deserialize(context)
34
+ }
35
+ }
36
+
37
+ RecordIndependentDependency.Template = class RecordIndependentDependencyTemplate {
38
+ apply () {
39
+ }
40
+ }
41
+
42
+ makeSerializable(RecordIndependentDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordIndependentDependency')
43
+
44
+ module.exports = RecordIndependentDependency
@@ -0,0 +1,62 @@
1
+ const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+
4
+ class RecordResourceMapDependency extends NullDependency {
5
+ constructor (resourcePath, resourceType, outputPath, packageRoot = '') {
6
+ super()
7
+ this.resourcePath = resourcePath
8
+ this.resourceType = resourceType
9
+ this.outputPath = outputPath
10
+ this.packageRoot = packageRoot
11
+ }
12
+
13
+ get type () {
14
+ return 'mpx record resource map'
15
+ }
16
+
17
+ mpxAction (module, compilation, callback) {
18
+ const mpx = compilation.__mpx__
19
+ const { resourcePath, resourceType, outputPath, packageRoot } = this
20
+ mpx.recordResourceMap({
21
+ resourcePath,
22
+ resourceType,
23
+ outputPath,
24
+ packageRoot,
25
+ recordOnly: true,
26
+ warn (e) {
27
+ compilation.warnings.push(e)
28
+ },
29
+ error (e) {
30
+ compilation.errors.push(e)
31
+ }
32
+ })
33
+ return callback()
34
+ }
35
+
36
+ serialize (context) {
37
+ const { write } = context
38
+ write(this.resourcePath)
39
+ write(this.resourceType)
40
+ write(this.outputPath)
41
+ write(this.packageRoot)
42
+ super.serialize(context)
43
+ }
44
+
45
+ deserialize (context) {
46
+ const { read } = context
47
+ this.resourcePath = read()
48
+ this.resourceType = read()
49
+ this.outputPath = read()
50
+ this.packageRoot = read()
51
+ super.deserialize(context)
52
+ }
53
+ }
54
+
55
+ RecordResourceMapDependency.Template = class RecordResourceMapDependencyTemplate {
56
+ apply () {
57
+ }
58
+ }
59
+
60
+ makeSerializable(RecordResourceMapDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordResourceMapDependency')
61
+
62
+ module.exports = RecordResourceMapDependency
@@ -0,0 +1,40 @@
1
+ const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+
4
+ class RemoveEntryDependency extends NullDependency {
5
+ constructor (entryName) {
6
+ super()
7
+ this.entryName = entryName
8
+ }
9
+
10
+ get type () {
11
+ return 'mpx remove entry'
12
+ }
13
+
14
+ serialize (context) {
15
+ const { write } = context
16
+ write(this.entryName)
17
+ super.serialize(context)
18
+ }
19
+
20
+ deserialize (context) {
21
+ const { read } = context
22
+ this.entryName = read()
23
+ super.deserialize(context)
24
+ }
25
+
26
+ mpxAction (module, compilation, callback) {
27
+ const { entryName } = this
28
+ compilation.entries.delete(entryName)
29
+ return callback()
30
+ }
31
+ }
32
+
33
+ RemoveEntryDependency.Template = class RemoveEntryDependencyTemplate {
34
+ apply () {
35
+ }
36
+ }
37
+
38
+ makeSerializable(RemoveEntryDependency, '@mpxjs/webpack-plugin/lib/dependencies/RemoveEntryDependency')
39
+
40
+ module.exports = RemoveEntryDependency
@@ -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,88 @@
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
+ this.resolved = this.getResolved()
41
+ const { resource, issuerResource, compilation } = this
42
+ if (this.resolved) {
43
+ hash.update(this.resolved)
44
+ } else {
45
+ compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
46
+ }
47
+ super.updateHash(hash, context)
48
+ }
49
+
50
+ serialize (context) {
51
+ const { write } = context
52
+ write(this.resource)
53
+ write(this.packageName)
54
+ write(this.issuerResource)
55
+ write(this.range)
56
+ super.serialize(context)
57
+ }
58
+
59
+ deserialize (context) {
60
+ const { read } = context
61
+ this.resource = read()
62
+ this.packageName = read()
63
+ this.issuerResource = read()
64
+ this.range = read()
65
+ super.deserialize(context)
66
+ }
67
+ }
68
+
69
+ ResolveDependency.Template = class ResolveDependencyTemplate {
70
+ apply (dep, source) {
71
+ const content = this.getContent(dep)
72
+ source.replace(dep.range[0], dep.range[1] - 1, content)
73
+ }
74
+
75
+ getContent (dep) {
76
+ const { resolved = '', compilation } = dep
77
+ const publicPath = compilation.outputOptions.publicPath || ''
78
+ // for tenon
79
+ if (dep.compilation.__mpx__.mode === 'tenon') {
80
+ return `getRelativePath(currentURL, ${JSON.stringify(resolved)}) + '.js'`
81
+ }
82
+ return JSON.stringify(publicPath + resolved)
83
+ }
84
+ }
85
+
86
+ makeSerializable(ResolveDependency, '@mpxjs/webpack-plugin/lib/dependencies/ResolveDependency')
87
+
88
+ module.exports = ResolveDependency