-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (37 loc) · 1.09 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
const fs = require('fs')
const schema = require('./options.json')
const utils = require('loader-utils')
const validateOptions = require('schema-utils')
/* eslint-disable require-jsdoc */
function matchesToFile(path, resourcePath, matches) {
const file = fs.readFileSync(path, {
encoding: 'utf-8',
flag: 'a+',
})
let searchResults = {}
try {
searchResults = JSON.parse(file)
} catch (err) {}
searchResults[resourcePath] = Array.from(new Set([...searchResults[resourcePath] || [], ...matches]))
fs.writeFileSync(path, JSON.stringify(searchResults, null, 2), {
encoding: 'utf-8',
flag: 'w',
})
}
module.exports = function loader(src) {
const options = utils.getOptions(this) || {}
options.verbose = options.verbose || false
validateOptions(schema, options, 'Search Loader')
const matchSet = new Set()
while((match = options.regex.exec(src)) !== null) {
matchSet.add(match[1])
}
const matches = [...matchSet]
if(options.verbose) {
console.log(`\n${this.resourcePath}: [${matches}]`)
}
if(matches.length > 0) {
matchesToFile(options.output, this.resourcePath, matches)
}
return src
}