@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.
- package/lib/dependencies/AppEntryDependency.js +56 -0
- package/lib/dependencies/CommonJsVariableDependency.js +74 -0
- package/lib/dependencies/DynamicEntryDependency.js +127 -0
- package/lib/dependencies/FlagPluginDependency.js +23 -0
- package/lib/dependencies/InjectDependency.js +43 -0
- package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
- package/lib/dependencies/RecordStaticResourceDependency.js +47 -0
- package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
- package/lib/dependencies/ResolveDependency.js +83 -0
- package/lib/extractor.js +72 -179
- package/lib/file-loader.js +7 -19
- package/lib/helpers.js +41 -331
- package/lib/index.js +475 -365
- package/lib/json-compiler/helper.js +152 -0
- package/lib/json-compiler/index.js +148 -407
- package/lib/json-compiler/plugin.js +134 -0
- package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
- package/lib/loader.js +78 -177
- package/lib/native-loader.js +40 -70
- package/lib/record-loader.js +11 -0
- package/lib/{path-loader.js → resolve-loader.js} +0 -0
- package/lib/runtime/i18n.wxs +3 -3
- package/lib/selector.js +8 -7
- package/lib/style-compiler/index.js +14 -15
- package/lib/template-compiler/compiler.js +16 -153
- package/lib/template-compiler/index.js +46 -132
- package/lib/url-loader.js +11 -29
- package/lib/utils/add-query.js +1 -1
- package/lib/utils/const.js +5 -0
- package/lib/utils/emit-file.js +10 -0
- package/lib/utils/get-entry-name.js +13 -0
- package/lib/utils/is-url-request.js +10 -1
- package/lib/utils/normalize.js +0 -13
- package/lib/utils/parse-request.js +3 -3
- package/lib/utils/set.js +47 -0
- package/lib/utils/stringify-loaders-resource.js +25 -0
- package/lib/utils/stringify-query.js +4 -0
- package/lib/web/processScript.js +3 -3
- package/lib/web/processTemplate.js +2 -0
- package/lib/wxml/{wxml-loader.js → loader.js} +24 -60
- package/lib/wxs/WxsModuleIdsPlugin.js +32 -0
- package/lib/wxs/WxsParserPlugin.js +2 -2
- package/lib/wxs/WxsPlugin.js +4 -8
- package/lib/wxs/WxsTemplatePlugin.js +46 -92
- package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +0 -0
- package/lib/wxs/{wxs-loader.js → loader.js} +33 -38
- package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +0 -0
- package/lib/wxss/loader.js +31 -43
- package/lib/wxss/localsLoader.js +1 -5
- package/package.json +4 -8
- package/lib/content-loader.js +0 -13
- package/lib/dependency/ChildCompileDependency.js +0 -24
- package/lib/dependency/InjectDependency.js +0 -26
- package/lib/dependency/RemovedModuleDependency.js +0 -23
- package/lib/dependency/ResolveDependency.js +0 -49
- package/lib/plugin-loader.js +0 -287
- package/lib/utils/try-require.js +0 -16
- package/lib/wxss/getImportPrefix.js +0 -30
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const normalize = require('../utils/normalize')
|
|
3
|
+
const nativeLoaderPath = normalize.lib('native-loader')
|
|
4
|
+
const isUrlRequestRaw = require('../utils/is-url-request')
|
|
5
|
+
const parseRequest = require('../utils/parse-request')
|
|
6
|
+
const loaderUtils = require('loader-utils')
|
|
7
|
+
|
|
8
|
+
module.exports = function createJSONHelper ({ loaderContext, emitWarning }) {
|
|
9
|
+
const mpx = loaderContext.getMpx()
|
|
10
|
+
const resolveMode = mpx.resolveMode
|
|
11
|
+
const externals = mpx.externals
|
|
12
|
+
const root = mpx.projectRoot
|
|
13
|
+
const publicPath = loaderContext._compilation.outputOptions.publicPath || ''
|
|
14
|
+
const pathHash = mpx.pathHash
|
|
15
|
+
|
|
16
|
+
const isUrlRequest = r => isUrlRequestRaw(r, root, externals)
|
|
17
|
+
const urlToRequest = r => loaderUtils.urlToRequest(r)
|
|
18
|
+
|
|
19
|
+
// todo 提供不记录dependency的resolve方法,非必要的情况下不记录dependency,提升缓存利用率
|
|
20
|
+
const resolve = (context, request, callback) => {
|
|
21
|
+
const { queryObj } = parseRequest(request)
|
|
22
|
+
context = queryObj.context || context
|
|
23
|
+
return loaderContext.resolve(context, request, callback)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const dynamicEntryMap = new Map()
|
|
27
|
+
|
|
28
|
+
let dynamicEntryCount = 0
|
|
29
|
+
|
|
30
|
+
const getDynamicEntry = (resource, type, outputPath = '', packageRoot = '', relativePath = '') => {
|
|
31
|
+
const key = `mpx_dynamic_entry_${dynamicEntryCount++}`
|
|
32
|
+
const value = `__mpx_dynamic_entry__( ${JSON.stringify(resource)},${JSON.stringify(type)},${JSON.stringify(outputPath)},${JSON.stringify(packageRoot)},${JSON.stringify(relativePath)})`
|
|
33
|
+
dynamicEntryMap.set(key, value)
|
|
34
|
+
return key
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const processDynamicEntry = (output) => {
|
|
38
|
+
return output.replace(/"mpx_dynamic_entry_\d+"/g, (match) => {
|
|
39
|
+
const key = match.slice(1, -1)
|
|
40
|
+
return dynamicEntryMap.get(key)
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const processComponent = (component, context, { tarRoot = '', outputPath = '', relativePath = '' }, callback) => {
|
|
45
|
+
if (!isUrlRequest(component)) return callback()
|
|
46
|
+
if (resolveMode === 'native') {
|
|
47
|
+
component = urlToRequest(component)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
resolve(context, component, (err, resource, info) => {
|
|
51
|
+
if (err) return callback(err)
|
|
52
|
+
const resourcePath = parseRequest(resource).resourcePath
|
|
53
|
+
const parsed = path.parse(resourcePath)
|
|
54
|
+
const ext = parsed.ext
|
|
55
|
+
const resourceName = path.join(parsed.dir, parsed.name)
|
|
56
|
+
|
|
57
|
+
if (!outputPath) {
|
|
58
|
+
if (ext === '.js' && resourceName.includes('node_modules')) {
|
|
59
|
+
let root = info.descriptionFileRoot
|
|
60
|
+
let name = 'nativeComponent'
|
|
61
|
+
if (info.descriptionFileData) {
|
|
62
|
+
if (info.descriptionFileData.miniprogram) {
|
|
63
|
+
root = path.join(root, info.descriptionFileData.miniprogram)
|
|
64
|
+
}
|
|
65
|
+
if (info.descriptionFileData.name) {
|
|
66
|
+
// 去掉name里面的@符号,因为支付宝不支持文件路径上有@
|
|
67
|
+
name = info.descriptionFileData.name.replace(/@/g, '')
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
let relative = path.relative(root, resourceName)
|
|
71
|
+
outputPath = path.join('components', name + pathHash(root), relative)
|
|
72
|
+
} else {
|
|
73
|
+
let componentName = parsed.name
|
|
74
|
+
outputPath = path.join('components', componentName + pathHash(resourcePath), componentName)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (ext === '.js') {
|
|
78
|
+
resource = `!!${nativeLoaderPath}!${resource}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath)
|
|
82
|
+
callback(null, entry)
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const getPageName = (resourcePath, ext) => {
|
|
87
|
+
const baseName = path.basename(resourcePath, ext)
|
|
88
|
+
return path.join('pages', baseName + pathHash(resourcePath), baseName)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const processPage = (page, context, tarRoot = '', callback) => {
|
|
92
|
+
let aliasPath = ''
|
|
93
|
+
if (typeof page !== 'string') {
|
|
94
|
+
aliasPath = page.path
|
|
95
|
+
page = page.src
|
|
96
|
+
}
|
|
97
|
+
if (!isUrlRequest(page)) return callback()
|
|
98
|
+
if (resolveMode === 'native') {
|
|
99
|
+
page = urlToRequest(page)
|
|
100
|
+
}
|
|
101
|
+
resolve(context, page, (err, resource) => {
|
|
102
|
+
if (err) return callback(err)
|
|
103
|
+
const { resourcePath } = parseRequest(resource)
|
|
104
|
+
const ext = path.extname(resourcePath)
|
|
105
|
+
let outputPath
|
|
106
|
+
if (aliasPath) {
|
|
107
|
+
outputPath = aliasPath
|
|
108
|
+
} else {
|
|
109
|
+
const relative = path.relative(context, resourcePath)
|
|
110
|
+
if (/^\./.test(relative)) {
|
|
111
|
+
// 如果当前page不存在于context中,对其进行重命名
|
|
112
|
+
outputPath = getPageName(resourcePath, ext)
|
|
113
|
+
emitWarning(`Current page [${resourcePath}] is not in current pages directory [${context}], the page path will be replaced with [${outputPath}], use ?resolve to get the page path and navigate to it!`)
|
|
114
|
+
} else {
|
|
115
|
+
outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (ext === '.js') {
|
|
119
|
+
resource = `!!${nativeLoaderPath}!${resource}`
|
|
120
|
+
}
|
|
121
|
+
const entry = getDynamicEntry(resource, 'page', outputPath, tarRoot, publicPath + tarRoot)
|
|
122
|
+
callback(null, entry)
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const processJsExport = (js, context, tarRoot = '', callback) => {
|
|
127
|
+
if (resolveMode === 'native') {
|
|
128
|
+
js = urlToRequest(js)
|
|
129
|
+
}
|
|
130
|
+
resolve(context, js, (err, resource) => {
|
|
131
|
+
if (err) return callback(err)
|
|
132
|
+
const { resourcePath } = parseRequest(resource)
|
|
133
|
+
const relative = path.relative(context, resourcePath)
|
|
134
|
+
if (/^\./.test(relative)) {
|
|
135
|
+
return callback(new Error(`The js export path ${resourcePath} must be in the context ${context}!`))
|
|
136
|
+
}
|
|
137
|
+
const outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1]
|
|
138
|
+
const entry = getDynamicEntry(resource, 'export', outputPath, tarRoot, publicPath + tarRoot)
|
|
139
|
+
callback(null, entry)
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
processComponent,
|
|
145
|
+
processDynamicEntry,
|
|
146
|
+
processPage,
|
|
147
|
+
processJsExport,
|
|
148
|
+
resolve,
|
|
149
|
+
isUrlRequest,
|
|
150
|
+
urlToRequest
|
|
151
|
+
}
|
|
152
|
+
}
|