mirror of
https://github.com/systemjs/systemjs.git
synced 2026-01-18 14:53:14 +00:00
commit
be199a22c2
40
lib/cjs.js
40
lib/cjs.js
@ -7,30 +7,42 @@
|
||||
var cjsExportsRegEx = /(?:^\uFEFF?|[^$_a-zA-Z\xA0-\uFFFF.]|module\.)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 = /(?:^\uFEFF?|[^$_a-zA-Z\xA0-\uFFFF."'])require\s*\(\s*("[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*')\s*\)/g;
|
||||
var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;
|
||||
var commentRegEx = /(^|[^\\])(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;
|
||||
|
||||
var stringRegEx = /(?:"[^"\\\n\r]*(?:\\.[^"\\\n\r]*)*"|'[^'\\\n\r]*(?:\\.[^'\\\n\r]*)*')/g;
|
||||
|
||||
function getCJSDeps(source) {
|
||||
cjsRequireRegEx.lastIndex = commentRegEx.lastIndex = 0;
|
||||
cjsRequireRegEx.lastIndex = commentRegEx.lastIndex = stringRegEx.lastIndex = 0;
|
||||
|
||||
var deps = [];
|
||||
|
||||
// track comments in the source
|
||||
var match;
|
||||
|
||||
var commentLocations = [];
|
||||
|
||||
// track string and comment locations for unminified source
|
||||
var stringLocations = [], commentLocations = [];
|
||||
|
||||
function inLocation(locations, match, starts) {
|
||||
var inLocation = false;
|
||||
for (var i = 0; i < locations.length; i++)
|
||||
if (locations[i][0] < match.index && locations[i][1] > match.index + (!starts ? match[0].length : 0))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (source.length / source.split('\n').length < 200) {
|
||||
while (match = commentRegEx.exec(source))
|
||||
commentLocations.push([match.index, match.index + match[0].length]);
|
||||
while (match = stringRegEx.exec(source))
|
||||
stringLocations.push([match.index, match.index + match[0].length]);
|
||||
|
||||
while (match = commentRegEx.exec(source)) {
|
||||
// only track comments not starting in strings
|
||||
if (!inLocation(stringLocations, match, true))
|
||||
commentLocations.push([match.index, match.index + match[0].length]);
|
||||
}
|
||||
}
|
||||
|
||||
while (match = cjsRequireRegEx.exec(source)) {
|
||||
// ensure we're not in a comment location
|
||||
var inComment = false;
|
||||
for (var i = 0; i < commentLocations.length; i++) {
|
||||
if (commentLocations[i][0] < match.index && commentLocations[i][1] > match.index + match[0].length)
|
||||
inComment = true;
|
||||
}
|
||||
if (!inComment)
|
||||
// ensure we're not within a string or comment location
|
||||
if (!inLocation(stringLocations, match) && !inLocation(commentLocations, match))
|
||||
deps.push(match[1].substr(1, match[1].length - 2));
|
||||
}
|
||||
|
||||
|
||||
@ -365,8 +365,9 @@ asyncTest('CommonJS require variations', function() {
|
||||
ok(m.d1 == 'd');
|
||||
ok(m.d2 == 'd');
|
||||
ok(m.d3 == "require('not a dep')");
|
||||
// ok(m.d4 == "text require('still not a dep') text");
|
||||
// ok(m.d5 == 'text \'quote\' require("yet still not a dep")');
|
||||
ok(m.d4 == "text/* require('still not a dep') text");
|
||||
ok(m.d5 == 'text \'quote\' require("yet still not a dep")');
|
||||
ok(m.d6 == 'd6');
|
||||
start();
|
||||
}, err);
|
||||
});
|
||||
|
||||
1
test/tests/commonjs-d2.js
Normal file
1
test/tests/commonjs-d2.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = 'd6';
|
||||
@ -5,8 +5,16 @@ exports.d1 = require(
|
||||
exports.d2 = (require
|
||||
("./commonjs-d.js"));
|
||||
|
||||
var regex = / \/* /;
|
||||
|
||||
exports.d3 = "require('not a dep')";
|
||||
|
||||
// exports.d4 = "text require('still not a dep') text";
|
||||
exports.d4 = "text/* require('still not a dep') text";
|
||||
|
||||
// exports.d5 = 'text \'quote\' require("yet still not a dep")';
|
||||
exports.d5 = 'text \'quote\' require("yet still not a dep")';
|
||||
|
||||
var regexWithString = /asdfasdf " /;
|
||||
|
||||
exports.d6 = require('./commonjs-d2.js');
|
||||
|
||||
var regexClose = /asdf " */;
|
||||
Loading…
x
Reference in New Issue
Block a user