How to partially include Node.js module using rollup with commonjs plugin





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to include bitcore-lib partially into my webpage using tree-shaking that rollup provides out of the box and rollup-plugin-commonjs to load Node.js module.



To better illustrate the problem I make a demo project that available on the github



You can have a look at bundle.js. If I define a module in the following way:



const useful = "3";
const useless = "4";

export {usefull, useless}


Tree shaking works correctly - the final bundle includes only useful dependency.



But if I define a module in the way it defined in bitcore-lib (node-lib.js) in demo project:



module.exports = {
useful: "1",
useless: "2"
};


In that case, the final bundle includes the whole module.



I've expected that useless: 2 dependency shouldn't be included because of tree-shaking. My index.js is here:



import {usefull as usefull1} from "./my-node-lib"
import {usefull as usefull2} from "./my-es-lib"

console.log(`hi! ${usefull1} ${usefull2}`);


My rollup.config.js is available here



Is it a problem of module definition or rollup config?










share|improve this question































    0















    I'm trying to include bitcore-lib partially into my webpage using tree-shaking that rollup provides out of the box and rollup-plugin-commonjs to load Node.js module.



    To better illustrate the problem I make a demo project that available on the github



    You can have a look at bundle.js. If I define a module in the following way:



    const useful = "3";
    const useless = "4";

    export {usefull, useless}


    Tree shaking works correctly - the final bundle includes only useful dependency.



    But if I define a module in the way it defined in bitcore-lib (node-lib.js) in demo project:



    module.exports = {
    useful: "1",
    useless: "2"
    };


    In that case, the final bundle includes the whole module.



    I've expected that useless: 2 dependency shouldn't be included because of tree-shaking. My index.js is here:



    import {usefull as usefull1} from "./my-node-lib"
    import {usefull as usefull2} from "./my-es-lib"

    console.log(`hi! ${usefull1} ${usefull2}`);


    My rollup.config.js is available here



    Is it a problem of module definition or rollup config?










    share|improve this question



























      0












      0








      0








      I'm trying to include bitcore-lib partially into my webpage using tree-shaking that rollup provides out of the box and rollup-plugin-commonjs to load Node.js module.



      To better illustrate the problem I make a demo project that available on the github



      You can have a look at bundle.js. If I define a module in the following way:



      const useful = "3";
      const useless = "4";

      export {usefull, useless}


      Tree shaking works correctly - the final bundle includes only useful dependency.



      But if I define a module in the way it defined in bitcore-lib (node-lib.js) in demo project:



      module.exports = {
      useful: "1",
      useless: "2"
      };


      In that case, the final bundle includes the whole module.



      I've expected that useless: 2 dependency shouldn't be included because of tree-shaking. My index.js is here:



      import {usefull as usefull1} from "./my-node-lib"
      import {usefull as usefull2} from "./my-es-lib"

      console.log(`hi! ${usefull1} ${usefull2}`);


      My rollup.config.js is available here



      Is it a problem of module definition or rollup config?










      share|improve this question
















      I'm trying to include bitcore-lib partially into my webpage using tree-shaking that rollup provides out of the box and rollup-plugin-commonjs to load Node.js module.



      To better illustrate the problem I make a demo project that available on the github



      You can have a look at bundle.js. If I define a module in the following way:



      const useful = "3";
      const useless = "4";

      export {usefull, useless}


      Tree shaking works correctly - the final bundle includes only useful dependency.



      But if I define a module in the way it defined in bitcore-lib (node-lib.js) in demo project:



      module.exports = {
      useful: "1",
      useless: "2"
      };


      In that case, the final bundle includes the whole module.



      I've expected that useless: 2 dependency shouldn't be included because of tree-shaking. My index.js is here:



      import {usefull as usefull1} from "./my-node-lib"
      import {usefull as usefull2} from "./my-es-lib"

      console.log(`hi! ${usefull1} ${usefull2}`);


      My rollup.config.js is available here



      Is it a problem of module definition or rollup config?







      javascript node.js commonjs rollup bitcore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 10:43







      Artem

















      asked Nov 17 '18 at 7:14









      ArtemArtem

      376114




      376114
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Tree shaking works only for ES6 modules. At least it's true for Webpack and I suppose for rollup as well. Your first definition is ES6, second is commonjs.



          Therefore if a library is not compiled/transpiled to ES6 modules tree shaking will not work.
          Another feature which will not work is module concatenation.



          Depending on the library you can try to recompile it.






          share|improve this answer
























          • Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

            – Artem
            Dec 13 '18 at 12:49












          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%2f53349078%2fhow-to-partially-include-node-js-module-using-rollup-with-commonjs-plugin%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









          1














          Tree shaking works only for ES6 modules. At least it's true for Webpack and I suppose for rollup as well. Your first definition is ES6, second is commonjs.



          Therefore if a library is not compiled/transpiled to ES6 modules tree shaking will not work.
          Another feature which will not work is module concatenation.



          Depending on the library you can try to recompile it.






          share|improve this answer
























          • Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

            – Artem
            Dec 13 '18 at 12:49
















          1














          Tree shaking works only for ES6 modules. At least it's true for Webpack and I suppose for rollup as well. Your first definition is ES6, second is commonjs.



          Therefore if a library is not compiled/transpiled to ES6 modules tree shaking will not work.
          Another feature which will not work is module concatenation.



          Depending on the library you can try to recompile it.






          share|improve this answer
























          • Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

            – Artem
            Dec 13 '18 at 12:49














          1












          1








          1







          Tree shaking works only for ES6 modules. At least it's true for Webpack and I suppose for rollup as well. Your first definition is ES6, second is commonjs.



          Therefore if a library is not compiled/transpiled to ES6 modules tree shaking will not work.
          Another feature which will not work is module concatenation.



          Depending on the library you can try to recompile it.






          share|improve this answer













          Tree shaking works only for ES6 modules. At least it's true for Webpack and I suppose for rollup as well. Your first definition is ES6, second is commonjs.



          Therefore if a library is not compiled/transpiled to ES6 modules tree shaking will not work.
          Another feature which will not work is module concatenation.



          Depending on the library you can try to recompile it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 18:00









          norekhovnorekhov

          1,5341023




          1,5341023













          • Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

            – Artem
            Dec 13 '18 at 12:49



















          • Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

            – Artem
            Dec 13 '18 at 12:49

















          Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

          – Artem
          Dec 13 '18 at 12:49





          Seems there are tree shaking should work, but it depends on the CommonJS module itself - see github.com/rollup/rollup-plugin-commonjs/issues/362

          – Artem
          Dec 13 '18 at 12:49




















          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%2f53349078%2fhow-to-partially-include-node-js-module-using-rollup-with-commonjs-plugin%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

          Bressuire

          Vorschmack

          Quarantine