From f77e52dc018a7f26d8961e55991c2082a5c632b1 Mon Sep 17 00:00:00 2001 From: David Willis Date: Wed, 18 May 2016 11:56:26 +0100 Subject: [PATCH] Fix sourcemap generation --- README.md | 16 +++++++++++++++- index.js | 25 ++++++++++++++++++------- package.json | 4 ++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e0c57b6..a7c8710 100644 --- a/README.md +++ b/README.md @@ -33,4 +33,18 @@ And then in browserify-jade-config.js: module.exports = { pretty: (process.env.NODE_ENV == 'production')?true:false - }; \ No newline at end of file + }; + +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 + } diff --git a/index.js b/index.js index afde902..ec6e5eb 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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; @@ -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()); @@ -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; } diff --git a/package.json b/package.json index 58969f0..70fd7f3 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -10,7 +10,7 @@ "through": "~2.3.8" }, "peerDependencies": { - "jade": "*" + "jade": "1.x" }, "devDependencies": { "tap": "^5.4.4",