Bundling images of dependencies into main module's bundle












0















I am facing this issue for which I have spent good two days on. Please help. I would greatly appreciate any leads, solutions or clarifications if this is not how webpack works or how to get it supported.



I have 2 projects:
1. consumer
2. dependency



Both are basic es6 modules bundled using webpack.



dependency module is a reusable module intended to be reused by other modules.
consumer module uses dependency module as a npm dependency.



dependency module ships a component with its own image asset (it's (es6) exported from the js as well). The bundling output correctly generates the image asset along with generated js in the 'dist' output directory directly with correct import address in the generated js. All good here.



consumer module uses the dependency. The consumer's generated bundle identifies the dependency bundle correctly and includes it.



However, the problem is, the image that was exported/bundled by dependency module is not readily included by consumer's bundle and not included/copied to the relevant folder specified for image assets (in file-loader config) while executing the webpack on config of consumer's .





dependency/src/index.js



import anyImage from './img/any.jpg'; // image any.jpg available at this path

const foo = function() {
return "hello!";
};

export {foo, anyImage};




dependency/webpack.config.js



const path = require('path');

module.exports = {
entry: './src/index.js',

output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
libraryTarget: 'umd'
},

module: {
rules: [
{
test: /.(png|jpg|gif)$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]"
}
}
]
},
{
test: /.js?$/,
loader: 'babel-loader'
}
]
}
};




consumer/src/index.js



import {foo, anyImage} from "dependency";
import placeholderImage from './img/placeholder_350x150.png';

document.getElementById('placeholderImage').src = placeholderImage;

setTimeout(() => {
document.getElementById('placeholderImage').src = anyImage;
}, 2000);




consumer/webpack.config.js



const path = require('path');

module.exports = {
entry: './src/index.js',

output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-widget.js',
libraryTarget: 'umd'
},

module: {
rules: [
{
test: /.(png|jpg|gif)$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
publicPath: "/dist/"
}
}
]
},
{
test: /.js?$/,
loader: 'babel-loader'
}
]
}
};




Thanks in advance!










