Skip to content

Commit

Permalink
Detection for files with byte order mark (BOM)
Browse files Browse the repository at this point in the history
If files begin with a BOM (U+FEFF) the AMD and CommonJS detection will fail. I noticed this when using the SystemJS builder: systemjs/builder#25
  • Loading branch information
marcfallows committed Nov 19, 2014
1 parent 0b8527b commit 3b1e768
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/extension-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function amd(loader) {
// AMD Module Format Detection RegEx
// define([.., .., ..], ...)
// define(varName); || define(function(require, exports) {}); || define({})
var amdRegEx = /(?:^|[^$_a-zA-Z\xA0-\uFFFF.])define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*(("[^"]+"|'[^']+')\s*,|\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*(\s*("[^"]+"|'[^']+')\s*,?)?(\s*(\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*\s*\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/;
var amdRegEx = /(?:^\uFEFF?|[^$_a-zA-Z\xA0-\uFFFF.])define\s*\(\s*("[^"]+"\s*,\s*|'[^']+'\s*,\s*)?\s*(\[(\s*(("[^"]+"|'[^']+')\s*,|\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*(\s*("[^"]+"|'[^']+')\s*,?)?(\s*(\/\/.*\r?\n|\/\*(.|\s)*?\*\/))*\s*\]|function\s*|{|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*\))/;
var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;

var cjsRequirePre = "(?:^|[^$_a-zA-Z\\xA0-\\uFFFF.])";
Expand Down
4 changes: 2 additions & 2 deletions lib/extension-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ function cjs(loader) {

// CJS Module Format
// require('...') || exports[''] = ... || exports.asd = ... || module.exports = ...
var cjsExportsRegEx = /(?:^|[^$_a-zA-Z\xA0-\uFFFF.]|module\.)(exports\s*\[['"]|\exports\s*\.)|(?:^|[^$_a-zA-Z\xA0-\uFFFF.])module\.exports\s*\=/;
var cjsExportsRegEx = /(?:^\uFEFF?|[^$_a-zA-Z\xA0-\uFFFF.]|module\.)(exports\s*\[['"]|\exports\s*\.)|(?:^\uFEFF?|[^$_a-zA-Z\xA0-\uFFFF.])module\.exports\s*\=/;
// RegEx adjusted from https://github.com/jbrantly/yabble/blob/master/lib/yabble.js#L339
var cjsRequireRegEx = /(?:^|[^$_a-zA-Z\xA0-\uFFFF."'])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*')\s*\)/g;
var cjsRequireRegEx = /(?:^\uFEFF?|[^$_a-zA-Z\xA0-\uFFFF."'])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*')\s*\)/g;
var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;

function getCJSDeps(source) {
Expand Down
25 changes: 23 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ asyncTest('AMD detection test with comments', function() {
}, err);
});

asyncTest('AMD detection test with byte order mark (BOM)', function() {
System['import']('tests/amd-module-bom').then(function(m) {
ok(m.amd);
start();
}, err);
});

System.bundles['tests/amd-bundle'] = ['bundle-1', 'bundle-2'];
asyncTest('Loading an AMD bundle', function() {
System['import']('bundle-1').then(function(m) {
Expand Down Expand Up @@ -273,18 +280,32 @@ asyncTest('Loading a CommonJS module with this', function() {

asyncTest('CommonJS setting module.exports', function() {
System['import']('tests/cjs-exports').then(function(m) {
ok(m.e = 'export');
ok(m.e == 'export');
start();
}, err);
});

asyncTest('CommonJS detection variattion', function() {
asyncTest('CommonJS detection variation', function() {
System['import']('tests/commonjs-variation').then(function(m) {
ok(m.e === System.get('@empty'));
start();
}, err);
});

asyncTest('CommonJS detection test with byte order mark (BOM)', function() {
System['import']('tests/cjs-exports-bom').then(function(m) {
ok(m.foo == 'bar');
start();
}, err);
});

asyncTest('CommonJS module detection test with byte order mark (BOM)', function() {
System['import']('tests/cjs-module-bom').then(function(m) {
ok(m.foo == 'bar');
start();
}, err);
});

asyncTest('CommonJS require variations', function() {
System['import']('tests/commonjs-requires').then(function(m) {
ok(m.d1 == 'd');
Expand Down
4 changes: 4 additions & 0 deletions test/tests/amd-module-bom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
define([], function () {
// file starts with a byte order mark (BOM)
return { amd: true };
});
1 change: 1 addition & 0 deletions test/tests/cjs-exports-bom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.foo = "bar";
1 change: 1 addition & 0 deletions test/tests/cjs-module-bom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.foo = "bar";

0 comments on commit 3b1e768

Please sign in to comment.