UNPKG

1.22 kBJavaScriptView Raw
1const { getOptions } = require('loader-utils')
2const { validate } = require('schema-utils')
3
4const loaderName = 'string-replace-loader'
5
6const optionsSchema = {
7 type: 'object',
8 properties: {
9 search: {
10 anyOf: [
11 {
12 instanceof: 'RegExp'
13 },
14 {
15 type: 'string'
16 }
17 ]
18 },
19 replace: {
20 anyOf: [
21 {
22 instanceof: 'Function'
23 },
24 {
25 type: 'string'
26 }
27 ]
28 },
29 flags: {
30 type: 'string',
31 },
32 strict: {
33 type: 'boolean'
34 }
35 },
36 additionalProperties: false
37}
38
39const defaultOptions = {
40 search: null,
41 replace: null,
42 flags: null,
43 strict: false
44}
45
46function getOptionsArray (config) {
47 const rawOptions = getOptions(config)
48 const rawOptionsArray = (
49 typeof rawOptions.multiple !== 'undefined'
50 ? rawOptions.multiple
51 : [rawOptions]
52 )
53 const optionsArray = []
54
55 for (const optionsIndex in rawOptionsArray) {
56 validate(optionsSchema, rawOptionsArray[optionsIndex], { name: loaderName })
57
58 optionsArray[optionsIndex] = Object.assign({}, defaultOptions, rawOptionsArray[optionsIndex])
59 }
60
61 return optionsArray
62}
63
64module.exports = getOptionsArray