Created
January 17, 2018 01:08
-
-
Save airtonix/8cd2199638d4a842c0cb41bb73addf9a to your computer and use it in GitHub Desktop.
Webpack Manifest Consumption in View Engines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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