@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
@@ -1,19 +1,14 @@
1
1
  const compiler = require('./compiler')
2
- const loaderUtils = require('loader-utils')
3
2
  const bindThis = require('./bind-this').transform
4
- const InjectDependency = require('../dependency/InjectDependency')
5
3
  const parseRequest = require('../utils/parse-request')
6
- const getMainCompilation = require('../utils/get-main-compilation')
7
- const matchCondition = require('../utils/match-condition')
8
- const path = require('path')
4
+ const { matchCondition } = require('../utils/match-condition')
5
+ const loaderUtils = require('loader-utils')
9
6
 
10
7
  module.exports = function (raw) {
11
8
  this.cacheable()
12
- const options = loaderUtils.getOptions(this) || {}
13
9
  const { resourcePath, queryObj } = parseRequest(this.resource)
14
- const compilation = this._compilation
15
- const mainCompilation = getMainCompilation(compilation)
16
- const mpx = mainCompilation.__mpx__
10
+ const mpx = this.getMpx()
11
+ const root = mpx.projectRoot
17
12
  const mode = mpx.mode
18
13
  const env = mpx.env
19
14
  const defs = mpx.defs
@@ -25,6 +20,11 @@ module.exports = function (raw) {
25
20
  const packageName = queryObj.packageRoot || mpx.currentPackageRoot || 'main'
26
21
  const componentsMap = mpx.componentsMap[packageName]
27
22
  const wxsContentMap = mpx.wxsContentMap
23
+ const usingComponents = queryObj.usingComponents || []
24
+ const hasComment = queryObj.hasComment
25
+ const isNative = queryObj.isNative
26
+ const hasScoped = queryObj.hasScoped
27
+ const moduleId = queryObj.moduleId || 'm' + mpx.pathHash(resourcePath)
28
28
 
29
29
  const warn = (msg) => {
30
30
  this.emitWarning(
@@ -38,13 +38,12 @@ module.exports = function (raw) {
38
38
  )
39
39
  }
40
40
 
41
- const parsed = compiler.parse(raw, {
41
+ const { root: ast, meta } = compiler.parse(raw, {
42
42
  warn,
43
43
  error,
44
- usingComponents: options.usingComponents,
45
- hasComment: options.hasComment,
46
- isNative: options.isNative,
47
- basename: path.basename(resourcePath),
44
+ usingComponents,
45
+ hasComment,
46
+ isNative,
48
47
  isComponent: !!componentsMap[resourcePath],
49
48
  mode,
50
49
  env,
@@ -52,49 +51,56 @@ module.exports = function (raw) {
52
51
  defs,
53
52
  decodeHTMLText,
54
53
  externalClasses,
55
- hasScoped: options.hasScoped,
56
- moduleId: options.moduleId,
57
- filePath: this.resourcePath,
54
+ hasScoped,
55
+ moduleId,
56
+ // 这里需传递resourcePath和wxsContentMap保持一致
57
+ filePath: resourcePath,
58
58
  i18n,
59
59
  checkUsingComponents: mpx.checkUsingComponents,
60
60
  globalComponents: Object.keys(mpx.usingComponents),
61
- // deprecated option
62
- globalMpxAttrsFilter: mpx.globalMpxAttrsFilter,
63
- forceProxyEvent: matchCondition(this.resourcePath, mpx.forceProxyEventRules),
64
- hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules)
61
+ forceProxyEvent: matchCondition(resourcePath, mpx.forceProxyEventRules),
62
+ hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules),
63
+ proxyComponentEventsRules: mpx.proxyComponentEventsRules
65
64
  })
66
65
 
67
- let ast = parsed.root
68
- let meta = parsed.meta
69
-
70
66
  if (meta.wxsContentMap) {
71
67
  for (let module in meta.wxsContentMap) {
72
68
  wxsContentMap[`${resourcePath}~${module}`] = meta.wxsContentMap[module]
73
69
  }
74
70
  }
75
71
 
72
+ let resultSource = ''
73
+
74
+ for (let module in meta.wxsModuleMap) {
75
+ const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], root)
76
+ resultSource += `var ${module} = require(${loaderUtils.stringifyRequest(this, src)});\n`
77
+ }
78
+
76
79
  let result = compiler.serialize(ast)
77
80
 
78
- if (options.isNative || mpx.forceDisableInject) {
81
+ if (isNative) {
79
82
  return result
80
83
  }
81
84
 
82
- const rawCode = `global.currentInject = {
83
- moduleId: ${JSON.stringify(options.moduleId)},
84
- render: function () {
85
- ${compiler.genNode(ast)}this._r();
86
- }
85
+ const rawCode = `
86
+ global.currentInject = {
87
+ moduleId: ${JSON.stringify(moduleId)},
88
+ render: function () {
89
+ ${compiler.genNode(ast)}
90
+ this._r();
91
+ }
87
92
  };\n`
88
93
 
89
- let renderResult
94
+ let bindResult
90
95
 
91
96
  try {
92
- renderResult = bindThis(rawCode, {
97
+ bindResult = bindThis(rawCode, {
93
98
  needCollect: true,
94
99
  ignoreMap: meta.wxsModuleMap
95
100
  })
96
101
  } catch (e) {
97
- error(`Invalid render function generated by the template, please check!\n
102
+ error(`
103
+ Invalid render function generated by the template, please check!\n
98
104
  Template result:
99
105
  ${result}\n
100
106
  Error code:
@@ -104,123 +110,30 @@ ${e.stack}`)
104
110
  return result
105
111
  }
106
112
 
107
- // todo 此处在loader中往其他模块addDep更加危险,考虑修改为通过抽取后的空模块的module.exports来传递信息
108
- let globalInjectCode = renderResult.code + '\n'
113
+ resultSource += bindResult.code + '\n'
109
114
 
110
- if ((mode === 'tt' || mode === 'swan') && renderResult.propKeys) {
111
- globalInjectCode += `global.currentInject.propKeys = ${JSON.stringify(renderResult.propKeys)};\n`
115
+ if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
116
+ resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
112
117
  }
113
118
 
114
119
  if (meta.computed) {
115
- globalInjectCode += bindThis(`global.currentInject.injectComputed = {
120
+ resultSource += bindThis(`
121
+ global.currentInject.injectComputed = {
116
122
  ${meta.computed.join(',')}
117
- };`).code + '\n'
123
+ };`).code + '\n'
118
124
  }
119
125
 
120
126
  if (meta.refs) {
121
- globalInjectCode += `global.currentInject.getRefsData = function () {
127
+ resultSource += `
128
+ global.currentInject.getRefsData = function () {
122
129
  return ${JSON.stringify(meta.refs)};
123
- };\n`
124
- }
125
-
126
- if (meta.options) {
127
- globalInjectCode += bindThis(`global.currentInject.injectOptions = ${JSON.stringify(meta.options)};`).code + '\n'
130
+ };\n`
128
131
  }
129
132
 
130
- const issuer = this._module.issuer
131
- const parser = issuer.parser
132
-
133
- // 同步issuer的dependencies,确保watch中issuer rebuild时template也进行rebuild,使该loader中往issuer中注入的依赖持续有效
134
- issuer.buildInfo.fileDependencies.forEach((dep) => {
135
- this.addDependency(dep)
136
- })
137
- issuer.buildInfo.contextDependencies.forEach((dep) => {
138
- this.addContextDependency(dep)
139
- })
140
-
141
- // 删除issuer中上次注入的dependencies,避免issuer本身不需要更新时上次的注入代码残留
142
- issuer.dependencies = issuer.dependencies.filter((dep) => {
143
- return !dep.templateInject
144
- })
145
-
146
- const dep = new InjectDependency({
147
- content: globalInjectCode,
148
- index: -2
133
+ this.emitFile(resourcePath, '', undefined, {
134
+ skipEmit: true,
135
+ extractedResultSource: resultSource
149
136
  })
150
137
 
151
- dep.templateInject = true
152
- issuer.addDependency(dep)
153
-
154
- let isSync = true
155
-
156
- const iterationOfArrayCallback = (arr, fn) => {
157
- for (let index = 0; index < arr.length; index++) {
158
- fn(arr[index])
159
- }
160
- }
161
-
162
- const dependencies = new Map()
163
-
164
- const addDependency = dep => {
165
- const resourceIdent = dep.getResourceIdentifier()
166
- if (resourceIdent) {
167
- const factory = compilation.dependencyFactories.get(dep.constructor)
168
- if (factory === undefined) {
169
- throw new Error(`No module factory available for dependency type: ${dep.constructor.name}`)
170
- }
171
- let innerMap = dependencies.get(factory)
172
- if (innerMap === undefined) {
173
- dependencies.set(factory, (innerMap = new Map()))
174
- }
175
- let list = innerMap.get(resourceIdent)
176
- if (list === undefined) innerMap.set(resourceIdent, (list = []))
177
- list.push(dep)
178
- }
179
- }
180
-
181
- for (let module in meta.wxsModuleMap) {
182
- isSync = false
183
- const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], options.root)
184
- // 编译render函数只在mpx文件中运行,此处issuer的context一定等同于当前loader的context
185
- const expression = `require(${loaderUtils.stringifyRequest(this, src)})`
186
- const deps = []
187
- parser.parse(expression, {
188
- current: {
189
- addDependency: dep => {
190
- dep.userRequest = module
191
- deps.push(dep)
192
- }
193
- },
194
- module: issuer
195
- })
196
- issuer.addVariable(module, expression, deps)
197
- iterationOfArrayCallback(deps, addDependency)
198
- }
199
-
200
- if (isSync) {
201
- return result
202
- } else {
203
- const callback = this.async()
204
-
205
- const sortedDependencies = []
206
- for (const pair1 of dependencies) {
207
- for (const pair2 of pair1[1]) {
208
- sortedDependencies.push({
209
- factory: pair1[0],
210
- dependencies: pair2[1]
211
- })
212
- }
213
- }
214
-
215
- compilation.addModuleDependencies(
216
- issuer,
217
- sortedDependencies,
218
- compilation.bail,
219
- null,
220
- true,
221
- () => {
222
- callback(null, result)
223
- }
224
- )
225
- }
138
+ return result
226
139
  }
@@ -1,29 +1,34 @@
1
1
  const babylon = require('@babel/parser')
2
2
  const t = require('@babel/types')
3
+ const traverse = require('@babel/traverse').default
3
4
  const generate = require('@babel/generator').default
4
5
 
5
6
  module.exports = function transDynamicClassExpr (expr, { error } = {}) {
6
7
  try {
7
- const ast = babylon.parseExpression(expr, {
8
+ const ast = babylon.parse(expr, {
8
9
  plugins: [
9
10
  'objectRestSpread'
10
11
  ]
11
12
  })
12
- if (t.isObjectExpression(ast)) {
13
- ast.properties.forEach((property) => {
14
- if (t.isObjectProperty(property) && !property.computed) {
15
- const propertyName = property.key.name || property.key.value
16
- if (/-/.test(propertyName)) {
17
- if (/\$/.test(propertyName)) {
18
- error(`Dynamic classname [${propertyName}] is not supported, which includes [-] char and [$] char at the same time.`)
13
+ traverse(ast, {
14
+ ObjectExpression (path) {
15
+ path.node.properties.forEach((property) => {
16
+ if (t.isObjectProperty(property) && !property.computed) {
17
+ const propertyName = property.key.name || property.key.value
18
+ if (/-/.test(propertyName)) {
19
+ if (/\$/.test(propertyName)) {
20
+ error && error(`Dynamic classname [${propertyName}] is not supported, which includes [-] char and [$] char at the same time.`)
21
+ } else {
22
+ property.key = t.identifier(propertyName.replace(/-/g, '$$') + 'MpxDash')
23
+ }
19
24
  } else {
20
- property.key = t.identifier(propertyName.replace(/-/g, '$$') + 'MpxDash')
25
+ property.key = t.identifier(propertyName)
21
26
  }
22
27
  }
23
- }
24
- })
25
- }
26
- return generate(ast, {
28
+ })
29
+ }
30
+ })
31
+ return generate(ast.program.body[0].expression, {
27
32
  compact: true
28
33
  }).code
29
34
  } catch (e) {
@@ -0,0 +1,105 @@
1
+ const processJSON = require('./processJSON')
2
+ const processScript = require('./processScript')
3
+ const processStyles = require('./processStyles')
4
+ const processTemplate = require('./processTemplate')
5
+
6
+ const async = require('async')
7
+
8
+ module.exports = function ({
9
+ mpx,
10
+ loaderContext,
11
+ isProduction,
12
+ parts,
13
+ ctorType,
14
+ filePath,
15
+ queryObj,
16
+ autoScope,
17
+ componentsMap,
18
+ vueContentCache,
19
+ moduleId,
20
+ callback
21
+ }) {
22
+ const hasComment = parts.template && parts.template.attrs && parts.template.attrs.comments
23
+ const isNative = false
24
+ const mode = mpx.mode
25
+ const srcMode = mpx.srcMode
26
+ const env = mpx.env
27
+ const defs = mpx.defs
28
+ const resolveMode = mpx.resolveMode
29
+ const pagesMap = mpx.pagesMap
30
+ const projectRoot = mpx.projectRoot
31
+
32
+ let output = ''
33
+ let usingComponents = [].concat(Object.keys(mpx.usingComponents))
34
+
35
+ return async.waterfall([
36
+ (callback) => {
37
+ async.parallel([
38
+ (callback) => {
39
+ processTemplate(parts.template, {
40
+ hasComment,
41
+ isNative,
42
+ mode,
43
+ srcMode,
44
+ defs,
45
+ loaderContext,
46
+ moduleId,
47
+ ctorType,
48
+ usingComponents,
49
+ decodeHTMLText: mpx.decodeHTMLText,
50
+ externalClasses: mpx.externalClasses,
51
+ checkUsingComponents: mpx.checkUsingComponents
52
+ }, callback)
53
+ },
54
+ (callback) => {
55
+ processStyles(parts.styles, {
56
+ ctorType,
57
+ autoScope
58
+ }, callback)
59
+ },
60
+ (callback) => {
61
+ processJSON(parts.json, {
62
+ mode,
63
+ env,
64
+ defs,
65
+ resolveMode,
66
+ loaderContext,
67
+ pagesMap,
68
+ pathHash: mpx.pathHash,
69
+ componentsMap,
70
+ projectRoot
71
+ }, callback)
72
+ }
73
+ ], (err, res) => {
74
+ callback(err, res)
75
+ })
76
+ },
77
+ ([templateRes, stylesRes, jsonRes], callback) => {
78
+ output += templateRes.output
79
+ output += stylesRes.output
80
+ output += jsonRes.output
81
+ if (ctorType === 'app' && jsonRes.jsonObj.window && jsonRes.jsonObj.window.navigationBarTitleText) {
82
+ mpx.appTitle = jsonRes.jsonObj.window.navigationBarTitleText
83
+ }
84
+
85
+ processScript(parts.script, {
86
+ ctorType,
87
+ srcMode,
88
+ loaderContext,
89
+ isProduction,
90
+ projectRoot,
91
+ jsonConfig: jsonRes.jsonObj,
92
+ componentId: queryObj.componentId || '',
93
+ builtInComponentsMap: templateRes.builtInComponentsMap,
94
+ localComponentsMap: jsonRes.localComponentsMap,
95
+ localPagesMap: jsonRes.localPagesMap,
96
+ forceDisableBuiltInLoader: mpx.forceDisableBuiltInLoader
97
+ }, callback)
98
+ }
99
+ ], (err, scriptRes) => {
100
+ if (err) return callback(err)
101
+ output += scriptRes.output
102
+ vueContentCache.set(filePath, output)
103
+ callback(null, output)
104
+ })
105
+ }