Skip to content

Commit

Permalink
don't load the whole path module just for the sep
Browse files Browse the repository at this point in the history
Fix: #138
  • Loading branch information
isaacs committed Feb 16, 2022
1 parent dfa4f22 commit cdc3188
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isWindows = typeof process === 'object' &&
process &&
process.platform === 'win32'
module.exports = isWindows ? { sep: '\\' } : { sep: '/' }
4 changes: 1 addition & 3 deletions minimatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ const minimatch = module.exports = (p, pattern, options = {}) => {

module.exports = minimatch

const path = (() => { try { return require('path') } catch (e) {}})() || {
sep: '/'
}
const path = require('./lib/path.js')
minimatch.sep = path.sep

const GLOBSTAR = Symbol('globstar **')
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"license": "ISC",
"files": [
"minimatch.js"
"minimatch.js",
"lib"
]
}
5 changes: 4 additions & 1 deletion test/no-path-module.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const t = require('tap')
const mm = t.mock('../', { path: null })
const proc = process
global.process = null
const mm = t.mock('../minimatch.js')
global.process = proc
t.equal(mm.sep, '/')
22 changes: 19 additions & 3 deletions test/win-path-sep.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const t = require('tap')
t.test('path separator /', t => {
const mm = t.mock('../', { path: { sep: '/' }})
const mm = t.mock('../', { '../lib/path.js': { sep: '/' }})

t.equal(mm('x/y/z', 'x/y/*/z'), false)
t.equal(mm('x/y/w/z', 'x/y/*/z'), true)
t.end()
})

t.test('path separator \\', t => {
const mm = t.mock('../', { path: { sep: '\\' }})
const mm = t.mock('../', { '../lib/path.js': { sep: '\\' }})

t.equal(mm('x\\y\\z', 'x/y/*/z'), false)
t.equal(mm('x\\y\\w\\z', 'x/y/*/z'), true)
Expand All @@ -17,9 +17,25 @@ t.test('path separator \\', t => {

// just in case Node every adds Mac OS 9 support 😅
t.test('path separator :', t => {
const mm = t.mock('../', { path: { sep: ':' }})
const mm = t.mock('../', { '../lib/path.js': { sep: ':' }})

t.equal(mm('x:y:z', 'x/y/*/z'), false)
t.equal(mm('x:y:w:z', 'x/y/*/z'), true)
t.end()
})

t.test('windows default', t => {
const proc = global.process
global.process = { platform: 'win32' }
t.equal(t.mock('../lib/path.js').sep, '\\')
global.process = proc
t.end()
})

t.test('posix default', t => {
const proc = global.process
global.process = { platform: 'posix' }
t.equal(t.mock('../lib/path.js').sep, '/')
global.process = proc
t.end()
})

0 comments on commit cdc3188

Please sign in to comment.