Skip to content

Commit 2ff90ee

Browse files
steploveddyerburgh
authored andcommitted
fix: use correct source map names for script files imported over src (#131)
1 parent 52b2d1a commit 2ff90ee

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

lib/process.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,23 @@ function processScript(scriptPart, filePath, config) {
3131

3232
module.exports = function(src, filePath, config) {
3333
const vueJestConfig = getVueJestConfig(config)
34-
var parts = vueCompiler.parseComponent(src, { pad: true })
34+
let parts = vueCompiler.parseComponent(src, { pad: true })
35+
let scriptSrc = src
36+
let sourceMapPath = filePath
3537

3638
if (parts.script && parts.script.src) {
37-
parts.script.content = fs.readFileSync(
38-
join(filePath, '..', parts.script.src),
39-
'utf8'
40-
)
39+
const externalScrPath = join(filePath, '..', parts.script.src)
40+
41+
parts.script.content = fs.readFileSync(externalScrPath, 'utf8')
42+
scriptSrc = parts.script.content
43+
sourceMapPath = externalScrPath
4144
}
4245

4346
const result = processScript(parts.script, filePath, config)
4447
const script = result.code
4548
const inputMap = result.map
4649

47-
let scriptSrc = src
48-
if (parts.script && parts.script.src) {
49-
scriptSrc = parts.script.content
50-
}
51-
52-
const map = generateSourceMap(script, '', filePath, scriptSrc, inputMap)
50+
const map = generateSourceMap(script, '', sourceMapPath, scriptSrc, inputMap)
5351

5452
let output =
5553
';(function(){\n' +

test/resources/SourceMapsSrc.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template src="./BasicSrc.html"></template>
2+
3+
<script src="./BasicSrc.js"></script>

test/sourceMaps.spec.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { resolve } from 'path'
2+
import { readFileSync } from 'fs'
3+
import clearModule from 'clear-module'
4+
import cache from '../lib/cache'
5+
import jestVue from '../vue-jest'
6+
7+
beforeEach(() => {
8+
cache.flushAll()
9+
clearModule.all()
10+
})
11+
12+
const getSourceMaps = code => {
13+
const sourceMapBase64 = /\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.*)/gim
14+
15+
let matches
16+
let values = []
17+
18+
while ((matches = sourceMapBase64.exec(code)) !== null) {
19+
values.push(JSON.parse(Buffer.from(matches[1], 'base64').toString('ascii')))
20+
}
21+
22+
return values
23+
}
24+
25+
test('generates source maps for .vue files', () => {
26+
const filePath = resolve(__dirname, './resources/Basic.vue')
27+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
28+
29+
const { code } = jestVue.process(fileString, filePath, {
30+
moduleFileExtensions: ['js', 'vue']
31+
})
32+
33+
const [template, js] = getSourceMaps(code)
34+
35+
expect(js.sources[0]).toBe('Basic.vue')
36+
expect(template.sources[0]).toBe('Basic.vue')
37+
})
38+
39+
test('generates source maps using src attributes', () => {
40+
const filePath = resolve(__dirname, './resources/SourceMapsSrc.vue')
41+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
42+
43+
const { code } = jestVue.process(fileString, filePath, {
44+
moduleFileExtensions: ['js', 'vue']
45+
})
46+
47+
const [template, js] = getSourceMaps(code)
48+
49+
expect(js.sources[0]).toBe('BasicSrc.js')
50+
expect(template.sources[0]).toBe('SourceMapsSrc.vue')
51+
})

0 commit comments

Comments
 (0)