-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (29 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const nodePath = require('path')
const fs = require('fs')
function findRelativePath(requiredPath, currentFileDir) {
if(!requiredPath || requiredPath[0] !== '#') {
return requiredPath
}
const pathParsed = nodePath.parse(requiredPath.slice(1))
const currentFileDirSplit = currentFileDir.split('/')
for(let i = currentFileDirSplit.length ; i >= 0; i -= 1) {
const testPath = (currentFileDir[0] === '/' ? '/' : '') + nodePath.join(...currentFileDirSplit.slice(0, i), pathParsed.dir, pathParsed.name + (pathParsed.ext || '.js'))
if(fs.existsSync(testPath)) {
return `./${nodePath.relative(currentFileDir, testPath)}`
}
}
return '#path-cant-find'
}
module.exports = function HashResolve({ types: t }) {
return {
visitor: {
ImportDeclaration: {
enter(path, state) {
const source = path.get('source')
const currentFileDir = nodePath.parse(state.file.opts.filename).dir
source.replaceWith(t.stringLiteral(findRelativePath(source.node.value, currentFileDir)))
},
},
},
}
}