Skip to content

Commit 07a5bbb

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 7edb790 commit 07a5bbb

40 files changed

Lines changed: 498 additions & 158 deletions

index.js

Lines changed: 14 additions & 9 deletions
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
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 30 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`
Lines changed: 24 additions & 0 deletions
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+
`

0 commit comments

Comments
 (0)