-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (45 loc) · 1.21 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
const tag = require('create-html-element')
const loaderUtils = require('loader-utils')
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const regexes = {
script: [/\.jsx?$/],
style: [/\.css$/, /\.s(a|c)ss$/, /\.stylus$/],
template: [/\.tpl$/, /\.template$/, /\.html$/]
}
module.exports = function (content) {
const dirPath = this.context
const options = loaderUtils.getOptions(this)
const filesName = fs.readdirSync(dirPath)
if (filesName.length === 0) {
return
}
const tags = []
Object.keys(regexes).forEach(type => {
try {
let code = []
const files = filesName.filter(file => regexes[type].some(re => re.test(file)))
if (files.length === 0) {
return
}
if (type === 'script' || type === 'template') {
if (files.length > 1) {
throw new Error('script and template file must not be over 1')
}
}
files.forEach(file => {
tags.push(tag({
name: type,
value: '' ,
attributes: Object.assign({}, {
src: path.join(dirPath, file)
}, _.omitBy(options, i => !i))
}))
})
} catch (e) {
throw e
}
})
return tags.join('')
}