Last active
October 11, 2018 18:31
-
-
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)
This file contains hidden or 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
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found this helpful - thanks!