blob: c507912186bf5b0b8741bf13d2d0f44cc6776989 [file] [log] [blame]
import loaderUtils from 'loader-utils'
import {
extractBlocks
} from './parser'
import {
splitSourceLine,
generateMap
} from './util'
module.exports = function (source) {
this.cacheable && this.cacheable()
const callback = this.async()
const loaderQuery = loaderUtils.parseQuery(this.query)
const type = loaderQuery.type
let index = loaderQuery.index
if (index != null && index.match(/^\d+$/)) {
index = parseInt(index)
}
extractBlocks(source, type)
.then(result => {
if (index != null) {
result = result[index]
}
const content = result.content.trim()
let map
if (this.sourceMap && type === 'scripts') {
let contentLineCount = 0
const iterator = splitSourceLine(content)
.map((input, line) => {
contentLineCount++
line = line + 1
return {
original: {
line: line + result.line,
column: 0
},
generated: {
line: line,
column: 0
}
}
})
const commentSource = splitSourceLine(source)
.map((input, line) => {
line = line + 1
if (line <= result.line
|| line > result.line + contentLineCount) {
return '// ' + input + ' /* generated by weex-loader */'
}
else {
return input
}
}).join('\n')
map = generateMap(this, commentSource, iterator)
}
return [content, map]
}).then(([content, map]) => {
callback(null, content, map && map.toJSON())
}).catch(e => {
callback(e, '')
})
}