<p dir="auto">I am trying to build a zip file only containing the code I need for production deployment.  Smaller builds benefit nearly all production users: cheaper to store indefinitely, quicker to roll out after build finish, faster to load cold start in serverless, and for large projects it is easy to go over serverless dist limits without optimized distribution.    I believe rollout is the best option for getting an optimized build.</p>
<p dir="auto">The good news, running rollup turns my 60m dist file into 6m.  I believe this concept will be a huge win for people who want their next.js app to start as quick as possible.</p>
<p dir="auto">Bad news: When I try to run rollup on my next.js build, I get a circular dependency issue that I believe stops the result from running:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(!) Circular dependency: node_modules\next\dist\lib\router\index.js -&gt; node_modules\next\dist\lib\router\router.js -&gt; node_modules\next\dist\lib\router\index.js
(!) Circular dependency: node_modules\next\dist\lib\router\index.js -&gt; node_modules\next\dist\lib\router\router.js -&gt;  commonjs-proxy:&lt;FULL_PATH_TO_HERE&gt;\node_modules\next\dist\lib\router\index.js -&gt; node_modules\next\dist\lib\router\index.js"><pre class="notranslate"><code class="notranslate">(!) Circular dependency: node_modules\next\dist\lib\router\index.js -&gt; node_modules\next\dist\lib\router\router.js -&gt; node_modules\next\dist\lib\router\index.js
(!) Circular dependency: node_modules\next\dist\lib\router\index.js -&gt; node_modules\next\dist\lib\router\router.js -&gt;  commonjs-proxy:&lt;FULL_PATH_TO_HERE&gt;\node_modules\next\dist\lib\router\index.js -&gt; node_modules\next\dist\lib\router\index.js
</code></pre></div>
<p dir="auto">Would it be possible to not have a circular dependency between index.js and routes.js?</p>
<p dir="auto">Here are the files I am using for testing this, hopefully this might help someone more experienced carry on from where I am stuck:</p>
<p dir="auto">rollup.config.js</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import babel from &quot;rollup-plugin-babel&quot;;
import commonjs from &quot;rollup-plugin-commonjs&quot;;
import resolve from &quot;rollup-plugin-node-resolve&quot;;
import replace from &quot;rollup-plugin-replace&quot;;
import json from 'rollup-plugin-json';

const NODE_ENV = process.env.NODE_ENV || &quot;development&quot;;
const outputFile = NODE_ENV === &quot;production&quot; ? &quot;./lib/prod.js&quot; : &quot;./lib/dev.js&quot;;

export default {
    input: &quot;./server.js&quot;,
    output: {
        file: outputFile,
        format: &quot;cjs&quot;
    },
    plugins: [
        replace({
            &quot;process.env.NODE_ENV&quot;: JSON.stringify(NODE_ENV)
        }),
        json(),
        babel({
            exclude: [&quot;node_modules/**&quot;]
        }),
        resolve({
            preferBuiltins: true
        }),
        commonjs()
    ],
    external: [&quot;webpack&quot;, &quot;readable-stream&quot;, &quot;glob&quot;, &quot;source-list-map&quot;, &quot;through2&quot; ]
};"><pre class="notranslate"><code class="notranslate">import babel from "rollup-plugin-babel";
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import replace from "rollup-plugin-replace";
import json from 'rollup-plugin-json';

const NODE_ENV = process.env.NODE_ENV || "development";
const outputFile = NODE_ENV === "production" ? "./lib/prod.js" : "./lib/dev.js";

export default {
    input: "./server.js",
    output: {
        file: outputFile,
        format: "cjs"
    },
    plugins: [
        replace({
            "process.env.NODE_ENV": JSON.stringify(NODE_ENV)
        }),
        json(),
        babel({
            exclude: ["node_modules/**"]
        }),
        resolve({
            preferBuiltins: true
        }),
        commonjs()
    ],
    external: ["webpack", "readable-stream", "glob", "source-list-map", "through2" ]
};
</code></pre></div>
<p dir="auto">(external has a few exclusions due to circular dependencies that need to be taken up with those package owners.)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&quot;devDependencies&quot;: {
    ...
    &quot;rollup&quot;: &quot;^0.66.4&quot;,
    &quot;rollup-plugin-babel&quot;: &quot;^4.0.3&quot;,
    &quot;rollup-plugin-commonjs&quot;: &quot;^9.1.8&quot;,
    &quot;rollup-plugin-json&quot;: &quot;^3.1.0&quot;,
    &quot;rollup-plugin-node-resolve&quot;: &quot;^3.4.0&quot;,
    &quot;rollup-plugin-replace&quot;: &quot;^2.1.0&quot;
  }"><pre class="notranslate"><code class="notranslate">"devDependencies": {
    ...
    "rollup": "^0.66.4",
    "rollup-plugin-babel": "^4.0.3",
    "rollup-plugin-commonjs": "^9.1.8",
    "rollup-plugin-json": "^3.1.0",
    "rollup-plugin-node-resolve": "^3.4.0",
    "rollup-plugin-replace": "^2.1.0"
  }
</code></pre></div>
<p dir="auto">create rollup (note circular dependency issue)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="NODE_ENV=production rollup -c"><pre class="notranslate"><code class="notranslate">NODE_ENV=production rollup -c
</code></pre></div>
<p dir="auto">to run</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="node lib/prod.js"><pre class="notranslate"><code class="notranslate">node lib/prod.js
</code></pre></div>
<p dir="auto">error I get but I suspect it is because of circular dependency</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="C:\Users\Adam\code\gell\gell-client-west\lib\prod.js:135129
const childModule  = commonjsRequire.resolve('./child/index');
                                     ^

TypeError: commonjsRequire.resolve is not a function
    at Object.&lt;anonymous&gt; (lib\prod.js:135129:38)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
"><pre class="notranslate"><code class="notranslate">C:\Users\Adam\code\gell\gell-client-west\lib\prod.js:135129
const childModule  = commonjsRequire.resolve('./child/index');
                                     ^

TypeError: commonjsRequire.resolve is not a function
    at Object.&lt;anonymous&gt; (lib\prod.js:135129:38)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)

</code></pre></div>
<p dir="auto">Can anyone weigh in on:</p>
<ul dir="auto">
<li>how hard it would be to rewrite next\dist\lib\router\index.js -&gt; next\dist\lib\router\router.js without circular dependency?  Could the router.js code just be included directly into index.js?</li>
<li>Is "commonjsRequire.resolve is not a function" error due to the circular dependency or something else?</li>
</ul>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://github.com/zeit/next.js/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h2 dir="auto">Expected Behavior</h2>


<p dir="auto">When I was reading source codes of next.js, I found an interesting place, <a href="https://github.com/zeit/next.js/blob/canary/lib/router/router.js#L138">https://github.com/zeit/next.js/blob/canary/lib/router/router.js#L138</a>.</p>
<p dir="auto">We'd better to return <code class="notranslate">true</code> there? Those codes were introduced by <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="209571735" data-permission-text="Title is private" data-url="https://github.com/vercel/next.js/issues/1250" data-hovercard-type="pull_request" data-hovercard-url="/vercel/next.js/pull/1250/hovercard" href="https://github.com/vercel/next.js/pull/1250">#1250</a>.</p>
<h2 dir="auto">Current Behavior</h2>


<p dir="auto">Return without explicit boolean value.</p>