Skip to content

Commit

Permalink
Build sourcemaps via magic-string
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSanchez committed Aug 10, 2015
1 parent d17e53b commit 62f1cd2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tmp/**/*
*.sublime-*
_site
dist/*.js
dist/*.map
coverage/
*.js.html
index.html
Expand Down
37 changes: 28 additions & 9 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var fs = require('fs'),
UglifyJS = require('uglify-js'),
zlib = require('zlib'),
MagicString = require('magic-string'),

deps = require('./deps.js').deps;

Expand Down Expand Up @@ -69,12 +70,21 @@ function loadSilently(path) {
}
}

function combineFiles(files) {
var content = '';
function bundleFiles(files, copy) {
var bundle = new MagicString.Bundle();

for (var i = 0, len = files.length; i < len; i++) {
content += fs.readFileSync(files[i], 'utf8') + '\n\n';
bundle.addSource({
filename: files[i],
content: new MagicString( fs.readFileSync(files[i], 'utf8') + '\n\n' )
});
}
return content;

bundle.prepend(
copy + '(function (window, document, undefined) {'
).append('}(window, document));');

return bundle;
}

function bytesToKB(bytes) {
Expand All @@ -85,15 +95,19 @@ exports.build = function (callback, version, compsBase32, buildName) {

var files = getFiles(compsBase32);

console.log('Concatenating and compressing ' + files.length + ' files...');
console.log('Bundling and compressing ' + files.length + ' files...');

var copy = fs.readFileSync('src/copyright.js', 'utf8').replace('{VERSION}', version),
intro = '(function (window, document, undefined) {',
outro = '}(window, document));',
newSrc = copy + intro + combineFiles(files) + outro,

pathPart = 'dist/leaflet' + (buildName ? '-' + buildName : ''),
filenamePart = 'leaflet' + (buildName ? '-' + buildName : ''),
pathPart = 'dist/' + filenamePart,
srcPath = pathPart + '-src.js',
mapPath = pathPart + '-src.map',
srcFilename = filenamePart + '-src.js',
mapFilename = filenamePart + '-src.map',

bundle = bundleFiles(files, copy),
newSrc = bundle.toString() + '\n//# sourceMappingURL=' + mapFilename,

oldSrc = loadSilently(srcPath),
srcDelta = getSizeDelta(newSrc, oldSrc, true);
Expand All @@ -102,6 +116,11 @@ exports.build = function (callback, version, compsBase32, buildName) {

if (newSrc !== oldSrc) {
fs.writeFileSync(srcPath, newSrc);
fs.writeFileSync(mapPath, bundle.generateMap({
file: srcFilename,
includeContent: true,
hires: false
}));
console.log('\tSaved to ' + srcPath);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"karma-mocha": "~0.2.0",
"karma-phantomjs-launcher": "^0.2.0",
"karma-safari-launcher": "~0.1.1",
"magic-string": "^0.6.4",
"mocha": "~2.2.5",
"tin": "^0.5.0",
"uglify-js": "~2.4.23"
Expand Down

0 comments on commit 62f1cd2

Please sign in to comment.