Skip to content

Instantly share code, notes, and snippets.

@joakimk
Last active October 11, 2018 18:31
Show Gist options
  • Select an option

  • Save joakimk/90fcd6144ef05478feea9560a87f878f to your computer and use it in GitHub Desktop.

Select an option

Save joakimk/90fcd6144ef05478feea9560a87f878f to your computer and use it in GitHub Desktop.

Revisions

  1. joakimk revised this gist Nov 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion webpacker-environment.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ environment.plugins.set("UndigestedAssets", function() {
    outputPath = outputPath.replace("_/assets/", "")

    // When an error happens in development, all assets are not compiled, we
    // need to prevent that from causing errors later on.
    // need to handle that so we don't crash webpack :)
    if(compilation.assets[assetKey]) {
    compilation.assets[outputPath] = compilation.assets[assetKey]
    }
  2. joakimk renamed this gist Nov 30, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion plugin.js → webpacker-environment.js
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,11 @@ environment.plugins.set("UndigestedAssets", function() {
    outputPath = outputPath.replace("_/assets/fonts/", "")
    outputPath = outputPath.replace("_/assets/", "")

    compilation.assets[outputPath] = compilation.assets[assetKey]
    // When an error happens in development, all assets are not compiled, we
    // need to prevent that from causing errors later on.
    if(compilation.assets[assetKey]) {
    compilation.assets[outputPath] = compilation.assets[assetKey]
    }
    }

    compileCallback()
  3. joakimk revised this gist Nov 30, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion plugin.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // 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.
    // 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:

  4. joakimk created this gist Nov 30, 2017.
    33 changes: 33 additions & 0 deletions plugin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // 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/", "")

    compilation.assets[outputPath] = compilation.assets[assetKey]
    }

    compileCallback()
    })
    })

    module.exports = environment