@mpxjs/webpack-plugin 2.6.114-alpha.8 → 2.6.115

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/README.md +1 -1
  2. package/lib/built-in-loader.js +49 -0
  3. package/lib/config.js +0 -14
  4. package/lib/content-loader.js +13 -0
  5. package/lib/dependency/ChildCompileDependency.js +24 -0
  6. package/lib/dependency/InjectDependency.js +26 -0
  7. package/lib/dependency/RemovedModuleDependency.js +23 -0
  8. package/lib/{dependencies → dependency}/ReplaceDependency.js +2 -19
  9. package/lib/dependency/ResolveDependency.js +49 -0
  10. package/lib/extractor.js +178 -82
  11. package/lib/file-loader.js +19 -7
  12. package/lib/helpers.js +334 -39
  13. package/lib/index.js +525 -889
  14. package/lib/json-compiler/index.js +451 -245
  15. package/lib/json-compiler/{theme.js → theme-loader.js} +3 -5
  16. package/lib/loader.js +241 -178
  17. package/lib/native-loader.js +133 -71
  18. package/lib/parser.js +2 -1
  19. package/lib/path-loader.js +3 -0
  20. package/lib/platform/json/wx/index.js +1 -1
  21. package/lib/platform/template/normalize-component-rules.js +3 -2
  22. package/lib/platform/template/wx/component-config/button.js +2 -14
  23. package/lib/platform/template/wx/component-config/image.js +0 -4
  24. package/lib/platform/template/wx/component-config/input.js +0 -4
  25. package/lib/platform/template/wx/component-config/rich-text.js +0 -4
  26. package/lib/platform/template/wx/component-config/scroll-view.js +0 -4
  27. package/lib/platform/template/wx/component-config/switch.js +0 -4
  28. package/lib/platform/template/wx/component-config/text.js +0 -4
  29. package/lib/platform/template/wx/component-config/textarea.js +0 -5
  30. package/lib/platform/template/wx/component-config/view.js +0 -4
  31. package/lib/platform/template/wx/index.js +3 -149
  32. package/lib/plugin-loader.js +287 -0
  33. package/lib/resolver/AddEnvPlugin.js +3 -4
  34. package/lib/resolver/AddModePlugin.js +3 -4
  35. package/lib/resolver/PackageEntryPlugin.js +36 -23
  36. package/lib/runtime/base.styl +0 -5
  37. package/lib/runtime/components/web/getInnerListeners.js +3 -1
  38. package/lib/runtime/components/web/mpx-image.vue +5 -20
  39. package/lib/runtime/components/web/mpx-movable-view.vue +2 -6
  40. package/lib/runtime/components/web/mpx-swiper.vue +3 -18
  41. package/lib/runtime/i18n.wxs +11 -31
  42. package/lib/runtime/optionProcessor.js +3 -48
  43. package/lib/selector.js +10 -29
  44. package/lib/staticConfig.js +4 -0
  45. package/lib/style-compiler/index.js +24 -16
  46. package/lib/style-compiler/load-postcss-config.js +1 -3
  47. package/lib/style-compiler/plugins/conditional-strip.js +65 -68
  48. package/lib/style-compiler/plugins/rpx.js +37 -43
  49. package/lib/style-compiler/plugins/scope-id.js +72 -79
  50. package/lib/style-compiler/plugins/trans-special.js +18 -25
  51. package/lib/style-compiler/plugins/trim.js +7 -13
  52. package/lib/style-compiler/plugins/vw.js +16 -22
  53. package/lib/template-compiler/compiler.js +199 -106
  54. package/lib/template-compiler/index.js +139 -52
  55. package/lib/template-compiler/trans-dynamic-class-expr.js +13 -18
  56. package/lib/url-loader.js +29 -11
  57. package/lib/utils/add-query.js +1 -1
  58. package/lib/utils/get-main-compilation.js +6 -0
  59. package/lib/utils/is-url-request.js +1 -10
  60. package/lib/utils/match-condition.js +1 -4
  61. package/lib/utils/normalize.js +15 -4
  62. package/lib/utils/parse-request.js +3 -3
  63. package/lib/utils/read-json-for-src.js +34 -0
  64. package/lib/utils/stringify-query.js +0 -4
  65. package/lib/utils/try-require.js +16 -0
  66. package/lib/web/processJSON.js +144 -113
  67. package/lib/web/processScript.js +34 -47
  68. package/lib/web/processTemplate.js +40 -57
  69. package/lib/wxml/{loader.js → wxml-loader.js} +62 -21
  70. package/lib/wxs/WxsParserPlugin.js +2 -2
  71. package/lib/wxs/WxsPlugin.js +8 -4
  72. package/lib/wxs/WxsTemplatePlugin.js +92 -46
  73. package/lib/wxs/{i18n-loader.js → wxs-i18n-loader.js} +4 -5
  74. package/lib/wxs/wxs-loader.js +117 -0
  75. package/lib/wxs/{pre-loader.js → wxs-pre-loader.js} +5 -20
  76. package/lib/wxss/getImportPrefix.js +30 -0
  77. package/lib/wxss/loader.js +43 -31
  78. package/lib/wxss/localsLoader.js +5 -1
  79. package/lib/wxss/processCss.js +103 -107
  80. package/package.json +18 -21
  81. package/LICENSE +0 -433
  82. package/lib/dependencies/AddEntryDependency.js +0 -24
  83. package/lib/dependencies/AppEntryDependency.js +0 -58
  84. package/lib/dependencies/CommonJsAsyncDependency.js +0 -51
  85. package/lib/dependencies/CommonJsVariableDependency.js +0 -81
  86. package/lib/dependencies/DynamicEntryDependency.js +0 -171
  87. package/lib/dependencies/FlagPluginDependency.js +0 -24
  88. package/lib/dependencies/InjectDependency.js +0 -43
  89. package/lib/dependencies/RecordGlobalComponentsDependency.js +0 -50
  90. package/lib/dependencies/RecordIndependentDependency.js +0 -44
  91. package/lib/dependencies/RecordResourceMapDependency.js +0 -62
  92. package/lib/dependencies/RemoveEntryDependency.js +0 -40
  93. package/lib/dependencies/ResolveDependency.js +0 -88
  94. package/lib/independent-loader.js +0 -52
  95. package/lib/json-compiler/helper.js +0 -156
  96. package/lib/json-compiler/plugin.js +0 -150
  97. package/lib/partial-compile/index.js +0 -35
  98. package/lib/record-loader.js +0 -11
  99. package/lib/resolve-loader.js +0 -6
  100. package/lib/resolver/FixDescriptionInfoPlugin.js +0 -28
  101. package/lib/runtime/components/tenon/getInnerListeners.js +0 -317
  102. package/lib/runtime/components/tenon/tenon-button.vue +0 -305
  103. package/lib/runtime/components/tenon/tenon-image.vue +0 -61
  104. package/lib/runtime/components/tenon/tenon-input.vue +0 -99
  105. package/lib/runtime/components/tenon/tenon-rich-text.vue +0 -21
  106. package/lib/runtime/components/tenon/tenon-scroll-view.vue +0 -124
  107. package/lib/runtime/components/tenon/tenon-switch.vue +0 -91
  108. package/lib/runtime/components/tenon/tenon-text-area.vue +0 -64
  109. package/lib/runtime/components/tenon/tenon-text.vue +0 -64
  110. package/lib/runtime/components/tenon/tenon-view.vue +0 -93
  111. package/lib/runtime/components/tenon/util.js +0 -44
  112. package/lib/runtime/optionProcessor.tenon.js +0 -386
  113. package/lib/style-compiler/plugins/hm.js +0 -20
  114. package/lib/tenon/index.js +0 -105
  115. package/lib/tenon/processJSON.js +0 -360
  116. package/lib/tenon/processScript.js +0 -260
  117. package/lib/tenon/processStyles.js +0 -21
  118. package/lib/tenon/processTemplate.js +0 -133
  119. package/lib/utils/const.js +0 -10
  120. package/lib/utils/emit-file.js +0 -10
  121. package/lib/utils/eval-json-js.js +0 -31
  122. package/lib/utils/get-entry-name.js +0 -13
  123. package/lib/utils/get-json-content.js +0 -42
  124. package/lib/utils/get-relative-path.js +0 -24
  125. package/lib/utils/resolve.js +0 -13
  126. package/lib/utils/set.js +0 -47
  127. package/lib/utils/stringify-loaders-resource.js +0 -25
  128. package/lib/wxs/WxsModuleIdsPlugin.js +0 -29
  129. package/lib/wxs/loader.js +0 -142
