Skip to content
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

Fix loading modules with AMD defines #1285

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});