Skip to content

Commit

Permalink
Merge branch 'v4.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed Apr 23, 2017
2 parents 884eced + 4fc42f1 commit ddb1813
Show file tree
Hide file tree
Showing 19 changed files with 648 additions and 446 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v4.4.1

1. 修正 `root` 配置的行为,如果 filename 为全局模块路径,会直接根据 `root` 来定位
2. 修复多行模板逻辑表达式下 sourceMap 行号记录不准确的 BUG
3. 标准化错误处理

## v4.4.0

1. 预编译 API 支持输出 sourceMap
Expand Down
48 changes: 23 additions & 25 deletions lib/precompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ const defaults = options => {


// 转换外部模板文件引入语句的 filename 参数节点
// 所有绝对路径都转换成相对路径
const convertFilenameNode = (node, options) => {
if (node.type === 'Literal') {

// to relative path
if (options.root && !LOCAL_MODULE.test(node.value)) {
node.value = './' + path.relative(options.root, node.value).replace(/^\.\//, '');
}

// add extname
if (options.extname && !path.extname(node.value)) {
node.value += options.extname;
const resolvePath = options.resolveFilename(node.value, options);
const dirname = path.dirname(options.filename);
const relativePath = path.relative(dirname, resolvePath);

if (LOCAL_MODULE.test(relativePath)) {
node.value = relativePath;
} else {
node.value = './' + relativePath;
}

delete node.raw;
Expand All @@ -56,25 +56,19 @@ const convertFilenameNode = (node, options) => {


// 获取原始渲染函数的 sourceMap
const getOldSourceMap = (map, {sourceRoot, source, file}) => {
const getOldSourceMap = (mappings, {
sourceRoot,
source,
file
}) => {
const oldSourceMap = new sourceMap.SourceMapGenerator({
file,
sourceRoot
});

map.forEach(map => {
oldSourceMap.addMapping({
generated: {
line: map.generated.line + 1,
column: map.generated.start + 1
},
source,
original: {
line: map.original.line + 1,
column: map.original.start + 1
},
name: 'christopher'
});
mappings.forEach(mapping => {
mapping.source = source;
oldSourceMap.addMapping(mapping);
});

return oldSourceMap.toJSON();
Expand Down Expand Up @@ -217,7 +211,7 @@ const precompile = options => {


if (options.sourceMap) {

const sourceRoot = options.sourceRoot;
const source = path.relative(sourceRoot, options.filename);
const file = path.basename(source);
Expand All @@ -230,7 +224,11 @@ const precompile = options => {
code = gen.code;

const newSourceMap = gen.map.toJSON();
const oldSourceMap = getOldSourceMap(fn.map, {sourceRoot, source, file});
const oldSourceMap = getOldSourceMap(fn.mappings, {
sourceRoot,
source,
file
});
sourceMap = mergeSourceMap(oldSourceMap, newSourceMap);
sourceMap.file = file;

Expand Down
Loading

0 comments on commit ddb1813

Please sign in to comment.