/
module.js
executable file
·175 lines (148 loc) · 4.24 KB
/
module.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
const fs = require('fs-extra')
const path = require('path')
const hasha = require('hasha')
const { fork } = require('child_process')
const { joinUrl, getRouteParams } = require('../utils')
module.exports = function (iconOptions) {
this.nuxt.hook('build:before', () => run.call(this, iconOptions, true))
if (this.options.mode === 'spa' && !this.options.dev) {
return run.call(this, iconOptions, false) // Fill meta
}
}
async function run (iconOptions, _emitAssets) {
const { publicPath } = getRouteParams(this.options)
// Defaults
const defaults = {
iconFileName: 'icon.png',
sizes: [64, 120, 144, 152, 192, 384, 512],
targetDir: 'icons',
accessibleIcons: true,
iconProperty: '$icon',
publicPath,
iconSrc: null,
_iconHash: null,
_cacheDir: null,
_icons: null,
_assetIcons: null
}
// Merge options
const options = {
...defaults,
...iconOptions
}
// Find iconSrc
options.iconSrc = await findIcon.call(this, options)
// Disable module if no icon specified
if (!options.iconSrc) {
return
}
// Generate icons
await generateIcons.call(this, options)
// Add manifest
addManifest.call(this, options)
// Add plugin
if (options.accessibleIcons) {
addPlugin.call(this, options)
}
// Emit assets in background
if (_emitAssets) {
emitAssets.call(this, options)
}
}
async function findIcon (options) {
const iconSearchPath = [
options.iconSrc,
path.resolve(this.options.srcDir, this.options.dir.static, options.iconFileName),
path.resolve(this.options.srcDir, this.options.dir.assets, options.iconFileName)
].filter(p => p)
for (const iconSrc of iconSearchPath) {
if (await fs.exists(iconSrc)) {
return iconSrc
}
}
}
async function addPlugin (options) {
const icons = {}
for (const icon of options._assetIcons) {
icons[icon.sizes] = icon.src
}
if (options.accessibleIcons) {
this.addPlugin({
src: path.resolve(__dirname, './plugin.js'),
fileName: 'nuxt-icons.js',
options: {
iconProperty: options.iconProperty,
icons
}
})
}
}
async function generateIcons (options) {
// Get hash of source image
if (!options.iconHash) {
options.iconHash = await hasha.fromFile(options.iconSrc).then(h => h.substring(0, 6))
}
if (!options._cacheDir) {
options._cacheDir = path.join(__dirname, '.cache', options.iconHash)
}
// Generate _icons
options._icons = {}
for (const size of options.sizes) {
options._icons[size] = `${options.targetDir}/icon_${size}.${options.iconHash}.png`
}
// Generate _assetIcons
options._assetIcons = options.sizes.map(size => ({
src: joinUrl(options.publicPath, options._icons[size]),
sizes: `${size}x${size}`,
type: `image/png`
}))
}
function addManifest (options) {
if (!this.options.manifest) {
this.options.manifest = {}
}
if (!this.options.manifest.icons) {
this.options.manifest.icons = []
}
this.options.manifest.icons.push(...options._assetIcons)
}
function emitAssets (options) {
// Start resize task in background
const resizePromise = resizeIcons.call(this, options)
// Register webpack plugin to emit icons
this.extendBuild((config, { isClient }) => {
if (isClient) {
config.plugins.push({
apply (compiler) {
compiler.hooks.emit.tapPromise('nuxt-pwa-icon', async compilation => {
await resizePromise
await Promise.all(options.sizes.map(async size => {
const targetFilename = options._icons[size]
const srcFileName = path.join(options._cacheDir, `${size}.png`)
const src = await fs.readFile(srcFileName)
compilation.assets[targetFilename] = { source: () => src, size: () => src.length }
}))
})
}
})
}
})
}
async function resizeIcons (options) {
if (await fs.exists(options._cacheDir)) {
return
}
await fs.mkdirp(options._cacheDir)
await new Promise((resolve, reject) => {
const child = fork(require.resolve('./resize'), [
JSON.stringify({
input: options.iconSrc,
distDir: options._cacheDir,
sizes: options.sizes
})
])
child.on('exit', (code) => {
return code ? reject(code) : resolve()
})
})
}