-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from jolting/next
Rewrite code generation using SourceMaps
- Loading branch information
Showing
5 changed files
with
108 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,37 @@ | ||
module.exports = function generateCode( | ||
{ scriptResult, scriptSetupResult }, | ||
templateResult, | ||
stylesResult, | ||
customBlocksResult, | ||
isFunctional | ||
) { | ||
let output = '' | ||
const { SourceNode, SourceMapConsumer } = require('source-map') | ||
|
||
if (scriptResult) { | ||
output += `${scriptResult.code};\n` | ||
function addToSourceMap(node, result) { | ||
if (result && result.code) { | ||
if (result.map) { | ||
node.add( | ||
SourceNode.fromStringWithSourceMap( | ||
result.code, | ||
new SourceMapConsumer(result.map) | ||
) | ||
) | ||
} else { | ||
node.add(result.code) | ||
} | ||
} | ||
} | ||
|
||
if (scriptSetupResult) { | ||
output += `${scriptSetupResult.code};\n` | ||
} | ||
module.exports = function generateCode( | ||
scriptResult, | ||
scriptSetupResult, | ||
templateResult, | ||
filename | ||
) { | ||
var node = new SourceNode(null, null, null) | ||
addToSourceMap(node, scriptResult) | ||
addToSourceMap(node, scriptSetupResult) | ||
addToSourceMap(node, templateResult) | ||
|
||
if (templateResult) { | ||
output += `${templateResult.code};\n` | ||
} | ||
var tempOutput = node.toString() | ||
|
||
if (output.includes('exports.render = render;')) { | ||
output += ';exports.default = {...exports.default, render};' | ||
if (tempOutput.includes('exports.render = render;')) { | ||
node.add(';exports.default = {...exports.default, render};') | ||
} else { | ||
output += ';exports.default = {...exports.default};' | ||
} | ||
|
||
return { | ||
code: output | ||
node.add(';exports.default = {...exports.default};') | ||
} | ||
return node.toStringWithSourceMap({ file: filename }) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const { SourceMapGenerator, SourceMapConsumer } = require('source-map') | ||
|
||
// based on @vue/compiler-sfc mapLines | ||
module.exports = function mapLines(oldMap, newMap) { | ||
if (!oldMap) return newMap | ||
if (!newMap) return oldMap | ||
|
||
const oldMapConsumer = new SourceMapConsumer(oldMap) | ||
const newMapConsumer = new SourceMapConsumer(newMap) | ||
const mergedMapGenerator = new SourceMapGenerator() | ||
|
||
newMapConsumer.eachMapping(m => { | ||
if (m.originalLine == null) { | ||
return | ||
} | ||
|
||
const origPosInOldMap = oldMapConsumer.originalPositionFor({ | ||
line: m.originalLine, | ||
column: m.originalColumn | ||
}) | ||
|
||
if (origPosInOldMap.source == null) { | ||
return | ||
} | ||
|
||
mergedMapGenerator.addMapping({ | ||
generated: { | ||
line: m.generatedLine, | ||
column: m.generatedColumn | ||
}, | ||
original: { | ||
line: origPosInOldMap.line, // map line | ||
// use current column, since the oldMap produced by @vue/compiler-sfc | ||
// does not | ||
column: m.originalColumn | ||
}, | ||
source: origPosInOldMap.source, | ||
name: origPosInOldMap.name | ||
}) | ||
}) | ||
|
||
// source-map's type definition is incomplete | ||
const generator = mergedMapGenerator | ||
oldMapConsumer.sources.forEach(sourceFile => { | ||
generator._sources.add(sourceFile) | ||
const sourceContent = oldMapConsumer.sourceContentFor(sourceFile) | ||
if (sourceContent != null) { | ||
mergedMapGenerator.setSourceContent(sourceFile, sourceContent) | ||
} | ||
}) | ||
|
||
generator._sourceRoot = oldMap.sourceRoot | ||
generator._file = oldMap.file | ||
return generator.toJSON() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters