@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
@@ -1,49 +0,0 @@
1
- const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
- const parseRequest = require('../utils/parse-request')
3
-
4
- class ResolveDependency extends NullDependency {
5
- constructor (resource, packageName, pagesMap, componentsMap, staticResourcesMap, publicPath, range, issuerResource, compilation) {
6
- super()
7
- this.resource = resource
8
- this.packageName = packageName
9
- this.pagesMap = pagesMap
10
- this.componentsMap = componentsMap
11
- this.staticResourcesMap = staticResourcesMap
12
- this.publicPath = publicPath
13
- this.range = range
14
- this.issuerResource = issuerResource
15
- this.compilation = compilation
16
- }
17
-
18
- get type () {
19
- return 'mpx resolve'
20
- }
21
-
22
- updateHash (hash) {
23
- super.updateHash(hash)
24
- hash.update(this.resource)
25
- }
26
- }
27
-
28
- ResolveDependency.Template = class ResolveDependencyTemplate {
29
- apply (dep, source) {
30
- const content = this.getContent(dep)
31
- source.replace(dep.range[0], dep.range[1] - 1, content)
32
- }
33
-
34
- getContent (dep) {
35
- const resourcePath = parseRequest(dep.resource).resourcePath
36
- const pagesMap = dep.pagesMap
37
- const componentsMap = dep.componentsMap[dep.packageName]
38
- const mainComponentsMap = dep.componentsMap.main
39
- const staticResourcesMap = dep.staticResourcesMap[dep.packageName]
40
- const mainStaticResourcesMap = dep.staticResourcesMap.main
41
- const resolved = pagesMap[resourcePath] || componentsMap[resourcePath] || mainComponentsMap[resourcePath] || staticResourcesMap[resourcePath] || mainStaticResourcesMap[resourcePath] || ''
42
- if (!resolved) {
43
- dep.compilation.errors.push(new Error(`Path ${dep.resource} is not a page/component/static resource, which is resolved from ${dep.issuerResource}!`))
44
- }
45
- return JSON.stringify(dep.publicPath + resolved)
46
- }
47
- }
48
-
49
- module.exports = ResolveDependency
@@ -1,287 +0,0 @@
1
- const path = require('path')
2
- const async = require('async')
3
- const JSON5 = require('json5')
4
- const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
5
- const parseRequest = require('./utils/parse-request')
6
- const toPosix = require('./utils/to-posix')
7
- const getMainCompilation = require('./utils/get-main-compilation')
8
- const isUrlRequestRaw = require('./utils/is-url-request')
9
- const loaderUtils = require('loader-utils')
10
- const normalize = require('./utils/normalize')
11
- const addQuery = require('./utils/add-query')
12
- const nativeLoaderPath = normalize.lib('native-loader')
13
-
14
- // webpack4中.json文件会走json parser,抽取内容的占位内容必须为合法json,否则会在parse阶段报错
15
- const defaultResultSource = '{}'
16
-
17
- // ali不导出的插件页面的固定prefix
18
- const NO_EXPORT = '__mpx_plugin_no_export_page__'
19
-
20
- module.exports = function (source) {
21
- // 该loader中会在每次编译中动态添加entry,不能缓存,否则watch不好使
22
- this.cacheable(false)
23
-
24
- const nativeCallback = this.async()
25
- const mainCompilation = getMainCompilation(this._compilation)
26
- const mpx = mainCompilation.__mpx__
27
-
28
- mpx.isPluginMode = true
29
-
30
- const isUrlRequest = r => isUrlRequestRaw(r, projectRoot)
31
- const urlToRequest = r => loaderUtils.urlToRequest(r, projectRoot)
32
- const resolve = (context, request, callback) => {
33
- const { queryObj } = parseRequest(request)
34
- context = queryObj.context || context
35
- return this.resolve(context, request, callback)
36
- }
37
-
38
- const emitWarning = (msg) => {
39
- this.emitWarning(
40
- new Error('[plugin loader][' + this.resource + ']: ' + msg)
41
- )
42
- }
43
-
44
- const emitError = (msg) => {
45
- this.emitError(
46
- new Error('[plugin loader][' + this.resource + ']: ' + msg)
47
- )
48
- }
49
-
50
- if (!mpx) {
51
- return nativeCallback(null, source)
52
- }
53
-
54
- const context = this.context
55
- const packageName = 'main'
56
- const pagesMap = mpx.pagesMap
57
- const componentsMap = mpx.componentsMap[packageName]
58
- const getEntryNode = mpx.getEntryNode
59
- const resolveMode = mpx.resolveMode
60
- const projectRoot = mpx.projectRoot
61
- const extract = mpx.extract
62
- const pathHash = mpx.pathHash
63
- const resourceName = this._compilation._preparedEntrypoints[0].name
64
-
65
- const entryModule = this._module
66
- // 通过rawRequest关联entryNode和entryModule
67
- const entryRequest = entryModule.rawRequest
68
- const entryType = 'Plugin'
69
- const currentEntry = getEntryNode(entryRequest, entryType, entryModule)
70
- // 最终输出中不需要为plugin.json产生chunk,而是使用extract直接输出json文件,删除plugin.json对应的entrypoint
71
- this._compilation._preparedEntrypoints.pop()
72
- // 为了在体积统计中能够统计到该entry,将其缓存在mpx.removedChunks中
73
- mpx.removedChunks.push({
74
- entryModule
75
- })
76
-
77
- let entryDeps = new Set()
78
-
79
- let cacheCallback
80
-
81
- const checkEntryDeps = (callback) => {
82
- callback = callback || cacheCallback
83
- if (callback && entryDeps.size === 0) {
84
- callback()
85
- } else {
86
- cacheCallback = callback
87
- }
88
- }
89
-
90
- const addEntrySafely = (resource, name, callback) => {
91
- const dep = SingleEntryPlugin.createDependency(resource, name)
92
- entryDeps.add(dep)
93
- this._compilation.addEntry(this._compiler.context, dep, name, (err, module) => {
94
- entryDeps.delete(dep)
95
- checkEntryDeps()
96
- callback(err, module)
97
- })
98
- }
99
-
100
- // 初次处理json
101
- const callback = (err) => {
102
- checkEntryDeps(() => {
103
- if (err) return nativeCallback(err)
104
- if (mpx.mode === 'ali' && pluginEntry.pages) {
105
- let publicPages = Object.keys(pluginEntry.pages).reduce((cur, key) => {
106
- if (!key.startsWith(NO_EXPORT)) {
107
- cur[key] = pluginEntry.pages[key]
108
- }
109
- return cur
110
- }, {})
111
- pluginEntry.publicPages = publicPages
112
- pluginEntry.pages = Object.values(pluginEntry.pages)
113
- }
114
- const sideEffects = []
115
- const file = resourceName + '.json'
116
- sideEffects.push((additionalAssets) => {
117
- additionalAssets[file].modules = additionalAssets[file].modules || []
118
- additionalAssets[file].modules.push(entryModule)
119
- })
120
- extract(JSON.stringify(pluginEntry), file, 0, sideEffects)
121
- nativeCallback(null, defaultResultSource)
122
- })
123
- }
124
-
125
- let pluginEntry
126
- try {
127
- pluginEntry = JSON5.parse(source)
128
- } catch (err) {
129
- return callback(err)
130
- }
131
-
132
- let processMain, processComponents, processPages
133
-
134
- processMain = processComponents = processPages = (callback) => {
135
- callback()
136
- }
137
-
138
- if (pluginEntry.main) {
139
- processMain = function (main, callback) {
140
- if (!isUrlRequest(main)) return callback()
141
- if (resolveMode === 'native') {
142
- main = urlToRequest(main)
143
- }
144
- resolve(context, main, (err, resource) => {
145
- if (err) return callback(err)
146
- const { resourcePath } = parseRequest(resource)
147
- // 获取pageName
148
- const relative = path.relative(context, resourcePath)
149
- const mainPath = toPosix(/^(.*?)(\.[^.]*)?$/.exec(relative)[1])
150
- if (/^\./.test(mainPath)) {
151
- emitError(`The plugin's main path [${main}] must be in the context [${context}]!`)
152
- return callback()
153
- }
154
- pluginEntry.main = mainPath + '.js'
155
- addEntrySafely(resource, mainPath, (err, module) => {
156
- if (err) return callback(err)
157
- mpx.pluginMainModule = module
158
- currentEntry.addChild(getEntryNode(resource, 'PluginMain', module))
159
- callback(err, module)
160
- })
161
- })
162
- }.bind(this, pluginEntry.main)
163
- }
164
-
165
- if (pluginEntry.publicComponents) {
166
- processComponents = function (components, callback) {
167
- async.forEachOf(components, (component, name, callback) => {
168
- if (!isUrlRequest(component)) return callback()
169
- if (resolveMode === 'native') {
170
- component = urlToRequest(component)
171
- }
172
- resolve(context, component, (err, resource, info) => {
173
- if (err) return callback(err)
174
- const resourcePath = parseRequest(resource).resourcePath
175
- const parsed = path.parse(resourcePath)
176
- const ext = parsed.ext
177
- let outputPath
178
- if (ext === '.js') {
179
- let root = info.descriptionFileRoot
180
- let name = 'nativeComponent'
181
- if (info.descriptionFileData) {
182
- if (info.descriptionFileData.miniprogram) {
183
- root = path.join(root, info.descriptionFileData.miniprogram)
184
- }
185
- if (info.descriptionFileData.name) {
186
- // 去掉name里面的@符号,因为支付宝不支持文件路径上有@
187
- name = info.descriptionFileData.name.split('@').join('')
188
- }
189
- }
190
- const resourceName = path.join(parsed.dir, parsed.name)
191
- let relativePath = path.relative(root, resourceName)
192
- outputPath = path.join('components', name + pathHash(root), relativePath)
193
- } else {
194
- let componentName = parsed.name
195
- outputPath = path.join('components', componentName + pathHash(resourcePath), componentName)
196
- }
197
- const componentPath = toPosix(outputPath)
198
- pluginEntry.publicComponents[name] = componentPath
199
- // 与json-compiler处理组件的行为表现一致
200
- resource = addQuery(resource, {
201
- packageName: 'main'
202
- })
203
- if (ext === '.js') {
204
- resource = '!!' + nativeLoaderPath + '!' + resource
205
- }
206
- currentEntry.addChild(getEntryNode(resource, 'Component'))
207
- // 如果之前已经创建了入口,直接return
208
- if (componentsMap[resourcePath] === componentPath) return callback()
209
- componentsMap[resourcePath] = componentPath
210
- addEntrySafely(resource, componentPath, callback)
211
- })
212
- }, callback)
213
- }.bind(this, pluginEntry.publicComponents)
214
- }
215
-
216
- if (pluginEntry.pages) {
217
- // 处理 pages
218
- if (mpx.srcMode === 'ali') { // 处理ali
219
- let pagesMap = pluginEntry.publicPages
220
- pluginEntry.pages = pluginEntry.pages.reduce((cur, val, index) => {
221
- if (!Object.values(pagesMap).includes(val)) { // no export page
222
- cur[`${NO_EXPORT}${index}`] = val
223
- }
224
- return cur
225
- }, pagesMap)
226
- }
227
- processPages = function (pages, callback) {
228
- async.forEachOf(pages, (page, name, callback) => {
229
- let aliasPath = ''
230
- if (typeof page !== 'string') {
231
- aliasPath = page.path
232
- page = page.src
233
- }
234
- if (!isUrlRequest(page)) return callback()
235
- if (resolveMode === 'native') {
236
- page = urlToRequest(page)
237
- }
238
- resolve(context, page, (err, resource) => {
239
- if (err) return callback(err)
240
- const { resourcePath } = parseRequest(resource)
241
- const ext = path.extname(resourcePath)
242
- // 获取pageName
243
- let pageName
244
- if (aliasPath) {
245
- pageName = toPosix(aliasPath)
246
- // 判断 key 存在重复情况直接报错
247
- for (let key in pagesMap) {
248
- if (pagesMap[key] === pageName && key !== resourcePath) {
249
- emitError(`Current page [${resourcePath}] registers a conflict page path [${pageName}] with existed page [${key}], which is not allowed, please rename it!`)
250
- return callback()
251
- }
252
- }
253
- } else {
254
- const relative = path.relative(context, resourcePath)
255
- pageName = toPosix(/^(.*?)(\.[^.]*)?$/.exec(relative)[1])
256
- if (/^\./.test(pageName)) {
257
- // 如果当前page不存在于context中,插件模式下报错
258
- emitError(`Current page [${resourcePath}] is not in current pages directory [${context}], which is not allowed in plugin mode!`)
259
- return callback()
260
- }
261
- // 如果当前page与已有page存在命名冲突,插件模式下报错
262
- for (let key in pagesMap) {
263
- if (pagesMap[key] === pageName && key !== resourcePath) {
264
- emitError(`Current page [${resourcePath}] is registered with a conflict page path [${pageName}], which is already existed in system, which is not allowed in plugin mode!`)
265
- return callback()
266
- }
267
- }
268
- }
269
- if (ext === '.js') {
270
- resource = '!!' + nativeLoaderPath + '!' + resource
271
- }
272
- // 如果之前已经创建了入口,直接return
273
- if (pagesMap[resourcePath]) {
274
- emitWarning(`Current page [${resourcePath}] which is imported from [${this.resourcePath}] has been registered in pagesMap already, it will be ignored, please check it and remove the redundant page declaration!`)
275
- return callback()
276
- }
277
- currentEntry.addChild(getEntryNode(resource, 'Page'))
278
- pagesMap[resourcePath] = pageName
279
- pluginEntry.pages[name] = pageName
280
- addEntrySafely(resource, pageName, callback)
281
- })
282
- }, callback)
283
- }.bind(this, pluginEntry.pages)
284
- }
285
-
286
- async.parallel([processMain, processComponents, processPages], callback)
287
- }
@@ -1,16 +0,0 @@
1
- const cwd = process.cwd()
2
- const resolve = require('resolve')
3
-
4
- module.exports = function tryRequire (dep) {
5
- let fromCwd
6
- try {
7
- fromCwd = resolve.sync(dep, { basedir: cwd })
8
- } catch (e) {}
9
- if (fromCwd) {
10
- return require(fromCwd)
11
- } else {
12
- try {
13
- return require(dep)
14
- } catch (e) {}
15
- }
16
- }
@@ -1,30 +0,0 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- Modified by @hiyuki
5
- */
6
- const normalize = require('../utils/normalize')
7
- const extractorPath = normalize.lib('extractor')
8
- const toPosix = require('../utils/to-posix')
9
-
10
- module.exports = function getImportPrefix (loaderContext, extract) {
11
- const selectorIndex = loaderContext.loaders.findIndex(({ path }) => {
12
- // 兼容windows路径
13
- return toPosix(path).indexOf('@mpxjs/webpack-plugin/lib/selector') !== -1
14
- })
15
- let loadersRequest = loaderContext.loaders.slice(
16
- loaderContext.loaderIndex,
17
- selectorIndex !== -1 ? selectorIndex : undefined
18
- ).map(function (x) {
19
- return x.request
20
- }).join('!')
21
- if (extract) {
22
- loadersRequest = extractorPath + '?' +
23
- JSON.stringify({
24
- type: 'styles',
25
- index: -1,
26
- fromImport: true
27
- }) + '!' + loadersRequest
28
- }
29
- return '-!' + loadersRequest + '!'
30
- }