Skip to content

Commit

Permalink
feature: add source-map for package code
Browse files Browse the repository at this point in the history
  • Loading branch information
xtx1130 committed Nov 30, 2018
1 parent d4f5024 commit 6b640dc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
59 changes: 32 additions & 27 deletions tool/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ function build() {
Object.keys(editions).forEach(edition => {
let option = editions[edition];
let editionSource = clearFeatureCode(source, option.ignoreFeatures);
let fileName = edition === '__' ? `san.js` : `san.${edition}.js`;
let filePath = path.join(distDir, fileName);
if (option.compress) {
let ast = uglifyJS.parse(editionSource);
ast.figure_out_scope({screw_ie8: false});
Expand All @@ -90,39 +92,42 @@ function build() {

editionSource = ast.print_to_string({screw_ie8: false});
}
let fileName = edition === '__' ? `san.js` : `san.${edition}.js`;
editionSource += `//@ sourceMappingURL=./${fileName}.map`
if(!/min/.test(edition)){
editionSource += '//@ sourceMappingURL=' + path.join('./', fileName + '.map');
}
fs.writeFileSync(
`${distDir}/` + fileName,
filePath,
editionSource,
'UTF-8'
);
let map = new MOZ_SourceMap.SourceMapGenerator({
file: fileName
})
let baseLine = fs.readFileSync(baseSource.base)
.toString('utf8').split('// #[main-dependencies]')[0]
.split('\n').length
for(var i = 0;i < baseSource.deps.length; i ++) {
let script = fs.readFileSync(baseSource.deps[i]);
let fileSplit = script.toString('utf8').split('\n');
let fileLine = fileSplit.length;
for(let j = 0; j< fileSplit.length;j ++){
map.addMapping({
source: baseSource.deps[i],
original: {
line: j + 1,
column: 0
},
generated: {
line: baseLine + j,
column: 0
}
});
if(!/min/.test(edition)){
let map = new MOZ_SourceMap.SourceMapGenerator({
file: filePath
});
let baseLineLength = fs.readFileSync(baseSource.base)
.toString('utf8').split('// #[main-dependencies]')[0]
.split('\n').length;
for (let i = 0; i < baseSource.deps.length; i++) {
let script = fs.readFileSync(baseSource.deps[i]);
let fileSplit = script.toString('utf8').split('\n');
let fileLineLength = fileSplit.length;
for (let j = 0; j < fileLineLength; j++){
map.addMapping({
source: baseSource.deps[i],
original: {
line: j + 1,
column: 0
},
generated: {
line: baseLineLength + j,
column: 0
}
});
}
baseLineLength = fileLineLength + 1 + baseLineLength;
}
baseLine = fileLine + 1 + baseLine;
fs.writeFileSync(`${filePath}.map`, map.toString(), 'UTF-8');
}
fs.writeFileSync(`${distDir}/` + fileName + '.map', map.toString(), 'UTF-8');
});
}

Expand Down
3 changes: 2 additions & 1 deletion tool/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function pack(rootDir, mainFile) {
content: fileContent(mainFile, 1, []).replace(
'// #[main-dependencies]',
deps.map(dep => fileContent(dep)).join('\n\n')),
deps: deps
deps: deps,
base: mainFile
};
}

Expand Down

0 comments on commit 6b640dc

Please sign in to comment.