@mpxjs/webpack-plugin 2.6.114-alpha.6 → 2.6.114-alpha.9
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/{dependency → dependencies}/AddEntryDependency.js +0 -0
- package/lib/dependencies/AppEntryDependency.js +58 -0
- package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
- package/lib/dependencies/CommonJsVariableDependency.js +81 -0
- package/lib/dependencies/DynamicEntryDependency.js +171 -0
- package/lib/dependencies/FlagPluginDependency.js +24 -0
- package/lib/dependencies/InjectDependency.js +43 -0
- package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
- package/lib/dependencies/RecordIndependentDependency.js +44 -0
- package/lib/dependencies/RecordResourceMapDependency.js +62 -0
- package/lib/dependencies/RemoveEntryDependency.js +40 -0
- package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
- package/lib/dependencies/ResolveDependency.js +88 -0
- package/lib/extractor.js +82 -178
- package/lib/file-loader.js +7 -19
- package/lib/helpers.js +39 -334
- package/lib/independent-loader.js +52 -0
- package/lib/index.js +854 -542
- package/lib/json-compiler/helper.js +156 -0
- package/lib/json-compiler/index.js +242 -451
- package/lib/json-compiler/plugin.js +150 -0
- package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
- package/lib/loader.js +137 -240
- package/lib/native-loader.js +71 -133
- package/lib/parser.js +1 -2
- package/lib/partial-compile/index.js +35 -0
- package/lib/platform/json/wx/index.js +1 -1
- package/lib/platform/template/normalize-component-rules.js +2 -3
- package/lib/platform/template/wx/index.js +30 -4
- package/lib/record-loader.js +11 -0
- package/lib/{path-loader.js → resolve-loader.js} +0 -0
- package/lib/resolver/AddEnvPlugin.js +4 -3
- package/lib/resolver/AddModePlugin.js +4 -3
- package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
- package/lib/resolver/PackageEntryPlugin.js +23 -36
- package/lib/runtime/base.styl +5 -0
- package/lib/runtime/components/web/mpx-image.vue +20 -5
- package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
- package/lib/runtime/components/web/mpx-swiper.vue +18 -3
- package/lib/runtime/i18n.wxs +31 -11
- package/lib/runtime/optionProcessor.js +48 -3
- package/lib/selector.js +29 -10
- package/lib/style-compiler/index.js +15 -23
- package/lib/style-compiler/load-postcss-config.js +3 -1
- package/lib/style-compiler/plugins/conditional-strip.js +68 -65
- package/lib/style-compiler/plugins/rpx.js +43 -37
- package/lib/style-compiler/plugins/scope-id.js +79 -72
- package/lib/style-compiler/plugins/trans-special.js +25 -18
- package/lib/style-compiler/plugins/trim.js +13 -7
- package/lib/style-compiler/plugins/vw.js +22 -16
- package/lib/template-compiler/compiler.js +95 -197
- package/lib/template-compiler/index.js +52 -139
- package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
- package/lib/tenon/index.js +1 -4
- package/lib/tenon/processJSON.js +71 -76
- package/lib/tenon/processScript.js +60 -59
- package/lib/url-loader.js +11 -29
- package/lib/utils/add-query.js +1 -1
- package/lib/utils/const.js +10 -0
- package/lib/utils/emit-file.js +10 -0
- package/lib/utils/eval-json-js.js +31 -0
- package/lib/utils/get-entry-name.js +13 -0
- package/lib/utils/get-json-content.js +42 -0
- package/lib/utils/get-relative-path.js +2 -1
- package/lib/utils/is-url-request.js +10 -1
- package/lib/utils/match-condition.js +4 -1
- package/lib/utils/normalize.js +4 -15
- package/lib/utils/parse-request.js +3 -3
- package/lib/utils/resolve.js +13 -0
- 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/processJSON.js +113 -144
- package/lib/web/processScript.js +47 -34
- package/lib/web/processTemplate.js +57 -40
- package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
- package/lib/wxs/WxsModuleIdsPlugin.js +29 -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} +5 -4
- package/lib/wxs/loader.js +142 -0
- package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
- package/lib/wxss/loader.js +31 -43
- package/lib/wxss/localsLoader.js +1 -5
- package/lib/wxss/processCss.js +107 -103
- package/package.json +18 -20
- package/lib/built-in-loader.js +0 -49
- 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 -53
- package/lib/plugin-loader.js +0 -287
- package/lib/staticConfig.js +0 -4
- package/lib/utils/get-main-compilation.js +0 -6
- package/lib/utils/read-json-for-src.js +0 -34
- package/lib/utils/try-require.js +0 -16
- package/lib/wxs/wxs-loader.js +0 -117
- package/lib/wxss/getImportPrefix.js +0 -30
|
@@ -2,10 +2,10 @@ const genComponentTag = require('../utils/gen-component-tag')
|
|
|
2
2
|
const loaderUtils = require('loader-utils')
|
|
3
3
|
const addQuery = require('../utils/add-query')
|
|
4
4
|
const normalize = require('../utils/normalize')
|
|
5
|
-
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
|
|
6
|
-
const AddEntryDependency = require('../dependency/AddEntryDependency')
|
|
7
5
|
const builtInLoaderPath = normalize.lib('built-in-loader')
|
|
8
6
|
const optionProcessorPath = normalize.lib('runtime/optionProcessor')
|
|
7
|
+
const createJSONHelper = require('../json-compiler/helper')
|
|
8
|
+
const async = require('async')
|
|
9
9
|
|
|
10
10
|
function shallowStringify (obj) {
|
|
11
11
|
let arr = []
|
|
@@ -21,11 +21,6 @@ function shallowStringify (obj) {
|
|
|
21
21
|
return `{${arr.join(',')}}`
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
let entryDeps = new Set()
|
|
25
|
-
|
|
26
|
-
let callbacked = false
|
|
27
|
-
let cacheCallback
|
|
28
|
-
|
|
29
24
|
module.exports = function (script, options, callback) {
|
|
30
25
|
const ctorType = options.ctorType
|
|
31
26
|
const builtInComponentsMap = options.builtInComponentsMap
|
|
@@ -35,7 +30,6 @@ module.exports = function (script, options, callback) {
|
|
|
35
30
|
const loaderContext = options.loaderContext
|
|
36
31
|
const isProduction = options.isProduction
|
|
37
32
|
const componentId = options.componentId
|
|
38
|
-
const getRequireForSrc = options.getRequireForSrc
|
|
39
33
|
// const i18n = options.i18n
|
|
40
34
|
const jsonConfig = options.jsonConfig
|
|
41
35
|
// const tabBar = jsonConfig.tabBar
|
|
@@ -46,37 +40,52 @@ module.exports = function (script, options, callback) {
|
|
|
46
40
|
const forceDisableBuiltInLoader = options.forceDisableBuiltInLoader
|
|
47
41
|
|
|
48
42
|
// add entry
|
|
49
|
-
const checkEntryDeps = (callback) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
43
|
+
// const checkEntryDeps = (callback) => {
|
|
44
|
+
// callback = callback || cacheCallback
|
|
45
|
+
// if (callback && entryDeps.size === 0) {
|
|
46
|
+
// callback()
|
|
47
|
+
// } else {
|
|
48
|
+
// cacheCallback = callback
|
|
49
|
+
// }
|
|
50
|
+
// }
|
|
51
|
+
|
|
52
|
+
// const addEntryDep = (context, resource, name) => {
|
|
53
|
+
// // 如果loader已经回调,就不再添加entry
|
|
54
|
+
// if (callbacked) return
|
|
55
|
+
// const dep = SingleEntryPlugin.createDependency(resource, name)
|
|
56
|
+
// entryDeps.add(dep)
|
|
57
|
+
// const virtualModule = new AddEntryDependency({
|
|
58
|
+
// context: context._compiler.context,
|
|
59
|
+
// dep,
|
|
60
|
+
// name
|
|
61
|
+
// })
|
|
62
|
+
// /* eslint-disable camelcase */
|
|
63
|
+
// context._module.__has_tenon_entry = true
|
|
64
|
+
// context._module.addDependency(virtualModule)
|
|
65
|
+
// entryDeps.delete(dep)
|
|
66
|
+
// checkEntryDeps()
|
|
67
|
+
// }
|
|
68
|
+
|
|
69
|
+
const emitWarning = (msg) => {
|
|
70
|
+
loaderContext.emitWarning(
|
|
71
|
+
new Error('[tenon script processor][' + loaderContext.resource + ']: ' + msg)
|
|
72
|
+
)
|
|
56
73
|
}
|
|
57
74
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
entryDeps.add(dep)
|
|
63
|
-
const virtualModule = new AddEntryDependency({
|
|
64
|
-
context: context._compiler.context,
|
|
65
|
-
dep,
|
|
66
|
-
name
|
|
67
|
-
})
|
|
68
|
-
/* eslint-disable camelcase */
|
|
69
|
-
context._module.__has_tenon_entry = true
|
|
70
|
-
context._module.addDependency(virtualModule)
|
|
71
|
-
entryDeps.delete(dep)
|
|
72
|
-
checkEntryDeps()
|
|
75
|
+
const emitError = (msg) => {
|
|
76
|
+
loaderContext.emitError(
|
|
77
|
+
new Error('[tenon script processor][' + loaderContext.resource + ']: ' + msg)
|
|
78
|
+
)
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
const {
|
|
82
|
+
processPage,
|
|
83
|
+
processDynamicEntry
|
|
84
|
+
} = createJSONHelper({
|
|
85
|
+
loaderContext,
|
|
86
|
+
emitWarning,
|
|
87
|
+
emitError
|
|
88
|
+
})
|
|
80
89
|
|
|
81
90
|
const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
|
|
82
91
|
// let tabBarPagesMap = {}
|
|
@@ -156,26 +165,6 @@ module.exports = function (script, options, callback) {
|
|
|
156
165
|
let firstPage = ''
|
|
157
166
|
const pagesMap = {}
|
|
158
167
|
const componentsMap = {}
|
|
159
|
-
Object.keys(localPagesMap).forEach((pagePath) => {
|
|
160
|
-
const pageCfg = localPagesMap[pagePath]
|
|
161
|
-
// const pageRequest = stringifyRequest(pageCfg.resource)
|
|
162
|
-
addEntryDep(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
|
|
163
|
-
// addEntrySafely(loaderContext, addQuery(pageCfg.resource, { tenon: true }), pagePath)
|
|
164
|
-
// loaderContext.resolve(loaderContext._compiler.context, addQuery(pageCfg.resource, { tenon: true }), (err, resource) => {
|
|
165
|
-
// if(err) return callback(err)
|
|
166
|
-
|
|
167
|
-
// })
|
|
168
|
-
// if (pageCfg.async) {
|
|
169
|
-
// pagesMap[pagePath] = `()=>import(${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))`
|
|
170
|
-
// } else {
|
|
171
|
-
// // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
|
|
172
|
-
// pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
|
|
173
|
-
// }
|
|
174
|
-
|
|
175
|
-
// if (pageCfg.isFirst) {
|
|
176
|
-
// firstPage = pagePath
|
|
177
|
-
// }
|
|
178
|
-
})
|
|
179
168
|
|
|
180
169
|
Object.keys(localComponentsMap).forEach((componentName) => {
|
|
181
170
|
const componentCfg = localComponentsMap[componentName]
|
|
@@ -199,7 +188,7 @@ module.exports = function (script, options, callback) {
|
|
|
199
188
|
}
|
|
200
189
|
// 为了正确获取currentSrcMode便于运行时进行转换,对于src引入的组件script采用require方式引入(由于webpack会将import的执行顺序上升至最顶),这意味着对于src引入脚本中的named export将不会生效,不过鉴于mpx和小程序中本身也没有在组件script中声明export的用法,所以应该没有影响
|
|
201
190
|
content += script.src
|
|
202
|
-
? (
|
|
191
|
+
? `require(${stringifyRequest(script.src)})\n`
|
|
203
192
|
: (script.content + '\n') + '\n'
|
|
204
193
|
// createApp/Page/Component执行完成后立刻获取当前的option并暂存
|
|
205
194
|
content += ` const currentOption = global.currentOption\n`
|
|
@@ -246,13 +235,25 @@ module.exports = function (script, options, callback) {
|
|
|
246
235
|
// i18n`
|
|
247
236
|
// }
|
|
248
237
|
// }
|
|
249
|
-
content += `\n )\n`
|
|
238
|
+
content += `\n )\n__dynamic_page_slot__\n`
|
|
250
239
|
return content
|
|
251
240
|
}
|
|
252
241
|
})
|
|
253
242
|
output += '\n'
|
|
254
|
-
|
|
255
|
-
|
|
243
|
+
// 处理pages
|
|
244
|
+
const pageSet = new Set()
|
|
245
|
+
let dynamicPageStr = ''
|
|
246
|
+
async.each(localPagesMap, (pageCfg, callback) => {
|
|
247
|
+
if (typeof pageCfg !== 'string') pageCfg.src = addQuery(pageCfg.src, { tenon: true })
|
|
248
|
+
processPage(pageCfg, loaderContext.context, '', (err, entry, { key }) => {
|
|
249
|
+
if (err) return callback()
|
|
250
|
+
if (pageSet.has(key)) return callback()
|
|
251
|
+
pageSet.add(key)
|
|
252
|
+
dynamicPageStr += `\n"${entry}"`
|
|
253
|
+
callback()
|
|
254
|
+
})
|
|
255
|
+
}, () => {
|
|
256
|
+
output = output.replace('__dynamic_page_slot__', processDynamicEntry(dynamicPageStr) || '')
|
|
256
257
|
callback(null, {
|
|
257
258
|
output
|
|
258
259
|
})
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
35
|
-
const mimetype = options.mimetype || mime.getType(
|
|
36
|
-
const
|
|
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 ||
|
|
41
|
-
const isStyle = (issuer && issuer.request && isStyleRequest(issuer.request)) ||
|
|
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 (
|
|
30
|
+
if (queryObj.fallback) {
|
|
49
31
|
transBase64 = false
|
|
50
32
|
}
|
|
51
33
|
} else {
|
package/lib/utils/add-query.js
CHANGED
|
@@ -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
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
function getRelativePath (source, target) {
|
|
2
|
-
source = source && source.replace(/\/[^/]*$/, ''); // get dirname
|
|
3
2
|
// make sure source and target are absolute path
|
|
4
3
|
/^\//.test(source) || (source = '/' + source);
|
|
5
4
|
/^\//.test(target) || (target = '/' + target)
|
|
5
|
+
|
|
6
|
+
source = source && source.replace(/\/[^/]*$/, '') // get dirname
|
|
6
7
|
// check if source or target is root path
|
|
7
8
|
let sourceArr = source.split('/').filter((item, index) => index !== 0 && !!item)
|
|
8
9
|
let targetArr = target.split('/').filter((item, index) => index !== 0 && !!item)
|
|
@@ -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
|
}
|
package/lib/utils/normalize.js
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const path = require('path')
|
|
1
|
+
// const path = require('path')
|
|
3
2
|
|
|
4
|
-
exports.lib = file => path.resolve(__dirname, '../', file)
|
|
5
|
-
|
|
6
|
-
exports.
|
|
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
|
+
}
|
package/lib/utils/set.js
ADDED
|
@@ -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
|