@mpxjs/webpack-plugin 2.6.114-alpha.5 → 2.6.114-alpha.8

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 (99) hide show
  1. package/lib/{dependency → dependencies}/AddEntryDependency.js +0 -0
  2. package/lib/dependencies/AppEntryDependency.js +58 -0
  3. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  4. package/lib/dependencies/CommonJsVariableDependency.js +81 -0
  5. package/lib/dependencies/DynamicEntryDependency.js +171 -0
  6. package/lib/dependencies/FlagPluginDependency.js +24 -0
  7. package/lib/dependencies/InjectDependency.js +43 -0
  8. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  9. package/lib/dependencies/RecordIndependentDependency.js +44 -0
  10. package/lib/dependencies/RecordResourceMapDependency.js +62 -0
  11. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  12. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  13. package/lib/dependencies/ResolveDependency.js +88 -0
  14. package/lib/extractor.js +82 -178
  15. package/lib/file-loader.js +7 -19
  16. package/lib/helpers.js +39 -334
  17. package/lib/independent-loader.js +52 -0
  18. package/lib/index.js +864 -541
  19. package/lib/json-compiler/helper.js +156 -0
  20. package/lib/json-compiler/index.js +242 -451
  21. package/lib/json-compiler/plugin.js +150 -0
  22. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  23. package/lib/loader.js +137 -240
  24. package/lib/native-loader.js +71 -133
  25. package/lib/parser.js +1 -2
  26. package/lib/partial-compile/index.js +35 -0
  27. package/lib/platform/json/wx/index.js +1 -1
  28. package/lib/platform/template/normalize-component-rules.js +2 -3
  29. package/lib/platform/template/wx/index.js +30 -4
  30. package/lib/record-loader.js +11 -0
  31. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  32. package/lib/resolver/AddEnvPlugin.js +4 -3
  33. package/lib/resolver/AddModePlugin.js +4 -3
  34. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  35. package/lib/resolver/PackageEntryPlugin.js +23 -36
  36. package/lib/runtime/base.styl +5 -0
  37. package/lib/runtime/components/web/mpx-image.vue +20 -5
  38. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  39. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  40. package/lib/runtime/i18n.wxs +31 -11
  41. package/lib/runtime/optionProcessor.js +48 -3
  42. package/lib/selector.js +29 -10
  43. package/lib/style-compiler/index.js +15 -23
  44. package/lib/style-compiler/load-postcss-config.js +3 -1
  45. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  46. package/lib/style-compiler/plugins/rpx.js +43 -37
  47. package/lib/style-compiler/plugins/scope-id.js +79 -72
  48. package/lib/style-compiler/plugins/trans-special.js +25 -18
  49. package/lib/style-compiler/plugins/trim.js +13 -7
  50. package/lib/style-compiler/plugins/vw.js +22 -16
  51. package/lib/template-compiler/compiler.js +95 -197
  52. package/lib/template-compiler/index.js +52 -139
  53. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  54. package/lib/tenon/index.js +1 -4
  55. package/lib/tenon/processJSON.js +8 -9
  56. package/lib/tenon/processScript.js +59 -59
  57. package/lib/url-loader.js +11 -29
  58. package/lib/utils/add-query.js +1 -1
  59. package/lib/utils/const.js +10 -0
  60. package/lib/utils/emit-file.js +10 -0
  61. package/lib/utils/eval-json-js.js +31 -0
  62. package/lib/utils/get-entry-name.js +13 -0
  63. package/lib/utils/get-json-content.js +42 -0
  64. package/lib/utils/is-url-request.js +10 -1
  65. package/lib/utils/match-condition.js +4 -1
  66. package/lib/utils/normalize.js +4 -15
  67. package/lib/utils/parse-request.js +3 -3
  68. package/lib/utils/resolve.js +13 -0
  69. package/lib/utils/set.js +47 -0
  70. package/lib/utils/stringify-loaders-resource.js +25 -0
  71. package/lib/utils/stringify-query.js +4 -0
  72. package/lib/web/processJSON.js +113 -144
  73. package/lib/web/processScript.js +47 -34
  74. package/lib/web/processTemplate.js +57 -40
  75. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  76. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  77. package/lib/wxs/WxsParserPlugin.js +2 -2
  78. package/lib/wxs/WxsPlugin.js +4 -8
  79. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  80. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  81. package/lib/wxs/loader.js +142 -0
  82. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  83. package/lib/wxss/loader.js +31 -43
  84. package/lib/wxss/localsLoader.js +1 -5
  85. package/lib/wxss/processCss.js +107 -103
  86. package/package.json +18 -20
  87. package/lib/built-in-loader.js +0 -49
  88. package/lib/content-loader.js +0 -13
  89. package/lib/dependency/ChildCompileDependency.js +0 -24
  90. package/lib/dependency/InjectDependency.js +0 -26
  91. package/lib/dependency/RemovedModuleDependency.js +0 -23
  92. package/lib/dependency/ResolveDependency.js +0 -53
  93. package/lib/plugin-loader.js +0 -287
  94. package/lib/staticConfig.js +0 -4
  95. package/lib/utils/get-main-compilation.js +0 -6
  96. package/lib/utils/read-json-for-src.js +0 -34
  97. package/lib/utils/try-require.js +0 -16
  98. package/lib/wxs/wxs-loader.js +0 -117
  99. 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 && 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) {
@@ -15,8 +15,6 @@ module.exports = function ({
15
15
  queryObj,
16
16
  autoScope,
17
17
  componentsMap,
18
- projectRoot,
19
- getRequireForSrc,
20
18
  vueContentCache,
21
19
  moduleId,
22
20
  callback
@@ -29,6 +27,7 @@ module.exports = function ({
29
27
  const defs = mpx.defs
30
28
  const resolveMode = mpx.resolveMode
31
29
  const pagesMap = mpx.pagesMap
30
+ const projectRoot = mpx.projectRoot
32
31
 
33
32
  let output = ''
34
33
  let usingComponents = [].concat(Object.keys(mpx.usingComponents))
@@ -66,7 +65,6 @@ module.exports = function ({
66
65
  resolveMode,
67
66
  loaderContext,
68
67
  pagesMap,
69
- pagesEntryMap: mpx.pagesEntryMap,
70
68
  pathHash: mpx.pathHash,
71
69
  componentsMap,
72
70
  projectRoot
@@ -89,7 +87,6 @@ module.exports = function ({
89
87
  srcMode,
90
88
  loaderContext,
91
89
  isProduction,
92
- getRequireForSrc,
93
90
  projectRoot,
94
91
  jsonConfig: jsonRes.jsonObj,
95
92
  componentId: queryObj.componentId || '',
@@ -6,7 +6,7 @@ const parseRequest = require('../utils/parse-request')
6
6
  const toPosix = require('../utils/to-posix')
7
7
  const addQuery = require('../utils/add-query')
8
8
  const parseComponent = require('../parser')
9
- const readJsonForSrc = require('../utils/read-json-for-src')
9
+ const getJSONContent = require('../utils/get-json-content')
10
10
  const isUrlRequest = require('../utils/is-url-request')
11
11
 
12
12
  module.exports = function (json, options, rawCallback) {
@@ -17,7 +17,6 @@ module.exports = function (json, options, rawCallback) {
17
17
  const resolveMode = options.resolveMode
18
18
  const pagesMap = options.pagesMap
19
19
  const componentsMap = options.componentsMap
20
- const pagesEntryMap = options.pagesEntryMap
21
20
  const projectRoot = options.projectRoot
22
21
  const pathHash = options.pathHash
23
22
  const localPagesMap = {}
@@ -134,8 +133,8 @@ module.exports = function (json, options, rawCallback) {
134
133
  if (json.content) {
135
134
  content = json.content
136
135
  } else if (json.src) {
137
- return readJsonForSrc(json.src, loaderContext, (content) => {
138
- callback(null, result, content)
136
+ return getJSONContent(json || {}, loaderContext, (err, content) => {
137
+ callback(err, result, content)
139
138
  })
140
139
  }
141
140
  }
@@ -230,8 +229,8 @@ module.exports = function (json, options, rawCallback) {
230
229
  pageName = toPosix(path.join(tarRoot, /^(.*?)(\.[^.]*)?$/.exec(relative)[1]))
231
230
  // 如果当前page与已有page存在命名冲突,也进行重命名
232
231
  for (let key in pagesMap) {
233
- // 此处引入pagesEntryMap确保相同entry下路由路径重复注册才报错,不同entry下的路由路径重复则无影响
234
- if (pagesMap[key] === pageName && key !== resourcePath && pagesEntryMap[key] === loaderContext.resourcePath) {
232
+ // 此处引入pagesMap确保相同entry下路由路径重复注册才报错,不同entry下的路由路径重复则无影响
233
+ if (pagesMap[key] === pageName && key !== resourcePath && pagesMap[key] === loaderContext.resourcePath) {
235
234
  const pageNameRaw = pageName
236
235
  pageName = toPosix(path.join(tarRoot, getPageName(resourcePath, ext)))
237
236
  emitWarning(`Current page [${resourcePath}] is registered with a conflict page path [${pageNameRaw}] which is already existed in system, the page path will be replaced with [${pageName}], use ?resolve to get the page path and navigate to it!`)
@@ -244,9 +243,9 @@ module.exports = function (json, options, rawCallback) {
244
243
  emitWarning(`Current page [${resourcePath}] which is imported from [${loaderContext.resourcePath}] has been registered in pagesMap already, it will be ignored, please check it and remove the redundant page declaration!`)
245
244
  return callback()
246
245
  }
247
- buildInfo.pagesMap = buildInfo.pagesMap || {}
248
- buildInfo.pagesMap[resourcePath] = pagesMap[resourcePath] = pageName
249
- pagesEntryMap[resourcePath] = loaderContext.resourcePath
246
+ // buildInfo.pagesMap = buildInfo.pagesMap || {}
247
+ // buildInfo.pagesMap[resourcePath] = pagesMap[resourcePath] = pageName
248
+ // pagesMap[resourcePath] = loaderContext.resourcePath
250
249
  localPagesMap[pageName] = {
251
250
  resource: addQuery(resource, { page: true }),
252
251
  async: tarRoot || queryObj.async,
@@ -2,10 +2,10 @@ const genComponentTag = require('../utils/gen-component-tag')
2
2
  const loaderUtils = require('loader-utils')
3
3
  const addQuery = require('../utils/add-query')
4
4
  const normalize = require('../utils/normalize')
5
- const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
6
- const AddEntryDependency = require('../dependency/AddEntryDependency')
7
5
  const builtInLoaderPath = normalize.lib('built-in-loader')
8
6
  const optionProcessorPath = normalize.lib('runtime/optionProcessor')
7
+ const createJSONHelper = require('../json-compiler/helper')
8
+ const async = require('async')
9
9
 
10
10
  function shallowStringify (obj) {
11
11
  let arr = []
@@ -21,11 +21,6 @@ function shallowStringify (obj) {
21
21
  return `{${arr.join(',')}}`
22
22
  }
23
23
 
24
- let entryDeps = new Set()
25
-
26
- let callbacked = false
27
- let cacheCallback
28
-
29
24
  module.exports = function (script, options, callback) {
30
25
  const ctorType = options.ctorType
31
26
  const builtInComponentsMap = options.builtInComponentsMap
@@ -35,7 +30,6 @@ module.exports = function (script, options, callback) {
35
30
  const loaderContext = options.loaderContext
36
31
  const isProduction = options.isProduction
37
32
  const componentId = options.componentId
38
- const getRequireForSrc = options.getRequireForSrc
39
33
  // const i18n = options.i18n
40
34
  const jsonConfig = options.jsonConfig
41
35
  // const tabBar = jsonConfig.tabBar
@@ -46,37 +40,52 @@ module.exports = function (script, options, callback) {
46
40
  const forceDisableBuiltInLoader = options.forceDisableBuiltInLoader
47
41
 
48
42
  // add entry
49
- const checkEntryDeps = (callback) => {
50
- callback = callback || cacheCallback
51
- if (callback && entryDeps.size === 0) {
52
- callback()
53
- } else {
54
- cacheCallback = callback
55
- }
43
+ // const checkEntryDeps = (callback) => {
44
+ // callback = callback || cacheCallback
45
+ // if (callback && entryDeps.size === 0) {
46
+ // callback()
47
+ // } else {
48
+ // cacheCallback = callback
49
+ // }
50
+ // }
51
+
52
+ // const addEntryDep = (context, resource, name) => {
53
+ // // 如果loader已经回调,就不再添加entry
54
+ // if (callbacked) return
55
+ // const dep = SingleEntryPlugin.createDependency(resource, name)
56
+ // entryDeps.add(dep)
57
+ // const virtualModule = new AddEntryDependency({
58
+ // context: context._compiler.context,
59
+ // dep,
60
+ // name
61
+ // })
62
+ // /* eslint-disable camelcase */
63
+ // context._module.__has_tenon_entry = true
64
+ // context._module.addDependency(virtualModule)
65
+ // entryDeps.delete(dep)
66
+ // checkEntryDeps()
67
+ // }
68
+
69
+ const emitWarning = (msg) => {
70
+ loaderContext.emitWarning(
71
+ new Error('[tenon script processor][' + loaderContext.resource + ']: ' + msg)
72
+ )
56
73
  }
57
74
 
58
- const addEntryDep = (context, resource, name) => {
59
- // 如果loader已经回调,就不再添加entry
60
- if (callbacked) return
61
- const dep = SingleEntryPlugin.createDependency(resource, name)
62
- entryDeps.add(dep)
63
- const virtualModule = new AddEntryDependency({
64
- context: context._compiler.context,
65
- dep,
66
- name
67
- })
68
- /* eslint-disable camelcase */
69
- context._module.__has_tenon_entry = true
70
- context._module.addDependency(virtualModule)
71
- entryDeps.delete(dep)
72
- checkEntryDeps()
75
+ const emitError = (msg) => {
76
+ loaderContext.emitError(
77
+ new Error('[tenon script processor][' + loaderContext.resource + ']: ' + msg)
78
+ )
73
79
  }
74
80
 
75
- // const emitWarning = (msg) => {
76
- // loaderContext.emitWarning(
77
- // new Error('[script processor][' + loaderContext.resource + ']: ' + msg)
78
- // )
79
- // }
81
+ const {
82
+ processPage,
83
+ processDynamicEntry
84
+ } = createJSONHelper({
85
+ loaderContext,
86
+ emitWarning,
87
+ emitError
88
+ })
80
89
 
81
90
  const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
82
91
  // let tabBarPagesMap = {}
@@ -156,26 +165,6 @@ module.exports = function (script, options, callback) {
156
165
  let firstPage = ''
157
166
  const pagesMap = {}
158
167
  const componentsMap = {}
159
- Object.keys(localPagesMap).forEach((pagePath) => {
160
- const pageCfg = localPagesMap[pagePath]
161
- // const pageRequest = stringifyRequest(pageCfg.resource)
162
- addEntryDep(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
163
- // addEntrySafely(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
164
- // loaderContext.resolve(loaderContext._compiler.context, addQuery(pageCfg.resource, { tenon: true }), (err, resource) => {
165
- // if(err) return callback(err)
166
-
167
- // })
168
- // if (pageCfg.async) {
169
- // pagesMap[pagePath] = `()=>import(${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
170
- // } else {
171
- // // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
172
- // pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
173
- // }
174
-
175
- // if (pageCfg.isFirst) {
176
- // firstPage = pagePath
177
- // }
178
- })
179
168
 
180
169
  Object.keys(localComponentsMap).forEach((componentName) => {
181
170
  const componentCfg = localComponentsMap[componentName]
@@ -199,7 +188,7 @@ module.exports = function (script, options, callback) {
199
188
  }
200
189
  // 为了正确获取currentSrcMode便于运行时进行转换,对于src引入的组件script采用require方式引入(由于webpack会将import的执行顺序上升至最顶),这意味着对于src引入脚本中的named export将不会生效,不过鉴于mpx和小程序中本身也没有在组件script中声明export的用法,所以应该没有影响
201
190
  content += script.src
202
- ? (getRequireForSrc('script', script) + '\n')
191
+ ? `require(${stringifyRequest(script.src)})\n`
203
192
  : (script.content + '\n') + '\n'
204
193
  // createApp/Page/Component执行完成后立刻获取当前的option并暂存
205
194
  content += ` const currentOption = global.currentOption\n`
@@ -246,13 +235,24 @@ module.exports = function (script, options, callback) {
246
235
  // i18n`
247
236
  // }
248
237
  // }
249
- content += `\n )\n`
238
+ content += `\n )\n__dynamic_page_slot__\n`
250
239
  return content
251
240
  }
252
241
  })
253
242
  output += '\n'
254
- checkEntryDeps(() => {
255
- callbacked = true
243
+ // 处理pages
244
+ const pageSet = new Set()
245
+ let dynamicPageStr = ''
246
+ async.each(localPagesMap, (pageCfg, callback) => {
247
+ processPage(addQuery(pageCfg.resource, { tenon: true }), loaderContext.context, '', (err, entry, { key }) => {
248
+ if (err) return callback()
249
+ if (pageSet.has(key)) return callback()
250
+ pageSet.add(key)
251
+ dynamicPageStr += `\n"${entry}"`
252
+ callback()
253
+ })
254
+ }, () => {
255
+ output = output.replace('__dynamic_page_slot__', processDynamicEntry(dynamicPageStr) || '')
256
256
  callback(null, {
257
257
  output
258
258
  })
package/lib/url-loader.js CHANGED
@@ -1,51 +1,33 @@
1
1
  const loaderUtils = require('loader-utils')
2
2
  const mime = require('mime')
3
+ const parseRequest = require('./utils/parse-request')
3
4
  const getOptions = loaderUtils.getOptions
4
- const parseQuery = loaderUtils.parseQuery
5
5
 
6
6
  function isStyleRequest (request) {
7
- let elements = request.replace(/^-?!+/, '').replace(/!!+/g, '!').split('!')
8
- elements.pop()
9
- for (let i = 0; i < elements.length; i++) {
10
- let element = elements[i]
11
- let queryString = '?'
12
- let loaderString = element
13
- let idx = element.indexOf('?')
14
- if (idx >= 0) {
15
- queryString = element.substr(idx)
16
- loaderString = element.substr(0, idx)
17
- }
18
- if (/css-loader/.test(loaderString)) {
19
- return true
20
- }
21
- if (/content-loader/.test(loaderString)) {
22
- let query = parseQuery(queryString)
23
- if (query.type === 'styles') {
24
- return true
25
- }
26
- }
27
- }
7
+ const { loaderString, queryObj } = parseRequest(request)
8
+ if (queryObj.type === 'styles') return true
9
+ if (/(css-loader|wxss\/loader)/.test(loaderString)) return true
28
10
  return false
29
11
  }
30
12
 
31
13
  module.exports = function (src) {
32
14
  let transBase64 = false
33
15
  const options = Object.assign({}, getOptions(this))
34
- const filePath = this.resourcePath
35
- const mimetype = options.mimetype || mime.getType(filePath)
36
- const issuer = this._module.issuer
16
+ const { resourcePath, queryObj } = parseRequest(this.resource)
17
+ const mimetype = options.mimetype || mime.getType(resourcePath)
18
+ const moduleGraph = this._compilation.moduleGraph
19
+ const issuer = moduleGraph.getIssuer(this._module)
37
20
  const publicPathScope = options.publicPathScope === 'all' ? 'all' : 'styleOnly'
38
- const queryOption = parseQuery(this.resourceQuery || '?')
39
21
  const limit = options.limit
40
- const useLocal = !limit || src.length < limit || queryOption.useLocal
41
- const isStyle = (issuer && issuer.request && isStyleRequest(issuer.request)) || queryOption.isStyle
22
+ const useLocal = !limit || src.length < limit || queryObj.useLocal
23
+ const isStyle = (issuer && issuer.request && isStyleRequest(issuer.request)) || queryObj.isStyle
42
24
 
43
25
  if (isStyle) {
44
26
  if (options.publicPath) {
45
27
  if (useLocal) {
46
28
  transBase64 = true
47
29
  }
48
- if (queryOption.fallback) {
30
+ if (queryObj.fallback) {
49
31
  transBase64 = false
50
32
  }
51
33
  } else {
@@ -3,7 +3,7 @@ const stringifyQuery = require('./stringify-query')
3
3
  const type = require('./type')
4
4
 
5
5
  // 默认为非强行覆盖原query,如需强行覆盖传递force为true
6
- module.exports = function (request, data = {}, force, removeKeys) {
6
+ module.exports = function addQuery (request, data = {}, force, removeKeys) {
7
7
  const { rawResourcePath: resourcePath, loaderString, queryObj: queryObjRaw } = parseRequest(request)
8
8
  const queryObj = Object.assign({}, queryObjRaw)
9
9
  if (force) {
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ MPX_PROCESSED_FLAG: 'mpx_processed',
3
+ MPX_DISABLE_EXTRACTOR_CACHE: 'mpx_disable_extractor_cache',
4
+ DEFAULT_RESULT_SOURCE: '',
5
+ RESOLVE_IGNORED_ERR: new Error('Resolve ignored!'),
6
+ JSON_JS_EXT: '.json.js',
7
+ MPX_ROOT_VIEW: 'mpx-root-view', // 根节点类名
8
+ MPX_APP_MODULE_ID: 'mpx-app-scope', // app文件moduleId
9
+ MPX_CURRENT_CHUNK: 'mpx_current_chunk'
10
+ }
@@ -0,0 +1,10 @@
1
+ const { RawSource } = require('webpack').sources
2
+
3
+ module.exports = (module, name, content, sourceMap, assetInfo) => {
4
+ if (!module.buildInfo.assets) {
5
+ module.buildInfo.assets = Object.create(null)
6
+ module.buildInfo.assetsInfo = new Map()
7
+ }
8
+ module.buildInfo.assets[name] = new RawSource(content)
9
+ module.buildInfo.assetsInfo.set(name, assetInfo)
10
+ }