Skip to content

test and improvement for #230 #245

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

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 3 additions & 29 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] || '//'
}
Expand Down
21 changes: 20 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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()
})
})
})
})
Expand Down