const { existsSync } = require('fs')
const { join, resolve, isAbsolute } = require('path')
module.exports = ({ types }) => {
return {
visitor: {
ImportDeclaration(path, { opts }) {
const { node } = path
let { value } = node.source
node.source = Object.assign(node.source, replaceDependence(value, opts))
},
CallExpression(path, { opts }) {
const { node } = path
const { name } = node.callee
if (types.isIdentifier(node.callee)
&& name === 'require'
) {
const [{value: v}] = node.arguments
if(v && typeof v === 'string'){
node.arguments[0] = Object.assign(node.arguments[0], replaceDependence(v, opts))
}
}
}
}
}
}
function getNodeValue(depName){
return {
value: depName,
extra: {
rawValue: depName,
raw: JSON.stringify(depName)
}
}
}
function replaceDependence(depName, {
platform,
extensions = ['.js', '.json', '.vue'],
path = 'dist'
}) {
let root = process.cwd()
let result = getNodeValue(depName)
let prefixPath = ''
if (!platform) return result
prefixPath = isAbsolute(path)
? join(path, platform)
: join(root, 'node_modules', depName, path, platform)
// if the appended path exists, replace the origin path with it
if (Array.isArray(extensions)) {
let ext, fullPath
for (let i in extensions) {
ext = extensions[i]
fullPath = join(prefixPath, `index${ext}`)
console.log(fullPath)
if (existsSync(fullPath)) {
return getNodeValue(join(depName, platform))
}
}
}
return result
}
|