Skip to content

Commit

Permalink
v0.3.3 - fix sourceMap generation
Browse files Browse the repository at this point in the history
  • Loading branch information
amarcruz committed Oct 24, 2016
1 parent f231a7b commit 77864e0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 97 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# rollup-plugin-jscc changes

### v0.3.3 - fix in jscc
- Using jscc v0.3.3 that fixes bug in sourceMap generation.

### v0.3.2 - sync with jscc
- From this release, the version number will be in sync with jscc.
- Updated `devDependencies`.
Expand Down
99 changes: 52 additions & 47 deletions dist/rollup-plugin-jscc.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ var _REPVARS = RegExp(((STRINGS.source) + "|" + (EVLVARS.source)), 'g');
* Method to perform the evaluation of the received string using
* a function instantiated dynamically.
*
* @param {object} ctx - Object with the current set of variables
* @param {string} str - String to evaluate, can include other defined vars
* @param {object} ctx - Set of variable definitions
* @returns {any} The result.
*/
function evalExpr (str, ctx) {
function evalExpr (ctx, str) {
var values = ctx.options.values;

// var replacement
var _repVars = function (m, p, v) {
return v
? p + (v in ctx ? ("this." + v) : v in global ? ("global." + v) : 'undefined')
? p + (v in values ? ("this." + v) : v in global ? ("global." + v) : 'undefined')
: m
};

Expand All @@ -52,10 +53,10 @@ function evalExpr (str, ctx) {
try {
// eslint-disable-next-line no-new-func
var fn = new Function('', ("return (" + expr + ");"));
result = fn.call(ctx);
result = fn.call(values);
} catch (e) {
e.message += " in expression: " + expr;
throw e
result = false;
ctx._emitError(((e.message) + " in expression \"" + expr + "\""));
}

return result
Expand Down Expand Up @@ -280,7 +281,7 @@ Parser.prototype = {
return ckey === 'ifnset' ? yes ^ 1 : yes
}
// returns the raw value of the expression
return evalExpr(expr, this.options.values)
return evalExpr(this, expr)
},

_set: function _set (s) {
Expand All @@ -289,7 +290,7 @@ Parser.prototype = {
var k = m[1];
var v = m[2];

this.options.values[k] = v ? evalExpr(v.trim(), this.options.values) : undefined;
this.options.values[k] = v ? evalExpr(this, v.trim()) : undefined;
} else {
this._emitError(("Invalid memvar assignment \"" + s + "\""));
}
Expand All @@ -305,7 +306,7 @@ Parser.prototype = {
},

_error: function _error (s) {
s = evalExpr(s, this.options.values);
s = evalExpr(this, s);
throw new Error(s)
}
};
Expand Down Expand Up @@ -341,7 +342,7 @@ function checkOptions (opts) {
}
});

// sequence starting a directive, default is `//|/*` (JS comment)
// sequence starting a directive
var prefixes = opts.prefixes;
if (!prefixes) {
opts.prefixes = ['//', '/*', '<!--'];
Expand Down Expand Up @@ -372,6 +373,8 @@ function parseOptions (file, opts) {

return {
sourceMap: opts.sourceMap !== false,
mapContent: opts.mapContent,
mapHires: opts.mapHires,
keepLines: opts.keepLines,
errorHandler: opts.errorHandler,
prefixes: opts.prefixes,
Expand All @@ -387,6 +390,9 @@ function remapVars (magicStr, values, str, start) {

re.lastIndex = 0; // `re` is global, so reset

// $1 = varname including the prefix '$'
// $2 = optional point + property name

while ((mm = re.exec(str))) {
var v = mm[1].slice(1);

Expand All @@ -397,7 +403,7 @@ function remapVars (magicStr, values, str, start) {
v = values[v];
if (p && p in v) {
v = v[p];
mm[1] += mm[2];
mm[1] = mm[0];
} else if (typeof v == 'object') {
v = JSON.stringify(v);
}
Expand Down Expand Up @@ -425,7 +431,6 @@ function preproc (code, filename, options) {

var changes = false;
var output = true;
var realStart = 0;
var hideStart = 0;
var lastIndex = 0;
var match, index;
Expand All @@ -436,26 +441,27 @@ function preproc (code, filename, options) {

index = match.index;

if (output && lastIndex < index) {
pushCache(code.slice(lastIndex, index), lastIndex);
if (output && lastIndex < index &&
pushCache(code.slice(lastIndex, index), lastIndex)) {
changes = true;
}

lastIndex = re.lastIndex;

if (output === parser.parse(match)) {
if (output) {
lastIndex = removeBlock(index, lastIndex);
changes = true;
}
} else if (output) {
// output ends, for now, all we do is to save
// the pos where the hidden block begins
hideStart = index;
output = false;
} else {
output = !output;
if (output) {
// output begins, remove the hidden block now
lastIndex = removeBlock(hideStart, lastIndex);
} else {
// output ends, for now, all we do is to save
// the pos where the hidden block begins
hideStart = index;
}
// output begins, remove the hidden block now
lastIndex = removeBlock(hideStart, lastIndex);
output = changes = true;
}

}
Expand All @@ -464,29 +470,36 @@ function preproc (code, filename, options) {
output = false;
}

if (output && code.length > lastIndex) {
pushCache(code.slice(lastIndex), lastIndex);
if (output && code.length > lastIndex &&
pushCache(code.slice(lastIndex), lastIndex)) {
changes = true;
}

// done, return an object if there was changes
if (changes) {
var result = {
code: magicStr.toString()
};
if (changes && options.sourceMap) {
result.map = magicStr.generateMap({ hires: true });
}
return result
// always returns an object
var result = {
code: changes ? magicStr.toString() : code
};

if (changes && options.sourceMap) {
result.map = magicStr.generateMap({
source: filename || null,
includeContent: options.mapContent !== false,
hires: options.mapHires !== false
});
}

return code
return result

// helpers ==============================================

function pushCache (str, start) {
if (str && ~str.indexOf('$_')) {
changes = remapVars(magicStr, options.values, str, start) || changes;
var change = str && ~str.indexOf('$_');

if (change) {
change = remapVars(magicStr, options.values, str, start);
}

return change
}

function removeBlock (start, end) {
Expand All @@ -495,22 +508,14 @@ function preproc (code, filename, options) {
if (options.keepLines) {
block = code.slice(start, end).replace(/[^\r\n]+/g, '');

// @TODO: Remove first jscc lines
} else if (start > realStart) {
--start;
if (code[start] === '\n' && code[start - 1] === '\r') {
--start;
}
} else if (end < code.length && /[\n\r]/.test(code[end])) {
} else if (end < code.length) {
++end;
if (code[end] === '\n' && code[end - 1] === '\r') {
++end;
}
realStart = end;
}
magicStr.overwrite(start, end, block);
changes = true;

magicStr.overwrite(start, end, block);
return end
}
}
Expand Down
Loading

0 comments on commit 77864e0

Please sign in to comment.