Skip to content

Commit

Permalink
Fix loading modules with AMD defines (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored May 4, 2018
1 parent 57be4e6 commit 674b17d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/assets/JSAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SourceMap = require('../SourceMap');

const IMPORT_RE = /\b(?:import\b|export\b|require\s*\()/;
const ENV_RE = /\b(?:process\.env)\b/;
const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer)\b/;
const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer|define)\b/;
const FS_RE = /\breadFileSync\b/;
const SW_RE = /\bnavigator\s*\.\s*serviceWorker\s*\.\s*register\s*\(/;
const WORKER_RE = /\bnew\s*Worker\s*\(/;
Expand Down
7 changes: 6 additions & 1 deletion src/visitors/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ const VARS = {
Buffer: asset => {
asset.addDependency('buffer');
return 'var Buffer = require("buffer").Buffer;';
}
},
// Prevent AMD defines from working when loading UMD bundles.
// Ideally the CommonJS check would come before the AMD check, but many
// existing modules do the checks the opposite way leading to modules
// not exporting anything to Parcel.
define: () => 'var define;'
};

module.exports = {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/define-amd/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if (typeof define === 'function' && define.amd) {
define(function () {
return 4;
});
} else if (typeof module === 'object') {
module.exports = 2;
}
12 changes: 12 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,4 +884,16 @@ describe('javascript', function() {
const ctx = run(b, null, {require: false});
assert.equal(ctx.window.testing(), 'Test!');
});

it('should set `define` to undefined so AMD checks in UMD modules do not pass', async function() {
let b = await bundle(__dirname + '/integration/define-amd/index.js');
let test;
const mockDefine = function(f) {
test = f();
};
mockDefine.amd = true;

run(b, {define: mockDefine});
assert.equal(test, 2);
});
});

0 comments on commit 674b17d

Please sign in to comment.