oipage 1.1.0 → 1.2.0-alpha.1

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 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
@@ -9,6 +9,7 @@ OIPage@v${packageValue.version}
9
9
  【1】oipage-cli serve 开发服务器
10
10
  --port|-p 端口号
11
11
  --baseUrl 服务器根目录
12
+ --config|-c 设置配置文件
12
13
 
13
14
  【2】oipage-cli disk 磁盘操作
14
15
  --force|-f 强制执行
package/bin/run CHANGED
@@ -5,19 +5,43 @@
5
5
  process.title = 'OIPage';
6
6
 
7
7
  const { exec } = require('child_process');
8
- const { formatArgv } = require("./format");
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
- require("./serve")({
18
- port: (argvObj["--port"] || [])[0] || 8080,
19
- baseUrl: (argvObj["--baseUrl"] || [])[0] || "./",
20
- });
21
+ let config = {
22
+ devServer: {
23
+ port: 8080,
24
+ baseUrl: "./",
25
+ },
26
+ bundle: []
27
+ };
28
+
29
+ // 如果设置了配置文件
30
+ if (argvObj["--config"]) {
31
+ let configPath = join(process.cwd(), argvObj["--config"][0] || "./oipage.config.js");
32
+ if (existsSync(configPath) && !lstatSync(configPath).isDirectory()) {
33
+ let configValue = require(configPath);
34
+ mergeOption(config, configValue);
35
+ } else {
36
+ console.log("\x1b[0m\x1b[31m" + configPath + "\x1b[0m");
37
+ throw new Error("OIPage: The configuration file does not exist or is not a file.");
38
+ }
39
+ }
40
+
41
+ if ((argvObj["--port"] || [])[0]) config.devServer.port = (argvObj["--port"] || [])[0];
42
+ if ((argvObj["--baseUrl"] || [])[0]) config.devServer.baseUrl = (argvObj["--baseUrl"] || [])[0];
43
+
44
+ require("./serve.js")(config);
21
45
  }
22
46
 
23
47
  // 磁盘操作
@@ -30,7 +54,7 @@ else if (process.argv[2] === "disk") {
30
54
  "-c": "--copy"
31
55
  }, true);
32
56
 
33
- require("./disk")(argvObj);
57
+ require("./disk.js")(argvObj);
34
58
  }
35
59
 
36
60
  // 运行多命令
@@ -66,5 +90,5 @@ else if (process.argv[2] === "run") {
66
90
 
67
91
  // 默认,帮助
68
92
  else {
69
- require("./help")();
93
+ require("./help.js")();
70
94
  }
package/bin/serve.js CHANGED
@@ -1,22 +1,26 @@
1
1
  const { join } = require("path");
2
- const { existsSync, lstatSync, readFileSync, statSync, createReadStream } = require("fs");
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) {
11
13
  let startTime = new Date().valueOf();
12
14
 
13
- const port = config.port; // 端口号
14
- const basePath = (/^\./.test(config.baseUrl)) ? join(process.cwd(), config.baseUrl) : config.baseUrl; // 服务器根路径
15
+ const port = config.devServer.port; // 端口号
16
+ const basePath = (/^\./.test(config.devServer.baseUrl)) ? join(process.cwd(), config.devServer.baseUrl) : config.devServer.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);
19
- url = url.split("?")[0];
21
+
22
+ let urlArray = url.split("?");
23
+ url = urlArray[0];
20
24
 
21
25
  // 请求的文件路径
