@mpxjs/webpack-plugin 2.6.115 → 2.7.0-alpha

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 (129) hide show
  1. package/LICENSE +433 -0
  2. package/README.md +1 -1
  3. package/lib/config.js +14 -0
  4. package/lib/dependencies/AddEntryDependency.js +24 -0
  5. package/lib/dependencies/AppEntryDependency.js +58 -0
  6. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  7. package/lib/dependencies/CommonJsVariableDependency.js +81 -0
  8. package/lib/dependencies/DynamicEntryDependency.js +171 -0
  9. package/lib/dependencies/FlagPluginDependency.js +24 -0
  10. package/lib/dependencies/InjectDependency.js +43 -0
  11. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  12. package/lib/dependencies/RecordIndependentDependency.js +44 -0
  13. package/lib/dependencies/RecordResourceMapDependency.js +62 -0
  14. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  15. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  16. package/lib/dependencies/ResolveDependency.js +88 -0
  17. package/lib/extractor.js +82 -178
  18. package/lib/file-loader.js +7 -19
  19. package/lib/helpers.js +39 -334
  20. package/lib/independent-loader.js +52 -0
  21. package/lib/index.js +889 -525
  22. package/lib/json-compiler/helper.js +156 -0
  23. package/lib/json-compiler/index.js +245 -451
  24. package/lib/json-compiler/plugin.js +150 -0
  25. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  26. package/lib/loader.js +178 -241
  27. package/lib/native-loader.js +71 -133
  28. package/lib/parser.js +1 -2
  29. package/lib/partial-compile/index.js +35 -0
  30. package/lib/platform/json/wx/index.js +1 -1
  31. package/lib/platform/template/normalize-component-rules.js +2 -3
  32. package/lib/platform/template/wx/component-config/button.js +14 -2
  33. package/lib/platform/template/wx/component-config/image.js +4 -0
  34. package/lib/platform/template/wx/component-config/input.js +4 -0
  35. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  36. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  37. package/lib/platform/template/wx/component-config/switch.js +4 -0
  38. package/lib/platform/template/wx/component-config/text.js +4 -0
  39. package/lib/platform/template/wx/component-config/textarea.js +5 -0
  40. package/lib/platform/template/wx/component-config/view.js +4 -0
  41. package/lib/platform/template/wx/index.js +149 -3
  42. package/lib/record-loader.js +11 -0
  43. package/lib/resolve-loader.js +6 -0
  44. package/lib/resolver/AddEnvPlugin.js +4 -3
  45. package/lib/resolver/AddModePlugin.js +4 -3
  46. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  47. package/lib/resolver/PackageEntryPlugin.js +23 -36
  48. package/lib/runtime/base.styl +5 -0
  49. package/lib/runtime/components/tenon/getInnerListeners.js +317 -0
  50. package/lib/runtime/components/tenon/tenon-button.vue +305 -0
  51. package/lib/runtime/components/tenon/tenon-image.vue +61 -0
  52. package/lib/runtime/components/tenon/tenon-input.vue +99 -0
  53. package/lib/runtime/components/tenon/tenon-rich-text.vue +21 -0
  54. package/lib/runtime/components/tenon/tenon-scroll-view.vue +124 -0
  55. package/lib/runtime/components/tenon/tenon-switch.vue +91 -0
  56. package/lib/runtime/components/tenon/tenon-text-area.vue +64 -0
  57. package/lib/runtime/components/tenon/tenon-text.vue +64 -0
  58. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  59. package/lib/runtime/components/tenon/util.js +44 -0
  60. package/lib/runtime/components/web/getInnerListeners.js +1 -3
  61. package/lib/runtime/components/web/mpx-image.vue +20 -5
  62. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  63. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  64. package/lib/runtime/i18n.wxs +31 -11
  65. package/lib/runtime/optionProcessor.js +48 -3
  66. package/lib/runtime/optionProcessor.tenon.js +386 -0
  67. package/lib/selector.js +29 -10
  68. package/lib/style-compiler/index.js +16 -24
  69. package/lib/style-compiler/load-postcss-config.js +3 -1
  70. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  71. package/lib/style-compiler/plugins/hm.js +20 -0
  72. package/lib/style-compiler/plugins/rpx.js +43 -37
  73. package/lib/style-compiler/plugins/scope-id.js +79 -72
  74. package/lib/style-compiler/plugins/trans-special.js +25 -18
  75. package/lib/style-compiler/plugins/trim.js +13 -7
  76. package/lib/style-compiler/plugins/vw.js +22 -16
  77. package/lib/template-compiler/compiler.js +106 -199
  78. package/lib/template-compiler/index.js +52 -139
  79. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  80. package/lib/tenon/index.js +105 -0
  81. package/lib/tenon/processJSON.js +356 -0
  82. package/lib/tenon/processScript.js +261 -0
  83. package/lib/tenon/processStyles.js +21 -0
  84. package/lib/tenon/processTemplate.js +133 -0
  85. package/lib/url-loader.js +11 -29
  86. package/lib/utils/add-query.js +1 -1
  87. package/lib/utils/const.js +10 -0
  88. package/lib/utils/emit-file.js +10 -0
  89. package/lib/utils/eval-json-js.js +31 -0
  90. package/lib/utils/get-entry-name.js +13 -0
  91. package/lib/utils/get-json-content.js +42 -0
  92. package/lib/utils/get-relative-path.js +25 -0
  93. package/lib/utils/is-url-request.js +10 -1
  94. package/lib/utils/match-condition.js +4 -1
  95. package/lib/utils/normalize.js +4 -15
  96. package/lib/utils/parse-request.js +3 -3
  97. package/lib/utils/resolve.js +13 -0
  98. package/lib/utils/set.js +47 -0
  99. package/lib/utils/stringify-loaders-resource.js +25 -0
  100. package/lib/utils/stringify-query.js +4 -0
  101. package/lib/web/processJSON.js +113 -144
  102. package/lib/web/processScript.js +47 -34
  103. package/lib/web/processTemplate.js +57 -40
  104. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  105. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  106. package/lib/wxs/WxsParserPlugin.js +2 -2
  107. package/lib/wxs/WxsPlugin.js +4 -8
  108. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  109. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  110. package/lib/wxs/loader.js +142 -0
  111. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  112. package/lib/wxss/loader.js +31 -43
  113. package/lib/wxss/localsLoader.js +1 -5
  114. package/lib/wxss/processCss.js +107 -103
  115. package/package.json +21 -18
  116. package/lib/built-in-loader.js +0 -49
  117. package/lib/content-loader.js +0 -13
  118. package/lib/dependency/ChildCompileDependency.js +0 -24
  119. package/lib/dependency/InjectDependency.js +0 -26
  120. package/lib/dependency/RemovedModuleDependency.js +0 -23
  121. package/lib/dependency/ResolveDependency.js +0 -49
  122. package/lib/path-loader.js +0 -3
  123. package/lib/plugin-loader.js +0 -287
  124. package/lib/staticConfig.js +0 -4
  125. package/lib/utils/get-main-compilation.js +0 -6
  126. package/lib/utils/read-json-for-src.js +0 -34
  127. package/lib/utils/try-require.js +0 -16
  128. package/lib/wxs/wxs-loader.js +0 -117
  129. package/lib/wxss/getImportPrefix.js +0 -30
