frontend-hamroun 1.1.40 → 1.1.41

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.
Files changed (2) hide show
  1. package/bin/cli.js +63 -0
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -7,10 +7,53 @@ import path from 'path';
7
7
  import { fileURLToPath } from 'url';
8
8
  import chalk from 'chalk';
9
9
  import { createSpinner } from 'nanospinner';
10
+ import { createServer } from 'http';
11
+ import * as esbuild from 'esbuild';
10
12
 
11
13
  const __filename = fileURLToPath(import.meta.url);
12
14
  const __dirname = path.dirname(__filename);
13
15
 
16
+ async function createDevServer(options = {}) {
17
+ const { port = 3000, isSSR = false } = options;
18
+
19
+ const ctx = await esbuild.context({
20
+ entryPoints: ['src/main.tsx'],
21
+ bundle: true,
22
+ outdir: 'dist',
23
+ format: 'esm',
24
+ platform: isSSR ? 'node' : 'browser',
25
+ sourcemap: true,
26
+ loader: { '.tsx': 'tsx', '.ts': 'tsx' },
27
+ jsxFactory: '_jsx',
28
+ jsxFragment: '_Fragment',
29
+ banner: {
30
+ js: `import { jsx as _jsx, Fragment as _Fragment } from 'frontend-hamroun';`,
31
+ }
32
+ });
33
+
34
+ await ctx.watch();
35
+
36
+ const server = createServer((req, res) => {
37
+ const url = new URL(req.url || '', `http://${req.headers.host}`);
38
+
39
+ if (url.pathname === '/') {
40
+ res.writeHead(200, { 'Content-Type': 'text/html' });
41
+ res.end(fs.readFileSync('index.html', 'utf8'));
42
+ } else if (url.pathname.endsWith('.js')) {
43
+ res.writeHead(200, { 'Content-Type': 'application/javascript' });
44
+ res.end(fs.readFileSync(path.join('dist', url.pathname), 'utf8'));
45
+ } else {
46
+ res.writeHead(404);
47
+ res.end();
48
+ }
49
+ });
50
+
51
+ server.listen(port);
52
+ console.log(chalk.green(`Development server running at http://localhost:${port}`));
53
+
54
+ return { server, ctx };
55
+ }
56
+
14
57
  async function init() {
15
58
  const program = new Command();
16
59
 
@@ -23,6 +66,26 @@ async function init() {
23
66
  await createProject(projectName);
24
67
  });
25
68
 
69
+ program
70
+ .command('dev')
71
+ .description('Start development server')
72
+ .option('-p, --port <port>', 'Port number', '3000')
73
+ .option('--ssr', 'Enable SSR mode')
74
+ .action(async (options) => {
75
+ const spinner = createSpinner('Starting development server...').start();
76
+ try {
77
+ await createDevServer({
78
+ port: parseInt(options.port),
79
+ isSSR: options.ssr
80
+ });
81
+ spinner.success({ text: 'Development server started' });
82
+ } catch (error) {
83
+ spinner.error({ text: chalk.red('Failed to start server') });
84
+ console.error(error);
85
+ process.exit(1);
86
+ }
87
+ });
88
+
26
89
  program.parse();
27
90
  }
28
91
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frontend-hamroun",
3
- "version": "1.1.40",
3
+ "version": "1.1.41",
4
4
  "description": "A lightweight frontend framework with hooks and virtual DOM",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",