Webpack: handling of static assets with webpack-manifest-plugin in a production build











up vote
0
down vote

favorite












I'm currently in the process of integrating Webpack in an existing serverside-rendered multi-page-application to leverage the modern tools and ecosystem around Webpack.



This has primarily been a good experience but I'm currently struggling with the integration into our serverside code, especially with a production build using hashing and a flat folder structure. The basic idea is very simple: all assets are managed by Webpack and with the help of webpack-manifest-plugin I generate a manifest.json that is read by our serverside application. And with a helper function I would turn a path like static/images/logo.png into logo-[hash].png.



This works great for js/css files (where only the bundle name of my entry points are relevant) but I have problems generating a manifest.json file that contains the necessary information I need for the static assets like images and documents.



Partial Webpack configuration



This is the relevant part of my webpack configuration:



{
entry: {
index: './js/app.js',
"static-assets": './js/static-assets.js'
},
output: {
filename: '[name].js'
},
resolve: {
alias: {
"vendor-assets": "dependencyX/assets",
}
},
plugins: [
new WebpackManifestPlugin()
],
module: {
rules: [
{
test: /.(png|svg|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: isDev ? '[path][name].[ext]' : '[hash].[ext]',
}
}
]
}
]
}
}


Approach 1: copy-webpack-plugin



I tried a configuration like this:



new CopyWebpackPlugin([
{
from: './static',
to: isDev ? '[path][name].[ext]' : '[name].[hash].[ext]'
},
{
from: 'node_modules/dependencyX/assets/img',
to: isDev ? 'vendor-assets/[path][name].[ext]' : '[name].[hash].[ext]'
},
])


Problem 1: In the manifest I only get the key of the new location but no information about the original path at all:



"logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png"


instead of:



"node_modules/dependencyX/assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


or even better considering my defined alias:



"vendor-assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


Problem 2: as copy-webpack-plugin seems to do a simple copy there will be a problem of duplication. If I reference one of these static assets also from a regular bundle it will appear a second time in the output folder.



Approach 2: static-assets bundle



My second approach was to define a bundle static-assets.js which references all possible locations of static assets using a request.context:



require.context('../static', true, /.+/);
require.context('vendor-assets/img', true, /.+/);


Problem: Like with the copy approach, the resulting manifest only contains the simple filename as key and not the original path.



