-
-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middleware redirection in vite (#2686)
* add middleware redirection * fix typo * Update vite.config.ts Co-authored-by: Martin Stone <1611702+d7415@users.noreply.github.com> --------- Co-authored-by: Martin Stone <1611702+d7415@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
132 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,142 @@ | ||
import { fileURLToPath, URL } from 'node:url'; | ||
import { defineConfig, loadEnv, UserConfig } from 'vite'; | ||
import laravel from 'laravel-vite-plugin'; | ||
import vue from '@vitejs/plugin-vue'; | ||
import i18n from 'laravel-vue-i18n/vite'; | ||
import { fileURLToPath, URL } from "node:url"; | ||
import { defineConfig, loadEnv, PluginOption, UserConfig } from "vite"; | ||
import laravel from "laravel-vite-plugin"; | ||
import vue from "@vitejs/plugin-vue"; | ||
import i18n from "laravel-vue-i18n/vite"; | ||
|
||
const laravelPlugin = laravel({ | ||
input: [ | ||
'resources/sass/app.scss', | ||
'resources/js/app.ts', | ||
], | ||
refresh: true, | ||
}) | ||
input: ["resources/sass/app.scss", "resources/js/app.ts"], | ||
refresh: true, | ||
}); | ||
|
||
const baseConfig = { | ||
base: './', | ||
plugins: [ | ||
vue( | ||
{ | ||
template: { | ||
transformAssetUrls: { | ||
base: null, | ||
includeAbsolute: false, | ||
}, | ||
}, | ||
}), | ||
i18n(), | ||
], | ||
server: { | ||
watch: { | ||
ignored: [ | ||
"**/.*/**", | ||
"**/app/**", | ||
"**/database/**", | ||
"**/node_modules/**", | ||
"**/public/**", | ||
"**/storage/**", | ||
"**/tests/**", | ||
"**/vendor/**", | ||
"**/presets/**", | ||
], | ||
}, | ||
}, | ||
resolve: { | ||
alias: { | ||
// @ts-ignore-next-line | ||
'@': fileURLToPath(new URL('./resources/js/', import.meta.url)), | ||
vue: 'vue/dist/vue.esm-bundler.js', | ||
}, | ||
}, | ||
build: { | ||
rollupOptions: { | ||
output:{ | ||
manualChunks(id) { | ||
if (id.includes('node_modules')) { | ||
return id.toString().split('node_modules/')[1].split('/')[0].toString(); | ||
} | ||
} | ||
} | ||
} | ||
const localDevelopMiddleware: PluginOption = { | ||
name: "develop-rewrite-middleware", | ||
configureServer(server) { | ||
const viteIndexPath = "/vite/index.html"; | ||
|
||
server.middlewares.use((req, res, next) => { | ||
if (!req.url) { | ||
next(); | ||
return; | ||
} | ||
|
||
const requestUrl : string = req.url; | ||
const doNotTouch = ["/@", "/api", "/resources", "/node_modules"]; | ||
if (doNotTouch.some((page) => requestUrl.startsWith(page))) { | ||
next(); | ||
return; | ||
} | ||
|
||
console.log(req.url); | ||
const startsWithPages = ["/gallery", "/frame", "/map", "/search"]; | ||
// Check if req.url starts with pages | ||
if (startsWithPages.some((page) => requestUrl.startsWith(page))) { | ||
req.url = viteIndexPath; | ||
} | ||
|
||
const pages = [ | ||
"/", | ||
"/diagnostics", | ||
"/permissions", | ||
"/jobs", | ||
"/maintenance", | ||
"/profile", | ||
"/settings", | ||
"/sharing", | ||
"/statistics", | ||
"/users", | ||
]; | ||
// Check if requestUrl is in pages | ||
if (pages.includes(requestUrl)) { | ||
req.url = viteIndexPath; | ||
} | ||
|
||
next(); | ||
}); | ||
}, | ||
css: { | ||
preprocessorOptions: { | ||
scss: { | ||
api: 'modern' | ||
}, | ||
} | ||
} | ||
} as UserConfig; | ||
} | ||
|
||
const baseConfig = { | ||
base: "./", | ||
plugins: [ | ||
vue({ template: { transformAssetUrls: { base: null, includeAbsolute: false } } }), | ||
i18n(), | ||
], | ||
server: { | ||
watch: { | ||
ignored: [ | ||
"**/.*/**", | ||
"**/app/**", | ||
"**/database/**", | ||
"**/node_modules/**", | ||
"**/public/**", | ||
"**/storage/**", | ||
"**/tests/**", | ||
"**/vendor/**", | ||
"**/presets/**", | ||
], | ||
}, | ||
}, | ||
resolve: { | ||
alias: { | ||
// @ts-ignore-next-line | ||
"@": fileURLToPath(new URL("./resources/js/", import.meta.url)), | ||
vue: "vue/dist/vue.esm-bundler.js", | ||
}, | ||
}, | ||
build: { | ||
rollupOptions: { | ||
output: { | ||
manualChunks(id) { | ||
if (id.includes("node_modules")) { | ||
return id.toString().split("node_modules/")[1].split("/")[0].toString(); | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
css: { | ||
preprocessorOptions: { | ||
scss: { | ||
api: "modern", | ||
}, | ||
}, | ||
}, | ||
} as UserConfig; | ||
|
||
/** @type {import('vite').UserConfig} */ | ||
export default defineConfig( | ||
({ command, mode, isSsrBuild, isPreview }) => { | ||
const config = baseConfig; | ||
const env = loadEnv(mode, process.cwd(), '') | ||
|
||
if (config.server === undefined) { | ||
throw new Error('server config is missing'); | ||
} | ||
if (config.plugins === undefined) { | ||
throw new Error('plugins list is missing'); | ||
} | ||
export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => { | ||
const config = baseConfig; | ||
const env = loadEnv(mode, process.cwd(), ""); | ||
|
||
if (command === 'serve') { | ||
if (config.server === undefined) { | ||
throw new Error("server config is missing"); | ||
} | ||
if (config.plugins === undefined) { | ||
throw new Error("plugins list is missing"); | ||
} | ||
|
||
console.log("LOCAL VITE MODE detected") | ||
if (env.VITE_HTTP_PROXY_TARGET !== undefined) { | ||
console.log("api calls will be forwarded to:") | ||
console.log(env.VITE_HTTP_PROXY_TARGET); | ||
} | ||
if (command === "serve") { | ||
console.log("LOCAL VITE MODE detected"); | ||
if (env.VITE_HTTP_PROXY_TARGET !== undefined) { | ||
console.log("api calls will be forwarded to:"); | ||
console.log(env.VITE_HTTP_PROXY_TARGET); | ||
} | ||
|
||
if (env.VITE_LOCAL_DEV === 'true') { | ||
if (env.VITE_HTTP_PROXY_TARGET === undefined) { | ||
throw new Error('VITE_HTTP_PROXY_TARGET is missing'); | ||
} | ||
config.server.open = 'vite/index.html'; | ||
config.server.proxy = { | ||
'/api/': env.VITE_HTTP_PROXY_TARGET, | ||
} | ||
return config; | ||
} | ||
} | ||
if (env.VITE_LOCAL_DEV === "true") { | ||
if (env.VITE_HTTP_PROXY_TARGET === undefined) { | ||
throw new Error("VITE_HTTP_PROXY_TARGET is missing"); | ||
} | ||
config.server.open = "vite/index.html"; | ||
config.server.proxy = { | ||
"/api/": env.VITE_HTTP_PROXY_TARGET, | ||
}; | ||
config.plugins.push(localDevelopMiddleware); | ||
return config; | ||
} | ||
} | ||
|
||
// command === 'build' | ||
config.plugins.push(laravelPlugin); | ||
return config; | ||
}); | ||
// command === 'build' | ||
config.plugins.push(laravelPlugin); | ||
return config; | ||
}); |