Skip to content

Commit 63d1e3e

Browse files
committed
Sort by type for compression benefits
Gzip is best optimized when files containing similar contents are nearby one another in the string of bytes being compressed. Sort by extname, then basename, then alphabetically, to increase the likelihood of similar files being found nearby one another in the resulting tarball archive. Fix #30
1 parent 4294bf9 commit 63d1e3e

40 files changed

+498
-158
lines changed

index.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,20 @@ const walkSync = options => {
249249
return walker.result
250250
}
251251

252-
// package.json first, node_modules last, files before folders, alphasort
253-
const sort = (a, b) =>
254-
a === 'package.json' ? -1
255-
: b === 'package.json' ? 1
256-
: /^node_modules/.test(a) && !/^node_modules/.test(b) ? 1
257-
: /^node_modules/.test(b) && !/^node_modules/.test(a) ? -1
258-
: path.dirname(a) === '.' && path.dirname(b) !== '.' ? -1
259-
: path.dirname(b) === '.' && path.dirname(a) !== '.' ? 1
260-
: a.localeCompare(b)
252+
// optimize for compressibility
253+
// extname, then basename, then locale alphabetically
254+
// https://twitter.com/isntitvacant/status/1131094910923231232
255+
const sort = (a, b) => {
256+
const exta = path.extname(a).toLowerCase()
257+
const extb = path.extname(b).toLowerCase()
258+
const basea = path.basename(a).toLowerCase()
259+
const baseb = path.basename(b).toLowerCase()
260+
261+
return exta.localeCompare(extb) ||
262+
basea.localeCompare(baseb) ||
263+
a.localeCompare(b)
264+
}
265+
261266

262267
module.exports = walk
263268
walk.sync = walkSync
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/bundled-scoped-symlink.js TAP includes bundled dependency async > must match snapshot 1`] = `
9+
Array [
10+
"elf.js",
11+
"node_modules/@npmwombat/history/index.js",
12+
"node_modules/@npmwombat/history/package.json",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/bundled-scoped-symlink.js TAP includes bundled dependency sync > must match snapshot 1`] = `
18+
Array [
19+
"elf.js",
20+
"node_modules/@npmwombat/history/index.js",
21+
"node_modules/@npmwombat/history/package.json",
22+
"package.json",
23+
]
24+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/bundled-scoped.js TAP includes bundled dependency async > must match snapshot 1`] = `
9+
Array [
10+
"elf.js",
11+
"node_modules/@npmwombat/history/index.js",
12+
"node_modules/@npmwombat/history/package.json",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/bundled-scoped.js TAP includes bundled dependency sync > must match snapshot 1`] = `
18+
Array [
19+
"elf.js",
20+
"node_modules/@npmwombat/history/index.js",
21+
"node_modules/@npmwombat/history/package.json",
22+
"package.json",
23+
]
24+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/bundled-symlink.js TAP includes bundled dependency async > must match snapshot 1`] = `
9+
Array [
10+
"elf.js",
11+
"node_modules/history/index.js",
12+
"node_modules/history/package.json",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/bundled-symlink.js TAP includes bundled dependency sync > must match snapshot 1`] = `
18+
Array [
19+
"elf.js",
20+
"node_modules/history/index.js",
21+
"node_modules/history/package.json",
22+
"package.json",
23+
]
24+
`
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/bundled.js TAP includes bundled dependency async > must match snapshot 1`] = `
9+
Array [
10+
"elf.js",
11+
"node_modules/history/index.js",
12+
"node_modules/history/package.json",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/bundled.js TAP includes bundled dependency sync > must match snapshot 1`] = `
18+
Array [
19+
"elf.js",
20+
"node_modules/history/index.js",
21+
"node_modules/history/package.json",
22+
"package.json",
23+
]
24+
`

tap-snapshots/test-cwd.js-TAP.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/cwd.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"deps/foo/config/config.gypi",
11+
"elf.js",
12+
"package.json",
13+
]
14+
`
15+
16+
exports[`test/cwd.js TAP follows npm package ignoring rules sync 1 > must match snapshot 1`] = `
17+
Array [
18+
"deps/foo/config/config.gypi",
19+
"elf.js",
20+
"package.json",
21+
]
22+
`
23+
24+
exports[`test/cwd.js TAP follows npm package ignoring rules sync 2 > must match snapshot 1`] = `
25+
Array [
26+
"deps/foo/config/config.gypi",
27+
"elf.js",
28+
"package.json",
29+
]
30+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/empty-npmignore.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"deps/foo/config/config.gypi",
11+
"elf.js",
12+
"lib/node_modules/foo/package.json",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/empty-npmignore.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
18+
Array [
19+
"deps/foo/config/config.gypi",
20+
"elf.js",
21+
"lib/node_modules/foo/package.json",
22+
"package.json",
23+
]
24+
`
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/ignores.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"deps/foo/config/config.gypi",
11+
"elf.js",
12+
"package.json",
13+
"readme.md",
14+
]
15+
`
16+
17+
exports[`test/ignores.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
18+
Array [
19+
"deps/foo/config/config.gypi",
20+
"elf.js",
21+
"package.json",
22+
"readme.md",
23+
]
24+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/include-gitignore.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"z/.gitignore",
11+
"deps/foo/config/config.gypi",
12+
"elf.js",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/include-gitignore.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
18+
Array [
19+
"z/.gitignore",
20+
"deps/foo/config/config.gypi",
21+
"elf.js",
22+
"package.json",
23+
]
24+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/nested-lock-and-core.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"lib/package-lock.json",
11+
"package.json",
12+
"lib/yarn.lock",
13+
"core/include-me.txt",
14+
]
15+
`
16+
17+
exports[`test/nested-lock-and-core.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
18+
Array [
19+
"lib/package-lock.json",
20+
"package.json",
21+
"lib/yarn.lock",
22+
"core/include-me.txt",
23+
]
24+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/package-json-bin-multiple.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"__bin_bar",
11+
"__bin_foo",
12+
"lib/elf.js",
13+
"package.json",
14+
]
15+
`
16+
17+
exports[`test/package-json-bin-multiple.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
18+
Array [
19+
"__bin_bar",
20+
"__bin_foo",
21+
"lib/elf.js",
22+
"package.json",
23+
]
24+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/package-json-bin-single.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"__bin",
11+
"lib/elf.js",
12+
"package.json",
13+
]
14+
`
15+
16+
exports[`test/package-json-bin-single.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
17+
Array [
18+
"__bin",
19+
"lib/elf.js",
20+
"package.json",
21+
]
22+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/package-json-empty.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"elf.js",
11+
"package.json",
12+
]
13+
`
14+
15+
exports[`test/package-json-empty.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
16+
Array [
17+
"elf.js",
18+
"package.json",
19+
]
20+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/package-json-main.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
9+
Array [
10+
"deps/foo/config/config.gypi",
11+
"__main.js",
12+
"browser.js",
13+
"elf.js",
14+
"package.json",
15+
]
16+
`
17+
18+
exports[`test/package-json-main.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
19+
Array [
20+
"deps/foo/config/config.gypi",
21+
"__main.js",
22+
"browser.js",
23+
"elf.js",
24+
"package.json",
25+
]
26+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* IMPORTANT
2+
* This snapshot file is auto-generated, but designed for humans.
3+
* It should be checked into source control and tracked carefully.
4+
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
5+
* Make sure to inspect the output below. Do not ignore changes!
6+
*/
7+
'use strict'
8+
exports[`test/package-json-nested.js TAP includes nested package.json file async > must match snapshot 1`] = `
9+
Array [
10+
"nest/index.js",
11+
"nest/package.json",
12+
"package.json",
13+
]
14+
`
15+
16+
exports[`test/package-json-nested.js TAP includes nested package.json file sync > must match snapshot 1`] = `
17+
Array [
18+
"nest/index.js",
19+
"nest/package.json",
20+
"package.json",
21+
]
22+
`

0 commit comments

Comments
 (0)