-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathselector.js
More file actions
57 lines (55 loc) · 1.71 KB
/
selector.js
File metadata and controls
57 lines (55 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const path = require('path')
const parseComponent = require('./parser')
const parseRequest = require('./utils/parse-request')
const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter')
module.exports = function (content) {
this.cacheable()
// 兼容处理处理ts-loader中watch-run/updateFile逻辑,直接跳过当前loader及后续的loader返回内容
const pathExtname = path.extname(this.resourcePath)
if (!['.vue', '.mpx'].includes(pathExtname)) {
this.loaderIndex = tsWatchRunLoaderFilter(this.loaders, this.loaderIndex)
return content
}
// 移除mpx访问依赖,支持 thread-loader
const { mode, env } = this.getOptions() || {}
if (!mode && !env) {
return content
}
const { queryObj } = parseRequest(this.resource)
const ctorType = queryObj.ctorType
const type = queryObj.type
const index = queryObj.index || 0
const filePath = this.resourcePath
const parts = parseComponent(content, {
filePath,
needMap: this.sourceMap,
mode,
env
})
let part = parts[type]
if (Array.isArray(part)) {
part = part[index]
}
if (!part) {
let content = ''
// 补全js内容
if (type === 'script') {
switch (ctorType) {
case 'app':
content += 'import {createApp} from "@mpxjs/core"\n' +
'createApp({})\n'
break
case 'page':
content += 'import {createPage} from "@mpxjs/core"\n' +
'createPage({})\n'
break
case 'component':
content += 'import {createComponent} from "@mpxjs/core"\n' +
'createComponent({})\n'
}
}
part = { content }
}
part = part || { content: '' }
this.callback(null, part.content, part.map)
}