-
Notifications
You must be signed in to change notification settings - Fork 739
Fix shelljs compatibility with esbuild bundler #1161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- shelljs#1160 - Add explicit file extension to dynamic command import. - This call to require() is dynamic and does not add the file extension. - Esbuild resolves files with their explicit file extension (eg './src/cat.js'). - Esbuild automatically adds .js extension in imports when not present but can't modify dynamic imports. For dynamic imports, file extension needs to be explicitly specified prior to bundling.
Have you tested this change on other bundlers? I'm not familiar with the current state of bundlers in the community. Is esbuild a popular one? |
I've tested:
For esbuild, details are in the issue #1160 rollupWith Reproducer// rollup.config.js
var commonjs = require('@rollup/plugin-commonjs');
var nodeResolve = require('@rollup/plugin-node-resolve').nodeResolve;
module.exports = {
input: 'index.js',
output: {
file: 'out.js',
format: 'cjs',
},
plugins: [commonjs({
dynamicRequireTargets: [
'node_modules/shelljs/src/*.js',
],
}), nodeResolve()],
};
// index.js
var shelljs = require('shelljs');
console.log(shelljs.pwd().stdout && '\n OK');
// bash
//$ npx rollup -c rollup.config.js nccWith |
Hello @nfischer , is there anything I can do to help make this change go live? |
Would also love to get this in.shelljs is very useful for github actions |
I am also highly interested in this, shelljs would be even more useful if it could be bundled! @nfischer |
I tried the fix proposed here, but it did not work in my setup with esbuild. However, just manually writing the |
I have the same issue. It's very common to use |
I'm willing to revisit this, however I need someone to show me how to test this change to make sure it actually works. I also see #1119 was raised for a similar issue, so I'm not sure which is the better approach. |
Hello @nfischer , thanks for answering!
As a general rule of thumb, I say dynamic imports is something to avoid because that will always mess with build-time tree shaking. Bundlers can cope with this but this relies on blindly putting all the file content in the bundle. TBH moving to static imports would be the preferred approach if it was my project, but I also think my change is fine if you want to keep the dynamic import.
For testing, I've put there a reproducer for ESBuild, do you want a reproducer for other bundlers? |
Summary
Fixes #1160
Test
Here is a script to reproduce (to be run in the repository root):
In the current master branch of this repository:
In this branch
The bundle size doesn't change because all deps are already in the bundle, it's a resolution issue, as discussed in #1160