Skip to content

Commit f3586fd

Browse files
committed
Merge pull request Leaflet#4455 from Leaflet/build-version
Build scripts: stop using magic-string and use git rev as semver metadata
2 parents 23890c5 + ca01120 commit f3586fd

File tree

5 files changed

+59
-31
lines changed

5 files changed

+59
-31
lines changed

Jakefile.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ For a custom build, open build/build.html in the browser and follow the instruct
1414

1515
var build = require('./build/build.js'),
1616
buildDocs = require('./build/docs'),
17-
version = require('./src/Leaflet.js').version;
17+
git = require('git-rev');
1818

1919
function hint(msg, args) {
2020
return function () {
@@ -27,23 +27,31 @@ function hint(msg, args) {
2727
};
2828
}
2929

30+
// Returns the version string in package.json, plus a semver build metadata if
31+
// this is not an official release
32+
function calculateVersion(officialRelease, callback) {
33+
34+
var version = require('./package.json').version;
35+
36+
if (officialRelease) {
37+
callback(version);
38+
} else {
39+
git.short(function(str) {
40+
callback (version + '+' + str);
41+
});
42+
}
43+
}
44+
3045
desc('Check Leaflet source for errors with ESLint');
3146
task('lint', {async: true}, hint('Checking for JS errors...', 'src --config .eslintrc'));
3247

3348
desc('Check Leaflet specs source for errors with ESLint');
3449
task('lintspec', {async: true}, hint('Checking for specs JS errors...', 'spec/suites --config spec/.eslintrc'));
3550

3651
desc('Combine and compress Leaflet source files');
37-
task('build', {async: true}, function (compsBase32, buildName) {
38-
var v;
39-
40-
jake.exec('git log -1 --pretty=format:"%h"', {breakOnError: false}, function () {
52+
task('build', {async: true}, function (compsBase32, buildName, officialRelease) {
53+
calculateVersion(officialRelease, function(v){
4154
build.build(complete, v, compsBase32, buildName);
42-
43-
}).on('stdout', function (data) {
44-
v = version + ' (' + data.toString() + ')';
45-
}).on('error', function () {
46-
v = version;
4755
});
4856
});
4957

build/build.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var fs = require('fs'),
22
UglifyJS = require('uglify-js'),
33
zlib = require('zlib'),
4-
MagicString = require('magic-string'),
4+
SourceNode = require( 'source-map' ).SourceNode;
55

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

@@ -70,21 +70,44 @@ function loadSilently(path) {
7070
}
7171
}
7272

73-
function bundleFiles(files, copy) {
74-
var bundle = new MagicString.Bundle();
73+
// Concatenate the files while building up a sourcemap for the concatenation,
74+
// and replace the line defining L.version with the string prepared in the jakefile
75+
function bundleFiles(files, copy, version) {
76+
var node = new SourceNode(null, null, null, '');
77+
78+
node.add(new SourceNode(null, null, null, copy + '(function (window, document, undefined) {'));
7579

7680
for (var i = 0, len = files.length; i < len; i++) {
77-
bundle.addSource({
78-
filename: files[i],
79-
content: new MagicString( fs.readFileSync(files[i], 'utf8') + '\n\n' )
80-
});
81+
var contents = fs.readFileSync(files[i], 'utf8');
82+
83+
if (files[i] === 'src/Leaflet.js') {
84+
contents = contents.replace(
85+
new RegExp('version: \'.*\''),
86+
'version: ' + JSON.stringify(version)
87+
);
88+
}
89+
90+
var lines = contents.split('\n');
91+
var lineCount = lines.length;
92+
var fileNode = new SourceNode(null, null, null, '');
93+
94+
fileNode.setSourceContent(files[i], contents);
95+
96+
for (var j=0; j<lineCount; j++) {
97+
fileNode.add(new SourceNode(j+1, 0, files[i], lines[j] + '\n'));
98+
}
99+
node.add(fileNode);
100+
101+
node.add(new SourceNode(null, null, null, '\n\n'));
81102
}
82103

83-
bundle.prepend(
84-
copy + '(function (window, document, undefined) {'
85-
).append('}(window, document));');
104+
node.add(new SourceNode(null, null, null, '}(window, document));'));
86105

87-
return bundle;
106+
var bundle = node.toStringWithSourceMap();
107+
return {
108+
src: bundle.code,
109+
srcmap: bundle.map.toString()
110+
};
88111
}
89112

90113
function bytesToKB(bytes) {
@@ -106,8 +129,8 @@ exports.build = function (callback, version, compsBase32, buildName) {
106129
srcFilename = filenamePart + '-src.js',
107130
mapFilename = filenamePart + '-src.map',
108131

109-
bundle = bundleFiles(files, copy),
110-
newSrc = bundle.toString() + '\n//# sourceMappingURL=' + mapFilename,
132+
bundle = bundleFiles(files, copy, version),
133+
newSrc = bundle.src + '\n//# sourceMappingURL=' + mapFilename,
111134

112135
oldSrc = loadSilently(srcPath),
113136
srcDelta = getSizeDelta(newSrc, oldSrc, true);
@@ -116,11 +139,7 @@ exports.build = function (callback, version, compsBase32, buildName) {
116139

117140
if (newSrc !== oldSrc) {
118141
fs.writeFileSync(srcPath, newSrc);
119-
fs.writeFileSync(mapPath, bundle.generateMap({
120-
file: srcFilename,
121-
includeContent: true,
122-
hires: false
123-
}));
142+
fs.writeFileSync(mapPath, bundle.srcmap);
124143
console.log('\tSaved to ' + srcPath);
125144
}
126145

build/publish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ npm test || exit 1
88

99
git checkout -b build
1010

11-
npm run build
11+
jake build[,,true]
1212
git add dist/leaflet-src.js dist/leaflet.js dist/leaflet-src.map -f
1313

1414
git commit -m "v$VERSION"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"devDependencies": {
66
"eslint": "^2.8.0",
77
"eslint-config-mourner": "^2.0.1",
8+
"git-rev": "^0.2.1",
89
"happen": "~0.3.1",
910
"jake": "~8.0.12",
1011
"karma": "~0.13.22",
@@ -15,10 +16,10 @@
1516
"karma-phantomjs-launcher": "^1.0.0",
1617
"karma-safari-launcher": "~0.1.1",
1718
"leafdoc": "^1.2.1",
18-
"magic-string": "^0.11.4",
1919
"mocha": "~2.4.5",
2020
"phantomjs-prebuilt": "^2.1.7",
2121
"prosthetic-hand": "^1.3.0",
22+
"source-map": "^0.5.3",
2223
"uglify-js": "~2.6.2"
2324
},
2425
"main": "dist/leaflet-src.js",

src/Leaflet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
var L = {
3-
version: '1.0.0-rc.1'
3+
version: 'dev'
44
};
55

66
function expose() {

0 commit comments

Comments
 (0)