@mpxjs/webpack-plugin 2.6.110 → 2.7.0-beta.10

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 (72) hide show
  1. package/lib/dependencies/AppEntryDependency.js +56 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +69 -0
  3. package/lib/dependencies/DynamicEntryDependency.js +131 -0
  4. package/lib/dependencies/FlagPluginDependency.js +23 -0
  5. package/lib/dependencies/InjectDependency.js +43 -0
  6. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  7. package/lib/dependencies/RecordResourceMapDependency.js +52 -0
  8. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  9. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  10. package/lib/dependencies/ResolveDependency.js +84 -0
  11. package/lib/extractor.js +73 -178
  12. package/lib/file-loader.js +7 -19
  13. package/lib/helpers.js +42 -330
  14. package/lib/index.js +497 -376
  15. package/lib/json-compiler/helper.js +148 -0
  16. package/lib/json-compiler/index.js +195 -439
  17. package/lib/json-compiler/plugin.js +150 -0
  18. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  19. package/lib/loader.js +107 -226
  20. package/lib/native-loader.js +65 -132
  21. package/lib/parser.js +1 -2
  22. package/lib/record-loader.js +11 -0
  23. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  24. package/lib/resolver/AddEnvPlugin.js +3 -2
  25. package/lib/resolver/AddModePlugin.js +3 -2
  26. package/lib/runtime/base.styl +5 -0
  27. package/lib/runtime/i18n.wxs +3 -3
  28. package/lib/runtime/optionProcessor.js +3 -3
  29. package/lib/selector.js +9 -11
  30. package/lib/style-compiler/index.js +12 -19
  31. package/lib/template-compiler/compiler.js +21 -162
  32. package/lib/template-compiler/index.js +47 -136
  33. package/lib/url-loader.js +11 -29
  34. package/lib/utils/add-query.js +1 -1
  35. package/lib/utils/const.js +9 -0
  36. package/lib/utils/emit-file.js +10 -0
  37. package/lib/utils/eval-json-js.js +31 -0
  38. package/lib/utils/get-entry-name.js +13 -0
  39. package/lib/utils/get-json-content.js +41 -0
  40. package/lib/utils/is-url-request.js +10 -1
  41. package/lib/utils/normalize.js +0 -13
  42. package/lib/utils/parse-request.js +3 -3
  43. package/lib/utils/resolve.js +13 -0
  44. package/lib/utils/set.js +47 -0
  45. package/lib/utils/stringify-loaders-resource.js +25 -0
  46. package/lib/utils/stringify-query.js +4 -0
  47. package/lib/web/processJSON.js +113 -142
  48. package/lib/web/processScript.js +32 -26
  49. package/lib/web/processTemplate.js +57 -39
  50. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  51. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  52. package/lib/wxs/WxsParserPlugin.js +2 -2
  53. package/lib/wxs/WxsPlugin.js +4 -8
  54. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  55. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +1 -3
  56. package/lib/wxs/{wxs-loader.js → loader.js} +41 -50
  57. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +3 -4
  58. package/lib/wxss/loader.js +31 -43
  59. package/lib/wxss/localsLoader.js +1 -5
  60. package/package.json +5 -9
  61. package/lib/built-in-loader.js +0 -49
  62. package/lib/content-loader.js +0 -13
  63. package/lib/dependency/ChildCompileDependency.js +0 -24
  64. package/lib/dependency/InjectDependency.js +0 -26
  65. package/lib/dependency/RemovedModuleDependency.js +0 -23
  66. package/lib/dependency/ResolveDependency.js +0 -49
  67. package/lib/plugin-loader.js +0 -287
  68. package/lib/staticConfig.js +0 -4
  69. package/lib/utils/get-main-compilation.js +0 -6
  70. package/lib/utils/read-json-for-src.js +0 -34
  71. package/lib/utils/try-require.js +0 -16
  72. package/lib/wxss/getImportPrefix.js +0 -30
