onelaraveljs 1.2.0 → 1.2.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/index.js +2 -0
- package/package.json +1 -1
- package/src/plugins/ViewRegistryPlugin.js +119 -0
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webpack Plugin to Generate View Registry
|
|
3
|
+
* Automatically creates viewRegistry.js with all views for lazy loading
|
|
4
|
+
*
|
|
5
|
+
* @author OneLaravel Team
|
|
6
|
+
* @since 2025-12-29
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from 'fs';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
import { fileURLToPath } from 'url';
|
|
12
|
+
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
15
|
+
|
|
16
|
+
export class ViewRegistryPlugin {
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
// Use process.cwd() to default to the project root, making this plugin portable
|
|
19
|
+
this.viewsDir = options.viewsDir || path.resolve(process.cwd(), 'resources/js/views');
|
|
20
|
+
this.outputFile = options.outputFile || path.resolve(process.cwd(), 'resources/js/.generated/config/viewRegistry.js');
|
|
21
|
+
this.preloadViews = options.preloadViews || [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
apply(compiler) {
|
|
25
|
+
compiler.hooks.beforeCompile.tapAsync('ViewRegistryPlugin', (params, callback) => {
|
|
26
|
+
this.generateViewRegistry();
|
|
27
|
+
callback();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
generateViewRegistry() {
|
|
32
|
+
const views = this.scanViews(this.viewsDir);
|
|
33
|
+
const content = this.generateContent(views);
|
|
34
|
+
|
|
35
|
+
// Ensure directory exists
|
|
36
|
+
const dir = path.dirname(this.outputFile);
|
|
37
|
+
if (!fs.existsSync(dir)) {
|
|
38
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fs.writeFileSync(this.outputFile, content, 'utf8');
|
|
42
|
+
console.log(`✅ Generated view registry with ${views.length} views`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
scanViews(dir, basePath = '') {
|
|
46
|
+
const views = [];
|
|
47
|
+
const files = fs.readdirSync(dir);
|
|
48
|
+
|
|
49
|
+
files.forEach(file => {
|
|
50
|
+
const fullPath = path.join(dir, file);
|
|
51
|
+
const stat = fs.statSync(fullPath);
|
|
52
|
+
|
|
53
|
+
if (stat.isDirectory()) {
|
|
54
|
+
views.push(...this.scanViews(fullPath, path.join(basePath, file)));
|
|
55
|
+
} else if (file.endsWith('.js')) {
|
|
56
|
+
const fileName = file.replace('.js', '');
|
|
57
|
+
const relativePath = path.join(basePath, file);
|
|
58
|
+
|
|
59
|
+
// Convert filename to view path
|
|
60
|
+
// WebPagesHome.js -> web.pages.home
|
|
61
|
+
const viewPath = fileName
|
|
62
|
+
.replace(/([A-Z])/g, '.$1')
|
|
63
|
+
.toLowerCase()
|
|
64
|
+
.replace(/^\./, '');
|
|
65
|
+
|
|
66
|
+
views.push({
|
|
67
|
+
viewPath,
|
|
68
|
+
fileName,
|
|
69
|
+
relativePath: relativePath.replace(/\\/g, '/'),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return views;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
generateContent(views) {
|
|
78
|
+
const imports = views.map(view => {
|
|
79
|
+
return ` '${view.viewPath}': () => import(/* webpackChunkName: "view.${view.viewPath}" */ '../views/${view.relativePath}'),`;
|
|
80
|
+
}).join('\n');
|
|
81
|
+
|
|
82
|
+
const preloadList = this.preloadViews.map(v => ` '${v}',`).join('\n');
|
|
83
|
+
|
|
84
|
+
return `/**
|
|
85
|
+
* Auto-generated View Registry
|
|
86
|
+
* DO NOT EDIT MANUALLY - Generated by ViewRegistryPlugin
|
|
87
|
+
*
|
|
88
|
+
* @module config/viewRegistry
|
|
89
|
+
* @generated ${new Date().toISOString()}
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* View registry with dynamic imports
|
|
94
|
+
* Maps view paths to webpack dynamic import functions
|
|
95
|
+
*
|
|
96
|
+
* @type {Object<string, () => Promise<any>>}
|
|
97
|
+
*/
|
|
98
|
+
export const viewRegistry = {
|
|
99
|
+
${imports}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Views to preload for better initial performance
|
|
104
|
+
*/
|
|
105
|
+
export const preloadViews = [
|
|
106
|
+
${preloadList}
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Total registered views
|
|
111
|
+
*/
|
|
112
|
+
export const totalViews = ${views.length};
|
|
113
|
+
|
|
114
|
+
export default viewRegistry;
|
|
115
|
+
`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default ViewRegistryPlugin;
|