-
Notifications
You must be signed in to change notification settings - Fork 744
Description
Node v20
ShellJS 0.8.5
Related issues
- Explicitly require the commands to help JS bundlers find all the code #962
- error with bundled (esbuild) code: cannot find module ./src/cat.js #1047
- Explicitly require commands #1119
- Bundling issues due to library dependency on shelljs kubernetes-client/javascript#944
- Revert azure-pipelines-task-lib to 2.12.2 to mitigate shelljs bundling issue aws/aws-toolkit-azure-devops#539
- Mark shelljs as external for esbuild bundling aws/aws-toolkit-azure-devops#537
- Cannot use JS bundler because of shelljs dependency microsoft/azure-pipelines-task-lib#942
Description of the bug
This library can't be bundled with esbuild
Cause
Line 25 in 3529d50
| require('./src/' + command); |
The combination of dynamic calls to require() and omiting .js extension in require() breaks bundlers. Bundlers can't know whether dynamic imports will include or not the file extension, so some of them (eg esbuild) assume it is included.
If we open the generated bundle, we can see that all required modules are there (src/cat.js, etc) but referenced with their extensions (eg src/cat.js @ L4684)

The dynamic require() is included as-is in the bundle (@ L4762):

But this fails at runtime with
Error: Module not found in bundle: ./src/cat
Because ./src/cat does not exist, ./src/cat.js does.
How to fix
To fix this, shell.js @ L25 can be modified to add the .js file extension. For consistency, all other calls to require could also use the .js extension in calls to require.
I do not know what solution you prefer @nfischer . Happy to contribute on this if you give me pointers to how you think it should be done.
Workaround
I've made an esbuild plugin to patch shelljs to unblock me as soon as now: