Skip to content

Commit

Permalink
Add pathRewrite to proxy options
Browse files Browse the repository at this point in the history
Fixes elm-land#176

The behavior is based on the `pathRewrite` in `http-proxy-middleware`
where the first matching pattern determines the rewrite:
https://github.com/chimurai/http-proxy-middleware/blob/f2a0af329969f8a70b3967a969146758a73d8b0e/src/path-rewriter.ts#L33-L39
  • Loading branch information
pete-murphy committed May 12, 2024
1 parent 0b61892 commit 3eba4fd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion projects/cli/src/effects.js
Expand Up @@ -170,7 +170,25 @@ let runServer = async (options) => {

// Check for optional proxy field:
let proxy = null
try { proxy = config.app.proxy }
try {
proxy = config.app.proxy

// Check for optional `pathRewrite` object, and use it to create a Vite-compatible `rewrite` function
for (const target of Object.values(proxy)) {
if (typeof target !== 'string' && target.pathRewrite != null) {
target.rewrite = path => {
for (const [pattern, replacement] of Object.entries(target.pathRewrite)) {
const regExp = new RegExp(pattern)
if (regExp.test(path)) {
// Use only the first match
return path.replace(regExp, replacement)
}
}
return path
}
}
}
}
catch (_) { }

/**
Expand Down

0 comments on commit 3eba4fd

Please sign in to comment.