-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
162 lines (152 loc) · 5.49 KB
/
index.ts
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
/**
* @license
* Copyright (c) 2017 Patrik Lindahl <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/** Webpack preprocessor loader
*
* Helps to disable parts of the code depending on preprocessor directives.
* Use //# to start a directive
* Supported directives:
* ifdef - If defined, start a new block
* ifndef - If not defined, start a new block
* else - If the directive was false, use the else block
* endif - Ends a block
* define - Defines a define variable
* undef - Undefines a define variable
* To use define variable substitution use:
* DEFINED_VARIABLE_NAME
* and it will be replaced with the value of the define variable
*/
import * as loaderUtils from 'loader-utils'
import { loader } from 'webpack'
interface Defines {
[name: string]: string | undefined
}
interface ConditionalState {
directive: string
condition: boolean
inElse: boolean
}
type ReplaceFunc = (substring: string, ...args: any[]) => string
const DIRECTIVE_REGEX = /^\/\/[ ]?#(.*)/
function checkIfShouldUseLine(conditionalStack: ConditionalState[]) {
let useLine = true
for (let i = conditionalStack.length - 1; i >= 0; i--) {
const cond = conditionalStack[i]
useLine = cond ? (cond.inElse ? !cond.condition : cond.condition) : true
if (!useLine) {
break
}
}
return useLine
}
function createDefineReplaceRegExp(defines: Defines) {
const defKeys = Object.keys(defines)
// This is not really according to the ECMAScript standards
// The standard allows for unicode variable names
if (defKeys.length === 0) {
return
}
const regExpStr = '([^a-zA-Z_\\$])(' + defKeys.join('|') + ')([^a-zA-Z0-9_\\$])?'
return new RegExp(regExpStr, 'g')
}
export default <loader.Loader> function processSource(source, sourceMap) {
const options = loaderUtils.getOptions(this)
const defines: Defines = options.defines || {}
let outSource = ''
const conditionalStack: ConditionalState[] = []
let defineReplaceRegExp = createDefineReplaceRegExp(defines)
const lines = source.toString().split('\n')
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
const tLine = line.trim()
const currCond = conditionalStack[conditionalStack.length - 1] || undefined
const useLine = checkIfShouldUseLine(conditionalStack)
if (DIRECTIVE_REGEX.test(tLine)) {
const directiveLine = tLine.match(DIRECTIVE_REGEX)[1].trim()
const directiveLineParts = directiveLine.split(' ')
const directive = directiveLineParts[0] || ''
const param1 = directiveLineParts[1] || undefined
switch (directive) {
case 'ifdef': {
if (param1) {
conditionalStack.push({
directive,
inElse: false,
condition: defines[param1] !== undefined,
})
}
break
}
case 'ifndef': {
if (param1) {
conditionalStack.push({
directive,
inElse: false,
condition: defines[param1] === undefined,
})
}
break
}
case 'else': {
const conditional = conditionalStack[conditionalStack.length - 1]
if (conditional.inElse) {
return this.callback(new Error('Unmatched else'))
}
conditional.inElse = true
break
}
case 'endif': {
const conditional = conditionalStack[conditionalStack.length - 1]
if (!conditional) {
return this.callback(new Error('Unmatched endif'))
}
conditionalStack.pop()
break
}
case 'define': {
if (useLine && param1) {
defines[param1] = directiveLineParts.slice(2).join(' ') || ''
defineReplaceRegExp = createDefineReplaceRegExp(defines)
}
break
}
case 'undef': {
if (useLine && param1) {
delete defines[directiveLineParts[1]]
defineReplaceRegExp = createDefineReplaceRegExp(defines)
}
break
}
default: {
return this.callback(new Error('Unknown processor directive: ' + directive))
}
}
} else {
if (useLine) {
const replacer: ReplaceFunc = (match, p1, p2, p3) => p1 + (defines[p2] || '') + (p3 || '')
const fixedLine = defineReplaceRegExp ? line.replace(defineReplaceRegExp, replacer) : line
outSource += fixedLine + (i < lines.length - 1 ? '\n' : '')
}
}
}
return outSource
}