Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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]
module.exports = environment

This comment has been minimized.

Copy link

@maryammouse maryammouse commented Oct 11, 2018

I found this helpful - thanks!

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