@@ -0,0 +1,133 @@
1
+ const templateCompiler = require('../template-compiler/compiler')
2
+ const genComponentTag = require('../utils/gen-component-tag')
3
+ const addQuery = require('../utils/add-query')
4
+ const path = require('path')
5
+ const parseRequest = require('../utils/parse-request')
6
+ // const getMainCompilation = require('../utils/get-main-compilation')
7
+
8
+ // function calculateRootEleChild (arr) {
9
+ // if (!arr) {
10
+ // return 0
11
+ // }
12
+ // return arr.reduce((total, item) => {
13
+ // if (item.type === 1) {
14
+ // if (item.tag === 'template') {
15
+ // total += calculateRootEleChild(item.children)
16
+ // } else {
17
+ // total += 1
18
+ // }
19
+ // }
20
+ // return total
21
+ // }, 0)
22
+ // }
23
+
24
+ module.exports = function (template, options, callback) {
25
+ const mode = options.mode
26
+ const srcMode = options.srcMode
27
+ const defs = options.defs
28
+ const moduleId = options.moduleId
29
+ const loaderContext = options.loaderContext
30
+ const ctorType = options.ctorType
31
+ const resourcePath = parseRequest(loaderContext.resource).resourcePath
32
+ const builtInComponentsMap = {}
33
+ // const compilation = loaderContext._compilation
34
+ // const mainCompilation = getMainCompilation(compilation)
35
+ // const mpx = mainCompilation.__mpx__
36
+ // const wxsContentMap = mpx.wxsContentMap
37
+ let wxsModuleMap, genericsInfo
38
+ let output = '/* template */\n'
39
+
40
+ if (ctorType === 'app') {
41
+ template = {
42
+ tag: 'template',
43
+ content: '<div class="app">this is app</div>'
44
+ }
45
+ // builtInComponentsMap['mpx-keep-alive'] = {
46
+ // resource: addQuery('@mpxjs/webpack-plugin/lib/runtime/components/web/mpx-keep-alive.vue', { component: true })
47
+ // }
48
+ }
49
+
50
+ if (template) {
51
+ // 由于远端src template资源引用的相对路径可能发生变化,暂时不支持。
52
+ if (template.src) {
53
+ return callback(new Error('[mpx loader][' + loaderContext.resource + ']: ' + 'template content must be inline in .mpx files!'))
54
+ }
55
+ if (template.lang) {
56
+ return callback(new Error('[mpx loader][' + loaderContext.resource + ']: ' + 'template lang is not supported in trans web mode temporarily, we will support it in the future!'))
57
+ }
58
+
59
+ output += genComponentTag(template, (template) => {
60
+ if (ctorType === 'app') {
61
+ return template.content
62
+ }
63
+ if (template.content) {
64
+ const templateSrcMode = template.mode || srcMode
65
+ const parsed = templateCompiler.parse(template.content, {
66
+ warn: (msg) => {
67
+ loaderContext.emitWarning(
68
+ new Error('[template compiler][' + loaderContext.resource + ']: ' + msg)
69
+ )
70
+ },
71
+ error: (msg) => {
72
+ loaderContext.emitError(
73
+ new Error('[template compiler][' + loaderContext.resource + ']: ' + msg)
74
+ )
75
+ },
76
+ usingComponents: options.usingComponents,
77
+ hasComment: options.hasComment,
78
+ isNative: options.isNative,
79
+ basename: path.basename(resourcePath),
80
+ isComponent: ctorType === 'component',
81
+ mode,
82
+ srcMode: templateSrcMode,
83
+ defs,
84
+ decodeHTMLText: options.decodeHTMLText,
85
+ // externalClasses: options.externalClasses,
86
+ hasScoped: false,
87
+ moduleId,
88
+ filePath: loaderContext.resourcePath,
89
+ i18n: null,
90
+ checkUsingComponents: options.checkUsingComponents,
91
+ // web模式下全局组件不会被合入usingComponents中,故globalComponents可以传空
92
+ globalComponents: [],
93
+ // web模式下实现抽象组件
94
+ componentGenerics: options.componentGenerics
95
+ })
96
+ // if (parsed.meta.wxsModuleMap) {
97
+ // wxsModuleMap = parsed.meta.wxsModuleMap
98
+ // }
99
+ // if (parsed.meta.wxsContentMap) {
100
+ // for (let module in parsed.meta.wxsContentMap) {
101
+ // wxsContentMap[`${resourcePath}~${module}`] = parsed.meta.wxsContentMap[module]
102
+ // }
103
+ // }
104
+ if (parsed.meta.builtInComponentsMap) {
105
+ Object.keys(parsed.meta.builtInComponentsMap).forEach((name) => {
106
+ builtInComponentsMap[name] = {
107
+ resource: addQuery(parsed.meta.builtInComponentsMap[name], { component: true })
108
+ }
109
+ })
110
+ }
111
+ // if (parsed.meta.genericsInfo) {
112
+ // genericsInfo = parsed.meta.genericsInfo
113
+ // }
114
+ // 输出H5有多个root element时, 使用div标签包裹
115
+ // if (parsed.root.tag === 'temp-node') {
116
+ // const childLen = calculateRootEleChild(parsed.root.children)
117
+ // if (childLen >= 2) {
118
+ // parsed.root.tag = 'div'
119
+ // }
120
+ // }
121
+ return templateCompiler.serialize(parsed.root)
122
+ }
123
+ })
124
+ output += '\n\n'
125
+ }
126
+
127
+ callback(null, {
128
+ output,
129
+ builtInComponentsMap,
130
+ genericsInfo,
131
+ wxsModuleMap
132
+ })
133
+ }
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
+ }
@@ -0,0 +1,31 @@
1
+ const path = require('path')
2
+
3
+ module.exports = function evalJSONJS (source, filename, loaderContext) {
4
+ if (!loaderContext._compiler) return {}
5
+ const fs = loaderContext._compiler.inputFileSystem
6
+ const defs = loaderContext.getMpx().defs
7
+ const defKeys = Object.keys(defs)
8
+ const defValues = defKeys.map((key) => {
9
+ return defs[key]
10
+ })
11
+ const dirname = path.dirname(filename)
12
+ // eslint-disable-next-line no-new-func
13
+ const func = new Function('module', 'exports', 'require', '__filename', '__dirname', ...defKeys, source)
14
+ const module = {
15
+ exports: {}
16
+ }
17
+ // 此处采用readFileSync+evalJSONJS而不直接使用require获取依赖内容有两个原因:
18
+ // 1. 支持依赖中正常访问defs变量
19
+ // 2. 避免对应的依赖文件被作为buildDependencies
20
+ func(module, module.exports, function (request) {
21
+ if (request.startsWith('.')) {
22
+ request = path.join(dirname, request)
23
+ }
24
+ const filename = require.resolve(request)
25
+ loaderContext.addDependency(filename)
26
+ const source = fs.readFileSync(filename).toString('utf-8')
27
+ return evalJSONJS(source, filename, loaderContext)
28
+ }, filename, dirname, ...defValues)
29
+
30
+ return module.exports
31
+ }
@@ -0,0 +1,13 @@
1
+ module.exports = function (loaderContext) {
2
+ if (!loaderContext._compilation) return ''
3
+ const moduleGraph = loaderContext._compilation.moduleGraph
4
+ let entryName = ''
5
+ for (const [name, { dependencies }] of loaderContext._compilation.entries) {
6
+ const entryModule = moduleGraph.getModule(dependencies[0])
7
+ if (entryModule.resource === loaderContext.resource) {
8
+ entryName = name
9
+ break
10
+ }
11
+ }
12
+ return entryName
13
+ }
@@ -0,0 +1,42 @@
1
+ const parseRequest = require('./parse-request')
2
+ const evalJSONJS = require('./eval-json-js')
3
+ const resolve = require('./resolve')
4
+ const async = require('async')
5
+ const { JSON_JS_EXT } = require('./const')
6
+
7
+ module.exports = function getJSONContent (json, loaderContext, callback) {
8
+ if (!loaderContext._compiler) return callback(null, '{}')
9
+ const fs = loaderContext._compiler.inputFileSystem
10
+ async.waterfall([
11
+ (callback) => {
12
+ if (json.src) {
13
+ resolve(loaderContext.context, json.src, loaderContext, (err, result) => {
14
+ if (err) return callback(err)
15
+ const { rawResourcePath: resourcePath } = parseRequest(result)
16
+ fs.readFile(resourcePath, (err, content) => {
17
+ if (err) return callback(err)
18
+ callback(null, {
19
+ content: content.toString('utf-8'),
20
+ useJSONJS: json.useJSONJS || resourcePath.endsWith(JSON_JS_EXT),
21
+ filename: resourcePath
22
+
23
+ })
24
+ })
25
+ })
26
+ } else {
27
+ callback(null, {
28
+ content: json.content,
29
+ useJSONJS: json.useJSONJS,
30
+ filename: loaderContext.resourcePath
31
+ })
32
+ }
33
+ },
34
+ ({ content, useJSONJS, filename }, callback) => {
35
+ if (!content) return callback(null, '{}')
36
+ if (useJSONJS) {
37
+ content = JSON.stringify(evalJSONJS(content, filename, loaderContext))
38
+ }
39
+ callback(null, content)
40
+ }
41
+ ], callback)
42
+ }
@@ -0,0 +1,25 @@
1
+ function getRelativePath (source, target) {
2
+ // make sure source and target are absolute path
3
+ /^\//.test(source) || (source = '/' + source);
4
+ /^\//.test(target) || (target = '/' + target)
5
+
6
+ source = source && source.replace(/\/[^/]*$/, '') // get dirname
7
+ // check if source or target is root path
8
+ let sourceArr = source.split('/').filter((item, index) => index !== 0 && !!item)
9
+ let targetArr = target.split('/').filter((item, index) => index !== 0 && !!item)
10
+ let i = 0
11
+ while (sourceArr[i] === targetArr[i] && i < sourceArr.length && i < targetArr.length) {
12
+ i++
13
+ }
14
+ let relativePath = ''
15
+ for (let j = 0; j < sourceArr.length - i; j++) {
16
+ relativePath += '../'
17
+ }
18
+ relativePath += targetArr.slice(i).join('/')
19
+ if (relativePath[0] !== '.') relativePath = './' + relativePath
20
+ return relativePath
21
+ }
22
+
23
+ module.exports = {
24
+ getRelativePath
25
+ }
@@ -1,7 +1,7 @@
1
1
  const isUrlRequestRaw = require('loader-utils').isUrlRequest
2
2
  const tagRE = /\{\{((?:.|\n|\r)+?)\}\}(?!})/
3
3
 
4
- module.exports = function isUrlRequest (url, root) {
4
+ module.exports = function isUrlRequest (url, root, externals) {
5
5
  // 对于非字符串或空字符串url直接返回false
6
6
  if (!url || typeof url !== 'string') return false
7
7
  // 对于@开头且后续字符串为合法标识符的情况也返回false,识别为theme变量
@@ -9,5 +9,14 @@ module.exports = function isUrlRequest (url, root) {
9
9
  if (/^.+:\/\//.test(url)) return false
10
10
  // 对于url中存在Mustache插值的情况也返回false
11
11
  if (tagRE.test(url)) return false
12
+ // url存在于externals中也返回false
13
+ if (externals && externals.some((external) => {
14
+ if (typeof external === 'string') {
15
+ return external === url
16
+ } else if (external instanceof RegExp) {
17
+ return external.test(url)
18
+ }
19
+ return false
20
+ })) return false
12
21
  return isUrlRequestRaw(url, root)
13
22
  }
@@ -45,4 +45,7 @@ const matchCondition = (resourcePath, condition = {}) => {
45
45
  return matched
46
46
  }
47
47
 
48
- module.exports = matchCondition
48
+ module.exports = {
49
+ matchCondition,
50
+ normalizeCondition
51
+ }
@@ -1,16 +1,5 @@
1
- const fs = require('fs')
2
- const path = require('path')
1
+ // const path = require('path')
3
2
 
4
- exports.lib = file => path.resolve(__dirname, '../', file)
5
-
6
- exports.dep = dep => {
7
- if (
8
- fs.existsSync(path.resolve(__dirname, '../../node_modules', dep))
9
- ) {
10
- // npm 2 or npm linked
11
- return '@mpxjs/webpack-plugin/node_modules/' + dep
12
- } else {
13
- // npm 3
14
- return dep
15
- }
16
- }
3
+ // exports.lib = file => path.resolve(__dirname, '../', file)
4
+ // support npm link debug
5
+ exports.lib = file => '@mpxjs/webpack-plugin/lib/' + file
@@ -4,7 +4,7 @@ const path = require('path')
4
4
 
5
5
  function genQueryObj (result) {
6
6
  // 避免外部修改queryObj影响缓存
7
- result.queryObj = parseQuery(result.resourceQuery)
7
+ result.queryObj = parseQuery(result.resourceQuery || '?')
8
8
  return result
9
9
  }
10
10
 
@@ -16,13 +16,13 @@ module.exports = function parseRequest (request) {
16
16
  let resource = elements.pop()
17
17
  let loaderString = elements.join('!')
18
18
  let resourcePath = resource
19
- let resourceQuery = '?'
19
+ let resourceQuery = ''
20
20
  const queryIndex = resource.indexOf('?')
21
21
  if (queryIndex >= 0) {
22
22
  resourcePath = resource.slice(0, queryIndex)
23
23
  resourceQuery = resource.slice(queryIndex)
24
24
  }
25
- const queryObj = parseQuery(resourceQuery)
25
+ const queryObj = parseQuery(resourceQuery || '?')
26
26
  const rawResourcePath = resourcePath
27
27
  if (queryObj.resourcePath) {
28
28
  resourcePath = queryObj.resourcePath
@@ -0,0 +1,13 @@
1
+ const parseRequest = require('./parse-request')
2
+ const { RESOLVE_IGNORED_ERR } = require('./const')
3
+
4
+ // todo 提供不记录dependency的resolve方法,非必要的情况下不记录dependency,提升缓存利用率
5
+ module.exports = (context, request, loaderContext, callback) => {
6
+ const { queryObj } = parseRequest(request)
7
+ context = queryObj.context || context
8
+ return loaderContext.resolve(context, request, (err, resource, info) => {
9
+ if (err) return callback(err)
10
+ if (resource === false) return callback(RESOLVE_IGNORED_ERR)
11
+ callback(null, resource, info)
12
+ })
13
+ }
@@ -0,0 +1,47 @@
1
+ module.exports = {
2
+ every (set, fn) {
3
+ for (const item of set) {
4
+ if (!fn(item)) return false
5
+ }
6
+ return true
7
+ },
8
+ has (set, fn) {
9
+ for (const item of set) {
10
+ if (fn(item)) return true
11
+ }
12
+ return false
13
+ },
14
+ map (set, fn) {
15
+ const result = new Set()
16
+ set.forEach((item) => {
17
+ result.add(fn(item))
18
+ })
19
+ return result
20
+ },
21
+ filter (set, fn) {
22
+ const result = new Set()
23
+ set.forEach((item) => {
24
+ if (fn(item)) {
25
+ result.add(item)
26
+ }
27
+ })
28
+ return result
29
+ },
30
+ concat (setA, setB) {
31
+ const result = new Set()
32
+ setA.forEach((item) => {
33
+ result.add(item)
34
+ })
35
+ setB.forEach((item) => {
36
+ result.add(item)
37
+ })
38
+ return result
39
+ },
40
+ mapToArr (set, fn) {
41
+ const result = []
42
+ set.forEach((item) => {
43
+ result.push(fn(item))
44
+ })
45
+ return result
46
+ }
47
+ }
@@ -0,0 +1,25 @@
1
+ const loaderToIdent = data => {
2
+ if (!data.options) {
3
+ return data.loader
4
+ }
5
+ if (typeof data.options === 'string') {
6
+ return data.loader + '?' + data.options
7
+ }
8
+ if (typeof data.options !== 'object') {
9
+ throw new Error('loader options must be string or object')
10
+ }
11
+ if (data.ident) {
12
+ return data.loader + '??' + data.ident
13
+ }
14
+ return data.loader + '?' + JSON.stringify(data.options)
15
+ }
16
+
17
+ const stringifyLoadersAndResource = (loaders, resource) => {
18
+ let str = ''
19
+ for (const loader of loaders) {
20
+ str += loaderToIdent(loader) + '!'
21
+ }
22
+ return str + resource
23
+ }
24
+
25
+ module.exports = stringifyLoadersAndResource
@@ -16,6 +16,10 @@ function stringifyQuery (obj, useJSON) {
16
16
  return
17
17
  }
18
18
 
19
+ if (val === true) {
20
+ return key
21
+ }
22
+
19
23
  if (Array.isArray(val)) {
20
24
  const key2 = `${key}[]`
21
25
  const result = []