mirror of
https://github.com/zhigang1992/esbuild.git
synced 2026-01-12 22:46:54 +08:00
fix #455: "module.require" forwards to "require"
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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'], {
|
||||
|
||||
Reference in New Issue
Block a user