Skip to content

Commit b3f96ab

Browse files
committed
Fixes requirejs#193, remove empty directories if removeCombined is used.
1 parent 96e66a1 commit b3f96ab

File tree

7 files changed

+80
-5
lines changed

7 files changed

+80
-5
lines changed

build/jslib/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ function (lang, logger, file, parse, optimize, pragma,
405405
});
406406
}
407407

408+
//If removeCombined in play, remove any empty directories that
409+
//may now exist because of its use
410+
if (config.removeCombined && !config.out && config.dir) {
411+
file.deleteEmptyDirs(config.dir);
412+
}
413+
408414
//Do other optimizations.
409415
if (config.out && !config.cssIn) {
410416
//Just need to worry about one JS file.

build/jslib/node/file.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,31 @@ define(['fs', 'path'], function (fs, path) {
263263
fs.unlinkSync(fileName);
264264
}
265265
}
266+
},
267+
268+
269+
/**
270+
* Deletes any empty directories under the given directory.
271+
*/
272+
deleteEmptyDirs: function (startDir) {
273+
var dirFileArray, i, fileName, filePath, stat;
274+
275+
if (file.exists(startDir)) {
276+
dirFileArray = fs.readdirSync(startDir);
277+
for (i = 0; i < dirFileArray.length; i++) {
278+
fileName = dirFileArray[i];
279+
filePath = path.join(startDir, fileName);
280+
stat = fs.statSync(filePath);
281+
if (stat.isDirectory()) {
282+
file.deleteEmptyDirs(filePath);
283+
}
284+
}
285+
286+
//If directory is now empty, remove it.
287+
if (fs.readdirSync(startDir).length === 0) {
288+
file.deleteFile(startDir);
289+
}
290+
}
266291
}
267292
};
268293

build/jslib/rhino/file.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
//Helper functions to deal with file I/O.
77

8-
/*jslint plusplus: false, strict: false */
8+
/*jslint plusplus: false, strict: true */
99
/*global java: false, define: false */
1010

1111
define(function () {
@@ -244,6 +244,35 @@ define(function () {
244244
}
245245
fileObj["delete"]();
246246
}
247+
},
248+
249+
/**
250+
* Deletes any empty directories under the given directory.
251+
* The startDirIsJavaObject is private to this implementation's
252+
* recursion needs.
253+
*/
254+
deleteEmptyDirs: function (startDir, startDirIsJavaObject) {
255+
var topDir = startDir,
256+
dirFileArray, i, fileObj;
257+
258+
if (!startDirIsJavaObject) {
259+
topDir = new java.io.File(startDir);
260+
}
261+
262+
if (topDir.exists()) {
263+
dirFileArray = topDir.listFiles();
264+
for (i = 0; i < dirFileArray.length; i++) {
265+
fileObj = dirFileArray[i];
266+
if (fileObj.isDirectory()) {
267+
file.deleteEmptyDirs(fileObj, true);
268+
}
269+
}
270+
271+
//If the directory is empty now, delete it.
272+
if (topDir.listFiles().length === 0) {
273+
file.deleteFile(String(topDir.getPath()));
274+
}
275+
}
247276
}
248277
};
249278

build/tests/builds.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,9 @@ define(['build', 'env!env/file'], function (build, file) {
10271027
t.is(false, file.exists("lib/removeCombined/app-built/js/c.js"));
10281028
t.is(true, file.exists("lib/removeCombined/app-built/js/d.js"));
10291029

1030+
//Make sure empty directories are pruned
1031+
t.is(false, file.exists('lib/removeCombined/app-built/js/sub'), 'empty directories removed');
1032+
10301033
require._buildReset();
10311034
}
10321035

@@ -1050,6 +1053,9 @@ define(['build', 'env!env/file'], function (build, file) {
10501053
t.is(false, file.exists("lib/removeCombined/baseUrl-built/c.js"));
10511054
t.is(true, file.exists("lib/removeCombined/baseUrl-built/d.js"));
10521055

1056+
//Make sure empty directories are pruned
1057+
t.is(false, file.exists('lib/removeCombined/baseUrl-built/sub'), 'empty directories removed');
1058+
10531059
require._buildReset();
10541060
}
10551061

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
define(['main', 'c'], function (main, c) {
1+
define(['main', 'c', 'sub/e'], function (main, c, e) {
22
return {
33
name: 'secondary',
44
main: main,
5-
c: c
5+
c: c,
6+
e: e
67
};
78
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define({
2+
name: 'e'
3+
});

build/tests/lib/removeCombined/expected-secondary.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ define('c',{
33
name: 'c'
44
});
55

6-
define('secondary',['main', 'c'], function (main, c) {
6+
define('sub/e',{
7+
name: 'e'
8+
});
9+
10+
define('secondary',['main', 'c', 'sub/e'], function (main, c, e) {
711
return {
812
name: 'secondary',
913
main: main,
10-
c: c
14+
c: c,
15+
e: e
1116
};
1217
});

0 commit comments

Comments
 (0)