share|improve this question



























    0















    I am facing this issue for which I have spent good two days on. Please help. I would greatly appreciate any leads, solutions or clarifications if this is not how webpack works or how to get it supported.



    I have 2 projects:
    1. consumer
    2. dependency



    Both are basic es6 modules bundled using webpack.



    dependency module is a reusable module intended to be reused by other modules.
    consumer module uses dependency module as a npm dependency.



    dependency module ships a component with its own image asset (it's (es6) exported from the js as well). The bundling output correctly generates the image asset along with generated js in the 'dist' output directory directly with correct import address in the generated js. All good here.



    consumer module uses the dependency. The consumer's generated bundle identifies the dependency bundle correctly and includes it.



    However, the problem is, the image that was exported/bundled by dependency module is not readily included by consumer's bundle and not included/copied to the relevant folder specified for image assets (in file-loader config) while executing the webpack on config of consumer's .





    dependency/src/index.js



    import anyImage from './img/any.jpg'; // image any.jpg available at this path

    const foo = function() {
    return "hello!";
    };

    export {foo, anyImage};




    dependency/webpack.config.js



    const path = require('path');

    module.exports = {
    entry: './src/index.js',

    output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js',
    libraryTarget: 'umd'
    },

    module: {
    rules: [
    {
    test: /.(png|jpg|gif)$/,
    use: [
    {
    loader: "file-loader",
    options: {
    name: "[name].[ext]"
    }
    }
    ]
    },
    {
    test: /.js?$/,
    loader: 'babel-loader'
    }
    ]
    }
    };




    consumer/src/index.js



    import {foo, anyImage} from "dependency";
    import placeholderImage from './img/placeholder_350x150.png';

    document.getElementById('placeholderImage').src = placeholderImage;

    setTimeout(() => {
    document.getElementById('placeholderImage').src = anyImage;
    }, 2000);




    consumer/webpack.config.js



    const path = require('path');

    module.exports = {
    entry: './src/index.js',

    output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'my-widget.js',
    libraryTarget: 'umd'
    },

    module: {
    rules: [
    {
    test: /.(png|jpg|gif)$/,
    use: [
    {
    loader: "file-loader",
    options: {
    name: "[name].[ext]",
    publicPath: "/dist/"
    }
    }
    ]
    },
    {
    test: /.js?$/,
    loader: 'babel-loader'
    }
    ]
    }
    };




    Thanks in advance!










    share|improve this question

























      0












      0








      0








      I am facing this issue for which I have spent good two days on. Please help. I would greatly appreciate any leads, solutions or clarifications if this is not how webpack works or how to get it supported.



      I have 2 projects:
      1. consumer
      2. dependency



      Both are basic es6 modules bundled using webpack.



      dependency module is a reusable module intended to be reused by other modules.
      consumer module uses dependency module as a npm dependency.



      dependency module ships a component with its own image asset (it's (es6) exported from the js as well). The bundling output correctly generates the image asset along with generated js in the 'dist' output directory directly with correct import address in the generated js. All good here.



      consumer module uses the dependency. The consumer's generated bundle identifies the dependency bundle correctly and includes it.



      However, the problem is, the image that was exported/bundled by dependency module is not readily included by consumer's bundle and not included/copied to the relevant folder specified for image assets (in file-loader config) while executing the webpack on config of consumer's .





      dependency/src/index.js



      import anyImage from './img/any.jpg'; // image any.jpg available at this path

      const foo = function() {
      return "hello!";
      };

      export {foo, anyImage};




      dependency/webpack.config.js



      const path = require('path');

      module.exports = {
      entry: './src/index.js',

      output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'index.js',
      libraryTarget: 'umd'
      },

      module: {
      rules: [
      {
      test: /.(png|jpg|gif)$/,
      use: [
      {
      loader: "file-loader",
      options: {
      name: "[name].[ext]"
      }
      }
      ]
      },
      {
      test: /.js?$/,
      loader: 'babel-loader'
      }
      ]
      }
      };




      consumer/src/index.js



      import {foo, anyImage} from "dependency";
      import placeholderImage from './img/placeholder_350x150.png';

      document.getElementById('placeholderImage').src = placeholderImage;

      setTimeout(() => {
      document.getElementById('placeholderImage').src = anyImage;
      }, 2000);




      consumer/webpack.config.js



      const path = require('path');

      module.exports = {
      entry: './src/index.js',

      output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'my-widget.js',
      libraryTarget: 'umd'
      },

      module: {
      rules: [
      {
      test: /.(png|jpg|gif)$/,
      use: [
      {
      loader: "file-loader",
      options: {
      name: "[name].[ext]",
      publicPath: "/dist/"
      }
      }
      ]
      },
      {
      test: /.js?$/,
      loader: 'babel-loader'
      }
      ]
      }
      };




      Thanks in advance!










      share|improve this question














      I am facing this issue for which I have spent good two days on. Please help. I would greatly appreciate any leads, solutions or clarifications if this is not how webpack works or how to get it supported.



      I have 2 projects:
      1. consumer
      2. dependency



      Both are basic es6 modules bundled using webpack.



      dependency module is a reusable module intended to be reused by other modules.
      consumer module uses dependency module as a npm dependency.



      dependency module ships a component with its own image asset (it's (es6) exported from the js as well). The bundling output correctly generates the image asset along with generated js in the 'dist' output directory directly with correct import address in the generated js. All good here.



      consumer module uses the dependency. The consumer's generated bundle identifies the dependency bundle correctly and includes it.



      However, the problem is, the image that was exported/bundled by dependency module is not readily included by consumer's bundle and not included/copied to the relevant folder specified for image assets (in file-loader config) while executing the webpack on config of consumer's .





      dependency/src/index.js



      import anyImage from './img/any.jpg'; // image any.jpg available at this path

      const foo = function() {
      return "hello!";
      };

      export {foo, anyImage};




      dependency/webpack.config.js



      const path = require('path');

      module.exports = {
      entry: './src/index.js',

      output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'index.js',
      libraryTarget: 'umd'
      },

      module: {
      rules: [
      {
      test: /.(png|jpg|gif)$/,
      use: [
      {
      loader: "file-loader",
      options: {
      name: "[name].[ext]"
      }
      }
      ]
      },
      {
      test: /.js?$/,
      loader: 'babel-loader'
      }
      ]
      }
      };




      consumer/src/index.js



      import {foo, anyImage} from "dependency";
      import placeholderImage from './img/placeholder_350x150.png';

      document.getElementById('placeholderImage').src = placeholderImage;

      setTimeout(() => {
      document.getElementById('placeholderImage').src = anyImage;
      }, 2000);




      consumer/webpack.config.js



      const path = require('path');

      module.exports = {
      entry: './src/index.js',

      output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'my-widget.js',
      libraryTarget: 'umd'
      },

      module: {
      rules: [
      {
      test: /.(png|jpg|gif)$/,
      use: [
      {
      loader: "file-loader",
      options: {
      name: "[name].[ext]",
      publicPath: "/dist/"
      }
      }
      ]
      },
      {
      test: /.js?$/,
      loader: 'babel-loader'
      }
      ]
      }
      };




      Thanks in advance!







      webpack






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 8:09









      Murtaza HaveliwalaMurtaza Haveliwala

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          i think you can make a console log for the anyImage in 'consumer/src/index.js' and 'dependency/src/index.js', i guess you will get the answer.



          The imported 'anyImg' in the consumer project is just a string -- '/static/img/xxx.png' instead of an accessed image path.






          share|improve this answer

























            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',
            autoActivateHeartbeat: false,
            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%2f53426407%2fbundling-images-of-dependencies-into-main-modules-bundle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            i think you can make a console log for the anyImage in 'consumer/src/index.js' and 'dependency/src/index.js', i guess you will get the answer.



            The imported 'anyImg' in the consumer project is just a string -- '/static/img/xxx.png' instead of an accessed image path.






            share|improve this answer






























              0














              i think you can make a console log for the anyImage in 'consumer/src/index.js' and 'dependency/src/index.js', i guess you will get the answer.



              The imported 'anyImg' in the consumer project is just a string -- '/static/img/xxx.png' instead of an accessed image path.






              share|improve this answer




























                0












                0








                0







                i think you can make a console log for the anyImage in 'consumer/src/index.js' and 'dependency/src/index.js', i guess you will get the answer.



                The imported 'anyImg' in the consumer project is just a string -- '/static/img/xxx.png' instead of an accessed image path.






                share|improve this answer















                i think you can make a console log for the anyImage in 'consumer/src/index.js' and 'dependency/src/index.js', i guess you will get the answer.



                The imported 'anyImg' in the consumer project is just a string -- '/static/img/xxx.png' instead of an accessed image path.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 10:59

























                answered Nov 22 '18 at 10:53









                J.HickeyJ.Hickey

                11




                11






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426407%2fbundling-images-of-dependencies-into-main-modules-bundle%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'