source mapping, dynanmic vite hmr server port
This commit is contained in:
@@ -29,6 +29,8 @@
|
|||||||
|
|
||||||
# npm
|
# npm
|
||||||
node_modules/
|
node_modules/
|
||||||
|
# Port réel du dev server Vite, écrit par dev.mjs à chaque lancement
|
||||||
|
.vite-dev-port
|
||||||
|
|
||||||
#*.patch
|
#*.patch
|
||||||
/.csslintrc
|
/.csslintrc
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,23 +0,0 @@
|
|||||||
import { createServer, build } from 'vite'
|
|
||||||
|
|
||||||
// Build watcher : écrit les fichiers dist sur disque en continu
|
|
||||||
const watcher = await build({
|
|
||||||
build: { watch: {} },
|
|
||||||
})
|
|
||||||
|
|
||||||
// Dev server : HMR sur localhost:5173
|
|
||||||
const server = await createServer()
|
|
||||||
await server.listen()
|
|
||||||
|
|
||||||
console.log('\n HMR actif + dist générés en watch\n')
|
|
||||||
|
|
||||||
const shutdown = async () => {
|
|
||||||
await Promise.allSettled([
|
|
||||||
watcher.close(),
|
|
||||||
server.close(),
|
|
||||||
])
|
|
||||||
process.exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
process.on('SIGINT', shutdown)
|
|
||||||
process.on('SIGTERM', shutdown)
|
|
||||||
@@ -8,13 +8,8 @@ global:
|
|||||||
assets/dist/main.css: {}
|
assets/dist/main.css: {}
|
||||||
# assets/scss/main.css: {}
|
# assets/scss/main.css: {}
|
||||||
|
|
||||||
vitehmr:
|
# En dev, les scripts du dev server Vite (@vite/client + main.js) ne sont
|
||||||
js:
|
# PAS dans une librairie statique : le port est dynamique (premier port
|
||||||
https://localhost:5173/@vite/client:
|
# libre à partir de 5173, voir le plugin devPortFilePlugin dans vite.config.js
|
||||||
type: external
|
# qui écrit .vite-dev-port), donc
|
||||||
attributes:
|
# mathallo_page_attachments_alter() les attache directement en html_head.
|
||||||
type: module
|
|
||||||
https://localhost:5173/assets/js/main.js:
|
|
||||||
type: external
|
|
||||||
attributes:
|
|
||||||
type: module
|
|
||||||
|
|||||||
@@ -7,11 +7,30 @@
|
|||||||
|
|
||||||
function mathallo_page_attachments_alter(&$page) {
|
function mathallo_page_attachments_alter(&$page) {
|
||||||
if (getenv('APP_ENV') === 'development') {
|
if (getenv('APP_ENV') === 'development') {
|
||||||
// $attachments['#attached']['html_head'][] = [['src' => 'http://localhost:5173/@vite/client', 'type' => 'module'],'vite-client'];
|
// Port réel du dev server Vite, écrit par le plugin devPortFilePlugin
|
||||||
// $page['#attached']['library'][] = 'http://localhost:5173/@vite/client';
|
// (vite.config.js) dans .vite-dev-port
|
||||||
// $attachments['#attached']['html_head'][] = [['src' => 'http://localhost:5173/assets/js/main.js', 'type' => 'module'],'vite-main'];
|
// (premier port libre à partir de 5173, strictPort: false) : permet de
|
||||||
// $page['#attached']['library'][] = 'http://localhost:5173/assets/js/main.js';
|
// lancer plusieurs sites sur le même modèle de thème en même temps.
|
||||||
$page['#attached']['library'][] = 'mathallo/vitehmr';
|
$port = (int) trim((string) @file_get_contents(__DIR__ . '/.vite-dev-port'));
|
||||||
|
if ($port <= 0) {
|
||||||
|
$port = 5173;
|
||||||
|
}
|
||||||
|
$page['#attached']['html_head'][] = [
|
||||||
|
[
|
||||||
|
'#type' => 'html_tag',
|
||||||
|
'#tag' => 'script',
|
||||||
|
'#attributes' => ['type' => 'module', 'src' => "https://localhost:{$port}/@vite/client"],
|
||||||
|
],
|
||||||
|
'vite-client',
|
||||||
|
];
|
||||||
|
$page['#attached']['html_head'][] = [
|
||||||
|
[
|
||||||
|
'#type' => 'html_tag',
|
||||||
|
'#tag' => 'script',
|
||||||
|
'#attributes' => ['type' => 'module', 'src' => "https://localhost:{$port}/assets/js/main.js"],
|
||||||
|
],
|
||||||
|
'vite-main',
|
||||||
|
];
|
||||||
}else{
|
}else{
|
||||||
$page['#attached']['library'][] = 'mathallo/global';
|
$page['#attached']['library'][] = 'mathallo/global';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "node dev.mjs",
|
"dev": "vite",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig, build } from 'vite'
|
||||||
import vue from '@vitejs/plugin-vue'
|
import vue from '@vitejs/plugin-vue'
|
||||||
import { fileURLToPath, URL } from 'node:url'
|
import { fileURLToPath, URL } from 'node:url'
|
||||||
import { readFileSync } from 'node:fs'
|
import { readFileSync, writeFileSync, unlinkSync } from 'node:fs'
|
||||||
|
|
||||||
// Certificat mkcert wildcard du reverse-proxy (SAN: localhost, 127.0.0.1,
|
// Certificat mkcert wildcard du reverse-proxy (SAN: localhost, 127.0.0.1,
|
||||||
// *.localhost, trusté par la CA locale) : permet de servir le dev server
|
// *.localhost, trusté par la CA locale) : permet de servir le dev server
|
||||||
@@ -9,9 +9,50 @@ import { readFileSync } from 'node:fs'
|
|||||||
const TLS_CERT = fileURLToPath(new URL('../../../../../../reverse-proxy/certs/wildcard.localhost.pem', import.meta.url))
|
const TLS_CERT = fileURLToPath(new URL('../../../../../../reverse-proxy/certs/wildcard.localhost.pem', import.meta.url))
|
||||||
const TLS_KEY = fileURLToPath(new URL('../../../../../../reverse-proxy/certs/wildcard.localhost-key.pem', import.meta.url))
|
const TLS_KEY = fileURLToPath(new URL('../../../../../../reverse-proxy/certs/wildcard.localhost-key.pem', import.meta.url))
|
||||||
|
|
||||||
|
// En dev uniquement : écrit le port réellement écouté par le dev server
|
||||||
|
// dans .vite-dev-port (mathallo.theme lit ce fichier pour charger le bon
|
||||||
|
// @vite/client). Avec strictPort: false, Vite prend le premier port libre
|
||||||
|
// à partir de 5173 : plusieurs sites sur le même modèle de thème peuvent
|
||||||
|
// tourner `npm run dev` en même temps. Le client HMR se connecte ensuite
|
||||||
|
// sur importMetaUrl (host:port du <script> chargé), donc automatiquement
|
||||||
|
// sur le bon port. Le plugin lance aussi le build watch (assets/dist en
|
||||||
|
// continu), donc `npm run dev` reste un simple `vite`.
|
||||||
|
function devPortFilePlugin() {
|
||||||
|
const portFile = fileURLToPath(new URL('./.vite-dev-port', import.meta.url))
|
||||||
|
return {
|
||||||
|
name: 'dev-port-file',
|
||||||
|
apply: 'serve',
|
||||||
|
async configureServer(server) {
|
||||||
|
// Build watch : écrit les fichiers dist sur disque en continu.
|
||||||
|
// configureServer est rappelé à chaque redémarrage interne de Vite
|
||||||
|
// (modif de vite.config.js, touche r) : le watcher est fermé à la
|
||||||
|
// fermeture du serveur pour ne pas en accumuler plusieurs.
|
||||||
|
const watcherP = build({ build: { watch: {} } })
|
||||||
|
watcherP.catch((e) => server.config.logger.error(e))
|
||||||
|
// Supprime un éventuel fichier résiduel d'un arrêt brutal (kill -9)
|
||||||
|
try { unlinkSync(portFile) } catch {}
|
||||||
|
server.httpServer?.once('listening', () => {
|
||||||
|
const address = server.httpServer.address()
|
||||||
|
const port = typeof address === 'object' && address !== null ? address.port : server.config.server.port
|
||||||
|
writeFileSync(portFile, String(port))
|
||||||
|
})
|
||||||
|
server.httpServer?.once('close', () => {
|
||||||
|
watcherP.then((w) => w.close()).catch(() => {})
|
||||||
|
try { unlinkSync(portFile) } catch {}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [vue()],
|
plugins: [vue(), devPortFilePlugin()],
|
||||||
|
css: {
|
||||||
|
// Sourcemaps des styles injectés en dev (balises <style data-vite-dev-id>)
|
||||||
|
// : l'inspecteur Firefox résout alors les règles vers les fichiers .scss
|
||||||
|
// sources au lieu d'afficher « inline ».
|
||||||
|
devSourcemap: true,
|
||||||
|
},
|
||||||
// Chemins relatifs dans les assets buildés : sinon Vite génère des url()
|
// Chemins relatifs dans les assets buildés : sinon Vite génère des url()
|
||||||
// absolues depuis la racine du site au lieu du dossier du thème, où Drupal
|
// absolues depuis la racine du site au lieu du dossier du thème, où Drupal
|
||||||
// sert réellement assets/dist/.
|
// sert réellement assets/dist/.
|
||||||
@@ -19,8 +60,12 @@ export default defineConfig({
|
|||||||
server: {
|
server: {
|
||||||
// The dev server must be reachable from the browser on the host.
|
// The dev server must be reachable from the browser on the host.
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
|
// Premier port libre à partir de 5173 : plusieurs sites sur le même
|
||||||
|
// modèle de thème peuvent tourner `npm run dev` en même temps
|
||||||
|
// (5173, 5174, ...). Le plugin devPortFilePlugin écrit le port choisi
|
||||||
|
// dans .vite-dev-port pour que le thème Drupal charge le bon client.
|
||||||
port: 5173,
|
port: 5173,
|
||||||
strictPort: true,
|
strictPort: false,
|
||||||
// HTTPS obligatoire depuis la migration Traefik : la page Drupal est
|
// HTTPS obligatoire depuis la migration Traefik : la page Drupal est
|
||||||
// servie en https://mathallo.fr.localhost, un dev server en
|
// servie en https://mathallo.fr.localhost, un dev server en
|
||||||
// http://localhost:5173 serait bloqué en mixed content (assets + WebSocket
|
// http://localhost:5173 serait bloqué en mixed content (assets + WebSocket
|
||||||
@@ -41,12 +86,13 @@ export default defineConfig({
|
|||||||
cors: {
|
cors: {
|
||||||
origin: [/^https:\/\/mathallo\.fr\.localhost(:\d+)?$/, /^https:\/\/dev\.mathallo\.fr(:\d+)?$/],
|
origin: [/^https:\/\/mathallo\.fr\.localhost(:\d+)?$/, /^https:\/\/dev\.mathallo\.fr(:\d+)?$/],
|
||||||
},
|
},
|
||||||
// Absolute base URL used by Vite to build asset/HMR URLs.
|
// Pas d'origin ni de clientPort en dur : en dev, les URLs des modules
|
||||||
origin: 'https://localhost:5173',
|
// et le WebSocket HMR sont résolus relativement à l'URL du @vite/client
|
||||||
|
// chargé par la page (importMetaUrl), donc automatiquement sur le port
|
||||||
|
// réel du serveur, quel qu'il soit.
|
||||||
hmr: {
|
hmr: {
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
protocol: 'wss',
|
protocol: 'wss',
|
||||||
clientPort: 5173,
|
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
usePolling: true,
|
usePolling: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user