@mpxjs/webpack-plugin 2.6.110 → 2.7.0-beta.10

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 (72) hide show
  1. package/lib/dependencies/AppEntryDependency.js +56 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +69 -0
  3. package/lib/dependencies/DynamicEntryDependency.js +131 -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/RecordResourceMapDependency.js +52 -0
  8. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  9. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  10. package/lib/dependencies/ResolveDependency.js +84 -0
  11. package/lib/extractor.js +73 -178
  12. package/lib/file-loader.js +7 -19
  13. package/lib/helpers.js +42 -330
  14. package/lib/index.js +497 -376
  15. package/lib/json-compiler/helper.js +148 -0
  16. package/lib/json-compiler/index.js +195 -439
  17. package/lib/json-compiler/plugin.js +150 -0
  18. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  19. package/lib/loader.js +107 -226
  20. package/lib/native-loader.js +65 -132
  21. package/lib/parser.js +1 -2
  22. package/lib/record-loader.js +11 -0
  23. package/lib/{path-loader.js → resolve-loader.js} +0 -0
  24. package/lib/resolver/AddEnvPlugin.js +3 -2
  25. package/lib/resolver/AddModePlugin.js +3 -2
  26. package/lib/runtime/base.styl +5 -0
  27. package/lib/runtime/i18n.wxs +3 -3
  28. package/lib/runtime/optionProcessor.js +3 -3
  29. package/lib/selector.js +9 -11
  30. package/lib/style-compiler/index.js +12 -19
  31. package/lib/template-compiler/compiler.js +21 -162
  32. package/lib/template-compiler/index.js +47 -136
  33. package/lib/url-loader.js +11 -29
  34. package/lib/utils/add-query.js +1 -1
  35. package/lib/utils/const.js +9 -0
  36. package/lib/utils/emit-file.js +10 -0
  37. package/lib/utils/eval-json-js.js +31 -0
  38. package/lib/utils/get-entry-name.js +13 -0
  39. package/lib/utils/get-json-content.js +41 -0
  40. package/lib/utils/is-url-request.js +10 -1
  41. package/lib/utils/normalize.js +0 -13
  42. package/lib/utils/parse-request.js +3 -3
  43. package/lib/utils/resolve.js +13 -0
  44. package/lib/utils/set.js +47 -0
  45. package/lib/utils/stringify-loaders-resource.js +25 -0
  46. package/lib/utils/stringify-query.js +4 -0
  47. package/lib/web/processJSON.js +113 -142
  48. package/lib/web/processScript.js +32 -26
  49. package/lib/web/processTemplate.js +57 -39
  50. package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
  51. package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
  52. package/lib/wxs/WxsParserPlugin.js +2 -2
  53. package/lib/wxs/WxsPlugin.js +4 -8
  54. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  55. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +1 -3
  56. package/lib/wxs/{wxs-loader.js → loader.js} +41 -50
  57. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +3 -4
  58. package/lib/wxss/loader.js +31 -43
  59. package/lib/wxss/localsLoader.js +1 -5
  60. package/package.json +5 -9
  61. package/lib/built-in-loader.js +0 -49
  62. package/lib/content-loader.js +0 -13
  63. package/lib/dependency/ChildCompileDependency.js +0 -24
  64. package/lib/dependency/InjectDependency.js +0 -26
  65. package/lib/dependency/RemovedModuleDependency.js +0 -23
  66. package/lib/dependency/ResolveDependency.js +0 -49
  67. package/lib/plugin-loader.js +0 -287
  68. package/lib/staticConfig.js +0 -4
  69. package/lib/utils/get-main-compilation.js +0 -6
  70. package/lib/utils/read-json-for-src.js +0 -34
  71. package/lib/utils/try-require.js +0 -16
  72. package/lib/wxss/getImportPrefix.js +0 -30
