@mpxjs/webpack-plugin 2.6.103 → 2.7.0-alpha.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 (58) hide show
  1. package/lib/dependencies/AppEntryDependency.js +56 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +74 -0
  3. package/lib/dependencies/DynamicEntryDependency.js +127 -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/RecordStaticResourceDependency.js +47 -0
  8. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  9. package/lib/dependencies/ResolveDependency.js +83 -0
  10. package/lib/extractor.js +72 -179
  11. package/lib/file-loader.js +7 -19
  12. package/lib/helpers.js +41 -331
  13. package/lib/index.js +475 -365
  14. package/lib/json-compiler/helper.js +152 -0
  15. package/lib/json-compiler/index.js +148 -407
  16. package/lib/json-compiler/plugin.js +134 -0
  17. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  18. package/lib/loader.js +78 -177
  19. package/lib/native-loader.js +40 -70
  20. package/lib/record-loader.js +11 -0
  21. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  22. package/lib/runtime/i18n.wxs +3 -3
  23. package/lib/selector.js +8 -7
  24. package/lib/style-compiler/index.js +14 -15
  25. package/lib/template-compiler/compiler.js +16 -153
  26. package/lib/template-compiler/index.js +46 -132
  27. package/lib/url-loader.js +11 -29
  28. package/lib/utils/add-query.js +1 -1
  29. package/lib/utils/const.js +5 -0
  30. package/lib/utils/emit-file.js +10 -0
  31. package/lib/utils/get-entry-name.js +13 -0
  32. package/lib/utils/is-url-request.js +10 -1
  33. package/lib/utils/normalize.js +0 -13
  34. package/lib/utils/parse-request.js +3 -3
  35. package/lib/utils/set.js +47 -0
  36. package/lib/utils/stringify-loaders-resource.js +25 -0
  37. package/lib/utils/stringify-query.js +4 -0
  38. package/lib/web/processScript.js +3 -3
  39. package/lib/web/processTemplate.js +2 -0
  40. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  41. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  42. package/lib/wxs/WxsParserPlugin.js +2 -2
  43. package/lib/wxs/WxsPlugin.js +4 -8
  44. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  45. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +0 -0
  46. package/lib/wxs/{wxs-loader.js → loader.js} +33 -38
  47. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +0 -0
  48. package/lib/wxss/loader.js +31 -43
  49. package/lib/wxss/localsLoader.js +1 -5
  50. package/package.json +4 -8
  51. package/lib/content-loader.js +0 -13
  52. package/lib/dependency/ChildCompileDependency.js +0 -24
  53. package/lib/dependency/InjectDependency.js +0 -26
  54. package/lib/dependency/RemovedModuleDependency.js +0 -23
  55. package/lib/dependency/ResolveDependency.js +0 -49
  56. package/lib/plugin-loader.js +0 -287
  57. package/lib/utils/try-require.js +0 -16
  58. package/lib/wxss/getImportPrefix.js +0 -30
package/lib/helpers.js CHANGED
@@ -1,35 +1,8 @@
1
- const querystring = require('querystring')
2
1
  const loaderUtils = require('loader-utils')
3
2
  const normalize = require('./utils/normalize')
4
- const tryRequire = require('./utils/try-require')
5
- const styleCompilerPath = normalize.lib('style-compiler/index')
6
- const templateCompilerPath = normalize.lib('template-compiler/index')
7
- const jsonCompilerPath = normalize.lib('json-compiler/index')
8
- const templatePreprocessorPath = normalize.lib('template-compiler/preprocessor')
9
- const wxsLoaderPath = normalize.lib('wxs/wxs-loader')
10
- const wxmlLoaderPath = normalize.lib('wxml/wxml-loader')
11
- const wxssLoaderPath = normalize.lib('wxss/loader')
12
- const config = require('./config')
13
3
  const selectorPath = normalize.lib('selector')
