-
Notifications
You must be signed in to change notification settings - Fork 6
/
static-resource-bundle.js
87 lines (78 loc) · 3.25 KB
/
static-resource-bundle.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
const yauzl = require('yauzl')
const yazl = require('yazl')
const path = require('path')
const _ = require('lodash')
const multimatch = require('multimatch')
module.exports = {
transform: async ({ config }, helpers) => {
const filesToFilter = new Set()
helpers.addRemapper(/^staticresources\/([^/]+)\/.*$/, (filename, regexp) => {
return `staticresources/${filename.match(regexp)[1]}.resource`
})
helpers.filterMetadata(fileName => {
return !filesToFilter.has(fileName)
})
helpers.xmlTransformer('staticresources/*-meta.xml', async (filename, xml, requireFiles, addFiles, cleanFiles) => {
if (xml.contentType[0] === 'application/zip') {
const resourceName = filename.replace('-meta.xml', '')
const dir = resourceName.replace('.resource', '')
cleanFiles(dir)
if (!multimatch(resourceName, _.get(config, 'staticResources.useBundleRenderer', []).map(x => `staticresources/${x}`)).length) return
filesToFilter.add(resourceName)
cleanFiles(resourceName)
const resource = await requireFiles(resourceName)
return new Promise(resolve => {
yauzl.fromBuffer(resource[0].data, { lazyEntries: true, autoClose: true }, function (err, zipfile) {
if (err) throw err
zipfile.readEntry()
zipfile.on('entry', async entry => {
if (!entry.fileName.endsWith('/')) {
zipfile.openReadStream(entry, async (err2, s) => {
const bufs = []
s.on('data', d => bufs.push(d))
s.on('end', () => {
entry.data = Buffer.concat(bufs)
entry.fileName = path.join(dir, entry.fileName)
addFiles(entry)
zipfile.readEntry()
})
})
} else zipfile.readEntry()
})
zipfile.on('end', resolve)
})
})
}
})
},
untransform: async ({ config }, helpers) => {
helpers.xmlTransformer('staticresources/*-meta.xml', async (filename, xml, requireFiles, addFiles) => {
if (xml.contentType[0] === 'application/zip') {
const resourceName = filename.replace('-meta.xml', '')
const folder = resourceName.replace('.resource', '')
if (!multimatch(resourceName, _.get(config, 'staticResources.useBundleRenderer', []).map(x => `staticresources/${x}`)).length) return
const filesToZip = await requireFiles(`${folder}/**/*`)
return new Promise(resolve => {
const zip = new yazl.ZipFile()
filesToZip.forEach(f => zip.addBuffer(f.data, f.fileName.replace(folder + '/', '')))
zip.end()
const bufs = []
zip.outputStream.on('data', d => bufs.push(d))
zip.outputStream.on('end', () => {
addFiles({
fileName: filename.replace('-meta.xml', ''),
data: Buffer.concat(bufs)
})
resolve()
})
})
}
})
helpers.filterMetadata(fileName => {
return !fileName.match(/^staticresources\/([^/]+)\/.*$/)
})
helpers.addRemapper(/^staticresources\/([^/]+)\/.*$/, (filename, regexp) => {
return `staticresources/${filename.match(regexp)[1]}.resource`
})
}
}