2020-10-27 14:31:37 +08:00

64 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const path = require('path');
const chokidar = require('chokidar');
const webpack = require('webpack');
const createDevServer = require('../helpers/createDevServer');
const getPort = require('../helpers/getPort');
const log = require('../helpers/log');
const createDevConfig = require('../configs/webpack.config');
const generateRoute = require('./route');
const generateComponent = require('./components');
function routeHandle(config) {
generateRoute(config);
// 监听pages变化重新生成路由
const pagesWatcher = chokidar.watch(path.resolve(config.folders.PROJECT_DIR, './src/pages'));
pagesWatcher.on('ready', () => {
pagesWatcher.on('add', (filePath) => {
if (path.extname(filePath) === '.fes' || path.extname(filePath) === '.vue') {
generateRoute(config);
}
}).on('unlink', (filePath) => {
if (path.extname(filePath) === '.fes' || path.extname(filePath) === '.vue') {
generateRoute(config);
}
});
});
}
function globalComponentHandle(config) {
generateComponent(config);
// 监听components变化重新生成组件注入文件
const compWatcher = chokidar.watch(path.resolve(config.folders.PROJECT_DIR, './src/components'));
compWatcher.on('ready', () => {
compWatcher.on('add', (filePath) => {
if (path.extname(filePath) === '.fes' || path.extname(filePath) === '.vue') {
generateComponent(config);
}
}).on('unlink', (filePath) => {
if (path.extname(filePath) === '.fes' || path.extname(filePath) === '.vue') {
generateComponent(config);
}
});
});
}
function startDev(config) {
routeHandle(config);
globalComponentHandle(config);
const webpackConfig = createDevConfig(config, webpack, 'dev');
if (!webpackConfig) return;
getPort(config.ports.server)
.then((port) => {
log.message(`dev本地http服务端口: ${port}`);
createDevServer(port, webpackConfig);
}).catch((err) => {
log.message('执行dev失败');
log.error(JSON.stringify(err));
});
}
module.exports = startDev;