hm-react-cli
Version:
Create a Huimei React project by module
56 lines (49 loc) • 1.96 kB
JavaScript
const path = require('path');
const fs = require('fs');
const webpackDev = require('../webpack/webpack.config.dev');
const processPath = process.cwd();
const curPathGraphList = processPath.split('/').slice(1);
const curPathGraph = curPathGraphList.reduce((pre, cur, index) => ((pre[cur] = index), pre), {});
module.exports = function serve(entry, options) {
const configPath = getConfigFilePath(options);
const config = require(configPath);
const distBasePath = getDistPath(config);
const pageBasePath = getPagePath(distBasePath, config);
process.env.mode = 'prod'
// 得出 webpack 的入口文件前缀地址、出口文件前缀地址。
webpackDev({
...config,
inDunshan: curPathGraph['dunshan'] !== undefined,
distBasePath,
pageBasePath
});
};
function getPagePath(distPath) {
const isTaishan = curPathGraph['taishan'] || curPathGraph['taishan_new'];
// /Users/zhangpeng/Desktop/work/dunshan/source/taishan/WebRoot/app/modules
let existWebRoot = fs.existsSync(path.resolve(distPath, 'WebRoot'));
if (isTaishan) {
return path.resolve(distPath, 'WebRoot/app/modules');
} else if (existWebRoot) {
return path.resolve(distPath, 'WebRoot');
} else {
return processPath;
}
}
function getDistPath() {
const inDunshan = curPathGraph['dunshan'] !== undefined;
if (inDunshan) {
const index = curPathGraph['source'] + 1;
return '/' + curPathGraphList.slice(0, index + 1).join('/');
}
return processPath;
}
function getConfigFilePath(options) {
const userRoot = process.cwd();
const { config = 'hm.config.js' } = options;
// 优先拿用户传入的 config 或者默认值的文件地址 hm.config.js
let configPath = path.join(userRoot, config);
let exist = fs.existsSync(configPath);
if (exist) return configPath;
throw new Error(`未在项目根目录找到 ${config} 文件`);
}