fix #455: "module.require" forwards to "require"

This commit is contained in:
Evan Wallace
2020-10-13 22:24:11 -07:00
parent adc2f2ff93
commit 9dbe4c8793
6 changed files with 28 additions and 7 deletions

View File

@@ -1,5 +1,13 @@
# Changelog
## Unreleased
* `module.require` forwards to the host's `require` ([#455](https://github.com/evanw/esbuild/issues/455))
Some packages such as [apollo-server](https://github.com/apollographql/apollo-server) use `module.require` with the intent of bypassing the bundler's `require` and calling the underlying function from `node` instead. Unfortunately that doesn't work because esbuild recognizes CommonJS module syntax and `module` has special meaning for CommonJS modules.
To get this package to work, `module.require` now forwards to `require` in the host environment. This will be the underlying `require` function in node and will be `window.require` in the browser, which will crash unless you provide a polyfill yourself.
## 0.7.15
* Lower `export * as` syntax for ES2019 and below

View File

@@ -1051,7 +1051,7 @@ console.log(shared_default);
================================================================================
TestMinifiedBundleCommonJS
---------- /out.js ----------
var n=e(r=>{r.foo=function(){return 123}});var u=e((j,t)=>{t.exports={test:!0}});const{foo:c}=n();console.log(c(),u());
var t=r(n=>{n.foo=function(){return 123}});var s=r((l,u)=>{u.exports={test:!0}});const{foo:f}=t();console.log(f(),s());
================================================================================
TestMinifiedBundleES6

View File

@@ -113,11 +113,11 @@ module.exports = require_entry();
TestExportSelfCommonJSMinified
---------- /out.js ----------
// /entry.js
var e = n((t, r) => {
r.exports = {foo: 123};
console.log(e());
var r = s((c, l) => {
l.exports = {foo: 123};
console.log(r());
});
module.exports = e();
module.exports = r();
================================================================================
TestExportSelfES6

View File

@@ -270,7 +270,7 @@ console.log(a, b, c, d, e, real);
================================================================================
TestTSMinifiedBundleCommonJS
---------- /out.js ----------
var n=e(r=>{r.foo=function(){return 123}});var u=e((j,t)=>{t.exports={test:!0}});const{foo:c}=n();console.log(c(),u());
var t=r(n=>{n.foo=function(){return 123}});var s=r((l,u)=>{u.exports={test:!0}});const{foo:f}=t();console.log(f(),s());
================================================================================
TestTSMinifiedBundleES6

View File

@@ -46,10 +46,14 @@ func code(isES6 bool) string {
return target
}
// Some libraries such as "apollo-server" use "module.require" to bypass
// the bundler and get at the underlying "require" function from node
export var __require = path => require(path)
// Wraps a CommonJS closure and returns a require() function
export var __commonJS = (callback, module) => () => {
if (!module) {
module = {exports: {}}
module = {exports: {}, require: __require}
callback(module.exports, module)
}
return module.exports

View File

@@ -111,6 +111,15 @@
}),
)
// Test CommonJS "module.require" bypassing the bundler's require
tests.push(
test(['--bundle', 'in.js', '--outfile=out.js', '--format=cjs'], {
'in.js': `export {foo} from './foo'`,
'foo.js': `exports.foo = module.require('fs').exists`,
'node.js': `if (require('./out').foo !== require('fs').exists) throw 'fail'`,
}),
)
// Test internal CommonJS export
tests.push(
test(['--bundle', 'in.js', '--outfile=node.js'], {