onelaraveljs 1.1.3 → 1.1.4
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 +142 -47
- package/package.json +1 -1
- package/scripts/dev-context.js +7 -4
package/README.md
CHANGED
|
@@ -1,87 +1,182 @@
|
|
|
1
|
-
#
|
|
1
|
+
# OneLaravelJS Framework
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**OneLaravelJS** là thư viện lõi JavaScript dành cho các ứng dụng OneLaravel. Nó cung cấp nền tảng runtime, quản lý view, routing và khả năng tương tác hai chiều mạnh mẽ giữa Laravel Blade và JavaScript.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Thư viện này đóng vai trò là "Engine", trong khi ứng dụng Laravel của bạn cung cấp "Map" (Views và Cấu hình).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 🏗 Cài đặt
|
|
10
|
+
|
|
11
|
+
Cài đặt thư viện thông qua npm:
|
|
6
12
|
|
|
7
13
|
```bash
|
|
8
14
|
npm install onelaraveljs
|
|
9
15
|
```
|
|
10
16
|
|
|
11
|
-
|
|
17
|
+
Sau khi cài đặt, bạn sẽ có quyền truy cập vào:
|
|
18
|
+
- **Runtime Library**: Các file JS để chạy ứng dụng.
|
|
19
|
+
- **CLI Tools**: Công cụ `onejs-build` để biên dịch Blade template thành JavaScript modules.
|
|
20
|
+
|
|
21
|
+
---
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
## 📂 Cấu trúc dự án khuyến nghị
|
|
14
24
|
|
|
15
|
-
|
|
25
|
+
Để OneLaravelJS hoạt động hiệu quả, dự án của bạn nên tuân thủ cấu trúc sau:
|
|
16
26
|
|
|
17
|
-
Ensure your project has the following recommended structure for frontend assets:
|
|
18
27
|
```
|
|
19
|
-
|
|
20
|
-
├──
|
|
21
|
-
├──
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
my-laravel-project/
|
|
29
|
+
├── build.config.json # File cấu hình build (BẮT BUỘC)
|
|
30
|
+
├── package.json
|
|
31
|
+
├── resources/
|
|
32
|
+
│ ├── js/
|
|
33
|
+
│ │ ├── app.js # Entry point chính
|
|
34
|
+
│ │ ├── build/ # Output của Webpack (Bundle)
|
|
35
|
+
│ │ ├── config/ # Output của Compiler (Registry maps)
|
|
36
|
+
│ │ ├── core/ # Output của Compiler (Proxy files)
|
|
37
|
+
│ │ └── views/ # Output của Compiler (Compiled View Components)
|
|
38
|
+
│ └── views/
|
|
39
|
+
│ ├── _system/ # System views required by Framework
|
|
40
|
+
│ └── web/ # User views
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## ⚙️ Cấu hình (`build.config.json`)
|
|
46
|
+
|
|
47
|
+
Tạo file `build.config.json` tại thư mục gốc dự án để định nghĩa các ngữ cảnh (contexts) build:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"contexts": {
|
|
52
|
+
"web": {
|
|
53
|
+
"sources": [
|
|
54
|
+
"resources/views/_system",
|
|
55
|
+
"resources/views/web"
|
|
56
|
+
],
|
|
57
|
+
"output": {
|
|
58
|
+
"views": "resources/js/views/web",
|
|
59
|
+
"register": "resources/js/config/templates.web.js",
|
|
60
|
+
"bundle": "resources/js/build/web.bundle.js"
|
|
61
|
+
},
|
|
62
|
+
"dist": {
|
|
63
|
+
"bundle": "public/static/web/js/main.bundle.js",
|
|
64
|
+
"css": "public/static/web/css",
|
|
65
|
+
"assets": "public/static/web/assets"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 🛠 Công cụ CLI (`onejs-build`)
|
|
75
|
+
|
|
76
|
+
OneLaravelJS đi kèm với trình biên dịch mạnh mẽ để chuyển đổi Blade Templates thành JavaScript Components.
|
|
77
|
+
|
|
78
|
+
### Các lệnh phổ biến:
|
|
79
|
+
|
|
80
|
+
Thêm vào `package.json` của bạn:
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
"scripts": {
|
|
84
|
+
"build:templates": "onejs-build all",
|
|
85
|
+
"build:templates:web": "onejs-build web",
|
|
86
|
+
"dev:blade": "onejs-build"
|
|
87
|
+
}
|
|
24
88
|
```
|
|
25
89
|
|
|
26
|
-
|
|
90
|
+
- **`onejs-build all`**: Biên dịch tất cả các context được định nghĩa trong `build.config.json`.
|
|
91
|
+
- **`onejs-build web`**: Chỉ biên dịch context `web`.
|
|
92
|
+
- **`onejs-build`**: Chạy chế độ Interactive (Menu chọn).
|
|
27
93
|
|
|
28
|
-
|
|
94
|
+
### Cơ chế hoạt động:
|
|
95
|
+
1. Đọc `build.config.json`.
|
|
96
|
+
2. Quét các file `.blade.php` trong thư mục `sources`.
|
|
97
|
+
3. Phân tích cú pháp Blade, Directives (@if, @foreach), và OneJS directives (x-data, x-bind).
|
|
98
|
+
4. Sinh ra các file ES6 Module tại thư mục `output.views`.
|
|
99
|
+
5. Tạo file Registry (`templates.web.js`) để map tên view sang file JS.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## 🚀 Sử dụng trong ứng dụng (`app.js`)
|
|
104
|
+
|
|
105
|
+
Tại `resources/js/app.js`, bạn cần kết nối Core Framework với Registry views đã được biên dịch:
|
|
29
106
|
|
|
30
107
|
```javascript
|
|
31
108
|
import { App, viewLoader } from 'onelaraveljs';
|
|
32
|
-
import { viewRegistry } from './config/viewRegistry.generated.js';
|
|
33
109
|
|
|
34
|
-
//
|
|
35
|
-
|
|
110
|
+
// Import Registry đã được CLI sinh ra (thông qua Proxy hoặc trực tiếp)
|
|
111
|
+
// Lưu ý: ViewTemplates thường được export từ file generated resources/js/core/ViewTemplate.js
|
|
112
|
+
import { ViewTemplates } from './core/ViewTemplate.js';
|
|
113
|
+
|
|
114
|
+
// 1. Dependency Injection: Nạp danh sách views vào Core
|
|
115
|
+
viewLoader.setRegistry(ViewTemplates);
|
|
36
116
|
|
|
37
|
-
// 2.
|
|
38
|
-
//
|
|
117
|
+
// 2. Khởi tạo App
|
|
118
|
+
// App sẽ tự động đọc window.APP_CONFIGS từ Blade để cấu hình Env, Routes...
|
|
39
119
|
if (window.APP_CONFIGS) {
|
|
40
120
|
App.init();
|
|
41
121
|
}
|
|
42
122
|
|
|
43
|
-
// 3. Export
|
|
123
|
+
// 3. Export global (Tùy chọn, dùng cho debug hoặc legacy scripts)
|
|
44
124
|
window.App = App;
|
|
45
125
|
```
|
|
46
126
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
Ensure your main layout file injects the configuration:
|
|
50
|
-
|
|
51
|
-
```html
|
|
52
|
-
<script>
|
|
53
|
-
window.APP_CONFIGS = {
|
|
54
|
-
env: { ... },
|
|
55
|
-
routes: [ ... ],
|
|
56
|
-
// ... other configurations
|
|
57
|
-
};
|
|
58
|
-
</script>
|
|
59
|
-
<script src="{{ mix('js/app.js') }}"></script>
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
## Core Concepts
|
|
127
|
+
---
|
|
63
128
|
|
|
64
|
-
|
|
129
|
+
## 🧠 Core Concepts
|
|
65
130
|
|
|
66
|
-
|
|
67
|
-
|
|
131
|
+
### 1. View System
|
|
132
|
+
OneJS coi mỗi file Blade là một Component.
|
|
133
|
+
- **Server**: Render HTML tĩnh (SEO).
|
|
134
|
+
- **Client**: Hydrate HTML đó thành Interactive Component.
|
|
68
135
|
|
|
69
|
-
###
|
|
136
|
+
### 2. ViewLoader & Registry
|
|
137
|
+
- **ViewLoader**: Là "bộ não" tải view lười (lazy-load). Nó không biết gì về ứng dụng của bạn cho đến khi được cung cấp Registry.
|
|
138
|
+
- **ViewRegistry**: Là "cuốn danh bạ" map tên view (`web.home`) tới file code (`WebHome.js`). File này được sinh tự động.
|
|
70
139
|
|
|
71
|
-
|
|
140
|
+
### 3. Event Service
|
|
141
|
+
Hệ thống Event Bus tích hợp sẵn:
|
|
72
142
|
|
|
73
143
|
```javascript
|
|
74
144
|
import { App } from 'onelaraveljs';
|
|
75
145
|
|
|
76
|
-
//
|
|
77
|
-
App.Event.on('
|
|
78
|
-
console.log(
|
|
146
|
+
// Lắng nghe
|
|
147
|
+
App.Event.on('cart:updated', (data) => {
|
|
148
|
+
console.log('Cart count:', data.count);
|
|
79
149
|
});
|
|
80
150
|
|
|
81
|
-
//
|
|
82
|
-
App.Event.emit('
|
|
151
|
+
// Phát sự kiện
|
|
152
|
+
App.Event.emit('cart:updated', { count: 5 });
|
|
83
153
|
```
|
|
84
154
|
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 🤝 Đóng góp & Phát triển (Development)
|
|
158
|
+
|
|
159
|
+
Nếu bạn muốn chỉnh sửa source code của chính thư viện `onelaraveljs`:
|
|
160
|
+
|
|
161
|
+
### Cấu trúc Source:
|
|
162
|
+
- `bin/`: Chứa file thực thi CLI.
|
|
163
|
+
- `scripts/`: Chứa logic biên dịch (Python/Node).
|
|
164
|
+
- `build.py`: Script chính điều phối build.
|
|
165
|
+
- `compiler/`: Bộ biên dịch Blade sang JS (Python).
|
|
166
|
+
- `src/`: Source code JS runtime.
|
|
167
|
+
- `core/`: Logic cốt lõi (Router, ViewEngine...).
|
|
168
|
+
|
|
169
|
+
### Quy trình phát triển:
|
|
170
|
+
1. Clone repo về máy.
|
|
171
|
+
2. Chạy `npm install`.
|
|
172
|
+
3. Symlink sang dự án test (`npm link` hoặc chỉnh sửa trực tiếp trong `node_modules` để debug nhanh).
|
|
173
|
+
4. Đảm bảo biến môi trường `ONEJS_PROJECT_ROOT` được xử lý đúng trong các script build nếu chạy thủ công.
|
|
174
|
+
|
|
175
|
+
### Testing:
|
|
176
|
+
Kiểm tra các thay đổi bằng cách chạy build trên một dự án Laravel thực tế sử dụng thư viện này (ví dụ `onelaravel`).
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
85
180
|
## License
|
|
86
181
|
|
|
87
|
-
MIT
|
|
182
|
+
MIT License.
|
package/package.json
CHANGED
package/scripts/dev-context.js
CHANGED
|
@@ -27,7 +27,8 @@ if (!['web', 'admin'].includes(context)) {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
// Load build config
|
|
30
|
-
const
|
|
30
|
+
const projectRoot = process.env.ONEJS_PROJECT_ROOT || process.cwd();
|
|
31
|
+
const buildConfigPath = path.resolve(projectRoot, 'build.config.json');
|
|
31
32
|
const buildConfig = JSON.parse(readFileSync(buildConfigPath, 'utf-8'));
|
|
32
33
|
const contextConfig = buildConfig.contexts[context];
|
|
33
34
|
|
|
@@ -40,9 +41,11 @@ const config = {
|
|
|
40
41
|
context: context,
|
|
41
42
|
watchPaths: {
|
|
42
43
|
blade: contextConfig.sources, // Array of blade source paths from config
|
|
43
|
-
jsCore: 'resources/js
|
|
44
|
-
jsViewsExclude: 'resources/js/
|
|
45
|
-
|
|
44
|
+
jsCore: 'resources/js', // Watch entire js directory
|
|
45
|
+
jsViewsExclude: 'resources/js/views', // Exclude views directory
|
|
46
|
+
jsBuildExclude: 'resources/js/build',
|
|
47
|
+
jsConfigExclude: 'resources/js/config',
|
|
48
|
+
compiler: 'node_modules/onelaraveljs/scripts/compiler'
|
|
46
49
|
},
|
|
47
50
|
buildCommand: `npm run build:${context}`,
|
|
48
51
|
phpServeCommand: 'php artisan serve',
|