diff --git a/package.json b/package.json index 80d8f97..bddcb5a 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "build": "vite build" }, "devDependencies": { + "@pollora/vite-config": "^1.0.0", "@roots/vite-plugin": "^2.0.0", "@tailwindcss/vite": "^4.2.3", "laravel-vite-plugin": "^3.0.1", diff --git a/vite.config.js b/vite.config.js index 8bb38d3..2058b13 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,107 +1,21 @@ -import { defineConfig } from "vite"; -import laravel, { refreshPaths } from 'laravel-vite-plugin'; -import { wordpressThemeJson } from '@roots/vite-plugin'; -import path from 'path'; -import tailwindcss from '@tailwindcss/vite'; - -const isDocker = process.env.IS_DOCKER || process.env.DOCKER_ENV || process.env.DDEV_PRIMARY_URL; -const port = 5173; -const publicDirectory = "../../public"; -const themeName = path.basename(__dirname); - -const getBaseUrl = () => { - return process.env.APP_URL || process.env.DDEV_PRIMARY_URL || 'http://localhost'; -}; - -const isHttps = getBaseUrl().startsWith('https'); - -const getDevServerConfig = () => { - const commonConfig = { - server: { - port, - strictPort: true, - cors: { - origin: '*', - methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], - credentials: true - }, - } - }; - - if (isDocker) { - return { - server: { - ...commonConfig.server, - host: '0.0.0.0', - origin: `${getBaseUrl()}:${port}`, - hmr: { - protocol: isHttps ? 'wss' : 'ws', - host: new URL(getBaseUrl()).hostname, - } - }, - }; - } - - return { - server: { - ...commonConfig.server, - https: isHttps, - host: isHttps ? new URL(getBaseUrl()).hostname : 'localhost', - hmr: { - protocol: isHttps ? 'wss' : 'ws', - host: new URL(getBaseUrl()).hostname - } - }, - }; -}; - -const getThemeConfig = () => ({ - base: "/build/theme/" + themeName, - input: ["./resources/assets/app.js"], - publicDirectory, - hotFile: path.join(publicDirectory, `${themeName}.hot`), - buildDirectory: path.join("build", "theme", themeName), - refresh: [ - ...refreshPaths.filter((refreshPath) => refreshPath !== 'resources/views/**'), - 'themes/' + themeName + '/resources/views/**/*.blade.php', - 'themes/' + themeName + '/templates/**/*.html', - 'themes/' + themeName + '/parts/**/*.html', - 'resources/views/**/*.blade.php', - ], - assets: [ - 'resources/assets/images/**', - 'resources/assets/fonts/**', - ], -}); +import { defineConfig } from 'vite'; +import pollora from '@pollora/vite-config'; export default defineConfig({ - base: "/build/theme/" + themeName, - build: { - emptyOutDir: false, - }, plugins: [ - tailwindcss(), - laravel(getThemeConfig()), - wordpressThemeJson({ - baseThemeJsonPath: './theme.json', - // The type scale, the two families (with their own @font-face) and - // the border radii are all hand-authored in theme.json: only the - // colour palette is meant to flow in from @theme static. - disableTailwindFonts: true, - disableTailwindFontSizes: true, - disableTailwindBorderRadius: true, - }), - { - name: "blade-and-block-templates", - handleHotUpdate({ file, server }) { - if (file.endsWith(".blade.php") || file.endsWith(".html")) { - server.ws.send({ - type: "full-reload", - path: "*", - }); - } + pollora({ + type: 'theme', + // A block theme: its templates and parts reload the page too + refresh: ['templates/**/*.html', 'parts/**/*.html'], + reloadOn: ['.blade.php', '.html'], + themeJson: { + // The type scale, the two families (with their own @font-face) and + // the border radii are all hand-authored in theme.json: only the + // colour palette is meant to flow in from @theme static. + disableTailwindFonts: true, + disableTailwindFontSizes: true, + disableTailwindBorderRadius: true, }, - }, + }), ], - ...getDevServerConfig() });