Skip to content

Commit

Permalink
expose format detection in builder
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 30, 2014
1 parent 52f22ba commit 9c4efde
Showing 1 changed file with 53 additions and 44 deletions.
97 changes: 53 additions & 44 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,54 @@ var metaPartRegEx = /\/\*.*\*\/|\/\/[^\n]*|"[^"]+"\s*;?|'[^']+'\s*;?/g;
// var commentRegEx = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;
var initialCommentRegEx = /^\s*(\/\*|\/\/)/;

function detectFormat(source) {
// first checkout if we have format meta
var meta = source.match(metaRegEx);
var metadata = {};
if (meta) {
var metaParts = meta[0].match(metaPartRegEx);
for (var i = 0; i < metaParts.length; i++) {
var len = metaParts[i].length;

var firstChar = metaParts[i].substr(0, 1);
if (metaParts[i].substr(len - 1, 1) == ';')
len--;

if (firstChar != '"' && firstChar != "'")
continue;

var metaString = metaParts[i].substr(1, metaParts[i].length - 3);

var metaName = metaString.substr(0, metaString.indexOf(' '));
if (metaName) {
var metaValue = metaString.substr(metaName.length + 1, metaString.length - metaName.length - 1);

if (metadata[metaName] instanceof Array)
metadata[metaName].push(metaValue);
else
metadata[metaName] = metaValue;
}
}
}

if (metadata.format)
return { format: metadata.format, meta: true };

cjsExportsRegEx.lastIndex = 0;
cjsRequireRegEx.lastIndex = 0;
if (source.match(es6RegEx))
return { format: 'es6' };

if (source.match(amdRegEx))
return { format: 'amd' };

if (cjsRequireRegEx.exec(source) || cjsExportsRegEx.exec(source))
return { format: 'cjs' };

return { format: 'global' };
}
exports.detectFormat = detectFormat;

/*
options.format
options.shim
Expand Down Expand Up @@ -370,54 +418,15 @@ exports.compileDir = function(dir, options) {
if (shimmed)
return;

// first checkout if we have format meta
var meta = source.match(metaRegEx);
var metadata = {};
if (meta) {
var metaParts = meta[0].match(metaPartRegEx);
for (var i = 0; i < metaParts.length; i++) {
var len = metaParts[i].length;

var firstChar = metaParts[i].substr(0, 1);
if (metaParts[i].substr(len - 1, 1) == ';')
len--;

if (firstChar != '"' && firstChar != "'")
continue;

var metaString = metaParts[i].substr(1, metaParts[i].length - 3);
var detected = detectFormat(source);

var metaName = metaString.substr(0, metaString.indexOf(' '));
if (metaName) {
var metaValue = metaString.substr(metaName.length + 1, metaString.length - metaName.length - 1);

if (metadata[metaName] instanceof Array)
metadata[metaName].push(metaValue);
else
metadata[metaName] = metaValue;
}
}
}

if (metadata.format) {
format = metadata.format;
// don't rewrite meta
if (detected.meta) {
format = detected.format;
return;
}

cjsExportsRegEx.lastIndex = 0;
cjsRequireRegEx.lastIndex = 0;
if (source.match(es6RegEx)) {
format = format || 'es6';
}
else if (source.match(amdRegEx)) {
format = format || 'amd';
}
else if (cjsRequireRegEx.exec(source) || cjsExportsRegEx.exec(source)) {
format = format || 'cjs';
}
else {
format = format || 'global';
}
format = detected.format;

changed = true;
source = '"format ' + format + '";' + nl + source;
Expand Down

0 comments on commit 9c4efde

Please sign in to comment.