出自 webpack 插件 enhanced-resolve:
const dirname = path => {
let idx = path.length - 1
while (idx >= 0) {
const c = path.charCodeAt(idx)
// slash or backslash
if (c === 47 || c === 92) break
idx--
}
if (idx < 0) return ''
return path.slice(0, idx)
}