I was trying to bundle a nestjs
application to run it in a lambda. But gave up. After a while I tried to do the same with a freshly created nestjs
application that connects to mysql
. The problem was that sequelize
requires mysql2
this way:
require(moduleName);
Well, it requires the dialect I'm asking it to require, but in this case it was mysql2
. Apparently webpack
can't do much about it on its own and bails out. I resolved it as they suggested. But then I thought, "I wonder if webpack
can replace a specific line in a specific file?" Or rather pretend that it differs. Pretend that instead of require(moduleName)
it reads require('mysql2')
.
There's a similar question, but I'm specifically targeting nodejs
. And I'm concerned about what one might call a context dependency in webpack
-speak. A dependency whose request is an expression, not a string.
ContextReplacementPlugin
can't be applied here, because for a single identifier the request is always .
(such requests are indistinguishible). NormalReplacementPlugin
isn't applicable because that's a context dependency. And DefinePlugin
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.
If you're interested in bundling a nodejs
application, you might want to check out this question. Here I'm concerned with webpack
and context dependencies.
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.
UPD And here's a case that can't be worked around like nestjs
+ sequelize
+ mysql2
. sequelize-typescript
loads models at runtime: