Skip to content

Commit

Permalink
Sort by type for compression benefits
Browse files Browse the repository at this point in the history
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
  • Loading branch information
isaacs committed Jun 29, 2019
1 parent 4294bf9 commit 63d1e3e
Show file tree
Hide file tree
Showing 40 changed files with 498 additions and 158 deletions.
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,20 @@ const walkSync = options => {
return walker.result
}

// package.json first, node_modules last, files before folders, alphasort
const sort = (a, b) =>
a === 'package.json' ? -1
: b === 'package.json' ? 1
: /^node_modules/.test(a) && !/^node_modules/.test(b) ? 1
: /^node_modules/.test(b) && !/^node_modules/.test(a) ? -1
: path.dirname(a) === '.' && path.dirname(b) !== '.' ? -1
: path.dirname(b) === '.' && path.dirname(a) !== '.' ? 1
: a.localeCompare(b)
// optimize for compressibility
// extname, then basename, then locale alphabetically
// https://twitter.com/isntitvacant/status/1131094910923231232
const sort = (a, b) => {
const exta = path.extname(a).toLowerCase()
const extb = path.extname(b).toLowerCase()
const basea = path.basename(a).toLowerCase()
const baseb = path.basename(b).toLowerCase()

return exta.localeCompare(extb) ||
basea.localeCompare(baseb) ||
a.localeCompare(b)
}


module.exports = walk
walk.sync = walkSync
Expand Down
24 changes: 24 additions & 0 deletions tap-snapshots/test-bundled-scoped-symlink.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/bundled-scoped-symlink.js TAP includes bundled dependency async > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/@npmwombat/history/index.js",
"node_modules/@npmwombat/history/package.json",
"package.json",
]
`

exports[`test/bundled-scoped-symlink.js TAP includes bundled dependency sync > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/@npmwombat/history/index.js",
"node_modules/@npmwombat/history/package.json",
"package.json",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-bundled-scoped.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/bundled-scoped.js TAP includes bundled dependency async > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/@npmwombat/history/index.js",
"node_modules/@npmwombat/history/package.json",
"package.json",
]
`

exports[`test/bundled-scoped.js TAP includes bundled dependency sync > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/@npmwombat/history/index.js",
"node_modules/@npmwombat/history/package.json",
"package.json",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-bundled-symlink.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/bundled-symlink.js TAP includes bundled dependency async > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/history/index.js",
"node_modules/history/package.json",
"package.json",
]
`

exports[`test/bundled-symlink.js TAP includes bundled dependency sync > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/history/index.js",
"node_modules/history/package.json",
"package.json",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-bundled.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/bundled.js TAP includes bundled dependency async > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/history/index.js",
"node_modules/history/package.json",
"package.json",
]
`

exports[`test/bundled.js TAP includes bundled dependency sync > must match snapshot 1`] = `
Array [
"elf.js",
"node_modules/history/index.js",
"node_modules/history/package.json",
"package.json",
]
`
30 changes: 30 additions & 0 deletions tap-snapshots/test-cwd.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/cwd.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
]
`

exports[`test/cwd.js TAP follows npm package ignoring rules sync 1 > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
]
`

exports[`test/cwd.js TAP follows npm package ignoring rules sync 2 > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-empty-npmignore.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/empty-npmignore.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"lib/node_modules/foo/package.json",
"package.json",
]
`

exports[`test/empty-npmignore.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"lib/node_modules/foo/package.json",
"package.json",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-ignores.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/ignores.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
"readme.md",
]
`

exports[`test/ignores.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
"readme.md",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-include-gitignore.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/include-gitignore.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"z/.gitignore",
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
]
`

exports[`test/include-gitignore.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"z/.gitignore",
"deps/foo/config/config.gypi",
"elf.js",
"package.json",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-nested-lock-and-core.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/nested-lock-and-core.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"lib/package-lock.json",
"package.json",
"lib/yarn.lock",
"core/include-me.txt",
]
`

exports[`test/nested-lock-and-core.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"lib/package-lock.json",
"package.json",
"lib/yarn.lock",
"core/include-me.txt",
]
`
24 changes: 24 additions & 0 deletions tap-snapshots/test-package-json-bin-multiple.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/package-json-bin-multiple.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"__bin_bar",
"__bin_foo",
"lib/elf.js",
"package.json",
]
`

exports[`test/package-json-bin-multiple.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"__bin_bar",
"__bin_foo",
"lib/elf.js",
"package.json",
]
`
22 changes: 22 additions & 0 deletions tap-snapshots/test-package-json-bin-single.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/package-json-bin-single.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"__bin",
"lib/elf.js",
"package.json",
]
`

exports[`test/package-json-bin-single.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"__bin",
"lib/elf.js",
"package.json",
]
`
20 changes: 20 additions & 0 deletions tap-snapshots/test-package-json-empty.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/package-json-empty.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"elf.js",
"package.json",
]
`

exports[`test/package-json-empty.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"elf.js",
"package.json",
]
`
26 changes: 26 additions & 0 deletions tap-snapshots/test-package-json-main.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/package-json-main.js TAP follows npm package ignoring rules async > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"__main.js",
"browser.js",
"elf.js",
"package.json",
]
`

exports[`test/package-json-main.js TAP follows npm package ignoring rules sync > must match snapshot 1`] = `
Array [
"deps/foo/config/config.gypi",
"__main.js",
"browser.js",
"elf.js",
"package.json",
]
`
22 changes: 22 additions & 0 deletions tap-snapshots/test-package-json-nested.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/package-json-nested.js TAP includes nested package.json file async > must match snapshot 1`] = `
Array [
"nest/index.js",
"nest/package.json",
"package.json",
]
`

exports[`test/package-json-nested.js TAP includes nested package.json file sync > must match snapshot 1`] = `
Array [
"nest/index.js",
"nest/package.json",
"package.json",
]
`
Loading

0 comments on commit 63d1e3e

Please sign in to comment.