-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
62 lines (49 loc) · 1.71 KB
/
index.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
const validate = require('schema-utils')
const {getOptions} = require('loader-utils')
const {escapeForRegExp, isEmpty, replaceIncludeRecursive} = require('./util')
const schema = {
type: 'object',
properties: {
includeStartTag: {type: 'string'},
includeEndTag: {type: 'string'},
variableStartTag: {type: 'string'},
variableEndTag: {type: 'string'},
},
}
module.exports = function (content) {
const defaultOptions = {
includeStartTag: '<%-',
includeEndTag: '%>',
variableStartTag: '<%=',
variableEndTag: '%>',
maxIncludes: 5,
}
const customOptions = getOptions(this)
validate(schema, customOptions)
if(!isEmpty(customOptions)) {
// 对自定义选项中需要正则转义的内容进行转义
Object.keys(customOptions).filter(key => key.endsWith('Tag')).forEach((tagKey) => {
customOptions[tagKey] = escapeForRegExp(customOptions[tagKey])
})
}
const options = Object.assign({}, defaultOptions, customOptions)
const {
includeStartTag, includeEndTag, maxIncludes, variableStartTag, variableEndTag,
} = options
const pathRelative = this.context
const pathnameREStr = '[-_.a-zA-Z0-9/]+'
const argsREStr = '{\\s*(\\s*\\S+?\\s*:\\s*\\S+?)(,\\s*(\\S+?\\s*:\\s*\\S+\\s*?)+?)*}'
const includeRE = new RegExp(
`${includeStartTag}\\s*include\\(\\s*(['|"])(${pathnameREStr})\\1\\s*(?:,\\s*(${argsREStr}))?\\s*\\)\\s*${includeEndTag}`,
'g',
)
const variableNameRE = '\\S+(\\s*\\?\.+:\.+)?'
const variableRE = new RegExp(
`${variableStartTag}\\s*(${variableNameRE})\\s*${variableEndTag}`,
'g',
)
const source = replaceIncludeRecursive({
apiContext: this, content, includeRE, variableRE, pathRelative, maxIncludes,
})
return source
}