ant-design-vue/antd-tools/utils/projectHelper.js

54 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-10-19 08:43:10 +00:00
const fs = require('fs');
const path = require('path');
const cwd = process.cwd();
function getProjectPath(...filePath) {
return path.join(cwd, ...filePath);
}
function resolve(moduleName) {
return require.resolve(moduleName);
}
// We need hack the require to ensure use package module first
// For example, `typescript` is required by `gulp-typescript` but provided by `antd`
let injected = false;
function injectRequire() {
if (injected) return;
const Module = require('module');
const oriRequire = Module.prototype.require;
2021-12-04 17:06:31 +00:00
Module.prototype.require = function (...args) {
2020-10-19 08:43:10 +00:00
const moduleName = args[0];
try {
return oriRequire.apply(this, args);
} catch (err) {
const newArgs = [...args];
if (moduleName[0] !== '/') {
newArgs[0] = getProjectPath('node_modules', moduleName);
}
return oriRequire.apply(this, newArgs);
}
};
injected = true;
}
function getConfig() {
const configPath = getProjectPath('.antd-tools.config.js');
if (fs.existsSync(configPath)) {
return require(configPath);
}
return {};
}
module.exports = {
getProjectPath,
resolve,
injectRequire,
getConfig,
};