@@ -1,133 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,31 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,42 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
1
- function getRelativePath (source, target) {
2
- source = source && source.replace(/\/[^/]*$/, ''); // get dirname
3
- // make sure source and target are absolute path
4
- /^\//.test(source) || (source = '/' + source);
5
- /^\//.test(target) || (target = '/' + target)
6
- // check if source or target is root path
7
- let sourceArr = source.split('/').filter((item, index) => index !== 0 && !!item)
8
- let targetArr = target.split('/').filter((item, index) => index !== 0 && !!item)
9
- let i = 0
10
- while (sourceArr[i] === targetArr[i] && i < sourceArr.length && i < targetArr.length) {
11
- i++
12
- }
13
- let relativePath = ''
14
- for (let j = 0; j < sourceArr.length - i; j++) {
15
- relativePath += '../'
16
- }
17
- relativePath += targetArr.slice(i).join('/')
18
- if (relativePath[0] !== '.') relativePath = './' + relativePath
19
- return relativePath
20
- }
21
-
22
- module.exports = {
23
- getRelativePath
24
- }
@@ -1,13 +0,0 @@
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
- }
package/lib/utils/set.js DELETED
@@ -1,47 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
@@ -1,29 +0,0 @@
1
- const {
2
- compareModulesByPreOrderIndexOrIdentifier
3
- } = require('webpack/lib/util/comparators')
4
- const {
5
- assignAscendingModuleIds,
6
- getUsedModuleIdsAndModules
7
- } = require('webpack/lib/ids/IdHelpers')
8
-
9
- /** @typedef {import("../Compiler")} Compiler */
10
- /** @typedef {import("../Module")} Module */
11
-
12
- class WxsModuleIdsPlugin {
13
- apply (compilation) {
14
- compilation.hooks.moduleIds.tap({
15
- name: 'WxsModuleIdsPlugin',
16
- // 放在最前面执行,确保生成的代码模块为数组形式,符合wxs规范
17
- stage: -1000
18
- }, () => {
19
- const [usedIds, modulesInNaturalOrder] =
20
- getUsedModuleIdsAndModules(compilation)
21
- modulesInNaturalOrder.sort(
22
- compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
23
- )
24
- assignAscendingModuleIds(usedIds, modulesInNaturalOrder, compilation)
25
- })
26
- }
27
- }
28
-
29
- module.exports = WxsModuleIdsPlugin
package/lib/wxs/loader.js DELETED
@@ -1,142 +0,0 @@
1
- const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin')
2
- const EntryPlugin = require('webpack/lib/EntryPlugin')
3
- const LazySet = require('webpack/lib/util/LazySet')
4
- const LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin')
5
- const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
6
- const path = require('path')
7
- const WxsPlugin = require('./WxsPlugin')
8
- const RecordResourceMapDependency = require('../dependencies/RecordResourceMapDependency')
9
- const parseRequest = require('../utils/parse-request')
10
- const toPosix = require('../utils/to-posix')
11
- const fixRelative = require('../utils/fix-relative')
12
- const addQuery = require('../utils/add-query')
13
- const config = require('../config')
14
-
15
- module.exports = content => content
16
-
17
- module.exports.pitch = function (remainingRequest) {
18
- const nativeCallback = this.async()
19
- const moduleGraph = this._compilation.moduleGraph
20
- const mpx = this.getMpx()
21
- const mode = mpx.mode
22
- const getOutputPath = mpx.getOutputPath
23
- let { resourcePath, queryObj } = parseRequest(this.resource)
24
- const issuer = moduleGraph.getIssuer(this._module)
25
- const { resourcePath: issuerResourcePath, queryObj: issuerQueryObj } = parseRequest(queryObj.issuerResource || issuer.resource)
26
- const issuerPackageName = issuerQueryObj.packageRoot || 'main'
27
- const pagesMap = mpx.pagesMap
28
- const componentsMap = mpx.componentsMap[issuerPackageName]
29
- const staticResourcesMap = mpx.staticResourcesMap[issuerPackageName]
30
- const issuerName = pagesMap[issuerResourcePath] || componentsMap[issuerResourcePath] || staticResourcesMap[issuerResourcePath]
31
- const issuerDir = path.dirname(issuerName)
32
-
33
- const getName = (raw) => {
34
- const match = /^(.*?)(\.[^.]*)?$/.exec(raw)
35
- return match[1]
36
- }
37
-
38
- const wxsModule = queryObj.wxsModule
39
- if (wxsModule) {
40
- resourcePath = `${resourcePath}~${wxsModule}`
41
- }
42
- const packageRoot = queryObj.packageRoot || ''
43
- const ext = config[mode].wxs.ext
44
- const filename = toPosix(path.join(packageRoot, getOutputPath(resourcePath, ext.slice(1), { ext })))
45
- this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', filename, packageRoot))
46
-
47
- const callback = (err) => {
48
- if (err) return nativeCallback(err)
49
- let relativePath = toPosix(path.relative(issuerDir, filename))
50
- relativePath = fixRelative(relativePath, mode)
51
- nativeCallback(null, `module.exports = ${JSON.stringify(relativePath)};`)
52
- }
53
-
54
- // 清空issuerResource query避免文件内容输出报错并进行子编译缓存优化
55
- const request = '!!' + addQuery(remainingRequest, {}, false, ['issuerResource'])
56
-
57
- // request中已经包含全量构成filename的信息,故可以直接使用request作为key来进行缓存
58
- if (!mpx.wxsAssetsCache.has(request)) {
59
- mpx.wxsAssetsCache.set(request, new Promise((resolve, reject) => {
60
- const outputOptions = {
61
- filename,
62
- // 避免输出的wxs中包含es语法
63
- environment: {
64
- // The environment supports arrow functions ('() => { ... }').
65
- arrowFunction: false,
66
- // The environment supports BigInt as literal (123n).
67
- bigIntLiteral: false,
68
- // The environment supports const and let for variable declarations.
69
- const: false,
70
- // The environment supports destructuring ('{ a, b } = obj').
71
- destructuring: false,
72
- // The environment supports an async import() function to import EcmaScript modules.
73
- dynamicImport: false,
74
- // The environment supports 'for of' iteration ('for (const x of array) { ... }').
75
- forOf: false,
76
- // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
77
- module: false
78
- }
79
- }
80
-
81
- const plugins = [
82
- new WxsPlugin({ mode }),
83
- new NodeTargetPlugin(),
84
- new EntryPlugin(this.context, request, { name: getName(filename) }),
85
- new LimitChunkCountPlugin({ maxChunks: 1 }),
86
- new FlagEntryExportAsUsedPlugin(true, 'entry')
87
- ]
88
-
89
- const childCompiler = this._compilation.createChildCompiler(resourcePath, outputOptions, plugins)
90
-
91
- let assets = []
92
-
93
- childCompiler.hooks.afterCompile.tap('MpxWebpackPlugin', (compilation) => {
94
- // 持久化缓存,使用module.buildInfo.assets来输出文件
95
- assets = compilation.getAssets()
96
- compilation.clearAssets()
97
- })
98
-
99
- childCompiler.runAsChild((err, entries, compilation) => {
100
- if (err) return reject(err)
101
- const fileDependencies = new LazySet()
102
- const contextDependencies = new LazySet()
103
- const missingDependencies = new LazySet()
104
- const buildDependencies = new LazySet()
105
- compilation.modules.forEach((module) => {
106
- module.addCacheDependencies(
107
- fileDependencies,
108
- contextDependencies,
109
- missingDependencies,
110
- buildDependencies
111
- )
112
- })
113
- resolve({
114
- assets,
115
- fileDependencies,
116
- contextDependencies,
117
- missingDependencies,
118
- buildDependencies
119
- })
120
- })
121
- }))
122
- }
123
-
124
- mpx.wxsAssetsCache.get(request).then(({ assets, fileDependencies, contextDependencies, missingDependencies, buildDependencies }) => {
125
- assets.forEach(({ name, source, info }) => {
126
- this.emitFile(name, source.source(), undefined, info)
127
- })
128
- fileDependencies.forEach((dep) => {
129
- this.addDependency(dep)
130
- })
131
- contextDependencies.forEach((dep) => {
132
- this.addContextDependency(dep)
133
- })
134
- missingDependencies.forEach((dep) => {
135
- this.addMissingDependency(dep)
136
- })
137
- buildDependencies.forEach((dep) => {
138
- this.addBuildDependency(dep)
139
- })
140
- callback()
141
- }).catch(callback)
142
- }