forked from romanlex/app-manifest-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (161 loc) · 4.51 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
const childCompiler = require('./lib/compiler.js')
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const DEFAULT_OPTIONS = {
emitStats: false,
prefix: '',
output: '/',
statsFilename: 'iconstats-[hash].json',
persistentCache: true,
inject: true,
config: {
appName: 'Webpack App',
appDescription: null,
developerName: null,
developerURL: null,
background: '#fff',
theme_color: '#fff',
path: '/',
display: 'standalone',
orientation: 'portrait',
start_url: '/?homescreen=1',
version: '1.0',
logging: false,
icons: {
android: true,
appleIcon: true,
appleStartup: true,
coast: { offset: 25 },
favicons: true,
firefox: true,
windows: true,
yandex: true,
},
},
}
const PLUGIN_NAME = 'AppManifestWebpackPlugin'
const EVENTS = {
MAKE: `${PLUGIN_NAME}Make`,
COMPILE: `${PLUGIN_NAME}Compile`,
EMIT: `${PLUGIN_NAME}Emit`,
HTML_BEFORE: `${PLUGIN_NAME}HtmlPluginBefore`,
}
class AppManifestWebpackPlugin {
constructor(options) {
if (typeof options === 'string') {
options = { logo: options }
}
assert(typeof options === 'object', 'app-manifest-webpack-plugin: options are required')
assert(options.logo, 'An input file of icon is required')
this.options = {
...DEFAULT_OPTIONS,
...options,
}
this.compilationResult = null
}
/**
* Tries to guess the name from the package.json
*
* @param {string} pwd Path of work dir
*/
guessAppName(pwd) {
let packageJson = path.resolve(pwd, 'package.json')
if (!fs.existsSync(packageJson)) {
packageJson = path.resolve(pwd, '../package.json')
if (!fs.existsSync(packageJson)) {
return 'Webpack App'
}
}
return JSON.parse(fs.readFileSync(packageJson)).name
}
/**
* Emit handler
*
* @param {object} compiler
*/
emitHandler(compiler) {
if (this.beforeV4) {
compiler.plugin('emit', (compilation, cb) => this.emitProccess(compilation, cb))
} else {
compiler.hooks['emit'].tap(EVENTS.EMIT, (compilation, cb) =>
this.emitProccess(compilation, cb),
)
}
}
emitProccess(compilation, cb) {
delete compilation.assets[this.compilationResult.outputName]
if (cb) cb(null)
}
pluginHandler(compilation) {
compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, cb) => {
this.htmlProccessingFn(htmlPluginData, cb)
})
}
hooksHandler(compilation) {
const beforeEmit =
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing ||
HtmlWebpackPlugin.getHooks(compilation).beforeEmit
if (!beforeEmit && this.options.inject) {
const message = `compilation.hooks.htmlWebpackPluginAfterHtmlProcessing is lost.
Please make sure you have installed html-webpack-plugin and put it before ${PLUGIN_NAME}`
throw new Error(message)
}
beforeEmit.tapAsync(PLUGIN_NAME, (htmlPluginData, cb) =>
this.htmlProccessingFn(htmlPluginData, cb),
)
}
htmlProccessingFn(htmlPluginData, cb) {
if (htmlPluginData.plugin.options.favicons !== false) {
htmlPluginData.html = htmlPluginData.html.replace(
/(<\/head>)/i,
this.compilationResult.stats.html.join('\n') + '$&',
)
}
cb(null, htmlPluginData)
}
/**
* Compile handler
* @param {object} compiler
* @param {object} compilation
* @param {function} cb
*/
compileHandler(compiler, compilation, cb) {
childCompiler
.compileTemplate(this.options, compiler.context, compilation)
.then(result => {
this.compilationResult = result
cb(null)
})
.catch(cb)
}
/**
* Apply plugin
*
* @param {object} compiler
*/
apply(compiler) {
this.beforeV4 = !compiler.hooks
if (!this.options.config.appName) {
this.options.config.appName = this.guessAppName(compiler.context)
}
const compile = this.compileHandler.bind(this, compiler)
if (this.beforeV4) {
compiler.plugin('make', compile)
} else {
compiler.hooks['make'].tapAsync(EVENTS.MAKE, compile)
}
if (this.options.inject) {
if (this.beforeV4) {
compiler.plugin('compilation', this.pluginHandler.bind(this))
} else {
compiler.hooks['compilation'].tap(EVENTS.COMPILE, this.hooksHandler.bind(this))
}
}
if (!this.options.emitStats) {
this.emitHandler(compiler)
}
}
}
module.exports = AppManifestWebpackPlugin