Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort for compression #33

Merged
merged 5 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '8'
- '10'
- '12'
Expand Down
31 changes: 20 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ const npmWalker = Class => class Walker extends Class {
}

onReaddir (entries) {
if (!this.parent)
entries = entries.filter(e => e !== '.git')
if (!this.parent) {
entries = entries.filter(e =>
e !== '.git' &&
!(e === 'node_modules' && this.bundled.length === 0)
)
}
return super.onReaddir(entries)
}

Expand Down Expand Up @@ -249,15 +253,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
Loading