refactor(compiler): downgrade to source-map v6 for sync API

This commit is contained in:
Evan You 2019-12-13 12:56:25 -05:00
parent a0ee4fbc36
commit 8277d131c4
6 changed files with 19 additions and 30 deletions

View File

@ -44,7 +44,7 @@ describe('compiler: integration tests', () => {
return res return res
} }
test('function mode', async () => { test('function mode', () => {
const { code, map } = compile(source, { const { code, map } = compile(source, {
sourceMap: true, sourceMap: true,
filename: `foo.vue` filename: `foo.vue`
@ -54,7 +54,7 @@ describe('compiler: integration tests', () => {
expect(map!.sources).toEqual([`foo.vue`]) expect(map!.sources).toEqual([`foo.vue`])
expect(map!.sourcesContent).toEqual([source]) expect(map!.sourcesContent).toEqual([source])
const consumer = await new SourceMapConsumer(map as RawSourceMap) const consumer = new SourceMapConsumer(map as RawSourceMap)
expect( expect(
consumer.originalPositionFor(getPositionInCode(code, `id`)) consumer.originalPositionFor(getPositionInCode(code, `id`))
@ -109,7 +109,7 @@ describe('compiler: integration tests', () => {
).toMatchObject(getPositionInCode(source, `value + index`)) ).toMatchObject(getPositionInCode(source, `value + index`))
}) })
test('function mode w/ prefixIdentifiers: true', async () => { test('function mode w/ prefixIdentifiers: true', () => {
const { code, map } = compile(source, { const { code, map } = compile(source, {
sourceMap: true, sourceMap: true,
filename: `foo.vue`, filename: `foo.vue`,
@ -120,7 +120,7 @@ describe('compiler: integration tests', () => {
expect(map!.sources).toEqual([`foo.vue`]) expect(map!.sources).toEqual([`foo.vue`])
expect(map!.sourcesContent).toEqual([source]) expect(map!.sourcesContent).toEqual([source])
const consumer = await new SourceMapConsumer(map as RawSourceMap) const consumer = new SourceMapConsumer(map as RawSourceMap)
expect( expect(
consumer.originalPositionFor(getPositionInCode(code, `id`)) consumer.originalPositionFor(getPositionInCode(code, `id`))
@ -184,7 +184,7 @@ describe('compiler: integration tests', () => {
).toMatchObject(getPositionInCode(source, `value + index`)) ).toMatchObject(getPositionInCode(source, `value + index`))
}) })
test('module mode', async () => { test('module mode', () => {
const { code, map } = compile(source, { const { code, map } = compile(source, {
mode: 'module', mode: 'module',
sourceMap: true, sourceMap: true,
@ -195,7 +195,7 @@ describe('compiler: integration tests', () => {
expect(map!.sources).toEqual([`foo.vue`]) expect(map!.sources).toEqual([`foo.vue`])
expect(map!.sourcesContent).toEqual([source]) expect(map!.sourcesContent).toEqual([source])
const consumer = await new SourceMapConsumer(map as RawSourceMap) const consumer = new SourceMapConsumer(map as RawSourceMap)
expect( expect(
consumer.originalPositionFor(getPositionInCode(code, `id`)) consumer.originalPositionFor(getPositionInCode(code, `id`))

View File

@ -31,6 +31,6 @@
"dependencies": { "dependencies": {
"acorn": "^7.1.0", "acorn": "^7.1.0",
"estree-walker": "^0.8.1", "estree-walker": "^0.8.1",
"source-map": "^0.7.3" "source-map": "^0.6.1"
} }
} }

View File

@ -10,7 +10,6 @@ import {
CallExpression, CallExpression,
ArrayExpression, ArrayExpression,
ObjectExpression, ObjectExpression,
SourceLocation,
Position, Position,
InterpolationNode, InterpolationNode,
CompoundExpressionNode, CompoundExpressionNode,
@ -18,7 +17,8 @@ import {
FunctionExpression, FunctionExpression,
SequenceExpression, SequenceExpression,
ConditionalExpression, ConditionalExpression,
CacheExpression CacheExpression,
locStub
} from './ast' } from './ast'
import { SourceMapGenerator, RawSourceMap } from 'source-map' import { SourceMapGenerator, RawSourceMap } from 'source-map'
import { import {
@ -58,8 +58,7 @@ export interface CodegenContext extends Required<CodegenOptions> {
indentLevel: number indentLevel: number
map?: SourceMapGenerator map?: SourceMapGenerator
helper(key: symbol): string helper(key: symbol): string
push(code: string, node?: CodegenNode, openOnly?: boolean): void push(code: string, node?: CodegenNode): void
resetMapping(loc: SourceLocation): void
indent(): void indent(): void
deindent(withoutNewLine?: boolean): void deindent(withoutNewLine?: boolean): void
newline(): void newline(): void
@ -96,7 +95,7 @@ function createCodegenContext(
const name = helperNameMap[key] const name = helperNameMap[key]
return prefixIdentifiers ? name : `_${name}` return prefixIdentifiers ? name : `_${name}`
}, },
push(code, node, openOnly) { push(code, node) {
context.code += code context.code += code
if (!__BROWSER__ && context.map) { if (!__BROWSER__ && context.map) {
if (node) { if (node) {
@ -110,16 +109,11 @@ function createCodegenContext(
addMapping(node.loc.start, name) addMapping(node.loc.start, name)
} }
advancePositionWithMutation(context, code) advancePositionWithMutation(context, code)
if (node && !openOnly) { if (node && node.loc !== locStub) {
addMapping(node.loc.end) addMapping(node.loc.end)
} }
} }
}, },
resetMapping(loc: SourceLocation) {
if (!__BROWSER__ && context.map) {
addMapping(loc.start)
}
},
indent() { indent() {
newline(++context.indentLevel) newline(++context.indentLevel)
}, },
@ -279,7 +273,8 @@ export function generate(
return { return {
ast, ast,
code: context.code, code: context.code,
map: context.map ? context.map.toJSON() : undefined // SourceMapGenerator does have toJSON() method but it's not in the types
map: context.map ? (context.map as any).toJSON() : undefined
} }
} }
@ -510,13 +505,13 @@ function genCallExpression(node: CallExpression, context: CodegenContext) {
const callee = isString(node.callee) const callee = isString(node.callee)
? node.callee ? node.callee
: context.helper(node.callee) : context.helper(node.callee)
context.push(callee + `(`, node, true) context.push(callee + `(`, node)
genNodeList(node.arguments, context) genNodeList(node.arguments, context)
context.push(`)`) context.push(`)`)
} }
function genObjectExpression(node: ObjectExpression, context: CodegenContext) { function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
const { push, indent, deindent, newline, resetMapping } = context const { push, indent, deindent, newline } = context
const { properties } = node const { properties } = node
if (!properties.length) { if (!properties.length) {
push(`{}`, node) push(`{}`, node)
@ -529,8 +524,7 @@ function genObjectExpression(node: ObjectExpression, context: CodegenContext) {
push(multilines ? `{` : `{ `) push(multilines ? `{` : `{ `)
multilines && indent() multilines && indent()
for (let i = 0; i < properties.length; i++) { for (let i = 0; i < properties.length; i++) {
const { key, value, loc } = properties[i] const { key, value } = properties[i]
resetMapping(loc) // reset source mapping for every property.
// key // key
genExpressionAsPropertyKey(key, context) genExpressionAsPropertyKey(key, context)
push(`: `) push(`: `)

View File

@ -2,7 +2,7 @@
exports[`source map 1`] = ` exports[`source map 1`] = `
Object { Object {
"mappings": ";;;;UAAA,aACE;IAAK,gCAAMA,WAAM", "mappings": ";;;;UAAA,aACE,YAA8B;IAAzB,YAAmB,oBAAbA,WAAM",
"names": Array [ "names": Array [
"render", "render",
], ],

View File

@ -35,7 +35,7 @@
"merge-source-map": "^1.1.0", "merge-source-map": "^1.1.0",
"postcss": "^7.0.21", "postcss": "^7.0.21",
"postcss-selector-parser": "^6.0.2", "postcss-selector-parser": "^6.0.2",
"source-map": "^0.7.3" "source-map": "^0.6.1"
}, },
"devDependencies": { "devDependencies": {
"@types/consolidate": "^0.14.0", "@types/consolidate": "^0.14.0",

View File

@ -4990,11 +4990,6 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
source-map@^0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
sourcemap-codec@^1.4.4: sourcemap-codec@^1.4.4:
version "1.4.6" version "1.4.6"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9" resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9"