-
Notifications
You must be signed in to change notification settings - Fork 0
/
metalsmithPlugin.js
28 lines (24 loc) · 1.07 KB
/
metalsmithPlugin.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
const manifestToAssets = require('./manifestToAssets');
function MetalsmithPlugin(opts, makeMetalsmithConfig) {
this.opts = Object.assign({
metalsmithDest: '/assets',
}, opts);
this.makeMetalsmithConfig = makeMetalsmithConfig ? makeMetalsmithConfig : require('./makeMetalsmithConfig');
}
MetalsmithPlugin.prototype.apply = function(compiler) {
compiler.plugin('after-emit', (compilation, callback) => {
if (compilation.assets && compilation.assets['manifest.json']) {
const manifest = JSON.parse(compilation.assets['manifest.json'].source());
const assets = manifestToAssets(manifest, this.opts.metalsmithDest, (file) => () => {
return compilation.assets[file] ? compilation.assets[file].source().toString() : null;
});
this.makeMetalsmithConfig(__dirname, {assets, callback}).build(function (err) {
if (err) throw err;
callback();
});
} else {
console.warn('Metalsmith Plugin requires ManifestPlugin, try running $ npm install webpack-manifest-plugin --save-dev');
}
});
};
module.exports = MetalsmithPlugin;