@mpxjs/webpack-plugin 2.9.18 → 2.9.19-react.0

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 (57) hide show
  1. package/lib/config.js +59 -97
  2. package/lib/dependencies/ResolveDependency.js +2 -2
  3. package/lib/helpers.js +5 -1
  4. package/lib/index.js +21 -18
  5. package/lib/loader.js +44 -97
  6. package/lib/native-loader.js +1 -1
  7. package/lib/platform/index.js +3 -0
  8. package/lib/platform/style/wx/index.js +413 -0
  9. package/lib/platform/template/wx/component-config/button.js +36 -0
  10. package/lib/platform/template/wx/component-config/image.js +15 -0
  11. package/lib/platform/template/wx/component-config/input.js +36 -0
  12. package/lib/platform/template/wx/component-config/scroll-view.js +27 -1
  13. package/lib/platform/template/wx/component-config/swiper-item.js +13 -1
  14. package/lib/platform/template/wx/component-config/swiper.js +25 -1
  15. package/lib/platform/template/wx/component-config/text.js +15 -0
  16. package/lib/platform/template/wx/component-config/textarea.js +39 -0
  17. package/lib/platform/template/wx/component-config/unsupported.js +18 -0
  18. package/lib/platform/template/wx/component-config/view.js +14 -0
  19. package/lib/platform/template/wx/index.js +88 -4
  20. package/lib/react/index.js +92 -0
  21. package/lib/react/processJSON.js +362 -0
  22. package/lib/react/processScript.js +40 -0
  23. package/lib/react/processStyles.js +63 -0
  24. package/lib/react/processTemplate.js +151 -0
  25. package/lib/react/script-helper.js +79 -0
  26. package/lib/react/style-helper.js +91 -0
  27. package/lib/runtime/components/react/event.config.ts +32 -0
  28. package/lib/runtime/components/react/getInnerListeners.ts +289 -0
  29. package/lib/runtime/components/react/getInnerListeners.type.ts +68 -0
  30. package/lib/runtime/components/react/mpx-button.tsx +402 -0
  31. package/lib/runtime/components/react/mpx-image/index.tsx +351 -0
  32. package/lib/runtime/components/react/mpx-image/svg.tsx +21 -0
  33. package/lib/runtime/components/react/mpx-input.tsx +389 -0
  34. package/lib/runtime/components/react/mpx-scroll-view.tsx +412 -0
  35. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +407 -0
  36. package/lib/runtime/components/react/mpx-swiper/index.tsx +68 -0
  37. package/lib/runtime/components/react/mpx-swiper/type.ts +69 -0
  38. package/lib/runtime/components/react/mpx-swiper-item.tsx +42 -0
  39. package/lib/runtime/components/react/mpx-text.tsx +106 -0
  40. package/lib/runtime/components/react/mpx-textarea.tsx +46 -0
  41. package/lib/runtime/components/react/mpx-view.tsx +397 -0
  42. package/lib/runtime/components/react/utils.ts +92 -0
  43. package/lib/runtime/stringify.wxs +3 -7
  44. package/lib/runtime/useNodesRef.ts +39 -0
  45. package/lib/style-compiler/index.js +2 -1
  46. package/lib/template-compiler/compiler.js +539 -287
  47. package/lib/template-compiler/gen-node-react.js +95 -0
  48. package/lib/template-compiler/index.js +19 -31
  49. package/lib/utils/env.js +17 -0
  50. package/lib/utils/make-map.js +1 -1
  51. package/lib/utils/shallow-stringify.js +17 -0
  52. package/lib/web/index.js +122 -0
  53. package/lib/web/processMainScript.js +3 -4
  54. package/lib/web/processScript.js +9 -5
  55. package/lib/web/processTemplate.js +14 -14
  56. package/lib/web/script-helper.js +11 -19
  57. package/package.json +7 -3
