oipage 1.1.0 → 1.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG +11 -1
- package/bin/help.js +1 -0
- package/bin/run +29 -8
- package/bin/serve.js +14 -9
- package/bin/{format.js → tools/format.js} +13 -0
- package/bin/{resolve404.js → tools/resolve404.js} +1 -1
- package/bin/tools/resolveImport.js +61 -0
- package/nodejs/animation/index.js +1 -1
- package/nodejs/cmdlog/index.js +1 -1
- package/nodejs/disk/index.js +1 -1
- package/nodejs/logform/index.js +1 -1
- package/nodejs/throttle/index.js +1 -1
- package/package.json +5 -5
- package/web/animation/index.js +1 -1
- package/web/onReady/index.js +1 -1
- package/web/style/index.js +1 -1
- package/web/throttle/index.js +1 -1
- /package/bin/{mineTypes.json → data/mineTypes.json} +0 -0
- /package/{images → bin/images}/file.png +0 -0
- /package/{images → bin/images}/folder.png +0 -0
- /package/{template → bin/template}/404.html +0 -0
- /package/bin/{network.js → tools/network.js} +0 -0
package/CHANGELOG
CHANGED
|
@@ -32,4 +32,14 @@ v1.1.0:
|
|
|
32
32
|
* logform 表单输入
|
|
33
33
|
2、命令(oipage-cli)
|
|
34
34
|
* dist 磁盘操作
|
|
35
|
-
* run 运行多命令
|
|
35
|
+
* run 运行多命令
|
|
36
|
+
v1.2.0:
|
|
37
|
+
date:
|
|
38
|
+
changes:
|
|
39
|
+
- 优化改造
|
|
40
|
+
1、优化npm包目录结构等
|
|
41
|
+
- 新增功能
|
|
42
|
+
1、开发服务器
|
|
43
|
+
* 添加配置文件
|
|
44
|
+
(oipage-cli serve --config ./oipage.config.js)
|
|
45
|
+
* 新增对node_modules包引入支持
|
package/bin/help.js
CHANGED
package/bin/run
CHANGED
|
@@ -5,19 +5,40 @@
|
|
|
5
5
|
process.title = 'OIPage';
|
|
6
6
|
|
|
7
7
|
const { exec } = require('child_process');
|
|
8
|
-
const {
|
|
8
|
+
const { join } = require("path");
|
|
9
|
+
const { existsSync, lstatSync } = require("fs");
|
|
10
|
+
const { mergeOption } = require("vislite");
|
|
11
|
+
const { formatArgv } = require("./tools/format.js");
|
|
9
12
|
|
|
10
13
|
// 开发服务器
|
|
11
14
|
if (process.argv[2] === "serve") {
|
|
12
15
|
|
|
13
16
|
let argvObj = formatArgv(process.argv, {
|
|
14
|
-
"-p": "--port"
|
|
17
|
+
"-p": "--port",
|
|
18
|
+
"-c": "--config"
|
|
15
19
|
});
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
port:
|
|
19
|
-
baseUrl:
|
|
20
|
-
}
|
|
21
|
+
let config = {
|
|
22
|
+
port: 8080,
|
|
23
|
+
baseUrl: "./"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// 如果设置了配置文件
|
|
27
|
+
if (argvObj["--config"]) {
|
|
28
|
+
let configPath = join(process.cwd(), argvObj["--config"][0] || "./oipage.config.js");
|
|
29
|
+
if (existsSync(configPath) && !lstatSync(configPath).isDirectory()) {
|
|
30
|
+
let configValue = require(configPath);
|
|
31
|
+
mergeOption(config, configValue.devServer);
|
|
32
|
+
} else {
|
|
33
|
+
console.log("\x1b[0m\x1b[31m" + configPath + "\x1b[0m");
|
|
34
|
+
throw new Error("OIPage: The configuration file does not exist or is not a file.");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if ((argvObj["--port"] || [])[0]) config.port = (argvObj["--port"] || [])[0];
|
|
39
|
+
if ((argvObj["--baseUrl"] || [])[0]) config.baseUrl = (argvObj["--baseUrl"] || [])[0];
|
|
40
|
+
|
|
41
|
+
require("./serve.js")(config);
|
|
21
42
|
}
|
|
22
43
|
|
|
23
44
|
// 磁盘操作
|
|
@@ -30,7 +51,7 @@ else if (process.argv[2] === "disk") {
|
|
|
30
51
|
"-c": "--copy"
|
|
31
52
|
}, true);
|
|
32
53
|
|
|
33
|
-
require("./disk")(argvObj);
|
|
54
|
+
require("./disk.js")(argvObj);
|
|
34
55
|
}
|
|
35
56
|
|
|
36
57
|
// 运行多命令
|
|
@@ -66,5 +87,5 @@ else if (process.argv[2] === "run") {
|
|
|
66
87
|
|
|
67
88
|
// 默认,帮助
|
|
68
89
|
else {
|
|
69
|
-
require("./help")();
|
|
90
|
+
require("./help.js")();
|
|
70
91
|
}
|
package/bin/serve.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
const { join } = require("path");
|
|
2
|
-
const { existsSync, lstatSync,
|
|
2
|
+
const { existsSync, lstatSync, statSync, createReadStream } = require("fs");
|
|
3
3
|
const { createServer } = require('http');
|
|
4
4
|
const packageValue = require("../package.json");
|
|
5
|
-
const network = require("./network");
|
|
6
|
-
const mineTypes = require("./mineTypes.json");
|
|
7
|
-
const resolve404 = require("./resolve404.js");
|
|
5
|
+
const network = require("./tools/network.js");
|
|
6
|
+
const mineTypes = require("./data/mineTypes.json");
|
|
7
|
+
const resolve404 = require("./tools/resolve404.js");
|
|
8
|
+
const resolveImport = require("./tools/resolveImport.js");
|
|
9
|
+
const { formatRawHeaders } = require("./tools/format.js");
|
|
8
10
|
|
|
9
11
|
// 开发服务器
|
|
10
12
|
module.exports = function (config) {
|
|
@@ -14,8 +16,9 @@ module.exports = function (config) {
|
|
|
14
16
|
const basePath = (/^\./.test(config.baseUrl)) ? join(process.cwd(), config.baseUrl) : config.baseUrl; // 服务器根路径
|
|
15
17
|
|
|
16
18
|
let Server = createServer(function (request, response) {
|
|
17
|
-
|
|
19
|
+
let headers = formatRawHeaders(request.rawHeaders);
|
|
18
20
|
let url = decodeURIComponent(request.url);
|
|
21
|
+
|
|
19
22
|
url = url.split("?")[0];
|
|
20
23
|
|
|
21
24
|
// 请求的文件路径
|
|
@@ -28,25 +31,27 @@ module.exports = function (config) {
|
|
|
28
31
|
let fileType = mineTypes[dotName]; // 文件类型
|
|
29
32
|
let fileInfo = statSync(filePath);
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
let responseHeader = {
|
|
32
35
|
'Content-type': (fileType || "text/plain") + ";charset=utf-8",
|
|
33
36
|
'Access-Control-Allow-Origin': '*',
|
|
34
|
-
'Content-Length': fileInfo.size,
|
|
35
37
|
'Server': 'Powered by OIPage-dev-server@' + packageValue.version
|
|
36
|
-
}
|
|
38
|
+
};
|
|
37
39
|
|
|
38
40
|
let sendType = "";
|
|
39
41
|
|
|
40
42
|
// 如果文件小于10M,认为不大,直接读取
|
|
41
43
|
if (fileInfo.size < 10 * 1024 * 1024) {
|
|
42
44
|
sendType = "Read";
|
|
43
|
-
response.
|
|
45
|
+
response.writeHead('200', responseHeader);
|
|
46
|
+
response.write(resolveImport(basePath, filePath, headers.Accept === "*/*"));
|
|
44
47
|
response.end();
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
// 对于大文件,使用流读取
|
|
48
51
|
else {
|
|
49
52
|
sendType = "Stream";
|
|
53
|
+
responseHeader["Content-Length"] = fileInfo.size;
|
|
54
|
+
response.writeHead('200', responseHeader);
|
|
50
55
|
createReadStream(filePath).pipe(response);
|
|
51
56
|
}
|
|
52
57
|
|
|
@@ -73,4 +73,17 @@ exports.formatArgv = function (argvs, shorts, isArray) {
|
|
|
73
73
|
pushValue();
|
|
74
74
|
|
|
75
75
|
return result;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 请求头解析
|
|
80
|
+
* @param {*} rawHeaders
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
exports.formatRawHeaders = function (rawHeaders) {
|
|
84
|
+
let headers = {};
|
|
85
|
+
for (let i = 0; i < rawHeaders.length - 1; i += 2) {
|
|
86
|
+
headers[rawHeaders[i]] = rawHeaders[i + 1];
|
|
87
|
+
}
|
|
88
|
+
return headers;
|
|
76
89
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { readdirSync, lstatSync, readFileSync, statSync } = require("fs");
|
|
2
2
|
const { join } = require("path");
|
|
3
3
|
const { formatByte, formatTime } = require("./format");
|
|
4
|
-
const mineTypes = require("
|
|
4
|
+
const mineTypes = require("../data/mineTypes.json");
|
|
5
5
|
|
|
6
6
|
const img_folder = "data:image/png;base64," + readFileSync(join(__dirname, "../images/folder.png")).toString('base64');
|
|
7
7
|
const img_file = "data:image/png;base64," + readFileSync(join(__dirname, "../images/file.png")).toString('base64');
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const { readFileSync, existsSync, lstatSync } = require("fs");
|
|
2
|
+
const { join } = require("path");
|
|
3
|
+
|
|
4
|
+
module.exports = function (basePath, filePath, needResolve) {
|
|
5
|
+
let code = readFileSync(filePath);
|
|
6
|
+
|
|
7
|
+
let resolveImport = function (content) {
|
|
8
|
+
return content.replace(/import [^'"]* from (['"])([^'"]*)['"]/sg, function (_importCode, _, _importUrl) {
|
|
9
|
+
if (/^[./]/.test(_importUrl)) {
|
|
10
|
+
return _importCode;
|
|
11
|
+
} else {
|
|
12
|
+
return _importCode.replace(_importUrl, _importUrl.replace(/([^/])+/s, function (npmName) {
|
|
13
|
+
|
|
14
|
+
let node_modulesRootPath = join(filePath, "../");
|
|
15
|
+
let prePath = "";
|
|
16
|
+
while (true) {
|
|
17
|
+
let npmBundlePath = join(node_modulesRootPath, "./node_modules/", npmName);
|
|
18
|
+
|
|
19
|
+
// 如果存在
|
|
20
|
+
if (existsSync(npmBundlePath) && lstatSync(npmBundlePath).isDirectory()) {
|
|
21
|
+
let npmNameValue = npmName;
|
|
22
|
+
|
|
23
|
+
// 对于类似 import VISLite from "vislite"
|
|
24
|
+
// 需要把包名解析成具体的文件
|
|
25
|
+
if (!/\//.test(_importUrl)) {
|
|
26
|
+
let bundlePackage = require(join(npmBundlePath, "./package.json"));
|
|
27
|
+
npmNameValue = npmName + "/" + bundlePackage.main;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return (prePath ? prePath : "./") + "node_modules/" + npmNameValue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (node_modulesRootPath === basePath) {
|
|
34
|
+
return npmName;
|
|
35
|
+
} else {
|
|
36
|
+
node_modulesRootPath = join(node_modulesRootPath, "../");
|
|
37
|
+
prePath = "../" + prePath;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// 如果需要解析
|
|
46
|
+
if (needResolve) {
|
|
47
|
+
return resolveImport(code + "");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 如果是.html或.htm结尾
|
|
51
|
+
else if (/\.html{0,1}$/.test(filePath)) {
|
|
52
|
+
return (code + "").replace(/<script type="module">(.*)<\/script>/sg, function (_, _matchCode) {
|
|
53
|
+
return `<script type="module">${resolveImport(_matchCode)}</script>`;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 否则直接返回即可
|
|
58
|
+
else {
|
|
59
|
+
return code;
|
|
60
|
+
}
|
|
61
|
+
};
|
package/nodejs/cmdlog/index.js
CHANGED
package/nodejs/disk/index.js
CHANGED
package/nodejs/logform/index.js
CHANGED
package/nodejs/throttle/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oipage",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-alpha.0",
|
|
4
4
|
"description": "前端网页或应用快速开发助手,包括开发服务器、辅助命令、实用API等",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "node ./build/index.js watch",
|
|
8
8
|
"build": "node ./build/index.js",
|
|
9
|
-
"serve": "node ./bin/run serve
|
|
9
|
+
"serve": "node ./bin/run serve --config ./oipage.config.js"
|
|
10
10
|
},
|
|
11
11
|
"bin": {
|
|
12
12
|
"oipage-cli": "bin/run"
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"url": "https://github.com/oi-contrib/OIPage/issues"
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://oi-contrib.github.io/OIPage",
|
|
37
|
-
"
|
|
38
|
-
"
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"vislite": "^1.3.0"
|
|
39
39
|
}
|
|
40
|
-
}
|
|
40
|
+
}
|
package/web/animation/index.js
CHANGED
package/web/onReady/index.js
CHANGED
package/web/style/index.js
CHANGED
package/web/throttle/index.js
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|