vite-plugin-web-config
v0.0.2
Published
Auto generate web.config file and vite server proxy in dev mode
Downloads
103
Readme
vite-plugin-web-config
- Configure vite server proxy in development mode.
- In build mode, it will generate
web.config(IIS server config) file with rewrite rules in thedistdirectory.
Install
# npm
npm i vite-plugin-web-config -D
# yarn
yarn add vite-plugin-web-config -D
# pnpm
pnpm add vite-plugin-web-config -DUsage
Add plugin to vite.config.ts:
import WebConfig from 'vite-plugin-web-config'
export default {
plugins: [
WebConfig({
proxy: [['/api', 'http://localhost:3000']],
output: 'web.config',
}),
],
}Options
| Name | Type | Default | Description |
| ------ | -------------------- | ------------ | ----------------- |
| proxy | [string, string][] | [] | Proxy list config |
| output | string \| false | web.config | Output file name. |
Except for the above options, you can also configure VITE_PROXY in .env.* file:
VITE_PROXY = [["/api","http://localhost:3000"]]Must be a valid JSON string.
If provideproxyoption,.env.*config will be ignored.
Whenoutputisfalse, it will not generateweb.configfile.
In development mode, vite will automatically configure the proxy according to the configuration in the .env.* file or options.proxy.
// vite.config.ts
export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, ''),
},
},
},
})After running vite build, the web.config file will be generated in the dist directory.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="api_rule" stopProcessing="true">
<match url="api/(.*)"/>
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="api/(.*)"/>
</conditions>
<action type="Rewrite" url="http://localhost:3000/{R:1}" logRewrittenUrl="true"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>