oipage 1.2.0-alpha.0 → 1.2.0-alpha.2

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/README.md CHANGED
@@ -45,7 +45,7 @@ npm install -g oipage
45
45
  npm install oipage --save
46
46
  ```
47
47
 
48
- 全局安装后,就可以直接作为命令行使用了。你可以打印帮助查看:
48
+ 安装后,就可以直接作为命令行使用了。你可以打印帮助查看:
49
49
 
50
50
  ```shell
51
51
  oipage-cli
package/bin/run CHANGED
@@ -19,8 +19,13 @@ if (process.argv[2] === "serve") {
19
19
  });
20
20
 
21
21
  let config = {
22
- port: 8080,
23
- baseUrl: "./"
22
+ devServer: {
23
+ port: 8080,
24
+ baseUrl: "./",
25
+ },
26
+ module: {
27
+ rules: []
28
+ }
24
29
  };
25
30
 
26
31
  // 如果设置了配置文件
@@ -28,15 +33,15 @@ if (process.argv[2] === "serve") {
28
33
  let configPath = join(process.cwd(), argvObj["--config"][0] || "./oipage.config.js");
29
34
  if (existsSync(configPath) && !lstatSync(configPath).isDirectory()) {
30
35
  let configValue = require(configPath);
31
- mergeOption(config, configValue.devServer);
36
+ mergeOption(config, configValue);
32
37
  } else {
33
38
  console.log("\x1b[0m\x1b[31m" + configPath + "\x1b[0m");
34
39
  throw new Error("OIPage: The configuration file does not exist or is not a file.");
35
40
  }
36
41
  }
37
42
 
38
- if ((argvObj["--port"] || [])[0]) config.port = (argvObj["--port"] || [])[0];
39
- if ((argvObj["--baseUrl"] || [])[0]) config.baseUrl = (argvObj["--baseUrl"] || [])[0];
43
+ if ((argvObj["--port"] || [])[0]) config.devServer.port = (argvObj["--port"] || [])[0];
44
+ if ((argvObj["--baseUrl"] || [])[0]) config.devServer.baseUrl = (argvObj["--baseUrl"] || [])[0];
40
45
 
41
46
  require("./serve.js")(config);
42
47
  }
package/bin/serve.js CHANGED
@@ -12,14 +12,15 @@ const { formatRawHeaders } = require("./tools/format.js");
12
12
  module.exports = function (config) {
13
13
  let startTime = new Date().valueOf();
14
14
 
15
- const port = config.port; // 端口号
16
- 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; // 服务器根路径
17
17
 
18
18
  let Server = createServer(function (request, response) {
19
19
  let headers = formatRawHeaders(request.rawHeaders);
20
20
  let url = decodeURIComponent(request.url);
21
21
 
22
- url = url.split("?")[0];
22
+ let urlArray = url.split("?");
23
+ url = urlArray[0];
23
24
 
24
25
  // 请求的文件路径
25
26
  let filePath = join(basePath, url == "/" ? "index.html" : url.replace(/^\//, ""));
@@ -41,9 +42,29 @@ module.exports = function (config) {
41
42
 
42
43
  // 如果文件小于10M,认为不大,直接读取
43
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.module.rules.length; i++) {
51
+ if (config.module.rules[i].test.test(filePath)) {
52
+ source = config.module.rules[i].use.call({
53
+ root: basePath, // 服务器根路径
54
+ path: filePath.replace(basePath, ""), // 文件相对路径
55
+ entry: headers.Accept !== "*/*", // 是否是浏览器地址栏直接访问
56
+ setFileType(fileType) { // 设置文件类型
57
+ responseHeader['Content-type'] = fileType + ";charset=utf-8";
58
+ }
59
+ }, source);
60
+ break;
61
+ }
62
+ }
63
+ }
64
+
44
65
  sendType = "Read";
45
66
  response.writeHead('200', responseHeader);
46
- response.write(resolveImport(basePath, filePath, headers.Accept === "*/*"));
67
+ response.write(source);
47
68
  response.end();
48
69
  }
49
70
 
@@ -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
  }
@@ -1,7 +1,7 @@
1
1
  const { readFileSync, existsSync, lstatSync } = require("fs");
2
2
  const { join } = require("path");
3
3
 
4
- module.exports = function (basePath, filePath, needResolve) {
4
+ module.exports = function (basePath, filePath, needResolve, isDownload) {
5
5
  let code = readFileSync(filePath);
6
6
 
7
7
  let resolveImport = function (content) {
@@ -42,8 +42,13 @@ module.exports = function (basePath, filePath, needResolve) {
42
42
  });
43
43
  };
44
44
 
45
+ // 如果是下载
46
+ if (isDownload) {
47
+ return code;
48
+ }
49
+
45
50
  // 如果需要解析
46
- if (needResolve) {
51
+ else if (needResolve) {
47
52
  return resolveImport(code + "");
48
53
  }
49
54
 
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * animation of OIPage v1.2.0-alpha.0
2
+ * animation of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * cmdlog of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * disk of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * logform of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * throttle of OIPage v1.2.0-alpha.2
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oipage",
3
- "version": "1.2.0-alpha.0",
3
+ "version": "1.2.0-alpha.2",
4
4
  "description": "前端网页或应用快速开发助手,包括开发服务器、辅助命令、实用API等",
5
5
  "sideEffects": false,
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * animation of OIPage v1.2.0-alpha.0
2
+ * animation of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * onReady of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * style of OIPage v1.2.0-alpha.2
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.2.0-alpha.0
2
+ * throttle of OIPage v1.2.0-alpha.2
3
3
  * git+https://github.com/oi-contrib/OIPage.git
4
4
  */
5
5