vanilla-jet 1.3.1 → 1.4.0

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/vite.config.js ADDED
@@ -0,0 +1,184 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { defineConfig } = require('vite');
4
+
5
+ function resolveConsumerRoot() {
6
+ return process.cwd()
7
+ .replace('/node_modules/vanilla-jet', '')
8
+ .replace('/.scripts', '')
9
+ .replace('/.grunt', '');
10
+ }
11
+
12
+ function collectScriptFiles(dirPath) {
13
+ if (!fs.existsSync(dirPath)) {
14
+ return [];
15
+ }
16
+
17
+ const files = [];
18
+ const entries = fs.readdirSync(dirPath, { withFileTypes: true });
19
+ entries.forEach((entry) => {
20
+ const entryPath = path.join(dirPath, entry.name);
21
+ if (entry.isDirectory()) {
22
+ files.push(...collectScriptFiles(entryPath));
23
+ return;
24
+ }
25
+
26
+ if (entry.isFile() && entry.name.endsWith('.js')) {
27
+ files.push(entryPath);
28
+ }
29
+ });
30
+ return files;
31
+ }
32
+
33
+ function normalize(filePath) {
34
+ return filePath.split(path.sep).join('/');
35
+ }
36
+
37
+ function scriptPriority(filePath) {
38
+ const normalized = normalize(filePath);
39
+ if (normalized.includes('/assets/scripts/controllers/')) return 0;
40
+ if (normalized.includes('/assets/scripts/views/')) return 1;
41
+ if (normalized.includes('/assets/scripts/api/')) return 2;
42
+ return 3;
43
+ }
44
+
45
+ function includeInBundle(filePath) {
46
+ const normalized = normalize(filePath);
47
+ if (normalized.includes('/assets/scripts/core/')) return false;
48
+ if (normalized.includes('/assets/scripts/plugins/')) return false;
49
+ return true;
50
+ }
51
+
52
+ function writeVirtualEntry(virtualEntryPath, orderedScripts, lessEntryPath, hasLess) {
53
+ const imports = orderedScripts.map((scriptPath, index) => {
54
+ const importPath = normalize(scriptPath) + '?raw';
55
+ return `import __vanillajet_script_${index} from ${JSON.stringify(importPath)};`;
56
+ });
57
+
58
+ if (hasLess) {
59
+ imports.push(`import ${JSON.stringify(normalize(lessEntryPath))};`);
60
+ }
61
+
62
+ const mappings = orderedScripts.map((scriptPath, index) => {
63
+ const rel = normalize(path.relative(path.dirname(virtualEntryPath), scriptPath));
64
+ return `{ source: __vanillajet_script_${index}, file: ${JSON.stringify(rel)} }`;
65
+ });
66
+
67
+ const lines = [
68
+ '/* Auto-generated by VanillaJet Vite config. */',
69
+ ...imports,
70
+ '',
71
+ 'function executeGlobalScript(sourceCode, sourceFile) {',
72
+ ' const tag = document.createElement("script");',
73
+ ' tag.type = "text/javascript";',
74
+ ' tag.setAttribute("data-vanillajet-source", sourceFile);',
75
+ ' tag.text = sourceCode;',
76
+ ' document.head.appendChild(tag);',
77
+ ' document.head.removeChild(tag);',
78
+ '}',
79
+ '',
80
+ `const scriptMap = [${mappings.join(', ')}];`,
81
+ 'scriptMap.forEach(({ source, file }) => executeGlobalScript(source, file));',
82
+ '',
83
+ 'if (import.meta.hot) {',
84
+ ' import.meta.hot.accept(() => {',
85
+ ' window.location.reload();',
86
+ ' });',
87
+ '}'
88
+ ];
89
+
90
+ fs.mkdirSync(path.dirname(virtualEntryPath), { recursive: true });
91
+ fs.writeFileSync(virtualEntryPath, lines.join('\n'), 'utf8');
92
+ }
93
+
94
+ module.exports = defineConfig(({ command, mode }) => {
95
+ const rootDir = resolveConsumerRoot();
96
+ const scriptsDir = path.join(rootDir, 'assets/scripts');
97
+ const lessEntryPath = path.join(rootDir, 'assets/styles/less/admin.less');
98
+ const virtualEntryPath = path.join(rootDir, '.vanillajet/vite-entry.js');
99
+
100
+ const scripts = collectScriptFiles(scriptsDir)
101
+ .filter(includeInBundle)
102
+ .sort((left, right) => {
103
+ const priorityDiff = scriptPriority(left) - scriptPriority(right);
104
+ if (priorityDiff !== 0) {
105
+ return priorityDiff;
106
+ }
107
+ return normalize(left).localeCompare(normalize(right));
108
+ });
109
+ const hasLess = fs.existsSync(lessEntryPath);
110
+ const hasSources = scripts.length > 0 || hasLess;
111
+
112
+ writeVirtualEntry(virtualEntryPath, scripts, lessEntryPath, hasLess);
113
+
114
+ const helperPath = '/__vanillajet__/';
115
+ const helperHtml = [
116
+ '<!doctype html>',
117
+ '<html lang="en">',
118
+ '<head>',
119
+ ' <meta charset="utf-8" />',
120
+ ' <meta name="viewport" content="width=device-width, initial-scale=1" />',
121
+ ' <title>VanillaJet Vite Dev</title>',
122
+ '</head>',
123
+ '<body>',
124
+ ' <h3>VanillaJet Vite dev helper</h3>',
125
+ ' <p>This page only loads JS/LESS from assets for DX.</p>',
126
+ ' <p>Nunjucks templates and legacy Node routes remain unchanged.</p>',
127
+ ` <script type="module" src="${normalize(path.relative(rootDir, virtualEntryPath)).startsWith('.') ? '/' + normalize(path.relative(rootDir, virtualEntryPath)).replace(/^\.\//, '') : '/' + normalize(path.relative(rootDir, virtualEntryPath))}"></script>`,
128
+ '</body>',
129
+ '</html>'
130
+ ].join('\n');
131
+
132
+ return {
133
+ root: rootDir,
134
+ publicDir: false,
135
+ server: {
136
+ host: true,
137
+ port: 5173
138
+ },
139
+ build: {
140
+ outDir: path.join(rootDir, 'public'),
141
+ emptyOutDir: false,
142
+ sourcemap: mode !== 'production',
143
+ rollupOptions: {
144
+ input: virtualEntryPath,
145
+ output: {
146
+ entryFileNames: 'scripts/vanilla.min.js',
147
+ chunkFileNames: 'scripts/chunks/[name]-[hash].js',
148
+ assetFileNames: (assetInfo) => {
149
+ if (assetInfo.name && assetInfo.name.endsWith('.css')) {
150
+ return 'styles/app.min.css';
151
+ }
152
+ return 'assets/[name]-[hash][extname]';
153
+ }
154
+ }
155
+ }
156
+ },
157
+ plugins: [
158
+ {
159
+ name: 'vanillajet-dev-helper',
160
+ configureServer(server) {
161
+ if (!hasSources) {
162
+ server.config.logger.warn(
163
+ '[vanillajet] No JS/LESS sources found under assets/. Vite will run with an empty entry.'
164
+ );
165
+ }
166
+
167
+ server.middlewares.use((req, res, next) => {
168
+ if (req.url === helperPath) {
169
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
170
+ res.end(helperHtml);
171
+ return;
172
+ }
173
+ next();
174
+ });
175
+ },
176
+ buildStart() {
177
+ if (command === 'build' && !hasSources) {
178
+ this.warn('[vanillajet] No JS/LESS sources found under assets/. Build will output an empty JS bundle.');
179
+ }
180
+ }
181
+ }
182
+ ]
183
+ };
184
+ });