vite-plugin-milpa 0.1.0 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +3 -1
  2. package/index.js +36 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -33,7 +33,9 @@ export default defineConfig({
33
33
 
34
34
  1. **`base` + `build.manifest` + entry** — lo que el helper `vite()` de milpa necesita para
35
35
  emitir `<link>`/`<script>` hasheados en prod. La base se deriva de la carpeta:
36
- `surcos/<app>` → `/vite/<app>/`.
36
+ `surcos/<app>` → `/vite/<app>/`. Los chunks salen con NOMBRE legible derivado de la ruta
37
+ del módulo (`modules/tienda/pages/productos/[id].jsx` → `tienda-productos-id-<hash>.js`)
38
+ — sin esto, el file-routing produce puros `index-<hash>.js` indistinguibles.
37
39
  2. **Build a `public/<app>` del proyecto** (estilo `mix.js` → `public/` de Laravel): milpa
38
40
  monta `public/` completo y un solo mount sirve a todas las apps.
39
41
  3. **Hot-file (modelo Laravel)** — `npm run dev` escribe `./hot` con la URL real del dev
package/index.js CHANGED
@@ -61,6 +61,26 @@ function resolveBase(options, root) {
61
61
  return url.endsWith('/') ? url : `${url}/`;
62
62
  }
63
63
 
64
+ // Nombre LEGIBLE para cada chunk, derivado de la ruta del módulo que lo origina
65
+ // (su facade): con file-routing casi todas las páginas se llaman index.jsx y
66
+ // Rollup nombra por basename → un desfile de "index-<hash>.js" indistinguibles.
67
+ // pages/acerca.jsx → acerca · modules/tienda/pages/productos/[id].jsx →
68
+ // tienda-productos-id · _layout → tienda-layout. Los segmentos ruido
69
+ // (src/modules/pages) se omiten; chunks sin facade (compartidos/vendor) caen
70
+ // al default [name] de Rollup.
71
+ function chunkName(facadeModuleId) {
72
+ if (!facadeModuleId) return null;
73
+ const match = facadeModuleId.split('?')[0].match(/\/src\/(.+)\.(jsx|tsx|js|ts)$/);
74
+ if (!match) return null;
75
+ const name = match[1]
76
+ .split('/')
77
+ .filter((segment) => segment !== 'modules' && segment !== 'pages')
78
+ .map((segment) => segment.replace(/^\[(.+)\]$/, '$1').replace(/^_/, ''))
79
+ .filter(Boolean)
80
+ .join('-');
81
+ return name || null;
82
+ }
83
+
64
84
  function configPlugin(options) {
65
85
  return {
66
86
  name: 'milpa:config',
@@ -72,7 +92,15 @@ function configPlugin(options) {
72
92
  base: command === 'build' ? resolveBase(options, config.root) : '/',
73
93
  build: {
74
94
  manifest: true,
75
- rollupOptions: {input: options.entry},
95
+ rollupOptions: {
96
+ input: options.entry,
97
+ output: {
98
+ chunkFileNames(chunkInfo) {
99
+ const name = chunkName(chunkInfo.facadeModuleId);
100
+ return name ? `assets/${name}-[hash].js` : 'assets/[name]-[hash].js';
101
+ },
102
+ },
103
+ },
76
104
  // El build cae en public/<app> del PROYECTO (como mix.js a
77
105
  // public/ en Laravel). emptyOutDir explícito: Vite no limpia
78
106
  // solo los outDir fuera del root del frontend.
@@ -129,8 +157,8 @@ function pwaPlugins(options) {
129
157
  // El shell HTML lo sirve el backend (no está en dist/): precachearlo da
130
158
  // cold-start offline y el fallback de documentos del sw.js.
131
159
  ...(pwa.shell ? {additionalPrecacheEntries: [{url: pwa.shell, revision: `${Date.now()}`}]} : {}),
132
- // GOTCHA (docs/prerelease/26): los entries salen RELATIVOS y resuelven
133
- // contra la URL del SW → 404 bajo subpath. Se prefijan con la base real.
160
+ // GOTCHA: los entries salen RELATIVOS y resuelven contra la URL del SW
161
+ // → 404 bajo subpath. Se prefijan con la base real.
134
162
  manifestTransforms: [
135
163
  (entries) => ({
136
164
  manifest: entries.map((entry) => (
@@ -141,5 +169,9 @@ function pwaPlugins(options) {
141
169
  });
142
170
  // El SW solo se genera en BUILD: en dev serviría assets stale y pelearía
143
171
  // con HMR. `apply: 'build'` es el switch idiomático de Vite para esto.
144
- return (Array.isArray(raw) ? raw : [raw]).map((plugin) => ({apply: 'build', ...plugin}));
172
+ // El override va DESPUÉS del spread: el sub-plugin dev de serwist trae su
173
+ // propio `apply: 'serve'` y con el orden invertido pisaría el nuestro —
174
+ // su middleware quedaba vivo en dev SIN el plugin principal que inicializa
175
+ // `ctx.options`, y tronaba en cada request del dev server.
176
+ return (Array.isArray(raw) ? raw : [raw]).map((plugin) => ({...plugin, apply: 'build'}));
145
177
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-milpa",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "El toolkit frontend del framework milpa (FastAPI + Jinja), estilo laravel-vite-plugin: hot-file para HMR, manifest para el helper vite() de Jinja, multi-app (surcos/), PWA opcional con Serwist, ASSET_URL para deploy bajo sub-ruta/CDN — y file-based routing para react-router 7 vía `vite-plugin-milpa/router`.",
5
5
  "type": "module",
6
6
  "main": "index.js",