Skip to content

Instantly share code, notes, and snippets.

@joakimk
Last active October 11, 2018 18:31
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 joakimk/90fcd6144ef05478feea9560a87f878f to your computer and use it in GitHub Desktop.
Save joakimk/90fcd6144ef05478feea9560a87f878f to your computer and use it in GitHub Desktop.
non-digested assets in webpack (e.g. non-stupid-digest-assets for webpacker)
// Didn't find anything on google for this so I wrote my own. Use as a starting point if you have a similar problem.
// Please write comments if there is a better way to do this (or solve the same problem in another way) in webpack.
// Contents of config/webpack/environment.js:
const { environment } = require('@rails/webpacker')
// Generate undigested assets for use in embedded javascript, emails, etc.
// We previously used non-stupid-digest-assets for this.
environment.plugins.set("UndigestedAssets", function() {
this.plugin("emit", function(compilation, compileCallback) {
var fs = require("fs")
var path = require("path")
var manifest = JSON.parse(compilation.assets["manifest.json"].source())
for(var sourceFile in manifest) {
var targetFile = manifest[sourceFile]
outputPath = "/" + path.basename(compilation.options.output.path) + "/"
assetKey = targetFile.replace(outputPath, "")
// Make output paths for undigested files the same as sprockets so that old links still work.
outputPath = sourceFile.replace("_/assets/images/", "")
outputPath = outputPath.replace("_/assets/fonts/", "")
outputPath = outputPath.replace("_/assets/", "")
// When an error happens in development, all assets are not compiled, we
// need to handle that so we don't crash webpack :)
if(compilation.assets[assetKey]) {
compilation.assets[outputPath] = compilation.assets[assetKey]
}
}
compileCallback()
})
})
module.exports = environment
@maryammouse
Copy link

I found this helpful - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment