@mpxjs/webpack-plugin 2.6.102 → 2.7.0-beta.1

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 (59) 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 -330
  13. package/lib/index.js +472 -356
  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 +76 -171
  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 +10 -7
  24. package/lib/style-compiler/index.js +20 -12
  25. package/lib/style-compiler/plugins/trans-special.js +21 -0
  26. package/lib/template-compiler/compiler.js +44 -176
  27. package/lib/template-compiler/index.js +47 -128
  28. package/lib/url-loader.js +11 -29
  29. package/lib/utils/add-query.js +1 -1
  30. package/lib/utils/const.js +5 -0
  31. package/lib/utils/emit-file.js +10 -0
  32. package/lib/utils/get-entry-name.js +13 -0
  33. package/lib/utils/is-url-request.js +10 -1
  34. package/lib/utils/normalize.js +0 -13
  35. package/lib/utils/parse-request.js +3 -3
  36. package/lib/utils/set.js +47 -0
  37. package/lib/utils/stringify-loaders-resource.js +25 -0
  38. package/lib/utils/stringify-query.js +4 -0
  39. package/lib/web/processScript.js +3 -3
  40. package/lib/web/processTemplate.js +2 -0
  41. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  42. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  43. package/lib/wxs/WxsParserPlugin.js +2 -2
  44. package/lib/wxs/WxsPlugin.js +4 -8
  45. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  46. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +0 -0
  47. package/lib/wxs/{wxs-loader.js → loader.js} +33 -38
  48. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +0 -0
  49. package/lib/wxss/loader.js +31 -43
  50. package/lib/wxss/localsLoader.js +1 -5
  51. package/package.json +4 -8
  52. package/lib/content-loader.js +0 -13
  53. package/lib/dependency/ChildCompileDependency.js +0 -24
  54. package/lib/dependency/InjectDependency.js +0 -26
  55. package/lib/dependency/RemovedModuleDependency.js +0 -23
  56. package/lib/dependency/ResolveDependency.js +0 -49
  57. package/lib/plugin-loader.js +0 -287
  58. package/lib/utils/try-require.js +0 -16
  59. 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,337 +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, 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
- })
258
- // normalize scss/sass/postcss if no specific loaders have been provided
259
- if (!loaders[lang]) {
260
- if (postcssExtensions.indexOf(lang) !== -1) {
261
- lang = 'css'
262
- } else if (lang === 'sass') {
263
- lang = `sass?${JSON.stringify({
264
- sassOptions: {
265
- indentedSyntax: true
266
- }
267
- })}`
268
- } else if (lang === 'scss') {
269
- lang = 'sass'
270
- }
271
- }
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
272
58
  }
273
59
 
274
- let templateCompiler = ''
275
-
276
- if (type === 'template') {
277
- const templateCompilerOptions = {
278
- usingComponents,
279
- hasScoped,
280
- hasComment,
281
- isNative,
282
- moduleId,
283
- root: projectRoot
284
- }
285
- 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
286
67
  }
287
68
 
288
- let loader = type === 'styles'
289
- ? loaders[lang] || getCSSLoaderString(lang)
290
- : loaders[lang]
291
-
292
- if (loader != null) {
293
- if (Array.isArray(loader)) {
294
- loader = stringifyLoaders(loader)
295
- } else if (typeof loader === 'object') {
296
- loader = stringifyLoaders([loader])
297
- }
298
- if (type === 'styles') {
299
- // add css modules
300
- loader = addCssModulesToLoader(loader, part, index)
301
- // inject rewriter before css loader for extractTextPlugin use cases
302
- if (rewriterInjectRE.test(loader)) {
303
- loader = loader.replace(
304
- rewriterInjectRE,
305
- (m, $1) => ensureBang($1) + ensureBang(styleCompiler)
306
- )
307
- } else {
308
- loader = ensureBang(loader) + ensureBang(styleCompiler)
309
- }
310
- loader = replaceCssLoader(loader)
311
- }
312
-
313
- if (type === 'template') {
314
- loader = ensureBang(loader) + ensureBang(templateCompiler)
315
- }
69
+ if (part.mode) options.mode = part.mode
316
70
 
317
- return ensureBang(loader)
71
+ if (src) {
72
+ return loaderUtils.stringifyRequest(loaderContext, addQuery(src, options, true))
318
73
  } else {
319
- // unknown lang, infer the loader to be used
320
- switch (type) {
321
- case 'template':
322
- // allow passing options to the template preprocessor via `templateOption` option
323
- const preprocessorOption = { engine: lang, templateOption: options.templateOption || {} }
324
- const templatePreprocessor = templatePreprocessorPath + '?' + JSON.stringify(preprocessorOption)
325
- return ensureBang(defaultLoaders.html) + ensureBang(templateCompiler) + ensureBang(templatePreprocessor)
326
- case 'styles':
327
- loader = addCssModulesToLoader(defaultLoaders.css, part, index)
328
- loader = replaceCssLoader(loader)
329
- return ensureBang(loader) + ensureBang(styleCompiler) + ensureBang(ensureLoader(lang))
330
- case 'script':
331
- return ensureBang(defaultLoaders.js) + ensureBang(ensureLoader(lang))
332
- default:
333
- loader = loaders[type]
334
- if (Array.isArray(loader)) {
335
- loader = stringifyLoaders(loader)
336
- }
337
- return ensureBang(loader + buildCustomBlockLoaderString(part.attrs))
338
- }
74
+ const fakeRequest = getFakeRequest(type, part)
75
+ const request = `${selectorPath}!${addQuery(rawRequest, options, true)}`
76
+ return loaderUtils.stringifyRequest(loaderContext, `${fakeRequest}!=!${request}`)
339
77
  }
340
78
  }
341
79
 
342
- function getSelectorString (type, index) {
343
- const selectorOptions = {
344
- type,
345
- index
346
- }
347
- return ensureBang(
348
- selectorPath + '?' +
349
- JSON.stringify(selectorOptions)
350
- )
351
- }
352
-
353
- function getExtractorString (type, index) {
354
- const extractorOptions = {
355
- type,
356
- index
357
- }
358
- return ensureBang(
359
- extractorPath + '?' +
360
- JSON.stringify(extractorOptions)
361
- )
362
- }
363
-
364
80
  return {
365
- loaders,
366
81
  getRequire,
367
82
  getImport,
368
83
  getNamedExports,
369
- getRequireForSrc,
370
- getImportForSrc,
371
- getNamedExportsForSrc,
372
- getRequestString,
373
- getSrcRequestString
84
+ getRequestString
374
85
  }
375
86
  }