22
26
  let filePath = join(basePath, url == "/" ? "index.html" : url.replace(/^\//, ""));
@@ -28,25 +32,44 @@ module.exports = function (config) {
28
32
  let fileType = mineTypes[dotName]; // 文件类型
29
33
  let fileInfo = statSync(filePath);
30
34
 
31
- response.writeHead('200', {
35
+ let responseHeader = {
32
36
  'Content-type': (fileType || "text/plain") + ";charset=utf-8",
33
37
  'Access-Control-Allow-Origin': '*',
34
- 'Content-Length': fileInfo.size,
35
38
  'Server': 'Powered by OIPage-dev-server@' + packageValue.version
36
- });
39
+ };
37
40
 
38
41
  let sendType = "";
39
42
 
40
43
  // 如果文件小于10M,认为不大,直接读取
41
44
  if (fileInfo.size < 10 * 1024 * 1024) {
45
+ let source = resolveImport(basePath, filePath, headers.Accept === "*/*", urlArray[1] === "download")
46
+
47
+ // 只处理非下载文件
48
+ // 过大的也不进行处理
49
+ if (urlArray[1] !== "download") {
50
+ for (let i = 0; i < config.bundle.length; i++) {
51
+ if (config.bundle[i].test.test(filePath)) {
52
+ source = config.bundle[i].handler.call({
53
+ root: basePath, // 服务器根路径
54
+ path: filePath.replace(basePath, ""), // 文件相对路径
55
+ entry: headers.Accept !== "*/*", // 是否是浏览器地址栏直接访问
56
+ }, source);
57
+ break;
58
+ }
59
+ }
60
+ }
61
+
42
62
  sendType = "Read";
43
- response.write(readFileSync(filePath));
63
+ response.writeHead('200', responseHeader);
64
+ response.write(source);
44
65
  response.end();
45
66
  }
46
67
 
47
68
  // 对于大文件,使用流读取
48
69
  else {
49
70
  sendType = "Stream";
71
+ responseHeader["Content-Length"] = fileInfo.size;
72
+ response.writeHead('200', responseHeader);
50
73
  createReadStream(filePath).pipe(response);
51
74
  }
52
75
 
@@ -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("./mineTypes.json");
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');
@@ -72,7 +72,7 @@ module.exports = function (filePath, url) {
72
72
  </th>
73
73
  <th>
74
74
  <a href='./${subItems[i]}' class="btn">访问</a>
75
- <a href='./${subItems[i]}' class="btn" download='${subItems[i]}'>下载</a>
75
+ <a href='./${subItems[i]}?download' class="btn" download='${subItems[i]}'>下载</a>
76
76
  </th>
77
77
  </tr>`;
78
78
  }
@@ -0,0 +1,66 @@
1
+ const { readFileSync, existsSync, lstatSync } = require("fs");
2
+ const { join } = require("path");
3
+
4
+ module.exports = function (basePath, filePath, needResolve, isDownload) {
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 (isDownload) {
47
+ return code;
48
+ }
49
+
50
+ // 如果需要解析
51
+ else if (needResolve) {
52
+ return resolveImport(code + "");
53
+ }
54
+
55
+ // 如果是.html或.htm结尾
56
+ else if (/\.html{0,1}$/.test(filePath)) {
57
+ return (code + "").replace(/<script type="module">(.*)<\/script>/sg, function (_, _matchCode) {
58
+ return `<script type="module">${resolveImport(_matchCode)}</script>`;
59
+ });
60
+ }
61
+
62
+ // 否则直接返回即可
63
+ else {
64
+ return code;
65
+ }
66
+ };
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * animation of OIPage v1.1.0
2
+ * animation of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * cmdlog of OIPage v1.1.0
2
+ * cmdlog of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * disk of OIPage v1.1.0
2
+ * disk of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * logform of OIPage v1.1.0
2
+ * logform of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
  const {linelog} = require("../cmdlog/index.js");
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * throttle of OIPage v1.1.0
2
+ * throttle of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "oipage",
3
- "version": "1.1.0",
3
+ "version": "1.2.0-alpha.1",
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 -p 20000"
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
- "devDependencies": {
38
- "oipage": "^0.2.0"
37
+ "dependencies": {
38
+ "vislite": "^1.3.0"
39
39
  }
40
- }
40
+ }
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * animation of OIPage v1.1.0
2
+ * animation of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * onReady of OIPage v1.1.0
2
+ * onReady of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * style of OIPage v1.1.0
2
+ * style of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * throttle of OIPage v1.1.0
2
+ * throttle of OIPage v1.2.0-alpha.1
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
File without changes
File without changes
File without changes
File without changes
File without changes