@@ -0,0 +1,148 @@
1
+ const path = require('path')
2
+ const normalize = require('../utils/normalize')
3
+ const nativeLoaderPath = normalize.lib('native-loader')
4
+ const isUrlRequestRaw = require('../utils/is-url-request')
5
+ const parseRequest = require('../utils/parse-request')
6
+ const loaderUtils = require('loader-utils')
7
+ const resolve = require('../utils/resolve')
8
+
9
+ module.exports = function createJSONHelper ({ loaderContext, emitWarning, customGetDynamicEntry }) {
10
+ const mpx = loaderContext.getMpx()
11
+ const resolveMode = mpx.resolveMode
12
+ const externals = mpx.externals
13
+ const root = mpx.projectRoot
14
+ const publicPath = loaderContext._compilation.outputOptions.publicPath || ''
15
+ const pathHash = mpx.pathHash
16
+ const getOutputPath = mpx.getOutputPath
17
+ const mode = mpx.mode
18
+
19
+ const isUrlRequest = r => isUrlRequestRaw(r, root, externals)
20
+ const urlToRequest = r => loaderUtils.urlToRequest(r)
21
+
22
+ const dynamicEntryMap = new Map()
23
+
24
+ let dynamicEntryCount = 0
25
+
26
+ const getDynamicEntry = (resource, type, outputPath = '', packageRoot = '', relativePath = '') => {
27
+ if (typeof customGetDynamicEntry === 'function') return customGetDynamicEntry(resource, type, outputPath, packageRoot, relativePath)
28
+ const key = `mpx_dynamic_entry_${dynamicEntryCount++}`
29
+ const value = `__mpx_dynamic_entry__( ${JSON.stringify(resource)},${JSON.stringify(type)},${JSON.stringify(outputPath)},${JSON.stringify(packageRoot)},${JSON.stringify(relativePath)})`
30
+ dynamicEntryMap.set(key, value)
31
+ return key
32
+ }
33
+
34
+ const processDynamicEntry = (output) => {
35
+ return output.replace(/"mpx_dynamic_entry_\d+"/g, (match) => {
36
+ const key = match.slice(1, -1)
37
+ return dynamicEntryMap.get(key)
38
+ })
39
+ }
40
+
41
+ const processComponent = (component, context, { tarRoot = '', outputPath = '', relativePath = '' }, callback) => {
42
+ if (!isUrlRequest(component)) return callback(null, component)
43
+ if (resolveMode === 'native') {
44
+ component = urlToRequest(component)
45
+ }
46
+
47
+ resolve(context, component, loaderContext, (err, resource, info) => {
48
+ if (err) return callback(err)
49
+ const { resourcePath, queryObj } = parseRequest(resource)
50
+ // 目前只有微信支持分包异步化
51
+ if (queryObj.root && mode === 'wx') tarRoot = queryObj.root
52
+ const parsed = path.parse(resourcePath)
53
+ const ext = parsed.ext
54
+ const resourceName = path.join(parsed.dir, parsed.name)
55
+
56
+ if (!outputPath) {
57
+ if (ext === '.js' && resourceName.includes('node_modules')) {
58
+ let root = info.descriptionFileRoot
59
+ let name = 'nativeComponent'
60
+ if (info.descriptionFileData) {
61
+ if (info.descriptionFileData.miniprogram) {
62
+ root = path.join(root, info.descriptionFileData.miniprogram)
63
+ }
64
+ if (info.descriptionFileData.name) {
65
+ // 去掉name里面的@符号,因为支付宝不支持文件路径上有@
66
+ name = info.descriptionFileData.name.replace(/@/g, '')
67
+ }
68
+ }
69
+ let relative = path.relative(root, resourceName)
70
+ outputPath = path.join('components', name + pathHash(root), relative)
71
+ } else {
72
+ outputPath = getOutputPath(resourcePath, 'component')
73
+ }
74
+ }
75
+ if (ext === '.js') {
76
+ resource = `!!${nativeLoaderPath}!${resource}`
77
+ }
78
+
79
+ const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath)
80
+ callback(null, entry)
81
+ })
82
+ }
83
+
84
+ const processPage = (page, context, tarRoot = '', callback) => {
85
+ let aliasPath = ''
86
+ if (typeof page !== 'string') {
87
+ aliasPath = page.path
88
+ page = page.src
89
+ }
90
+ if (!isUrlRequest(page)) return callback(null, page)
91
+ if (resolveMode === 'native') {
92
+ page = urlToRequest(page)
93
+ }
94
+ resolve(context, page, loaderContext, (err, resource) => {
95
+ if (err) return callback(err)
96
+ const { resourcePath, queryObj: { isFirst } } = parseRequest(resource)
97
+ const ext = path.extname(resourcePath)
98
+ let outputPath
99
+ if (aliasPath) {
100
+ outputPath = aliasPath
101
+ } else {
102
+ const relative = path.relative(context, resourcePath)
103
+ if (/^\./.test(relative)) {
104
+ // 如果当前page不存在于context中,对其进行重命名
105
+ outputPath = getOutputPath(resourcePath, 'page')
106
+ emitWarning(`Current page [${resourcePath}] is not in current pages directory [${context}], the page path will be replaced with [${outputPath}], use ?resolve to get the page path and navigate to it!`)
107
+ } else {
108
+ outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1]
109
+ }
110
+ }
111
+ if (ext === '.js') {
112
+ resource = `!!${nativeLoaderPath}!${resource}`
113
+ }
114
+ const entry = getDynamicEntry(resource, 'page', outputPath, tarRoot, publicPath + tarRoot)
115
+ const key = [resourcePath, outputPath, tarRoot].join('|')
116
+ callback(null, entry, {
117
+ isFirst,
118
+ key
119
+ })
120
+ })
121
+ }
122
+
123
+ const processJsExport = (js, context, tarRoot = '', callback) => {
124
+ if (resolveMode === 'native') {
125
+ js = urlToRequest(js)
126
+ }
127
+ resolve(context, js, loaderContext, (err, resource) => {
128
+ if (err) return callback(err)
129
+ const { resourcePath } = parseRequest(resource)
130
+ const relative = path.relative(context, resourcePath)
131
+ if (/^\./.test(relative)) {
132
+ return callback(new Error(`The js export path ${resourcePath} must be in the context ${context}!`))
133
+ }
134
+ const outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1]
135
+ const entry = getDynamicEntry(resource, 'export', outputPath, tarRoot, publicPath + tarRoot)
136
+ callback(null, entry)
137
+ })
138
+ }
139
+
140
+ return {
141
+ processComponent,
142
+ processDynamicEntry,
143
+ processPage,
144
+ processJsExport,
145
+ isUrlRequest,
146
+ urlToRequest
147
+ }
148
+ }