Skip to content

Commit

Permalink
feat(server): support starting server inside project
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhaoju committed Jun 20, 2017
1 parent f566684 commit baee9ea
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 48 deletions.
60 changes: 36 additions & 24 deletions lib/commands/server.js
Expand Up @@ -169,7 +169,7 @@ exports.run = function (options) {
app.use(function (req, res, next) {
try {
var projectInfo = getProjectInfo(req);
var project = Manager.getProject(projectInfo.projectCwd, { cache: false });
var project = Manager.getProject(projectInfo.projectDir, { cache: false });
var cache = customMiddlewareCache;

if (cache.apps.indexOf(projectInfo.projectName) === -1) {
Expand All @@ -194,25 +194,25 @@ exports.run = function (options) {
}
});

app.use(hostReplaceMiddleware);
// app.use(hostReplaceMiddleware);

// compiler
app.use(function (req, res, next) {
var url = req.url,
keys = url.split('/'),
compiler = null;

var projectInfo = getProjectInfo(req);
var projectName = projectInfo.projectName;
var projectCwd = projectInfo.projectCwd;
var project = Manager.getProject(projectCwd, { cache: false });
var projectDir = projectInfo.projectDir;
var project = Manager.getProject(projectDir, { cache: false });
var wpConfig = project.config._config;
var outputDir = project.config._config.output.local.path || 'prd';
var outputConfigDir = project.config._config.output.local.path || 'prd';
var outputAbsDir = sysPath.isAbsolute(outputConfigDir) ? outputConfigDir : sysPath.join(projectDir, outputConfigDir);

// 非 output.path 下的资源不做处理
if (keys[2] !== sysPath.relative(projectCwd, outputDir)) {
next();
return;
url = url.split(projectName).length > 1 ? url.split(projectName)[1] : url;
if (!projectName || sysPath.join(projectDir, url).indexOf(outputAbsDir) === -1) {
return next();
}

// 清除 YKIT_CACHE_DIR 资源
Expand All @@ -223,17 +223,17 @@ exports.run = function (options) {
}
});
if (isFirstCompileDir) {
UtilFs.deleteFolderRecursive(sysPath.join(projectCwd, YKIT_CACHE_DIR), true);
UtilFs.deleteFolderRecursive(sysPath.join(projectDir, YKIT_CACHE_DIR), true);
}

// 处理资源路径, 去掉 query & 版本号
var rquery = /\?.+$/;
var rversion = /@[^\.]+(?=\.\w+)/;
req.url = '/' + keys.slice(3).join('/').replace(rversion, '').replace(rquery, '');
req.url = url = '/' + sysPath.relative(outputAbsDir, sysPath.join(projectDir, url)).replace(rversion, '').replace(rquery, '');

// 生成 cacheId
var requestUrl = req.url.replace('.map', '').slice(1);
var cacheId = sysPath.join(projectName, requestUrl);
url = url.replace('.map', '').slice(1);
var cacheId = sysPath.join(projectName, url);

// 寻找已有的 middlewareCache
if (middlewareCache[cacheId]) {
Expand All @@ -257,7 +257,7 @@ exports.run = function (options) {
wpConfig.output.local.publicPath = localPublicPath;
} else {
// hot 且 未指定 publicPath 需要手动设置方式 hot.json 404
var relativePath = sysPath.relative(projectCwd, wpConfig.output.local.path);
var relativePath = sysPath.relative(projectDir, wpConfig.output.local.path);
wpConfig.output.local.publicPath = 'http://127.0.0.1:' + port + '/' + projectName + '/' + relativePath + '/';
}
}
Expand Down Expand Up @@ -292,11 +292,11 @@ exports.run = function (options) {
});

if (shouldCompileAllEntries && !allAssetsEntry[projectName]) {
allAssetsEntry[projectName] = requestUrl;
allAssetsEntry[projectName] = url;
}

var nextConfig = void 0;
if (!shouldCompileAllEntries || allAssetsEntry[projectName] === requestUrl) {
if (!shouldCompileAllEntries || allAssetsEntry[projectName] === url) {
compiler = project.getServerCompiler(function (config) {

config.plugins.push(require('../plugins/progressBarPlugin.js'));
Expand Down Expand Up @@ -358,8 +358,8 @@ exports.run = function (options) {
}

// 判断所请求的资源是否在入口配置中
var matchingPath = sysPath.normalize(entryPath) === sysPath.normalize(requestUrl);
var matchingKey = sysPath.normalize(requestUrl) === entryKey + sysPath.extname(requestUrl);
var matchingPath = sysPath.normalize(entryPath) === sysPath.normalize(url);
var matchingKey = sysPath.normalize(url) === entryKey + sysPath.extname(url);

if (matchingPath || matchingKey) {
isRequestingEntry = true;
Expand All @@ -380,7 +380,7 @@ exports.run = function (options) {
setTimeout(function () {
if (promiseCache[projectName]) {
_promise2.default.all(promiseCache[projectName]).then(function () {
var assetKey = sysPath.join(projectName, requestUrl);
var assetKey = sysPath.join(projectName, url);
if (middlewareCache[assetKey]) {
middlewareCache[assetKey](req, res, next);
} else {
Expand Down Expand Up @@ -568,14 +568,26 @@ exports.run = function (options) {
}

function getProjectInfo(req) {
var url = req.url,
keys = url.split('/'),
projectName = keys[1],
projectCwd = sysPath.join(cwd, projectName);
var dirSections = req.url.split('/');
var dirLevel = '',
projectDir = '',
projectName = '';

for (var i = 0, len = dirSections.length; i < len; i++) {
dirLevel += dirSections[i] + '/';

var searchDir = sysPath.join(cwd, dirLevel, '');
var ykitConf = globby.sync(['ykit.*.js', 'ykit.js'], { cwd: searchDir })[0];

if (ykitConf) {
projectDir = searchDir;
projectName = sysPath.basename(searchDir);
break;
}
}
return {
projectName: projectName,
projectCwd: projectCwd
projectDir: projectDir
};
}
};
65 changes: 41 additions & 24 deletions src/commands/server.js
Expand Up @@ -151,7 +151,7 @@ exports.run = (options) => {
app.use(function (req, res, next) {
try {
const projectInfo = getProjectInfo(req);
const project = Manager.getProject(projectInfo.projectCwd, { cache: false });
const project = Manager.getProject(projectInfo.projectDir, { cache: false });
const cache = customMiddlewareCache;

if(cache.apps.indexOf(projectInfo.projectName) === -1) {
Expand All @@ -176,25 +176,27 @@ exports.run = (options) => {
}
});

app.use(hostReplaceMiddleware);
// app.use(hostReplaceMiddleware);

// compiler
app.use(function (req, res, next) {
let url = req.url,
keys = url.split('/'),
compiler = null;

const projectInfo = getProjectInfo(req);
const projectName = projectInfo.projectName;
const projectCwd = projectInfo.projectCwd;
const project = Manager.getProject(projectCwd, { cache: false });
const projectDir = projectInfo.projectDir;
const project = Manager.getProject(projectDir, { cache: false });
const wpConfig = project.config._config;
const outputDir = project.config._config.output.local.path || 'prd';
const outputConfigDir = project.config._config.output.local.path || 'prd';
const outputAbsDir = sysPath.isAbsolute(outputConfigDir)
? outputConfigDir
: sysPath.join(projectDir, outputConfigDir);

// 非 output.path 下的资源不做处理
if(keys[2] !== sysPath.relative(projectCwd, outputDir)) {
next();
return;
url = url.split(projectName).length > 1 ? url.split(projectName)[1] : url;
if(!projectName || sysPath.join(projectDir, url).indexOf(outputAbsDir) === -1) {
return next();
}

// 清除 YKIT_CACHE_DIR 资源
Expand All @@ -205,17 +207,20 @@ exports.run = (options) => {
}
});
if(isFirstCompileDir) {
UtilFs.deleteFolderRecursive(sysPath.join(projectCwd, YKIT_CACHE_DIR), true);
UtilFs.deleteFolderRecursive(sysPath.join(projectDir, YKIT_CACHE_DIR), true);
}

// 处理资源路径, 去掉 query & 版本号
const rquery = /\?.+$/;
const rversion = /@[^\.]+(?=\.\w+)/;
req.url = '/' + keys.slice(3).join('/').replace(rversion, '').replace(rquery, '');
req.url = url = '/' + sysPath.relative(outputAbsDir, sysPath.join(projectDir, url))
.replace(rversion, '')
.replace(rquery, '');

// 生成 cacheId
const requestUrl = req.url.replace('.map', '').slice(1);
const cacheId = sysPath.join(projectName, requestUrl);
url = url.replace('.map', '').slice(1);
const cacheId = sysPath.join(projectName, url);


// 寻找已有的 middlewareCache
if(middlewareCache[cacheId]) {
Expand All @@ -239,7 +244,7 @@ exports.run = (options) => {
wpConfig.output.local.publicPath = localPublicPath;
} else {
// hot 且 未指定 publicPath 需要手动设置方式 hot.json 404
const relativePath = sysPath.relative(projectCwd, wpConfig.output.local.path);
const relativePath = sysPath.relative(projectDir, wpConfig.output.local.path);
wpConfig.output.local.publicPath = `http://127.0.0.1:${port}/${projectName}/${relativePath}/`;
}
}
Expand Down Expand Up @@ -274,11 +279,11 @@ exports.run = (options) => {
});

if(shouldCompileAllEntries && !allAssetsEntry[projectName]) {
allAssetsEntry[projectName] = requestUrl;
allAssetsEntry[projectName] = url;
}

let nextConfig;
if(!shouldCompileAllEntries || allAssetsEntry[projectName] === requestUrl) {
if(!shouldCompileAllEntries || allAssetsEntry[projectName] === url) {
compiler = project.getServerCompiler(function (config) {

config.plugins.push(require('../plugins/progressBarPlugin.js'));
Expand Down Expand Up @@ -342,8 +347,8 @@ exports.run = (options) => {
}

// 判断所请求的资源是否在入口配置中
const matchingPath = sysPath.normalize(entryPath) === sysPath.normalize(requestUrl);
const matchingKey = sysPath.normalize(requestUrl) === entryKey + sysPath.extname(requestUrl);
const matchingPath = sysPath.normalize(entryPath) === sysPath.normalize(url);
const matchingKey = sysPath.normalize(url) === entryKey + sysPath.extname(url);

if (matchingPath || matchingKey) {
isRequestingEntry = true;
Expand All @@ -366,7 +371,7 @@ exports.run = (options) => {
setTimeout(() => {
if (promiseCache[projectName]) {
Promise.all(promiseCache[projectName]).then(function () {
const assetKey = sysPath.join(projectName, requestUrl);
const assetKey = sysPath.join(projectName, url);
if(middlewareCache[assetKey]) {
middlewareCache[assetKey](req, res, next);
} else {
Expand Down Expand Up @@ -556,14 +561,26 @@ exports.run = (options) => {
}

function getProjectInfo(req) {
var url = req.url,
keys = url.split('/'),
projectName = keys[1],
projectCwd = sysPath.join(cwd, projectName);
const dirSections = req.url.split('/');
let dirLevel = '',
projectDir = '',
projectName = '';

for(let i = 0, len = dirSections.length; i < len; i++) {
dirLevel += dirSections[i] + '/';

const searchDir = sysPath.join(cwd, dirLevel, '');
const ykitConf = globby.sync(['ykit.*.js', 'ykit.js'], {cwd: searchDir})[0];

if(ykitConf) {
projectDir = searchDir;
projectName = sysPath.basename(searchDir);
break;
}
}
return {
projectName: projectName,
projectCwd: projectCwd
projectDir: projectDir
};
}
};

0 comments on commit baee9ea

Please sign in to comment.