onelaraveljs 1.1.1 → 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/index.js +3 -1
- package/package.json +1 -1
- package/scripts/build.py +9 -8
- package/scripts/compiler/main_compiler.py +1 -1
- package/scripts/dev-context.js +7 -4
- package/scripts/generate-assets-order.js +13 -5
- package/src/core/ViewManager.js +4 -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/index.js
CHANGED
|
@@ -3,13 +3,15 @@ import { App } from './src/app.js';
|
|
|
3
3
|
import { viewLoader } from './src/core/ViewLoader.js';
|
|
4
4
|
import { EventService } from './src/core/services/EventService.js';
|
|
5
5
|
import initApp from './src/init.js';
|
|
6
|
+
import { View } from './src/core/View.js';
|
|
6
7
|
|
|
7
8
|
// Export Core Components
|
|
8
9
|
export {
|
|
9
10
|
App,
|
|
10
11
|
viewLoader,
|
|
11
12
|
EventService,
|
|
12
|
-
initApp
|
|
13
|
+
initApp,
|
|
14
|
+
View
|
|
13
15
|
};
|
|
14
16
|
|
|
15
17
|
// Default export
|
package/package.json
CHANGED
package/scripts/build.py
CHANGED
|
@@ -221,7 +221,7 @@ def build_view_template_importer(created_files, output_path, context='all'):
|
|
|
221
221
|
|
|
222
222
|
if build_config and context != 'all' and context in build_config['contexts']:
|
|
223
223
|
# Use context-specific registry file
|
|
224
|
-
output_path = os.path.join(
|
|
224
|
+
output_path = os.path.join(config.project_root, build_config['contexts'][context]['output']['register'])
|
|
225
225
|
|
|
226
226
|
# Create output directory if it doesn't exist
|
|
227
227
|
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
|
@@ -385,8 +385,9 @@ def build_main_spa_file():
|
|
|
385
385
|
|
|
386
386
|
def load_build_config():
|
|
387
387
|
"""Load build.config.json"""
|
|
388
|
-
|
|
389
|
-
|
|
388
|
+
# Use the config object to find the project root
|
|
389
|
+
config_path = os.path.join(config.project_root, 'build.config.json')
|
|
390
|
+
if not os.path.exists(config_path):
|
|
390
391
|
return None
|
|
391
392
|
|
|
392
393
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
@@ -417,15 +418,15 @@ def get_source_directories(context_name=None):
|
|
|
417
418
|
# Return sources for specific context
|
|
418
419
|
sources = build_config['contexts'][context_name]['sources']
|
|
419
420
|
# Convert relative paths to absolute
|
|
420
|
-
base_path =
|
|
421
|
-
return [
|
|
421
|
+
base_path = config.project_root
|
|
422
|
+
return [os.path.join(base_path, src) for src in sources]
|
|
422
423
|
elif build_config and context_name == 'all':
|
|
423
424
|
# Return all sources from all contexts
|
|
424
|
-
base_path =
|
|
425
|
+
base_path = config.project_root
|
|
425
426
|
all_sources = set()
|
|
426
427
|
for ctx in build_config['contexts'].values():
|
|
427
428
|
for src in ctx['sources']:
|
|
428
|
-
all_sources.add(
|
|
429
|
+
all_sources.add(os.path.join(base_path, src))
|
|
429
430
|
return list(all_sources)
|
|
430
431
|
else:
|
|
431
432
|
# Fallback to config.get_build_directories()
|
|
@@ -537,7 +538,7 @@ def main():
|
|
|
537
538
|
# Create a proxy ViewTemplate.js in core/ that re-exports from context registry
|
|
538
539
|
core_template_path = os.path.join(config.js_input_path, 'core', 'ViewTemplate.js')
|
|
539
540
|
relative_to_registry = os.path.relpath(
|
|
540
|
-
os.path.join(
|
|
541
|
+
os.path.join(config.project_root, registry_path),
|
|
541
542
|
os.path.dirname(core_template_path)
|
|
542
543
|
).replace('\\', '/')
|
|
543
544
|
|
|
@@ -1005,7 +1005,7 @@ class BladeCompiler:
|
|
|
1005
1005
|
# Add View import if scripts are registered
|
|
1006
1006
|
view_import = ""
|
|
1007
1007
|
if script_registrations:
|
|
1008
|
-
view_import = "import { View } from '
|
|
1008
|
+
view_import = "import { View } from 'onelaraveljs';\n\n"
|
|
1009
1009
|
|
|
1010
1010
|
return_template = setup_script_line + view_import + script_registrations_line + """export function """ + function_name + """($$$DATA$$$ = {}, systemData = {}) {
|
|
1011
1011
|
const {App, View, __base__, __layout__, __page__, __component__, __template__, __context__, __partial__, __system__, __env = {}, __helper = {}} = systemData;
|
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',
|
|
@@ -13,23 +13,31 @@ import { fileURLToPath } from 'url';
|
|
|
13
13
|
const __filename = fileURLToPath(import.meta.url);
|
|
14
14
|
const __dirname = path.dirname(__filename);
|
|
15
15
|
|
|
16
|
-
// Load build.config.json
|
|
17
|
-
const
|
|
16
|
+
// Load build.config.json from project root
|
|
17
|
+
const projectRoot = process.env.ONEJS_PROJECT_ROOT || process.cwd();
|
|
18
|
+
const configPath = path.join(projectRoot, 'build.config.json');
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
var buildConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.error(`Error loading build.config.json from ${configPath}:`, e.message);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
18
26
|
|
|
19
27
|
// Get context from environment variable
|
|
20
28
|
const buildContext = process.env.BUILD_CONTEXT || 'web';
|
|
21
29
|
|
|
22
30
|
// Determine static path based on context
|
|
23
31
|
const STATIC_PATH = buildConfig.contexts[buildContext]
|
|
24
|
-
? path.join(
|
|
25
|
-
: path.join(
|
|
32
|
+
? path.join(projectRoot, path.dirname(buildConfig.contexts[buildContext].dist.bundle))
|
|
33
|
+
: path.join(projectRoot, 'public/static/app');
|
|
26
34
|
|
|
27
35
|
// Determine base URL path for assets
|
|
28
36
|
const BASE_URL = buildConfig.contexts[buildContext]
|
|
29
37
|
? `/static/${buildContext}/js/`
|
|
30
38
|
: '/static/app/';
|
|
31
39
|
|
|
32
|
-
const OUTPUT_FILE = path.join(
|
|
40
|
+
const OUTPUT_FILE = path.join(projectRoot, 'resources/views/partials/assets-scripts.blade.php');
|
|
33
41
|
|
|
34
42
|
function getAssetsByCategory() {
|
|
35
43
|
const assets = {
|
package/src/core/ViewManager.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SEOTagConfig } from './SEOConfig.js';
|
|
2
|
-
|
|
2
|
+
// ViewTemplates import removed
|
|
3
3
|
import { hasData, uniqId } from '../helpers/utils.js';
|
|
4
4
|
import { View, ViewEngine } from './View.js';
|
|
5
5
|
import { ViewState } from './ViewState.js';
|
|
@@ -126,7 +126,7 @@ export class ViewManager {
|
|
|
126
126
|
/**
|
|
127
127
|
* @type {Object<string, ViewEngine>}
|
|
128
128
|
*/
|
|
129
|
-
this.templates =
|
|
129
|
+
this.templates = viewLoader.registry || {};
|
|
130
130
|
/**
|
|
131
131
|
* @type {Object<string, string>}
|
|
132
132
|
*/
|
|
@@ -295,8 +295,8 @@ export class ViewManager {
|
|
|
295
295
|
this._changedSections = [];
|
|
296
296
|
|
|
297
297
|
// Initialize views if not already done
|
|
298
|
-
if (!this.templates) {
|
|
299
|
-
this.templates =
|
|
298
|
+
if (!this.templates || Object.keys(this.templates).length === 0) {
|
|
299
|
+
this.templates = viewLoader.registry || {};
|
|
300
300
|
}
|
|
301
301
|
|
|
302
302
|
// Initialize current scope
|