This does not work with dynamic or conditional module imports
See original GitHub issueSome Node modules do this:
try {
require('moduleName')
} catch (error) {}
Or this:
if (condition) {
require('moduleName')
}
eval(`require('moduleName')`)
Or this:
require(getModuleName())
A common example is node-fetch which conditionally require encoding. encoding is only used with the textConverted method, which throws if it’s missing.
Another example is @nestjs/graphql which uses apollo-server-core but does not declare it in its production dependencies.
The reason why some modules might want to do this and not use optionalDependencies is to allow users to opt-in to installing specific modules.
However this creates the following two issues with zip-it-and-ship-it. When the conditional require() is performed:
- Directly from a function file,
zip-it-and-ship-italways bundles the dependency. Users should have a way to exclude such modules in the archive if they want to. - From a node module,
zip-it-and-ship-itnever bundles the dependency. This is because we find nested dependencies by only checking thepackage.jsondependencies,peerDependenciesandoptionalDependencieskeys (as opposed to look forrequire()statements). Users should have a way to include such modules in the archive if they want to.
The current workaround are (for the points above):
- User should
npm installthe dependency - User should
npm installthe dependency and add a nooprequire()call in its function file code.
However this feels hacky, so we should think of a better solution.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:21 (7 by maintainers)
Top Results From Across the Web
Dynamically/conditionally importing JavaScript modules
Dynamic imports are a great way to specify dynamic paths to import modules, as well as conditionalise whether they should be imported at...
Read more >reactjs - Conditionally import module using next js dynamic ...
So it works fine in terms of downloading the bundle but and rendering the component BUT it doesn't do server side rendering of...
Read more >Dynamic, Conditional Imports - CSS-Tricks
Dynamic, Conditional Imports ... With ES Modules, you can natively import other JavaScript. Like confetti, duh: import confetti from 'https://cdn.
Read more >Dynamic imports - The Modern JavaScript Tutorial
First, we can't dynamically generate any parameters of import . The module path must be a primitive string, can't be a function call....
Read more >JavaScript modules - MDN Web Docs
Use of native JavaScript modules is dependent on the import and export ... but don't want to run into the problem described above,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Almost all function bundling solutions have the concept of
includes&excludesoptionsThis covered the missing pieces when trying to parse the AST or use recursive
package.jsonfiles to includes the exact files needed for deployment.example
We most likely need a higher level option for users to explicitly include/exclude files from the packaging step during the build
Little more background, when the remix request handler gets a request, it matches against the routes and then dynamically requires the “route loaders” (functions that fetch data server side for the view). We’re considering changing our build step to turn all of that into static requires in the entry of the function, but at the moment it’s dynamic.
An
includeoption would let us use netlify right away.