Skip to content
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

Add support for sourcemap #5

Merged
merged 3 commits into from
Mar 22, 2017
Merged
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
46 changes: 40 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ const estraverse = require('estraverse')
const genCode = require('escodegen')
const genId = require('./gen-id')

module.exports = function(contents) {
const sourceMap = require('source-map')
const SourceMapConsumer = sourceMap.SourceMapConsumer

module.exports = function(contents, sourcemap) {
const id = genId(this.resourcePath)
let hasComponent = false

const ast = acorn.parse(contents, {
sourceType: 'module'
})
const parseOptions = { sourceType: 'module' }

if (this.sourceMap) {
parseOptions.locations = true
parseOptions.sourceFile = this.resourcePath
}

const ast = acorn.parse(contents, parseOptions)

const res = estraverse.replace(ast, {
enter (node, parent) {
Expand Down Expand Up @@ -39,8 +47,34 @@ module.exports = function(contents) {
}
})

return genCode.generate(res)
+ (hasComponent ? genHotReload(id) : '')
if (!hasComponent) {
this.callback(null, contents, sourcemap)
return
}

if (this.sourceMap) {
const generatedContents = genCode.generate(res, {
sourceMap: this.resourcePath,
sourceMapWithCode: true,
sourceContent: contents
})

// apply the original sourcemap to the sourcemap generated by escodegen.
const sourceMapGenerator = generatedContents.map

if (sourcemap) {
const originalSmc = new SourceMapConsumer(sourcemap)
sourceMapGenerator.applySourceMap(originalSmc)
}

const outSourcemap = sourceMapGenerator.toJSON()

this.callback(null, generatedContents.code + genHotReload(id), outSourcemap)
} else {
const generatedContents = genCode.generate(res)

this.callback(null, generatedContents + genHotReload(id))
}
}

function genHotReload (id) {
Expand Down
9 changes: 7 additions & 2 deletions test/specs/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const path = require('path')
const fs = require('fs')
const loader = require('../../lib/index')
const DefaultLoader = require('../../lib/index')

class Loader extends DefaultLoader {
callback(err, contents, sourcemap) { this.callbackAnswer = { err, contents, sourcemap } }
}

function test (name) {
const src = fs.readFileSync(path.resolve(__dirname, '../fixtures', name), 'utf8')
const expected = fs.readFileSync(path.resolve(__dirname, '../expects', name), 'utf8')

it(name, () => {
expect(loader(src).replace(/if \(module\.hot\)[\s\S]*$/, '').trim()).toBe(expected.trim())
const loader = new Loader(src)
expect(loader.callbackAnswer.contents.replace(/if \(module\.hot\)[\s\S]*$/, '').trim()).toBe(expected.trim())
})
}

Expand Down