Skip to content

Instantly share code, notes, and snippets.

@airtonix
Created January 17, 2018 01:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airtonix/8cd2199638d4a842c0cb41bb73addf9a to your computer and use it in GitHub Desktop.
Save airtonix/8cd2199638d4a842c0cb41bb73addf9a to your computer and use it in GitHub Desktop.
Webpack Manifest Consumption in View Engines
var path = require('path');
var CWD = process.cwd();
var pkg = require(path.join(CWD, 'package.json'));
module.exports.register = function (Handlebars) {
var manifestPath = path.resolve(process.cwd(), 'dist', 'Static', 'theme', 'manifest.json');
var manifest;
var errorMessage;
var filecontent;
try {
manifest = require(manifestPath);
} catch (e) {
}
// {{ asseturl '/static/theme/foo.js' }}
Handlebars.registerHelper('asseturl', function (filepath) {
if (!manifest) {
return filepath;
}
if (manifest[filepath]) {
return manifest[filepath];
} else {
return filepath;
}
});
};
new ManifestPlugin({
reduce: function (manifest, {path = '', chunk = {}, name = ''}) {
const publicPath = ProjectSettings.get('target:output:publicPath');
let fileName = path
.replace(/\\/g, '/')
/* START HACKS */
if (chunk && chunk.renderedHash) {
fileName = fileName
.replace(chunk.renderedHash, '')
.replace('--', '');
}
if (fileName.toLowerCase().endsWith('.svg')) {
const re = /(.*)\.min\-(.*)\.svg$/;
fileName = fileName.replace(re, '$1.svg');
}
/* END HACKS */
const filePath = join(publicPath, path);
log('reduced', {path, filePath, name, fileName});
manifest[join(publicPath, fileName)] = filePath;
return manifest;
},
writeToFileEmit: true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment