Skip to content

Commit

Permalink
Merge pull request #21 from deedw/master
Browse files Browse the repository at this point in the history
Fix sourcemap generation
  • Loading branch information
sidorares committed May 18, 2016
2 parents 6c7a994 + f77e52d commit 179c4b5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,18 @@ And then in browserify-jade-config.js:

module.exports = {
pretty: (process.env.NODE_ENV == 'production')?true:false
};
};

To disable sourcemap generation, which results in smaller compiled files for production builds,
set jade option `compileDebug` to false in the options:

var b = browserify();
b.transform(require('browserify-jade').jade({
compileDebug: false
}));

or in package.json:

"browserify-jade": {
"compileDebug": false
}
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ module.exports.root = null;
module.exports.register = register;

function register() {
require.extensions['.pug', '.jade'] = function(module, filename) {
require.extensions['.pug'] = require.extensions['.jade'] = function(module, filename) {
var result = compile(filename, fs.readFileSync(filename, 'utf-8'), {compileDebug: true});
return module._compile(result.body, filename);
}
};
}

function replaceMatchWith(match, newContent)
Expand All @@ -77,13 +77,13 @@ function replaceMatchWith(match, newContent)

function withSourceMap(src, compiled, name) {

//return compiled;

var compiledLines = compiled.split('\n');
var generator = new SourceMapGenerator({file: name + '.js'});

compiledLines.forEach(function(l, lineno) {
var m = l.match(/^(pug|jade)(_|\.)debug\.unshift\(\{ lineno: ([0-9]+)/);
var m = l.match(/^jade(_|\.)debug\.unshift\(new jade\.DebugItem\( ([0-9]+)/);
// Check for older jade debug line format
if (!m) m = l.match(/^(pug|jade)(_|\.)debug\.unshift\(\{ lineno: ([0-9]+)/);
if (m) {
var originalLine = Number(m[2]);
var generatedLine = lineno + 2;
Expand All @@ -110,6 +110,15 @@ function withSourceMap(src, compiled, name) {
}
compiledLines[lineno] =l;
});

// Remove jade debug lines at beginning and end of compiled version
if (/var jade_debug = /.test(compiledLines[1])) compiledLines[1] = '';
if (/try \{/.test(compiledLines[2])) compiledLines[2] = '';
var l = compiledLines.length;
if (/\} catch \(err\) \{/.test(compiledLines[l-4])) {
compiledLines[l-2] = compiledLines[l-3] = compiledLines[l-4] = '';
}

generator.setSourceContent(name, src);

var map = convert.fromJSON(generator.toString());
Expand All @@ -133,9 +142,11 @@ function compile(file, template, options) {
result = {
body: jade.compile(template, options).toString(),
dependencies: []
}
};
}
if (options.compileDebug)
result.body = withSourceMap(template, result.body, file);

result.body = PREFIX + withSourceMap(template, result.body, file);
result.body = PREFIX + result.body;
return result;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pugify",
"version": "1.0.3",
"version": "1.0.4",
"description": "browserify v2 plugin for jade with sourcemaps support",
"main": "index.js",
"dependencies": {
Expand All @@ -10,7 +10,7 @@
"through": "~2.3.8"
},
"peerDependencies": {
"jade": "*"
"jade": "1.x"
},
"devDependencies": {
"tap": "^5.4.4",
Expand Down

0 comments on commit 179c4b5

Please sign in to comment.