@@ -0,0 +1,362 @@
1
+ const async = require('async')
2
+ const path = require('path')
3
+ const JSON5 = require('json5')
4
+ const loaderUtils = require('loader-utils')
5
+ const parseRequest = require('../utils/parse-request')
6
+ const toPosix = require('../utils/to-posix')
7
+ const addQuery = require('../utils/add-query')
8
+ const parseComponent = require('../parser')
9
+ const getJSONContent = require('../utils/get-json-content')
10
+ const resolve = require('../utils/resolve')
11
+ const createJSONHelper = require('../json-compiler/helper')
12
+ const getRulesRunner = require('../platform/index')
13
+ const { RESOLVE_IGNORED_ERR } = require('../utils/const')
14
+ const RecordResourceMapDependency = require('../dependencies/RecordResourceMapDependency')
15
+ const RecordGlobalComponentsDependency = require('../dependencies/RecordGlobalComponentsDependency')
16
+
17
+ module.exports = function (json, {
18
+ loaderContext,
19
+ pagesMap,
20
+ componentsMap
21
+ }, rawCallback) {
22
+ const localPagesMap = {}
23
+ const localComponentsMap = {}
24
+ const output = '/* json */\n'
25
+ let jsonObj = {}
26
+ let tabBarMap
27
+ let tabBarStr
28
+ const mpx = loaderContext.getMpx()
29
+ const {
30
+ mode,
31
+ srcMode,
32
+ env,
33
+ projectRoot
34
+ } = mpx
35
+
36
+ const context = loaderContext.context
37
+
38
+ const emitWarning = (msg) => {
39
+ loaderContext.emitWarning(
40
+ new Error('[json processor][' + loaderContext.resource + ']: ' + msg)
41
+ )
42
+ }
43
+
44
+ const emitError = (msg) => {
45
+ loaderContext.emitError(
46
+ new Error('[json compiler][' + loaderContext.resource + ']: ' + msg)
47
+ )
48
+ }
49
+
50
+ const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
51
+
52
+ const {
53
+ isUrlRequest,
54
+ urlToRequest,
55
+ processPage,
56
+ processComponent
57
+ } = createJSONHelper({
58
+ loaderContext,
59
+ emitWarning,
60
+ emitError,
61
+ customGetDynamicEntry (resource, type, outputPath, packageRoot) {
62
+ return {
63
+ resource,
64
+ // 输出react时组件outputPath不需要拼接packageRoot
65
+ outputPath: type === 'page' ? toPosix(path.join(packageRoot, outputPath)) : outputPath,
66
+ packageRoot
67
+ }
68
+ }
69
+ })
70
+
71
+ const callback = (err) => {
72
+ return rawCallback(err, {
73
+ output,
74
+ jsonObj,
75
+ localPagesMap,
76
+ localComponentsMap,
77
+ tabBarMap,
78
+ tabBarStr
79
+ })
80
+ }
81
+
82
+ const { resourcePath } = parseRequest(loaderContext.resource)
83
+ const isApp = !(pagesMap[resourcePath] || componentsMap[resourcePath])
84
+
85
+ if (!json) {
86
+ return callback()
87
+ }
88
+ // 由于json需要提前读取在template处理中使用,src的场景已经在loader中处理了,此处无需考虑json.src的场景
89
+ try {
90
+ jsonObj = JSON5.parse(json.content)
91
+ // 处理runner
92
+ const rulesRunnerOptions = {
93
+ mode,
94
+ srcMode,
95
+ type: 'json',
96
+ waterfall: true,
97
+ warn: emitWarning,
98
+ error: emitError,
99
+ data: {
100
+ // polyfill global usingComponents & record globalComponents
101
+ globalComponents: mpx.usingComponents
102
+ }
103
+ }
104
+
105
+ if (!isApp) {
106
+ rulesRunnerOptions.mainKey = pagesMap[resourcePath] ? 'page' : 'component'
107
+ }
108
+
109
+ const rulesRunner = getRulesRunner(rulesRunnerOptions)
110
+
111
+ if (rulesRunner) {
112
+ rulesRunner(jsonObj)
113
+ }
114
+ if (isApp) {
115
+ // 收集全局组件
116
+ Object.assign(mpx.usingComponents, jsonObj.usingComponents)
117
+ // 在 rulesRunner 运行后保存全局注册组件
118
+ loaderContext._module.addPresentationalDependency(new RecordGlobalComponentsDependency(mpx.usingComponents, loaderContext.context))
119
+ }
120
+ } catch (e) {
121
+ return callback(e)
122
+ }
123
+
124
+ const fs = loaderContext._compiler.inputFileSystem
125
+
126
+ const defaultTabbar = {
127
+ borderStyle: 'black',
128
+ position: 'bottom',
129
+ custom: false,
130
+ isShow: true
131
+ }
132
+
133
+ const processTabBar = (tabBar, callback) => {
134
+ if (tabBar) {
135
+ tabBar = Object.assign({}, defaultTabbar, tabBar)
136
+ tabBarMap = {}
137
+ jsonObj.tabBar.list.forEach(({ pagePath }) => {
138
+ tabBarMap[pagePath] = true
139
+ })
140
+ tabBarStr = JSON.stringify(tabBar)
141
+ tabBarStr = tabBarStr.replace(/"(iconPath|selectedIconPath)":"([^"]+)"/g, function (matched, $1, $2) {
142
+ if (isUrlRequest($2, projectRoot)) {
143
+ return `"${$1}":require(${stringifyRequest(urlToRequest($2, projectRoot))})`
144
+ }
145
+ return matched
146
+ })
147
+ }
148
+ callback()
149
+ }
150
+
151
+ const processPackages = (packages, context, callback) => {
152
+ if (packages) {
153
+ async.each(packages, (packagePath, callback) => {
154
+ const { queryObj } = parseRequest(packagePath)
155
+ async.waterfall([
156
+ (callback) => {
157
+ resolve(context, packagePath, loaderContext, (err, result) => {
158
+ if (err) return callback(err)
159
+ const { rawResourcePath } = parseRequest(result)
160
+ callback(err, rawResourcePath)
161
+ })
162
+ },
163
+ (result, callback) => {
164
+ fs.readFile(result, (err, content) => {
165
+ if (err) return callback(err)
166
+ callback(err, result, content.toString('utf-8'))
167
+ })
168
+ },
169
+ (result, content, callback) => {
170
+ const extName = path.extname(result)
171
+ if (extName === '.mpx') {
172
+ const parts = parseComponent(content, {
173
+ filePath: result,
174
+ needMap: loaderContext.sourceMap,
175
+ mode,
176
+ env
177
+ })
178
+ getJSONContent(parts.json || {}, result, loaderContext, (err, content) => {
179
+ callback(err, result, content)
180
+ })
181
+ } else {
182
+ callback(null, result, content)
183
+ }
184
+ },
185
+ (result, content, callback) => {
186
+ try {
187
+ content = JSON5.parse(content)
188
+ } catch (err) {
189
+ return callback(err)
190
+ }
191
+
192
+ const processSelfQueue = []
193
+ const context = path.dirname(result)
194
+
195
+ if (content.pages) {
196
+ const tarRoot = queryObj.root
197
+ if (tarRoot) {
198
+ delete queryObj.root
199
+ const subPackage = {
200
+ tarRoot,
201
+ pages: content.pages,
202
+ ...queryObj
203
+ }
204
+
205
+ if (content.plugins) {
206
+ subPackage.plugins = content.plugins
207
+ }
208
+
209
+ processSelfQueue.push((callback) => {
210
+ processSubPackage(subPackage, context, callback)
211
+ })
212
+ } else {
213
+ processSelfQueue.push((callback) => {
214
+ processPages(content.pages, context, '', callback)
215
+ })
216
+ }
217
+ }
218
+ if (content.packages) {
219
+ processSelfQueue.push((callback) => {
220
+ processPackages(content.packages, context, callback)
221
+ })
222
+ }
223
+ if (processSelfQueue.length) {
224
+ async.parallel(processSelfQueue, callback)
225
+ } else {
226
+ callback()
227
+ }
228
+ }
229
+ ], (err) => {
230
+ callback(err === RESOLVE_IGNORED_ERR ? null : err)
231
+ })
232
+ }, callback)
233
+ } else {
234
+ callback()
235
+ }
236
+ }
237
+
238
+ const pageKeySet = new Set()
239
+
240
+ const processPages = (pages, context, tarRoot = '', callback) => {
241
+ if (pages) {
242
+ async.each(pages, (page, callback) => {
243
+ processPage(page, context, tarRoot, (err, { resource, outputPath } = {}, { isFirst, key } = {}) => {
244
+ if (err) return callback(err === RESOLVE_IGNORED_ERR ? null : err)
245
+ if (pageKeySet.has(key)) return callback()
246
+ pageKeySet.add(key)
247
+ const { resourcePath, queryObj } = parseRequest(resource)
248
+ if (localPagesMap[outputPath]) {
249
+ const { resourcePath: oldResourcePath } = parseRequest(localPagesMap[outputPath].resource)
250
+ if (oldResourcePath !== resourcePath) {
251
+ const oldOutputPath = outputPath
252
+ outputPath = mpx.getOutputPath(resourcePath, 'page', { conflictPath: outputPath })
253
+ emitWarning(new Error(`Current page [${resourcePath}] is registered with a conflict outputPath [${oldOutputPath}] which is already existed in system, will be renamed with [${outputPath}], use ?resolve to get the real outputPath!`))
254
+ }
255
+ }
256
+
257
+ pagesMap[resourcePath] = outputPath
258
+ loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'page', outputPath))
259
+ localPagesMap[outputPath] = {
260
+ resource: addQuery(resource, { isPage: true }),
261
+ async: queryObj.async || tarRoot,
262
+ isFirst
263
+ }
264
+ callback()
265
+ })
266
+ }, callback)
267
+ } else {
268
+ callback()
269
+ }
270
+ }
271
+
272
+ const processSubPackage = (subPackage, context, callback) => {
273
+ if (subPackage) {
274
+ if (typeof subPackage.root === 'string' && subPackage.root.startsWith('.')) {
275
+ emitError(`Current subpackage root [${subPackage.root}] is not allow starts with '.'`)
276
+ return callback()
277
+ }
278
+ const tarRoot = subPackage.tarRoot || subPackage.root || ''
279
+ const srcRoot = subPackage.srcRoot || subPackage.root || ''
280
+ if (!tarRoot) return callback()
281
+ context = path.join(context, srcRoot)
282
+ processPages(subPackage.pages, context, tarRoot, callback)
283
+ } else {
284
+ callback()
285
+ }
286
+ }
287
+
288
+ const processSubPackages = (subPackages, context, callback) => {
289
+ if (subPackages) {
290
+ async.each(subPackages, (subPackage, callback) => {
291
+ processSubPackage(subPackage, context, callback)
292
+ }, callback)
293
+ } else {
294
+ callback()
295
+ }
296
+ }
297
+
298
+ const processComponents = (components, context, callback) => {
299
+ if (components) {
300
+ async.eachOf(components, (component, name, callback) => {
301
+ processComponent(component, context, {}, (err, { resource, outputPath } = {}, { tarRoot } = {}) => {
302
+ if (err) return callback(err === RESOLVE_IGNORED_ERR ? null : err)
303
+ const { resourcePath, queryObj } = parseRequest(resource)
304
+ componentsMap[resourcePath] = outputPath
305
+ loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'component', outputPath))
306
+ localComponentsMap[name] = {
307
+ resource: addQuery(resource, {
308
+ isComponent: true,
309
+ outputPath
310
+ }),
311
+ async: queryObj.async || tarRoot
312
+ }
313
+ callback()
314
+ })
315
+ }, callback)
316
+ } else {
317
+ callback()
318
+ }
319
+ }
320
+
321
+ const processGenerics = (generics, context, callback) => {
322
+ if (generics) {
323
+ const genericsComponents = {}
324
+ Object.keys(generics).forEach((name) => {
325
+ const generic = generics[name]
326
+ if (generic.default) genericsComponents[`${name}default`] = generic.default
327
+ })
328
+ processComponents(genericsComponents, context, callback)
329
+ } else {
330
+ callback()
331
+ }
332
+ }
333
+
334
+ async.parallel([
335
+ (callback) => {
336
+ // 添加首页标识
337
+ if (jsonObj.pages && jsonObj.pages[0]) {
338
+ if (typeof jsonObj.pages[0] !== 'string') {
339
+ jsonObj.pages[0].src = addQuery(jsonObj.pages[0].src, { isFirst: true })
340
+ } else {
341
+ jsonObj.pages[0] = addQuery(jsonObj.pages[0], { isFirst: true })
342
+ }
343
+ }
344
+ processPages(jsonObj.pages, context, '', callback)
345
+ },
346
+ (callback) => {
347
+ processComponents(jsonObj.usingComponents, context, callback)
348
+ },
349
+ (callback) => {
350
+ processPackages(jsonObj.packages, context, callback)
351
+ },
352
+ (callback) => {
353
+ processSubPackages(jsonObj.subPackages || jsonObj.subpackages, context, callback)
354
+ },
355
+ (callback) => {
356
+ processGenerics(jsonObj.componentGenerics, context, callback)
357
+ },
358
+ (callback) => {
359
+ processTabBar(jsonObj.tabBar, callback)
360
+ }
361
+ ], callback)
362
+ }
@@ -0,0 +1,40 @@
1
+ const normalize = require('../utils/normalize')
2
+ const optionProcessorPath = normalize.lib('runtime/optionProcessor')
3
+ const { buildComponentsMap, getRequireScript, buildGlobalParams, stringifyRequest } = require('./script-helper')
4
+
5
+ module.exports = function (script, {
6
+ loaderContext,
7
+ ctorType,
8
+ srcMode,
9
+ moduleId,
10
+ isProduction,
11
+ componentGenerics,
12
+ jsonConfig,
13
+ outputPath,
14
+ builtInComponentsMap,
15
+ genericsInfo,
16
+ wxsModuleMap,
17
+ localComponentsMap
18
+ }, callback) {
19
+ let scriptSrcMode = srcMode
20
+ if (script) {
21
+ scriptSrcMode = script.mode || scriptSrcMode
22
+ } else {
23
+ script = { tag: 'script' }
24
+ }
25
+
26
+ let output = '/* script */\n'
27
+ output += 'import { lazy } from \'react\'\n'
28
+ output += `import { getComponent } from ${stringifyRequest(loaderContext, optionProcessorPath)}\n`
29
+
30
+ // 获取组件集合
31
+ const componentsMap = buildComponentsMap({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig })
32
+
33
+ output += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction, componentsMap })
34
+ output += getRequireScript({ ctorType, script, loaderContext })
35
+ output += `export default global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n`
36
+
37
+ callback(null, {
38
+ output
39
+ })
40
+ }
@@ -0,0 +1,63 @@
1
+ const createHelpers = require('../helpers')
2
+ const async = require('async')
3
+ const getClassMap = require('./style-helper').getClassMap
4
+ const shallowStringify = require('../utils/shallow-stringify')
5
+
6
+ module.exports = function (styles, {
7
+ loaderContext,
8
+ srcMode,
9
+ ctorType,
10
+ autoScope,
11
+ moduleId
12
+ }, callback) {
13
+ const { getRequestString } = createHelpers(loaderContext)
14
+ let content = ''
15
+ let output = '/* styles */\n'
16
+ if (styles.length) {
17
+ const { mode } = loaderContext.getMpx() || {}
18
+ async.eachOfSeries(styles, (style, i, callback) => {
19
+ const scoped = style.scoped || autoScope
20
+ const extraOptions = {
21
+ moduleId,
22
+ scoped,
23
+ extract: false
24
+ }
25
+ loaderContext.importModule(JSON.parse(getRequestString('styles', style, extraOptions, i))).then((result) => {
26
+ if (Array.isArray(result)) {
27
+ result = result.map((item) => {
28
+ return item[1]
29
+ }).join('\n')
30
+ }
31
+ content += result.trim() + '\n'
32
+ callback()
33
+ }).catch((e) => {
34
+ callback(e)
35
+ })
36
+ // require style
37
+ }, (err) => {
38
+ if (err) return callback(err)
39
+ try {
40
+ const classMap = getClassMap({
41
+ content,
42
+ filename: loaderContext.resourcePath,
43
+ mode,
44
+ srcMode
45
+ })
46
+ output += `global.currentInject.injectMethods = {
47
+ __getClassMap: function() {
48
+ return ${shallowStringify(classMap)};
49
+ }
50
+ };\n`
51
+ } catch (e) {
52
+ return callback(e)
53
+ }
54
+ callback(null, {
55
+ output
56
+ })
57
+ })
58
+ } else {
59
+ callback(null, {
60
+ output: ''
61
+ })
62
+ }
63
+ }
@@ -0,0 +1,151 @@
1
+ const addQuery = require('../utils/add-query')
2
+ const parseRequest = require('../utils/parse-request')
3
+ const { matchCondition } = require('../utils/match-condition')
4
+ const loaderUtils = require('loader-utils')
5
+ const templateCompiler = require('../template-compiler/compiler')
6
+ const genNode = require('../template-compiler/gen-node-react')
7
+ const bindThis = require('../template-compiler/bind-this')
8
+
9
+ module.exports = function (template, {
10
+ loaderContext,
11
+ // hasScoped,
12
+ hasComment,
13
+ isNative,
14
+ srcMode,
15
+ moduleId,
16
+ ctorType,
17
+ usingComponents,
18
+ componentGenerics
19
+ }, callback) {
20
+ const mpx = loaderContext.getMpx()
21
+ const {
22
+ projectRoot,
23
+ mode,
24
+ defs,
25
+ wxsContentMap,
26
+ decodeHTMLText,
27
+ externalClasses,
28
+ checkUsingComponents,
29
+ autoVirtualHostRules
30
+ } = mpx
31
+ const { resourcePath } = parseRequest(loaderContext.resource)
32
+ const builtInComponentsMap = {}
33
+
34
+ let genericsInfo
35
+ let output = '/* template */\n'
36
+
37
+ output += `global.currentInject = {
38
+ moduleId: ${JSON.stringify(moduleId)}
39
+ };\n`
40
+
41
+ if (template) {
42
+ // 由于远端src template资源引用的相对路径可能发生变化,暂时不支持。
43
+ if (template.src) {
44
+ return callback(new Error('[mpx loader][' + loaderContext.resource + ']: ' + 'template content must be inline in .mpx files!'))
45
+ }
46
+ if (template.lang) {
47
+ return callback(new Error('[mpx loader][' + loaderContext.resource + ']: ' + 'template lang is not supported in trans react native mode temporarily, we will support it in the future!'))
48
+ }
49
+
50
+ if (template.content) {
51
+ const templateSrcMode = template.mode || srcMode
52
+ const warn = (msg) => {
53
+ loaderContext.emitWarning(
54
+ new Error('[template compiler][' + loaderContext.resource + ']: ' + msg)
55
+ )
56
+ }
57
+ const error = (msg) => {
58
+ loaderContext.emitError(
59
+ new Error('[template compiler][' + loaderContext.resource + ']: ' + msg)
60
+ )
61
+ }
62
+ const { root, meta } = templateCompiler.parse(template.content, {
63
+ warn,
64
+ error,
65
+ usingComponents,
66
+ hasComment,
67
+ isNative,
68
+ ctorType,
69
+ mode,
70
+ srcMode: templateSrcMode,
71
+ defs,
72
+ decodeHTMLText,
73
+ externalClasses,
74
+ // todo 后续输出web也采用mpx的scoped处理
75
+ hasScoped: false,
76
+ moduleId,
77
+ filePath: resourcePath,
78
+ i18n: null,
79
+ checkUsingComponents,
80
+ // web模式下全局组件不会被合入usingComponents中,故globalComponents可以传空
81
+ globalComponents: [],
82
+ // web模式下实现抽象组件
83
+ componentGenerics,
84
+ hasVirtualHost: matchCondition(resourcePath, autoVirtualHostRules)
85
+ })
86
+
87
+ if (meta.wxsContentMap) {
88
+ for (const module in meta.wxsContentMap) {
89
+ wxsContentMap[`${resourcePath}~${module}`] = meta.wxsContentMap[module]
90
+ }
91
+ }
92
+ if (meta.builtInComponentsMap) {
93
+ Object.keys(meta.builtInComponentsMap).forEach((name) => {
94
+ builtInComponentsMap[name] = {
95
+ resource: addQuery(meta.builtInComponentsMap[name], { isComponent: true })
96
+ }
97
+ })
98
+ }
99
+ if (meta.genericsInfo) {
100
+ genericsInfo = meta.genericsInfo
101
+ }
102
+
103
+ for (const module in meta.wxsModuleMap) {
104
+ const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], projectRoot)
105
+ output += `var ${module} = require(${loaderUtils.stringifyRequest(this, src)});\n`
106
+ }
107
+
108
+ const rawCode = genNode(root)
109
+ if (rawCode) {
110
+ try {
111
+ const ignoreMap = Object.assign({
112
+ createElement: true,
113
+ components: true,
114
+ getNativeComponent: true,
115
+ rootProps: true
116
+ }, meta.wxsModuleMap)
117
+ const bindResult = bindThis.transform(rawCode, {
118
+ ignoreMap
119
+ })
120
+ output += `global.currentInject.render = function (createElement, components, getNativeComponent, rootProps) {
121
+ return ${bindResult.code}
122
+ };\n`
123
+ } catch (e) {
124
+ error(`Invalid render function generated by the template, please check!
125
+ Error code:
126
+ ${rawCode}
127
+ Error Detail:
128
+ ${e.stack}`)
129
+ }
130
+ }
131
+
132
+ if (meta.computed) {
133
+ output += bindThis.transform(`global.currentInject.injectComputed = {${meta.computed.join(',')}};`).code + '\n'
134
+ }
135
+
136
+ if (meta.refs) {
137
+ output += `global.currentInject.getRefsData = function () {return ${JSON.stringify(meta.refs)};};\n`
138
+ }
139
+
140
+ if (meta.options) {
141
+ output += `global.currentInject.injectOptions = ${JSON.stringify(meta.options)};\n`
142
+ }
143
+ }
144
+ }
145
+
146
+ callback(null, {
147
+ output,
148
+ builtInComponentsMap,
149
+ genericsInfo
150
+ })
151
+ }
@@ -0,0 +1,79 @@
1
+ const loaderUtils = require('loader-utils')
2
+ const createHelpers = require('../helpers')
3
+ const parseRequest = require('../utils/parse-request')
4
+ const shallowStringify = require('../utils/shallow-stringify')
5
+
6
+ function stringifyRequest (loaderContext, request) {
7
+ return loaderUtils.stringifyRequest(loaderContext, request)
8
+ }
9
+
10
+ function getAsyncChunkName (chunkName) {
11
+ if (chunkName && typeof chunkName !== 'boolean') {
12
+ return `/* webpackChunkName: "${chunkName}" */`
13
+ }
14
+ return ''
15
+ }
16
+
17
+ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig }) {
18
+ const componentsMap = {}
19
+ if (localComponentsMap) {
20
+ Object.keys(localComponentsMap).forEach((componentName) => {
21
+ const componentCfg = localComponentsMap[componentName]
22
+ const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
23
+ if (componentCfg.async) {
24
+ componentsMap[componentName] = `lazy(function(){return import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(function(res){return getComponent(res, {displayName: ${JSON.stringify(componentName)}})})})`
25
+ } else {
26
+ componentsMap[componentName] = `getComponent(require(${componentRequest}), {displayName: ${JSON.stringify(componentName)}})`
27
+ }
28
+ })
29
+ }
30
+ if (builtInComponentsMap) {
31
+ Object.keys(builtInComponentsMap).forEach((componentName) => {
32
+ const componentCfg = builtInComponentsMap[componentName]
33
+ const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
34
+ componentsMap[componentName] = `getComponent(require(${componentRequest}), {__mpxBuiltIn: true})`
35
+ })
36
+ }
37
+ return componentsMap
38
+ }
39
+
40
+ function getRequireScript ({ script, ctorType, loaderContext }) {
41
+ let content = ' /** script content **/\n'
42
+ const { getRequire } = createHelpers(loaderContext)
43
+ const { resourcePath, queryObj } = parseRequest(loaderContext.resource)
44
+ const extraOptions = {
45
+ ...script.src
46
+ ? { ...queryObj, resourcePath }
47
+ : null,
48
+ ctorType,
49
+ lang: script.lang || 'js'
50
+ }
51
+ content += ` ${getRequire('script', script, extraOptions)}\n`
52
+ return content
53
+ }
54
+
55
+ function buildGlobalParams ({
56
+ moduleId,
57
+ scriptSrcMode,
58
+ loaderContext,
59
+ isProduction,
60
+ componentsMap
61
+ }) {
62
+ let content = ''
63
+ content += `global.currentModuleId = ${JSON.stringify(moduleId)};\n`
64
+ content += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)};\n`
65
+ if (!isProduction) {
66
+ content += `global.currentResource = ${JSON.stringify(loaderContext.resourcePath)};\n`
67
+ }
68
+ content += `global.currentInject.getComponents = function() {
69
+ return ${shallowStringify(componentsMap)};
70
+ };\n`
71
+ return content
72
+ }
73
+
74
+ module.exports = {
75
+ buildComponentsMap,
76
+ getRequireScript,
77
+ buildGlobalParams,
78
+ stringifyRequest
79
+ }