14
- const extractorPath = normalize.lib('extractor')
15
4
  const addQuery = require('./utils/add-query')
16
-
17
- // check whether default js loader exists
18
- const hasBabel = !!tryRequire('babel-loader')
19
-
20
- const rewriterInjectRE = /\b(css(?:-loader)?(?:\?[^!]+)?)(?:!|$)/
21
-
22
- const defaultLang = {
23
- template: 'html',
24
- styles: 'css',
25
- script: 'js',
26
- json: 'json',
27
- wxs: 'wxs'
28
- }
29
-
30
- const postcssExtensions = [
31
- 'postcss', 'pcss', 'sugarss', 'sss'
32
- ]
5
+ const parseRequest = require('./utils/parse-request')
33
6
 
34
7
  function getRawRequest ({ resource, loaderIndex, loaders }, excludedPreLoaders = /eslint-loader/) {
35
8
  return loaderUtils.getRemainingRequest({
@@ -39,338 +12,75 @@ function getRawRequest ({ resource, loaderIndex, loaders }, excludedPreLoaders =
39
12
  })
40
13
  }
41
14
 
42
- // sass => sass-loader
43
- // sass-loader => sass-loader
44
- // sass?indentedSyntax!css => sass-loader?indentedSyntax&!css-loader
45
- function ensureLoader (lang) {
46
- return lang
47
- .split('!')
48
- .map(loader =>
49
- loader.replace(
50
- /^([\w-]+)(\?.*)?/,
51
- (_, name, query) =>
52
- (/-loader$/.test(name) ? name : name + '-loader') + (query || '')
53
- )
54
- )
55
- .join('!')
56
- }
57
-
58
- function ensureBang (loader) {
59
- if (loader && loader.charAt(loader.length - 1) !== '!') {
60
- return loader + '!'
61
- } else {
62
- return loader
63
- }
64
- }
65
-
66
- function resolveLoaders ({ options, projectRoot }) {
67
- let cssLoaderOptions = ''
68
- let wxmlLoaderOptions = ''
69
- let jsonCompilerOptions = ''
70
-
71
- wxmlLoaderOptions += '?root=' + projectRoot
72
- jsonCompilerOptions += '?root=' + projectRoot
73
- // 由于css-loader@1.0之后不再支持root,暂时不允许在css中使用/开头的路径,后续迁移至postcss-loader再进行支持
74
- // 现在切回css-loader@0.28.11了,先加回来和原生小程序保持一致
75
- cssLoaderOptions += (cssLoaderOptions ? '&' : '?') + 'root=' + projectRoot + '&extract=true'
76
-
77
- const defaultLoaders = {
78
- html: wxmlLoaderPath + wxmlLoaderOptions,
79
- css: getCSSLoaderString(),
80
- js: hasBabel ? 'babel-loader' : '',
81
- json: jsonCompilerPath + jsonCompilerOptions,
82
- wxs: wxsLoaderPath
83
- }
84
-
85
- function getCSSLoaderString (lang) {
86
- const langLoader = lang ? ensureBang(ensureLoader(lang)) : ''
87
- return ensureBang('css-loader' + cssLoaderOptions) + langLoader
88
- }
89
-
90
- return {
91
- defaultLoaders,
92
- getCSSLoaderString,
93
- loaders: Object.assign({}, defaultLoaders, options.loaders),
94
- preLoaders: options.preLoaders || {},
95
- postLoaders: options.postLoaders || {}
96
- }
15
+ const defaultLang = {
16
+ template: 'wxml',
17
+ styles: 'wxss',
18
+ script: 'js',
19
+ json: 'json',
20
+ wxs: 'wxs'
97
21
  }
98
22
 
99
- module.exports = function createHelpers ({ loaderContext, options, moduleId, hasScoped, ctorType, hasComment, usingComponents, srcMode, isNative, projectRoot }) {
100
- const rawRequest = getRawRequest(loaderContext, options.excludedPreLoaders)
101
- const {
102
- defaultLoaders,
103
- getCSSLoaderString,
104
- loaders,
105
- preLoaders,
106
- postLoaders
107
- } = resolveLoaders({
108
- options,
109
- projectRoot
110
- })
23
+ module.exports = function createHelpers (loaderContext) {
24
+ const rawRequest = getRawRequest(loaderContext)
111
25
 
112
- function getRequire (type, part, index, scoped) {
113
- return 'require(' + getRequestString(type, part, index, scoped) + ')'
26
+ function getRequire (type, part, extraOptions, index) {
27
+ return 'require(' + getRequestString(type, part, extraOptions, index) + ')'
114
28
  }
115
29
 
116
- function getImport (type, part, index, scoped) {
30
+ function getImport (type, part, extraOptions, index) {
117
31
  return (
118
32
  'import __' + type + '__ from ' +
119
- getRequestString(type, part, index, scoped)
33
+ getRequestString(type, part, extraOptions, index)
120
34
  )
121
35
  }
122
36
 
123
- function getNamedExports (type, part, index, scoped) {
37
+ function getNamedExports (type, part, extraOptions, index) {
124
38
  return (
125
39
  'export * from ' +
126
- getRequestString(type, part, index, scoped)
127
- )
128
- }
129
-
130
- function processMode (request, mode) {
131
- if (mode) {
132
- // 当前区块如声明了mode则强制覆盖已有request中的mode
133
- request = addQuery(request, { mode }, true)
134
- }
135
- return request
136
- }
137
-
138
- function getRequestString (type, part, index = 0, scoped) {
139
- return loaderUtils.stringifyRequest(
140
- loaderContext,
141
- // disable all configuration loaders
142
- '!!' +
143
- // get loader string for pre-processors
144
- getLoaderString(type, part, index, scoped) +
145
- // select the corresponding part from the mpx file
146
- getSelectorString(type, index) +
147
- // the url to the actual mpx file, including remaining requests
148
- processMode(rawRequest, part.mode)
40
+ getRequestString(type, part, extraOptions, index)
149
41
  )
150
42
  }
151
43
 
152
- function getRequireForSrc (type, impt, index, scoped, prefix) {
153
- return 'require(' + getSrcRequestString(type, impt, index, scoped, prefix) + ')'
154
- }
155
-
156
- function getImportForSrc (type, impt, index, scoped, prefix) {
157
- return (
158
- 'import __' + type + '__ from ' +
159
- getSrcRequestString(type, impt, index, scoped, prefix)
160
- )
44
+ function getFakeRequest (type, part) {
45
+ const lang = part.lang || defaultLang[type] || type
46
+ const { resourcePath, queryObj } = parseRequest(loaderContext.resource)
47
+ if (lang === 'json') queryObj.asScript = true
48
+ return addQuery(`${resourcePath}.${lang}`, queryObj)
161
49
  }
162
50
 
163
- function getNamedExportsForSrc (type, impt, index, scoped, prefix) {
164
- return (
165
- 'export * from ' +
166
- getSrcRequestString(type, impt, index, scoped, prefix)
167
- )
168
- }
169
-
170
- function getSrcRequestString (type, impt, index = 0, scoped, prefix = '!') {
171
- let loaderString = type === 'script' ? '' : prefix + getLoaderString(type, impt, index, scoped)
172
- let src = impt.src
173
- return loaderUtils.stringifyRequest(
174
- loaderContext,
175
- loaderString + processMode(src, impt.mode)
176
- )
177
- }
178
-
179
- function addCssModulesToLoader (loader, part, index) {
180
- if (!part.module) return loader
181
- const option = options.cssModules || {}
182
- const DEFAULT_OPTIONS = {
183
- modules: true
184
- }
185
- const OPTIONS = {
186
- localIdentName: '[hash:base64]'
187
- }
188
- return loader.replace(/((?:^|!)css(?:-loader)?)(\?[^!]*)?/, (m, $1, $2) => {
189
- // $1: !css-loader
190
- // $2: ?a=b
191
- const query = loaderUtils.parseQuery($2 || '?')
192
- Object.assign(query, OPTIONS, option, DEFAULT_OPTIONS)
193
- if (index !== -1) {
194
- query.localIdentName += '_' + index
195
- }
196
- return $1 + '?' + JSON.stringify(query)
197
- })
198
- }
199
-
200
- function buildCustomBlockLoaderString (attrs) {
201
- const noSrcAttrs = Object.assign({}, attrs)
202
- delete noSrcAttrs.src
203
- const qs = querystring.stringify(noSrcAttrs)
204
- return qs ? '?' + qs : qs
205
- }
206
-
207
- // stringify an Array of loader objects
208
- function stringifyLoaders (loaders) {
209
- return loaders
210
- .map(
211
- obj =>
212
- obj && typeof obj === 'object' && typeof obj.loader === 'string'
213
- ? obj.loader +
214
- (obj.options ? '?' + JSON.stringify(obj.options) : '')
215
- : obj
216
- )
217
- .join('!')
218
- }
219
-
220
- function getLoaderString (type, part, index, scoped) {
221
- let loader = getRawLoaderString(type, part, index, scoped)
222
- const lang = getLangString(type, part)
223
- if (type !== 'script' && type !== 'wxs') {
224
- loader = getExtractorString(type, index) + loader
225
- }
226
- if (preLoaders[lang]) {
227
- loader = loader + ensureBang(preLoaders[lang])
228
- }
229
- if (postLoaders[lang]) {
230
- loader = ensureBang(postLoaders[lang]) + loader
231
- }
232
- return loader
233
- }
234
-
235
- function getLangString (type, { lang }) {
236
- if (type === 'script' || type === 'template' || type === 'styles') {
237
- return lang || defaultLang[type]
238
- } else {
239
- return type
240
- }
241
- }
242
-
243
- function replaceCssLoader (rawLoader) {
244
- return rawLoader.replace(/css(?:-loader)?/, wxssLoaderPath)
245
- }
246
-
247
- function getRawLoaderString (type, part, index, scoped) {
248
- let lang = (part.lang && part.lang !== config[srcMode].typeExtMap.template.slice(1)) ? part.lang : defaultLang[type]
249
-
250
- let styleCompiler = ''
251
- if (type === 'styles') {
252
- // style compiler that needs to be applied for all styles
253
- styleCompiler = styleCompilerPath + '?' +
254
- JSON.stringify({
255
- moduleId,
256
- scoped: !!scoped,
257
- ctorType: ctorType
258
- })
259
- // normalize scss/sass/postcss if no specific loaders have been provided
260
- if (!loaders[lang]) {
261
- if (postcssExtensions.indexOf(lang) !== -1) {
262
- lang = 'css'
263
- } else if (lang === 'sass') {
264
- lang = `sass?${JSON.stringify({
265
- sassOptions: {
266
- indentedSyntax: true
267
- }
268
- })}`
269
- } else if (lang === 'scss') {
270
- lang = 'sass'
271
- }
272
- }
51
+ function getRequestString (type, part, extraOptions = {}, index = 0) {
52
+ const src = part.src
53
+ const options = {
54
+ mpx: true,
55
+ type,
56
+ index,
57
+ ...extraOptions
273
58
  }
274
59
 
275
- let templateCompiler = ''
276
-
277
- if (type === 'template') {
278
- const templateCompilerOptions = {
279
- usingComponents,
280
- hasScoped,
281
- hasComment,
282
- isNative,
283
- moduleId,
284
- root: projectRoot
285
- }
286
- templateCompiler = templateCompilerPath + '?' + JSON.stringify(templateCompilerOptions)
60
+ switch (type) {
61
+ case 'json':
62
+ options.asScript = true
63
+ // eslint-disable-next-line no-fallthrough
64
+ case 'styles':
65
+ case 'template':
66
+ options.extract = true
287
67
  }
288
68
 
289
- let loader = type === 'styles'
290
- ? loaders[lang] || getCSSLoaderString(lang)
291
- : loaders[lang]
292
-
293
- if (loader != null) {
294
- if (Array.isArray(loader)) {
295
- loader = stringifyLoaders(loader)
296
- } else if (typeof loader === 'object') {
297
- loader = stringifyLoaders([loader])
298
- }
299
- if (type === 'styles') {
300
- // add css modules
301
- loader = addCssModulesToLoader(loader, part, index)
302
- // inject rewriter before css loader for extractTextPlugin use cases
303
- if (rewriterInjectRE.test(loader)) {
304
- loader = loader.replace(
305
- rewriterInjectRE,
306
- (m, $1) => ensureBang($1) + ensureBang(styleCompiler)
307
- )
308
- } else {
309
- loader = ensureBang(loader) + ensureBang(styleCompiler)
310
- }
311
- loader = replaceCssLoader(loader)
312
- }
313
-
314
- if (type === 'template') {
315
- loader = ensureBang(loader) + ensureBang(templateCompiler)
316
- }
69
+ if (part.mode) options.mode = part.mode
317
70
 
318
- return ensureBang(loader)
71
+ if (src) {
72
+ return loaderUtils.stringifyRequest(loaderContext, addQuery(src, options, true))
319
73
  } else {
320
- // unknown lang, infer the loader to be used
321
- switch (type) {
322
- case 'template':
323
- // allow passing options to the template preprocessor via `templateOption` option
324
- const preprocessorOption = { engine: lang, templateOption: options.templateOption || {} }
325
- const templatePreprocessor = templatePreprocessorPath + '?' + JSON.stringify(preprocessorOption)
326
- return ensureBang(defaultLoaders.html) + ensureBang(templateCompiler) + ensureBang(templatePreprocessor)
327
- case 'styles':
328
- loader = addCssModulesToLoader(defaultLoaders.css, part, index)
329
- loader = replaceCssLoader(loader)
330
- return ensureBang(loader) + ensureBang(styleCompiler) + ensureBang(ensureLoader(lang))
331
- case 'script':
332
- return ensureBang(defaultLoaders.js) + ensureBang(ensureLoader(lang))
333
- default:
334
- loader = loaders[type]
335
- if (Array.isArray(loader)) {
336
- loader = stringifyLoaders(loader)
337
- }
338
- return ensureBang(loader + buildCustomBlockLoaderString(part.attrs))
339
- }
74
+ const fakeRequest = getFakeRequest(type, part)
75
+ const request = `${selectorPath}!${addQuery(rawRequest, options, true)}`
76
+ return loaderUtils.stringifyRequest(loaderContext, `${fakeRequest}!=!${request}`)
340
77
  }
341
78
  }
342
79
 
343
- function getSelectorString (type, index) {
344
- const selectorOptions = {
345
- type,
346
- index
347
- }
348
- return ensureBang(
349
- selectorPath + '?' +
350
- JSON.stringify(selectorOptions)
351
- )
352
- }
353
-
354
- function getExtractorString (type, index) {
355
- const extractorOptions = {
356
- type,
357
- index
358
- }
359
- return ensureBang(
360
- extractorPath + '?' +
361
- JSON.stringify(extractorOptions)
362
- )
363
- }
364
-
365
80
  return {
366
- loaders,
367
81
  getRequire,
368
82
  getImport,
369
83
  getNamedExports,
370
- getRequireForSrc,
371
- getImportForSrc,
372
- getNamedExportsForSrc,
373
- getRequestString,
374
- getSrcRequestString
84
+ getRequestString
375
85
  }
376
86
  }