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

add sourcemaps support for @if directives. #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ The syntax type of source string to preprocess. There are 3 main syntax variants
`ts`, `tsx`, `peg`, `pegjs`, `jade`, `styl`
- `coffee`, aliases: `bash`, `shell`, `sh`

##### options.supportSourceMaps
Type: `Boolean`
Default: `false`

**Note:** Currently only the @if directive is supported.

When set to `true`, the preprocessor will replace excluded lines with empty lines, by that maintaining correct line
mapping when sourcemaps are generated.

### preprocessFile(srcFile, destFile[, context[, callback[, options]]])

Preprocesses a `sourceFile` and saves the result to `destFile`. Simple wrapper around `fs.readFile()` and `fs.writeFile()`.
Expand Down
20 changes: 17 additions & 3 deletions lib/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function preprocess(src, context, typeOrOptions) {
options.fileNotFoundSilentFail = typeOrOptions.fileNotFoundSilentFail || options.fileNotFoundSilentFail;
options.srcEol = typeOrOptions.srcEol || options.srcEol;
options.type = delim[typeOrOptions.type] || options.type;
options.supportSourceMaps = typeOrOptions.supportSourceMaps || false;
}

context = copy(context);
Expand Down Expand Up @@ -160,17 +161,30 @@ function preprocessor(src, context, opts, noRestoreEol) {
});
}

function compensateDirectiveRemoval(content){
return opts.supportSourceMaps ? opts.srcEol + content + opts.srcEol : content;
}

function getSourceMapCompatiableReplacementBlock(content) {
if (opts.supportSourceMaps) {
var lineCount = content.split(/\r\n|\r|\n/).length;
return Array(lineCount).join(opts.srcEol);
} else {
return '';
}
}

if (opts.type.if) {
rv = replaceRecursive(rv, opts.type.if, function (startMatches, endMatches, include, recurse) {
var variant = startMatches[1];
var test = (startMatches[2] || '').trim();
switch(variant) {
case 'if':
return testPasses(test,context) ? recurse(include) : '';
return compensateDirectiveRemoval(testPasses(test,context) ? recurse(include) : getSourceMapCompatiableReplacementBlock(include));
case 'ifdef':
return typeof getDeepPropFromObj(context, test) !== 'undefined' ? recurse(include) : '';
return compensateDirectiveRemoval(typeof getDeepPropFromObj(context, test) !== 'undefined' ? recurse(include) : getSourceMapCompatiableReplacementBlock(include));
case 'ifndef':
return typeof getDeepPropFromObj(context, test) === 'undefined' ? recurse(include) : '';
return compensateDirectiveRemoval(typeof getDeepPropFromObj(context, test) === 'undefined' ? recurse(include) : getSourceMapCompatiableReplacementBlock(include));
default:
throw new Error('Unknown if variant ' + variant + '.');
}
Expand Down
27 changes: 27 additions & 0 deletions test/sourcemaps.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var chai = require('chai'),
pp = require('../lib/preprocess');

chai.should();

describe('when the sourcemaps option is true it should maintain the line count', function () {

describe('with @if directive', function () {
var input = "a\n" +
"// @if NODE_ENV=='production' \n" +
"b\n" +
"c\n" +
"// @endif \n" +
"d";

it('when condition evals to true', function () {
pp.preprocess(input, {NODE_ENV: 'production'}, {type: 'js', supportSourceMaps: true}).should.equal("a\n\nb\nc\n\nd");
});

it('when condition evals to false', function () {
pp.preprocess(input, {NODE_ENV: 'dev'}, {type: 'js', supportSourceMaps: true}).should.equal("a\n\n\n\n\nd");
});
});

});