ike-weblca-html 2.1.2 → 2.1.3

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/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "ike-weblca-html",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "weblca-html-header&footer",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
7
7
  "node": ">=14.20.1"
8
8
  },
9
9
  "scripts": {
10
+ "start": "node scripts/dev-server.js",
10
11
  "build": "webpack",
11
12
  "test": "echo \"Error: no test specified\" && exit 1",
12
13
  "prepublishOnly": "npm run build",
@@ -0,0 +1,125 @@
1
+ const http = require('http');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const { spawn } = require('child_process');
5
+ const os = require('os');
6
+
7
+ const rootDir = path.resolve(__dirname, '..');
8
+ const demoDir = path.join(rootDir, 'demo');
9
+ const distDir = path.join(rootDir, 'dist');
10
+ const port = Number(process.env.PORT) || 8080;
11
+
12
+ function getWebpackBin() {
13
+ const binName = process.platform === 'win32' ? 'webpack.cmd' : 'webpack';
14
+ return path.join(rootDir, 'node_modules', '.bin', binName);
15
+ }
16
+
17
+ function startWebpackWatch() {
18
+ const webpackBin = getWebpackBin();
19
+ if (!fs.existsSync(webpackBin)) {
20
+ console.error('[dev-server] webpack not found. Please run "npm install" first.');
21
+ process.exit(1);
22
+ }
23
+
24
+ const child = spawn(webpackBin, ['--watch', '--mode', 'development'], {
25
+ stdio: 'inherit',
26
+ cwd: rootDir,
27
+ shell: false,
28
+ });
29
+
30
+ child.on('exit', (code) => {
31
+ if (code && code !== 0) {
32
+ console.error(`[dev-server] webpack exited with code ${code}`);
33
+ }
34
+ });
35
+
36
+ process.on('exit', () => {
37
+ child.kill();
38
+ });
39
+
40
+ process.on('SIGINT', () => {
41
+ child.kill('SIGINT');
42
+ process.exit(0);
43
+ });
44
+ }
45
+
46
+ const contentTypes = {
47
+ '.html': 'text/html; charset=utf-8',
48
+ '.js': 'application/javascript; charset=utf-8',
49
+ '.css': 'text/css; charset=utf-8',
50
+ '.json': 'application/json; charset=utf-8',
51
+ '.png': 'image/png',
52
+ '.jpg': 'image/jpeg',
53
+ '.jpeg': 'image/jpeg',
54
+ '.svg': 'image/svg+xml',
55
+ '.ico': 'image/x-icon',
56
+ '.map': 'application/json; charset=utf-8',
57
+ };
58
+
59
+ function sendFile(res, filePath) {
60
+ fs.readFile(filePath, (err, data) => {
61
+ if (err) {
62
+ res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
63
+ res.end('Not Found');
64
+ return;
65
+ }
66
+ const ext = path.extname(filePath).toLowerCase();
67
+ const contentType = contentTypes[ext] || 'application/octet-stream';
68
+ res.writeHead(200, { 'Content-Type': contentType });
69
+ res.end(data);
70
+ });
71
+ }
72
+
73
+ function resolveRequestPath(urlPath) {
74
+ if (urlPath === '/' || urlPath === '') {
75
+ return path.join(demoDir, 'index.html');
76
+ }
77
+
78
+ const safePath = urlPath.replace(/\0/g, '');
79
+ const demoPath = path.join(demoDir, safePath);
80
+ if (fs.existsSync(demoPath) && fs.statSync(demoPath).isFile()) {
81
+ return demoPath;
82
+ }
83
+
84
+ const distPath = path.join(distDir, safePath);
85
+ if (fs.existsSync(distPath) && fs.statSync(distPath).isFile()) {
86
+ return distPath;
87
+ }
88
+
89
+ return null;
90
+ }
91
+
92
+ function startServer() {
93
+ const server = http.createServer((req, res) => {
94
+ const urlPath = decodeURIComponent((req.url || '').split('?')[0]);
95
+ const filePath = resolveRequestPath(urlPath);
96
+ if (!filePath) {
97
+ res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
98
+ res.end('Not Found');
99
+ return;
100
+ }
101
+ sendFile(res, filePath);
102
+ });
103
+
104
+ server.listen(port, '0.0.0.0', () => {
105
+ console.log(`[dev-server] running at http://localhost:${port}`);
106
+ const ips = [];
107
+ const nets = os.networkInterfaces();
108
+ Object.values(nets).forEach((list) => {
109
+ if (!list) return;
110
+ list.forEach((net) => {
111
+ if (net.family === 'IPv4' && !net.internal) {
112
+ ips.push(net.address);
113
+ }
114
+ });
115
+ });
116
+ if (ips.length) {
117
+ ips.forEach((ip) => {
118
+ console.log(`[dev-server] running at http://${ip}:${port}`);
119
+ });
120
+ }
121
+ });
122
+ }
123
+
124
+ startWebpackWatch();
125
+ startServer();