From 425d859c0f6ceaf46c7822ce239181a27ca3740a Mon Sep 17 00:00:00 2001 From: Paul Pflugradt Date: Thu, 26 May 2016 11:52:15 +0200 Subject: [PATCH] test and improvement for #230 --- lib/parser.js | 32 +++----------------------------- test/test.js | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index a353635d1..69012c845 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -96,21 +96,9 @@ module.exports = function (content, filename, needMap) { // extract part var start = node.childNodes[0].__location.startOffset var end = node.childNodes[node.childNodes.length - 1].__location.endOffset - var result - if (type === 'script') { - // preserve other parts as commenets so that linters - // and babel can output correct line numbers in warnings - result = - commentScript(content.slice(0, start), lang) + - deindent(content.slice(start, end)) + - commentScript(content.slice(end), lang) - } else { - var lineOffset = content.slice(0, start).split(splitRE).length - 1 - result = deindent(content.slice(start, end)) - - // pad whith whitespace so that error messages are correct - result = Array(lineOffset + 1).join('\n') + result - } + var lineOffset = content.slice(0, start).split(splitRE).length - 1 + var result = Array(lineOffset + 1).join('\n') + + deindent(content.slice(start, end)) if (needMap) { // generate source map @@ -161,20 +149,6 @@ module.exports = function (content, filename, needMap) { return output } -function commentScript (content, lang) { - var symbol = getCommentSymbol(lang) - var lines = content.split(splitRE) - return lines.map(function (line, index) { - // preserve EOL - if (index === lines.length - 1 && emptyRE.test(line)) { - return '' - } else { - return symbol + (emptyRE.test(line) ? '' : ' ' + line) - } - }) - .join('\n') -} - function getCommentSymbol (lang) { return commentSymbols[lang] || '//' } diff --git a/test/test.js b/test/test.js index 8644b3ef1..2592490c9 100644 --- a/test/test.js +++ b/test/test.js @@ -202,6 +202,7 @@ describe('vue-loader', function () { it('extract CSS', function (done) { webpack(Object.assign({}, globalConfig, { + devtool: 'source-map', entry: './test/fixtures/extract-css.vue', vue: { loaders: { @@ -216,7 +217,25 @@ describe('vue-loader', function () { expect(err).to.be.null getFile('test.output.css', function (data) { expect(data).to.contain('h1 {\n color: #f00;\n}\n\n\n\n\n\n\nh2 {\n color: green;\n}') - done() + getFile('test.output.css.map', function (map) { + var smc = new SourceMapConsumer(JSON.parse(map)) + var line, col + var targetRE = /^\s+color: #f00;/ + data.split(/\r?\n/g).some(function (l, i) { + if (targetRE.test(l)) { + line = i + 1 + col = l.length + return true + } + }) + var pos = smc.originalPositionFor({ + line: line, + column: col + }) + expect(pos.source.indexOf('extract-css.vue') > -1) + expect(pos.line).to.equal(3) + done() + }) }) }) })