Skip to content

Commit d6553d5

Browse files
committed
Preserve license comments in built file. Closes requirejs#41
Finds all license comments and puts them at the top of the minified file.
1 parent bd21cf7 commit d6553d5

File tree

7 files changed

+74
-2
lines changed

7 files changed

+74
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.DS_Store
22
r.js
33
build/tests/builds
4+
build/tests/lib/comments/built.js
5+
build/tests/lib/empty/built
46
build/tests/lib/namespace/foo.js
57
build/tests/lib/plugins/main-built.js
68
build/tests/lib/dotpackage/built

build/jslib/optimize.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ function (lang, logger, envOptimize, file, parse,
128128
}
129129

130130
optimize = {
131+
licenseCommentRegExp: /\/\*[\s\S]*?\*\//g,
132+
131133
/**
132134
* Optimizes a file that contains JavaScript content. Optionally collects
133135
* plugin resources mentioned in a file, and then passes the content
@@ -146,7 +148,8 @@ function (lang, logger, envOptimize, file, parse,
146148
var parts = (config.optimize + "").split('.'),
147149
optimizerName = parts[0],
148150
keepLines = parts[1] === 'keepLines',
149-
fileContents, optFunc, deps, i, dep;
151+
licenseContents = '',
152+
fileContents, optFunc, match, comment;
150153

151154
fileContents = file.readFile(fileName);
152155

@@ -161,7 +164,19 @@ function (lang, logger, envOptimize, file, parse,
161164
optimizerName +
162165
'" not found for this environment');
163166
}
164-
fileContents = optFunc(fileName, fileContents, keepLines,
167+
168+
//Pull out any license comments for prepending after optimization.
169+
optimize.licenseCommentRegExp.lastIndex = 0;
170+
while ((match = optimize.licenseCommentRegExp.exec(fileContents))) {
171+
comment = match[0];
172+
//Only keep the comments if they are license comments.
173+
if (comment.indexOf('@license') !== -1 ||
174+
comment.indexOf('/*!') === 0) {
175+
licenseContents += comment + '\n';
176+
}
177+
}
178+
179+
fileContents = licenseContents + optFunc(fileName, fileContents, keepLines,
165180
config[optimizerName]);
166181
}
167182

build/tests/builds.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,22 @@ define(['build', 'env!env/file'], function (build, file) {
259259
);
260260
doh.run();
261261

262+
doh.register("preserveLicense",
263+
[
264+
function preserveLicense(t) {
265+
file.deleteFile("lib/comments/built.js");
266+
267+
build(["lib/comments/build.js"]);
268+
269+
t.is(nol(c("lib/comments/expected.js")),
270+
nol(c("lib/comments/built.js")));
271+
272+
require._buildReset();
273+
}
274+
275+
]
276+
);
277+
doh.run();
278+
262279

263280
});

build/tests/lib/comments/bang.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*!
2+
* this is a BANG LICENSE COMMENT
3+
*/
4+
5+
define(function () {
6+
/* This comment should be IGNORED */
7+
return {
8+
name: 'bang'
9+
};
10+
});

build/tests/lib/comments/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
({
2+
baseUrl: '.',
3+
name: 'bang',
4+
include: ['license'],
5+
out: 'built.js'
6+
})

build/tests/lib/comments/expected.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*!
2+
* this is a BANG LICENSE COMMENT
3+
*/
4+
/** vim: et:ts=4:sw=4:sts=4
5+
* @license This is a LICENSE COMMENT that should be preserved.
6+
* Available via the MIT or new BSD license.
7+
* see: http://example.com/license for details
8+
*/
9+
define("bang",[],function(){return{name:"bang"}}),define("license",{name:"license"})

build/tests/lib/comments/license.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** vim: et:ts=4:sw=4:sts=4
2+
* @license This is a LICENSE COMMENT that should be preserved.
3+
* Available via the MIT or new BSD license.
4+
* see: http://example.com/license for details
5+
*/
6+
7+
define({
8+
//@license comment to be IGNORED
9+
name: 'license'
10+
/**
11+
* A license fake-out, should be IGNORED
12+
*/
13+
});

0 commit comments

Comments
 (0)