-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathutil.js
163 lines (145 loc) · 4.06 KB
/
util.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const path = require('path')
const fs = require('fs')
const resolveSrc = require('../utils/resolve-src')
const pagesNameMap = Object.create(null)
function cacheFileInfo (resourcePath, ...arg) {
pagesNameMap[resourcePath] = Object.assign({}, pagesNameMap[resourcePath], ...arg)
}
function getFileInfo (resourcePath) {
return pagesNameMap[resourcePath] || {}
}
// 单文件的名字+hash
// TODO: 调试时取个全名
var hash = require('hash-sum')
const cache = Object.create(null)
function getCompNameAndSrc (context, file) {
const filePath = `/${resolveSrc(context, file)}.wxml`
if (!cache[file]) {
cache[file] = hash(file)
}
return {
filePath,
name: cache[file]
}
}
function getFiltersOutputSrc (context, file) {
const filePath = `/${resolveSrc(context, file)}.wxs`
const name = (path.basename(file) + hash(file)).replace(/[^\d|\w]/g, '')
return {
filePath,
name: name
}
}
// 根据路径获得组件名
function getNameByFile (dir) {
// const arr = dir.match(/[pages?/components?]\/(.*?)(\/)/)
const arr = dir.match(/pages\/(.*?)\//)
if (arr && arr[1]) {
return arr[1]
}
return path.parse(dir).name
}
function getKeyFromObjByVal (obj, val) {
for (const i in obj) {
if (path.resolve(obj[i]) === path.resolve(val)) {
return i
}
}
}
function getPageSrc (pageName) {
return path.parse(pageName).dir ? pageName : `pages/${pageName}/${pageName}`
}
// TODO, 这儿应该按照 main.js 导出的 config 来进行 isApp isPage 识别,暂时不改,下次大版本升级 loader 的时候改
// 计算目标输出的路径等信息
// pageType 默认为 null === component, 目前共 3 种类型: component, app, page
function resolveTarget (dir, entry) {
const originName = getKeyFromObjByVal(entry, dir)
const name = originName || getNameByFile(dir)
const isApp = name === 'app'
const pageType = isApp ? 'app' : (originName ? 'page' : 'component')
const isPage = pageType === 'page'
let src = 'app'
if (isPage) {
src = getPageSrc(name)
}
return { pageType, src, name, isApp, isPage }
}
// 简单的转换驼峰大写为中横线
const hyphenateRE = /([^-])([A-Z])/g
function covertCCVar (str) {
return str
.replace(hyphenateRE, '$1-$2')
.replace(hyphenateRE, '$1-$2')
.toLowerCase()
}
// 缓存所有的 slots 节点,生成一个文件
const slotsCache = Object.create(null)
const importCodeCache = Object.create(null)
function cacheSlots (slots, importCode) {
Object.keys(slots).forEach(k => {
slotsCache[k] = slots[k]
})
importCodeCache[importCode] = importCode
}
function getSlots () {
const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('<import src="/components/slots" />', '')
const allSlots = Object.keys(slotsCache).map(v => slotsCache[v].code).join('\n')
return allImportCode + allSlots
}
// 包大小优化: build 模式不需要美化 wxml
const jsBeautify = require('js-beautify')
const isProduction = process.env.NODE_ENV === 'production'
function htmlBeautify (content) {
const htmlBeautifyOptions = {
// wrap_line_length: '80',
indent_size: 2,
preserve_newlines: false,
max_preserve_newlines: 0,
e4x: true,
unformatted: ['a', 'span', 'img', 'code', 'pre', 'sub', 'sup', 'em', 'strong', 'b', 'i', 'u', 'strike', 'big', 'small', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
}
if (isProduction) {
return content
}
return jsBeautify.html(content, htmlBeautifyOptions)
}
function getBabelrc (src) {
if (src && fs.existsSync(src)) {
return src
}
const curBabelRc = path.resolve('./.babelrc')
if (fs.existsSync(curBabelRc)) {
return curBabelRc
}
return ''
}
const defaultPart = type => {
return {
type,
content: '\n',
start: 0,
attrs: {},
end: 1,
map: {
version: 3,
sources: [],
names: [],
mappings: '',
sourcesContent: []
}
}
}
module.exports = {
defaultPart,
cacheFileInfo,
getFileInfo,
getCompNameAndSrc,
getFiltersOutputSrc,
resolveTarget,
covertCCVar,
cacheSlots,
getSlots,
htmlBeautify,
getBabelrc,
getPageSrc
}