@@ -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 getOutputPath = mpx.getOutputPath
58
- const componentsMap = mpx.componentsMap[packageName]
59
- const getEntryNode = mpx.getEntryNode
60
- const resolveMode = mpx.resolveMode
61
- const projectRoot = mpx.projectRoot
62
- const extract = mpx.extract
63
- const pathHash = mpx.pathHash
64
- const resourceName = this._compilation._preparedEntrypoints[0].name
65
-
66
- const entryModule = this._module
67
- // 通过rawRequest关联entryNode和entryModule
68
- const entryRequest = entryModule.rawRequest
69
- const entryType = 'Plugin'
70
- const currentEntry = getEntryNode(entryRequest, entryType, entryModule)
71
- // 最终输出中不需要为plugin.json产生chunk,而是使用extract直接输出json文件,删除plugin.json对应的entrypoint
72
- this._compilation._preparedEntrypoints.pop()
73
- // 为了在体积统计中能够统计到该entry,将其缓存在mpx.removedChunks中
74
- mpx.removedChunks.push({
75
- entryModule
76
- })
77
-
78
- let entryDeps = new Set()
79
-
80
- let cacheCallback
81
-
82
- const checkEntryDeps = (callback) => {
83
- callback = callback || cacheCallback
84
- if (callback && entryDeps.size === 0) {
85
- callback()
86
- } else {
87
- cacheCallback = callback
88
- }
89
- }
90
-
91
- const addEntrySafely = (resource, name, callback) => {
92
- const dep = SingleEntryPlugin.createDependency(resource, name)
93
- entryDeps.add(dep)
94
- this._compilation.addEntry(this._compiler.context, dep, name, (err, module) => {
95
- entryDeps.delete(dep)
96
- checkEntryDeps()
97
- callback(err, module)
98
- })
99
- }
100
-
101
- // 初次处理json
102
- const callback = (err) => {
103
- checkEntryDeps(() => {
104
- if (err) return nativeCallback(err)
105
- if (mpx.mode === 'ali' && pluginEntry.pages) {
106
- let publicPages = Object.keys(pluginEntry.pages).reduce((cur, key) => {
107
- if (!key.startsWith(NO_EXPORT)) {
108
- cur[key] = pluginEntry.pages[key]
109
- }
110
- return cur
111
- }, {})
112
- pluginEntry.publicPages = publicPages
113
- pluginEntry.pages = Object.values(pluginEntry.pages)
114
- }
115
- const sideEffects = []
116
- const file = resourceName + '.json'
117
- sideEffects.push((additionalAssets) => {
118
- additionalAssets[file].modules = additionalAssets[file].modules || []
119
- additionalAssets[file].modules.push(entryModule)
120
- })
121
- extract(JSON.stringify(pluginEntry), file, 0, sideEffects)
122
- nativeCallback(null, defaultResultSource)
123
- })
124
- }
125
-
126
- let pluginEntry
127
- try {
128
- pluginEntry = JSON5.parse(source)
129
- } catch (err) {
130
- return callback(err)
131
- }
132
-
133
- let processMain, processComponents, processPages
134
-
135
- processMain = processComponents = processPages = (callback) => {
136
- callback()
137
- }
138
-
139
- if (pluginEntry.main) {
140
- processMain = function (main, callback) {
141
- if (!isUrlRequest(main)) return callback()
142
- if (resolveMode === 'native') {
143
- main = urlToRequest(main)
144
- }
145
- resolve(context, main, (err, resource) => {
146
- if (err) return callback(err)
147
- const { resourcePath } = parseRequest(resource)
148
- // 获取pageName
149
- const relative = path.relative(context, resourcePath)
150
- const mainPath = toPosix(/^(.*?)(\.[^.]*)?$/.exec(relative)[1])
151
- if (/^\./.test(mainPath)) {
152
- emitError(`The plugin's main path [${main}] must be in the context [${context}]!`)
153
- return callback()
154
- }
155
- pluginEntry.main = mainPath + '.js'
156
- addEntrySafely(resource, mainPath, (err, module) => {
157
- if (err) return callback(err)
158
- mpx.pluginMainModule = module
159
- currentEntry.addChild(getEntryNode(resource, 'PluginMain', module))
160
- callback(err, module)
161
- })
162
- })
163
- }.bind(this, pluginEntry.main)
164
- }
165
-
166
- if (pluginEntry.publicComponents) {
167
- processComponents = function (components, callback) {
168
- async.forEachOf(components, (component, name, callback) => {
169
- if (!isUrlRequest(component)) return callback()
170
- if (resolveMode === 'native') {
171
- component = urlToRequest(component)
172
- }
173
- resolve(context, component, (err, resource, info) => {
174
- if (err) return callback(err)
175
- const resourcePath = parseRequest(resource).resourcePath
176
- const parsed = path.parse(resourcePath)
177
- const ext = parsed.ext
178
- let outputPath
179
- if (ext === '.js') {
180
- let root = info.descriptionFileRoot
181
- let name = 'nativeComponent'
182
- if (info.descriptionFileData) {
183
- if (info.descriptionFileData.miniprogram) {
184
- root = path.join(root, info.descriptionFileData.miniprogram)
185
- }
186
- if (info.descriptionFileData.name) {
187
- // 去掉name里面的@符号,因为支付宝不支持文件路径上有@
188
- name = info.descriptionFileData.name.split('@').join('')
189
- }
190
- }
191
- const resourceName = path.join(parsed.dir, parsed.name)
192
- let relativePath = path.relative(root, resourceName)
193
- outputPath = path.join('components', name + pathHash(root), relativePath)
194
- } else {
195
- outputPath = getOutputPath(resourcePath, 'component')
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,4 +0,0 @@
1
- module.exports = {
2
- MPX_ROOT_VIEW: 'mpx-root-view', // 根节点类名
3
- MPX_APP_MODULE_ID: 'mpx-app-scope' // app文件moduleId
4
- }
@@ -1,6 +0,0 @@
1
- module.exports = function (compilation) {
2
- while (compilation.compiler.parentCompilation) {
3
- compilation = compilation.compiler.parentCompilation
4
- }
5
- return compilation
6
- }
@@ -1,34 +0,0 @@
1
- const parseRequest = require('./parse-request')
2
- const mpxJSON = require('./mpx-json')
3
- const getMainCompilation = require('./get-main-compilation')
4
-
5
- module.exports = function readJsonForSrc (src, loaderContext, callback) {
6
- const fs = loaderContext._compiler.inputFileSystem
7
- const mpx = getMainCompilation(loaderContext._compilation).__mpx__
8
- const defs = mpx.defs
9
- const resolve = (context, request, callback) => {
10
- const { queryObj } = parseRequest(request)
11
- context = queryObj.context || context
12
- return loaderContext.resolve(context, request, callback)
13
- }
14
-
15
- resolve(loaderContext.context, src, (err, result) => {
16
- if (err) return callback(err)
17
- const { rawResourcePath: resourcePath } = parseRequest(result)
18
- loaderContext.addDependency(resourcePath)
19
- fs.readFile(resourcePath, (err, content) => {
20
- if (err) {
21
- return callback(err)
22
- }
23
- content = content.toString('utf-8')
24
- if (resourcePath.endsWith('.json.js')) {
25
- try {
26
- content = mpxJSON.compileMPXJSONText({ source: content, defs, filePath: resourcePath })
27
- } catch (e) {
28
- return callback(e)
29
- }
30
- }
31
- callback(null, content)
32
- })
33
- })
34
- }
@@ -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
- }