{"path": "/posts/problems-when-reexporting-javascript-modules/", "title": "problems when reexporting javascript modules · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"problems when reexporting javascript modules\">\n  <title>problems when reexporting javascript modules · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/problems-when-reexporting-javascript-modules/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"problems when reexporting javascript modules\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/problems-when-reexporting-javascript-modules/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2023-09-17T22:29:09Z\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/images/reexport.png\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/problems-when-reexporting-javascript-modules/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>problems when reexporting javascript modules</h1>\n\n      <p><time datetime=\"2023-09-17T22:29:09Z\">2023-09-17</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<p>we all get excited when we see an interesting trick.</p>\n<p>some are &quot;one-liners&quot;, or an exotic optimization...</p>\n<p>but everytime there is something that looks convenient,\nit will probably collect your soul later...</p>\n<p>some people don't like when their files starts with a\nhuge list of imports. I can agree that is not that aesthetic.</p>\n<p>to avoid huge lists of imports, we can use a file to reexport\nall the modules into a single entry.</p>\n<pre><code class=\"language-js\">import {\n    a,\n    b,\n    c,\n    d,\n    e,\n    f,\n    g,\n    h,\n    i,\n    j,\n    k,\n    l,\n    m,\n    n,\n    o,\n    p,\n    q,\n    r,\n    s,\n    t,\n    u,\n    v,\n    x,\n    z }\n    from &quot;@/components&quot;;\n</code></pre>\n<p>well...that doesn't look good either.</p>\n<p>here is a list of problems you may found when reexporting\nmodules:</p>\n<ul>\n<li>circular dependencies</li>\n<li>loading more resource than necessary</li>\n<li>import modules that instantiate objects on top-level</li>\n<li>test pollution and unnecessary mocking</li>\n</ul>\n<h3>circular dependencies</h3>\n<p>If you have a file that exports everything,\nyou better use it!</p>\n<pre><code class=\"language-js\">// Component A\n\nimport { B, C } from &quot;@/components&quot;;\n</code></pre>\n<pre><code class=\"language-js\">// Component C\nimport { A } from &quot;@/components&quot;;\n</code></pre>\n<pre><code class=\"language-js\">// Components (syntax of this file is not important...)\n\n/* correct import order */\nexport { default as C } from &quot;./ComponentC&quot;;\nexport { default as B } from &quot;./ComponentB&quot;;\nexport { default as A } from &quot;./ComponentA&quot;;\n\n/* incorrect import order */\nexport { default as A } from &quot;./ComponentA&quot;;\nexport { default as B } from &quot;./ComponentB&quot;;\nexport { default as C } from &quot;./ComponentC&quot;;\n</code></pre>\n<p>depending on the order of the imports,\nyou may not see the problem...</p>\n<p>but when it's wrong, you will probably spend hours trying\nto remember what is going on.</p>\n<p>some will say that a simple solution to fix this is that\n&quot;components should not use the file that reexports everything&quot;,\nor basically getting back from where we started.</p>\n<p>this is the first rule we need to make the entire team to follow\njust to have a single file.</p>\n<h3>loading more resource than necessary</h3>\n<p>getting the tools to properly do a &quot;tree-shake&quot;\nor to better bundle the application is the last thing\nI want to spend time.</p>\n<p>this makes it harder for the compiler to decide what to do.\nso we are starting to introduce problems to create tools\n(we don't need), just to have a file to group modules.</p>\n<h3>import modules that instantiate objects on top-level</h3>\n<p>once you import a module, if it evaluates an expressions on the top-level,\nthis work can be unncessary an slow down specially tests.</p>\n<p>it is almost impossible to mock or to avoid running the code,\nor, you will end up writing a lot of unnecessary code\nto &quot;skip this code when testing&quot;.</p>\n<h3>test pollution and unnecessary mocking</h3>\n<p>as you may have modules that instantiates objects on the top-level,\nthey are kinda unpredictable or you may know, and now, you have\nto create a general mocking file that every test must import.</p>\n<p>this creates all sorts of problems:</p>\n<ul>\n<li>people assuming the mock has &quot;correct&quot; data</li>\n<li>incomplete use cases testing because &quot;it just works&quot;</li>\n</ul>\n<p>so, just because of this trick, we are risking to introduce\na lot of unnecessary code and all kinds of problems...</p>\n<p>this doesn't look like a good idea...</p>\n<p><a href=\"https://codesandbox.io/s/javascript-reexporting-problems-vzmw5h\">codesandbox with some problems</a></p>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
