Skip to content

Commit cdc3188

Browse files
committed
don't load the whole path module just for the sep
Fix: #138
1 parent dfa4f22 commit cdc3188

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

lib/path.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const isWindows = typeof process === 'object' &&
2+
process &&
3+
process.platform === 'win32'
4+
module.exports = isWindows ? { sep: '\\' } : { sep: '/' }

minimatch.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ const minimatch = module.exports = (p, pattern, options = {}) => {
1111

1212
module.exports = minimatch
1313

14-
const path = (() => { try { return require('path') } catch (e) {}})() || {
15-
sep: '/'
16-
}
14+
const path = require('./lib/path.js')
1715
minimatch.sep = path.sep
1816

1917
const GLOBSTAR = Symbol('globstar **')

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"license": "ISC",
2828
"files": [
29-
"minimatch.js"
29+
"minimatch.js",
30+
"lib"
3031
]
3132
}

test/no-path-module.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
const t = require('tap')
2-
const mm = t.mock('../', { path: null })
2+
const proc = process
3+
global.process = null
4+
const mm = t.mock('../minimatch.js')
5+
global.process = proc
36
t.equal(mm.sep, '/')

test/win-path-sep.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const t = require('tap')
22
t.test('path separator /', t => {
3-
const mm = t.mock('../', { path: { sep: '/' }})
3+
const mm = t.mock('../', { '../lib/path.js': { sep: '/' }})
44

55
t.equal(mm('x/y/z', 'x/y/*/z'), false)
66
t.equal(mm('x/y/w/z', 'x/y/*/z'), true)
77
t.end()
88
})
99

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

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

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

2222
t.equal(mm('x:y:z', 'x/y/*/z'), false)
2323
t.equal(mm('x:y:w:z', 'x/y/*/z'), true)
2424
t.end()
2525
})
26+
27+
t.test('windows default', t => {
28+
const proc = global.process
29+
global.process = { platform: 'win32' }
30+
t.equal(t.mock('../lib/path.js').sep, '\\')
31+
global.process = proc
32+
t.end()
33+
})
34+
35+
t.test('posix default', t => {
36+
const proc = global.process
37+
global.process = { platform: 'posix' }
38+
t.equal(t.mock('../lib/path.js').sep, '/')
39+
global.process = proc
40+
t.end()
41+
})

0 commit comments

Comments
 (0)