Questions




  • Is there a simple way to solve this by configuration?

  • Are there other approaches?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm currently in the process of integrating Webpack in an existing serverside-rendered multi-page-application to leverage the modern tools and ecosystem around Webpack.



    This has primarily been a good experience but I'm currently struggling with the integration into our serverside code, especially with a production build using hashing and a flat folder structure. The basic idea is very simple: all assets are managed by Webpack and with the help of webpack-manifest-plugin I generate a manifest.json that is read by our serverside application. And with a helper function I would turn a path like static/images/logo.png into logo-[hash].png.



    This works great for js/css files (where only the bundle name of my entry points are relevant) but I have problems generating a manifest.json file that contains the necessary information I need for the static assets like images and documents.



    Partial Webpack configuration



    This is the relevant part of my webpack configuration:



    {
    entry: {
    index: './js/app.js',
    "static-assets": './js/static-assets.js'
    },
    output: {
    filename: '[name].js'
    },
    resolve: {
    alias: {
    "vendor-assets": "dependencyX/assets",
    }
    },
    plugins: [
    new WebpackManifestPlugin()
    ],
    module: {
    rules: [
    {
    test: /.(png|svg|jpg|gif)$/,
    use: [
    {
    loader: 'file-loader',
    options: {
    name: isDev ? '[path][name].[ext]' : '[hash].[ext]',
    }
    }
    ]
    }
    ]
    }
    }


    Approach 1: copy-webpack-plugin



    I tried a configuration like this:



    new CopyWebpackPlugin([
    {
    from: './static',
    to: isDev ? '[path][name].[ext]' : '[name].[hash].[ext]'
    },
    {
    from: 'node_modules/dependencyX/assets/img',
    to: isDev ? 'vendor-assets/[path][name].[ext]' : '[name].[hash].[ext]'
    },
    ])


    Problem 1: In the manifest I only get the key of the new location but no information about the original path at all:



    "logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png"


    instead of:



    "node_modules/dependencyX/assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


    or even better considering my defined alias:



    "vendor-assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


    Problem 2: as copy-webpack-plugin seems to do a simple copy there will be a problem of duplication. If I reference one of these static assets also from a regular bundle it will appear a second time in the output folder.



    Approach 2: static-assets bundle



    My second approach was to define a bundle static-assets.js which references all possible locations of static assets using a request.context:



    require.context('../static', true, /.+/);
    require.context('vendor-assets/img', true, /.+/);


    Problem: Like with the copy approach, the resulting manifest only contains the simple filename as key and not the original path.



    Questions




    • Is there a simple way to solve this by configuration?

    • Are there other approaches?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm currently in the process of integrating Webpack in an existing serverside-rendered multi-page-application to leverage the modern tools and ecosystem around Webpack.



      This has primarily been a good experience but I'm currently struggling with the integration into our serverside code, especially with a production build using hashing and a flat folder structure. The basic idea is very simple: all assets are managed by Webpack and with the help of webpack-manifest-plugin I generate a manifest.json that is read by our serverside application. And with a helper function I would turn a path like static/images/logo.png into logo-[hash].png.



      This works great for js/css files (where only the bundle name of my entry points are relevant) but I have problems generating a manifest.json file that contains the necessary information I need for the static assets like images and documents.



      Partial Webpack configuration



      This is the relevant part of my webpack configuration:



      {
      entry: {
      index: './js/app.js',
      "static-assets": './js/static-assets.js'
      },
      output: {
      filename: '[name].js'
      },
      resolve: {
      alias: {
      "vendor-assets": "dependencyX/assets",
      }
      },
      plugins: [
      new WebpackManifestPlugin()
      ],
      module: {
      rules: [
      {
      test: /.(png|svg|jpg|gif)$/,
      use: [
      {
      loader: 'file-loader',
      options: {
      name: isDev ? '[path][name].[ext]' : '[hash].[ext]',
      }
      }
      ]
      }
      ]
      }
      }


      Approach 1: copy-webpack-plugin



      I tried a configuration like this:



      new CopyWebpackPlugin([
      {
      from: './static',
      to: isDev ? '[path][name].[ext]' : '[name].[hash].[ext]'
      },
      {
      from: 'node_modules/dependencyX/assets/img',
      to: isDev ? 'vendor-assets/[path][name].[ext]' : '[name].[hash].[ext]'
      },
      ])


      Problem 1: In the manifest I only get the key of the new location but no information about the original path at all:



      "logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png"


      instead of:



      "node_modules/dependencyX/assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


      or even better considering my defined alias:



      "vendor-assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


      Problem 2: as copy-webpack-plugin seems to do a simple copy there will be a problem of duplication. If I reference one of these static assets also from a regular bundle it will appear a second time in the output folder.



      Approach 2: static-assets bundle



      My second approach was to define a bundle static-assets.js which references all possible locations of static assets using a request.context:



      require.context('../static', true, /.+/);
      require.context('vendor-assets/img', true, /.+/);


      Problem: Like with the copy approach, the resulting manifest only contains the simple filename as key and not the original path.



      Questions




      • Is there a simple way to solve this by configuration?

      • Are there other approaches?










      share|improve this question













      I'm currently in the process of integrating Webpack in an existing serverside-rendered multi-page-application to leverage the modern tools and ecosystem around Webpack.



      This has primarily been a good experience but I'm currently struggling with the integration into our serverside code, especially with a production build using hashing and a flat folder structure. The basic idea is very simple: all assets are managed by Webpack and with the help of webpack-manifest-plugin I generate a manifest.json that is read by our serverside application. And with a helper function I would turn a path like static/images/logo.png into logo-[hash].png.



      This works great for js/css files (where only the bundle name of my entry points are relevant) but I have problems generating a manifest.json file that contains the necessary information I need for the static assets like images and documents.



      Partial Webpack configuration



      This is the relevant part of my webpack configuration:



      {
      entry: {
      index: './js/app.js',
      "static-assets": './js/static-assets.js'
      },
      output: {
      filename: '[name].js'
      },
      resolve: {
      alias: {
      "vendor-assets": "dependencyX/assets",
      }
      },
      plugins: [
      new WebpackManifestPlugin()
      ],
      module: {
      rules: [
      {
      test: /.(png|svg|jpg|gif)$/,
      use: [
      {
      loader: 'file-loader',
      options: {
      name: isDev ? '[path][name].[ext]' : '[hash].[ext]',
      }
      }
      ]
      }
      ]
      }
      }


      Approach 1: copy-webpack-plugin



      I tried a configuration like this:



      new CopyWebpackPlugin([
      {
      from: './static',
      to: isDev ? '[path][name].[ext]' : '[name].[hash].[ext]'
      },
      {
      from: 'node_modules/dependencyX/assets/img',
      to: isDev ? 'vendor-assets/[path][name].[ext]' : '[name].[hash].[ext]'
      },
      ])


      Problem 1: In the manifest I only get the key of the new location but no information about the original path at all:



      "logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png"


      instead of:



      "node_modules/dependencyX/assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


      or even better considering my defined alias:



      "vendor-assets/img/logo.png": "logo.d985ee06aa950d9232f80c09a9e1329f.png",


      Problem 2: as copy-webpack-plugin seems to do a simple copy there will be a problem of duplication. If I reference one of these static assets also from a regular bundle it will appear a second time in the output folder.



      Approach 2: static-assets bundle



      My second approach was to define a bundle static-assets.js which references all possible locations of static assets using a request.context:



      require.context('../static', true, /.+/);
      require.context('vendor-assets/img', true, /.+/);


      Problem: Like with the copy approach, the resulting manifest only contains the simple filename as key and not the original path.



      Questions




      • Is there a simple way to solve this by configuration?

      • Are there other approaches?







      webpack assets serverside-rendering multi-page-application






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 at 15:08









      Dave

      67116




      67116





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352477%2fwebpack-handling-of-static-assets-with-webpack-manifest-plugin-in-a-production%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352477%2fwebpack-handling-of-static-assets-with-webpack-manifest-plugin-in-a-production%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?