Skip to content

Commit

Permalink
fix #455: "module.require" forwards to "require"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 14, 2020
1 parent adc2f2f commit 9dbe4c8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/bundler/snapshots/snapshots_importstar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'], {
Expand Down

0 comments on commit 9dbe4c8

Please sign in to comment.