-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
193 lines (182 loc) · 6.1 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const fs = require('fs-extra')
const filter = require('lodash/filter')
const includes = require('lodash/includes')
const isString = require('lodash/isString')
const isFunction = require('lodash/isFunction')
const { extname } = require('path')
function InjectTwigWebpackPlugin(options) {
this.options = options
options.chunks = options.chunks || []
options.more = options.more || {}
options.auto = options.auto || false
options.startJS = options.startJS || '<!-- start:js -->'
options.endJS = options.endJS || '<!-- end:js -->'
options.startCSS = options.startCSS || '<!-- start:css -->'
options.endCSS = options.endCSS || '<!-- end:css -->'
options.transducer = options.transducer || ''
this.runing = false
}
function assetsOfChunks(chunks, selected) {
let assets = {
js: [],
css: []
}
filter(chunks, chunk => includes(selected, chunk.name)).forEach(chunk => {
chunk.files.forEach(file => {
let ext = extname(file).replace('.', '')
assets[ext] && assets[ext].push(file)
})
})
return assets
}
function injectWithinByIndentifier(
html,
startIdentifier,
endIdentifier,
content,
purified
) {
let start = html.indexOf(startIdentifier)
let end = html.indexOf(endIdentifier)
if (start < 0 || end < 0) {
return html
}
let previousInnerContent = html.substring(start + startIdentifier.length, end)
let indent = leadingWhitespace(previousInnerContent)
indent = indent.replace(/(\n[\s|\t]*\r*\n)/g, '\n')
let injected = Array.isArray(content) ? content.slice() : [content]
purified
? injected.unshift(html.substr(0, start))
: injected.unshift(html.substr(0, start + startIdentifier.length))
purified
? injected.push(html.substr(end + endIdentifier.length))
: injected.push(html.substr(end))
return injected.join(indent)
}
function injectWithin(html, content, head = true) {
let before = head ? html.indexOf('</head>') : html.indexOf('</body>')
if (before < 0) return html
let injected = Array.isArray(content) ? content.slice() : [content]
injected.unshift(html.substr(0, before))
injected.push(html.substr(before))
return injected.join('\n')
}
function applyTransducer(originURL, transducer) {
let url = originURL
if (typeof transducer === 'string') {
url = transducer + originURL
} else if (typeof transducer === 'function') {
typeof transducer(originURL) === 'string' && (url = transducer(originURL))
}
return url
}
function leadingWhitespace(str) {
return str.match(/^\s*/)[0]
}
InjectTwigWebpackPlugin.prototype.apply = function (compiler) {
let that = this
let options = that.options
let filename = options.filename
let output =
isString(options.output) || isFunction(options.output)
? options.output
: false
let purified = !!output
let selected = options.chunks
let more = options.more
let transducer = options.transducer
let autoInject = options.auto
let startInjectJS = options.startJS
let endInjectJS = options.endJS
let startInjectCSS = options.startCSS
let endInjectCSS = options.endCSS
let emit = function (compilation, callback = () => { }) {
let chunks = compilation.chunks
let html
if (that.runing) {
callback()
return
}
if (!options.filename) {
callback()
return
}
let assets = assetsOfChunks(chunks, selected)
let jsLabel = assets['js'].map(function (v) {
return '{% do view.registerJsFile(siteUrl ~ "' + applyTransducer(v, transducer) + '") %}'
})
let cssLabel = assets['css'].map(function (v) {
return (
'{% do view.registerCssFile(siteUrl ~ "' + applyTransducer(v, transducer) + '") %}'
)
})
if (more) {
if (Array.isArray(more.js)) {
for (let i = 0; i < more.js.length; i++) {
jsLabel.unshift('{% do view.registerJsFile(siteUrl ~ "' + more.js[i] + '") %}')
}
}
if (Array.isArray(more.css)) {
for (var j = 0; j < more.css.length; j++) {
cssLabel.unshift(
'{% do view.registerCssFile(siteUrl ~ "' + more.css[j] + '") %}'
)
}
}
}
if (isString(output) && output) {
try {
fs.copySync(filename, output)
filename = output
} catch (e) {
compilation.errors.push(
new Error('InjectTwigWebpackPlugin copy filename to output failed')
)
}
}
function injector(filename) {
try {
html = fs.readFileSync(filename, 'utf8')
} catch (e) {
compilation.errors.push(
new Error('InjectTwigWebpackPlugin read filename failed')
)
callback()
return
}
if (autoInject) {
html = injectWithin(html, jsLabel, false)
html = injectWithin(html, cssLabel)
} else {
html = injectWithinByIndentifier(
html,
startInjectJS,
endInjectJS,
jsLabel,
purified
)
html = injectWithinByIndentifier(
html,
startInjectCSS,
endInjectCSS,
cssLabel,
purified
)
}
return html
}
if (isFunction(output)) {
output(filename, injector)
} else {
fs.writeFileSync(filename, injector(filename))
}
that.runing = true
callback()
}
if (compiler.hooks) {
compiler.hooks.emit.tap('InjectTwigWebpackPlugin', emit)
} else {
compiler.plugin('emit', emit)
}
}
module.exports = InjectTwigWebpackPlugin