-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·53 lines (46 loc) · 1.2 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
const path = require('path')
const fs = require('fs')
const logger = require('@parcel/logger')
module.exports = function(bundler) {
const readAsset = path => {
try {
return fs.readFileSync(path, 'utf8')
} catch (e) {
logger.error('file is invalid')
throw e
}
}
const writeAsset = (name, {header = '', footer = ''}) => {
fs.writeFileSync(
name,
`${header}
${readAsset(name)}
${footer}`
)
}
const processAsset = async (bundle, processFn) => {
const {name} = bundle
if (name !== undefined) {
const wrappingCode = await processFn({name, bundler})
if (wrappingCode) {
writeAsset(name, wrappingCode)
}
}
bundle.childBundles.forEach((bundle) => {
processAsset(bundle, processFn)
})
}
bundler.on('bundled', async bundle => {
try {
const CWD = process.cwd()
const processFn = require(path.join(CWD, '.assetWrapper.js'))
if (processFn && typeof processFn === 'function') {
await processAsset(bundle, processFn)
}
} catch (error) {
logger.warn(
'parcel-plugin-wrapper cannot work without a .assetWrapper.js in the root of your project!'
)
}
})
}