const fs = require("fs");
const path = require("path");
function diffDirAndFile(arr = [], basePath = "") {
const result = {
dirs: [],
files: [],
};
arr.forEach((name) => {
const currentFileStat = fs.statSync(
path.resolve(__dirname, `${basePath}/${name}`)
);
const isDirectory = currentFileStat.isDirectory();
if (isDirectory) {
result.dirs.push(name);
} else {
result.files.push(name);
}
});
return result;
}
function getDir(keyName) {
const basePath = "../../src";
const result = fs.readdirSync(path.resolve(__dirname, basePath));
const diffResult = diffDirAndFile(result, basePath);
const resolveAlias = {};
diffResult.dirs.forEach((dirName) => {
const key = `${keyName}${dirName}`;
const absolutePath = path.resolve(__dirname, `${basePath}/${dirName}`);
resolveAlias[key] = absolutePath;
});
return resolveAlias;
}
module.exports = ({ keyName = "@" } = {}) => {
return {
config(config, env) {
const aliasRes = getDir(keyName);
return {
resolve: {
alias: aliasRes,
},
};
},
};
};