title: Make webpack resolve context dependencies of the follow kind: require(someVariable)
body: <p>I was trying to bundle a <code>nestjs</code> application to run it in a lambda. But gave up. After a while I tried to do the same with a <a href="https://github.com/x-yuri/nestjs-mysql-webpack" rel="nofollow noreferrer">freshly created <code>nestjs</code> application</a> that connects to <code>mysql</code>. The <a href="https://github.com/sequelize/sequelize/issues/9489" rel="nofollow noreferrer">problem</a> was that <code>sequelize</code> requires <code>mysql2</code> <a href="https://github.com/sequelize/sequelize/blob/v6.11.0/lib/dialects/abstract/connection-manager.js#L66-L86" rel="nofollow noreferrer">this way</a>:</p>
  <pre><code>require(moduleName);
  </code></pre>
  <p>Well, it requires the dialect I'm asking it to require, but in this case it was <code>mysql2</code>. Apparently <code>webpack</code> can't do much about it on its own and bails out. I resolved it <a href="https://github.com/x-yuri/nestjs-mysql-webpack/commit/4037d12b737fbf976bf20d3de04131470e504a52#diff-089f4f2474b64391c42b6e66aed33977e132058d92108f0a63234a7862e1f8b8" rel="nofollow noreferrer">as they suggested</a>. But then I thought, &quot;I wonder if <code>webpack</code> can replace a specific line in a specific file?&quot; Or rather pretend that it differs. Pretend that instead of <code>require(moduleName)</code> it reads <code>require('mysql2')</code>.</p>
  <p>There's a <a href="https://stackoverflow.com/questions/51338574/how-can-i-replace-files-at-compile-time-using-webpack">similar question</a>, but I'm specifically targeting <code>nodejs</code>. And I'm concerned about what one might call a <a href="https://webpack.js.org/guides/dependency-management/#require-with-expression" rel="nofollow noreferrer">context dependency</a> in <code>webpack</code>-speak. A dependency whose request is an expression, not a string.</p>
  <p><a href="https://webpack.js.org/plugins/context-replacement-plugin/" rel="nofollow noreferrer"><code>ContextReplacementPlugin</code></a> can't be applied here, because for a single identifier the request is always <code>.</code> (such requests are indistinguishible). <a href="https://webpack.js.org/plugins/normal-module-replacement-plugin/" rel="nofollow noreferrer"><code>NormalReplacementPlugin</code></a> isn't applicable because that's a context dependency. And <a href="https://webpack.js.org/plugins/define-plugin/" rel="nofollow noreferrer"><code>DefinePlugin</code></a> doesn't seem to be up to the task, because it doesn't let you replace whatever you like, particularly local variables, and function parameters.</p>
  <p>If you're interested in bundling a <code>nodejs</code> application, you might want to check out <a href="https://stackoverflow.com/questions/59635276/how-to-correctly-build-nestjs-app-for-production-with-node-modules-dependencies/59689862#59689862">this question</a>. Here I'm concerned with <code>webpack</code> and context dependencies.</p>
  <p>P.S. Although I found a solution that doesn't require resolving context dependencies, I might run into it down the road. Or somebody else.</p>
  <p><strong>UPD</strong> And here's a case that can't be worked around like <code>nestjs</code> + <code>sequelize</code> + <code>mysql2</code>. <code>sequelize-typescript</code> loads models at runtime:</p>
  <p><a href="https://github.com/RobinBuschmann/sequelize-typescript/blob/v2.1.1/src/sequelize/sequelize/sequelize-service.ts#L51" rel="nofollow noreferrer">https://github.com/RobinBuschmann/sequelize-typescript/blob/v2.1.1/src/sequelize/sequelize/sequelize-service.ts#L51</a></p>