vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.22k stars 6.15k forks source link

vite preview proxy config read null #6662

Closed hehehai closed 2 years ago

hehehai commented 2 years ago

Describe the bug

script: $ vite preview --config ./vite.config.preview.ts

config file port

export default defineConfig({
  mode: 'development',
  preview: {
    host: '0.0.0.0',
    port: 4173,
    strictPort: true,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/'),
      },
    },
  },
  ......
});

Problem location

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at proxyMiddleware (/Users/guanwei/code/company/cbj_pro/cbj_app/node_modules/.pnpm/registry.npmmirror.com+vite@2.7.10_less@4.1.2/node_modules/vite/dist/node/chunks/dep-76613303.js:50062:12)
    at Object.preview (/Users/guanwei/code/company/cbj_pro/cbj_app/node_modules/.pnpm/registry.npmmirror.com+vite@2.7.10_less@4.1.2/node_modules/vite/dist/node/chunks/dep-76613303.js:73391:17)

https://github.com/vitejs/vite/blob/870e1c076272960a5f390b2cfdd3ae275b3891a5/packages/vite/src/node/preview.ts#L81-L83 Local dependency code

async function preview(inlineConfig) {
    ......
    // proxy
    if (config.preview.proxy) {
        app.use(proxyMiddleware(httpServer, config));
    }
    ......
}

https://github.com/vitejs/vite/blob/870e1c076272960a5f390b2cfdd3ae275b3891a5/packages/vite/src/node/server/middlewares/proxy.ts#L31-L40 Local dependency code

function proxyMiddleware(httpServer, config) {
    const options = config.server.proxy;
    // lazy require only when proxy is used
    const proxies = {};
    Object.keys(options) ......
}

Judge is config.preview.proxy, but read is config.server.proxy

Reproduction

-

System Info

System:
    OS: macOS 12.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 5.07 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.3 - /usr/local/bin/node
    Yarn: 1.22.17 - ~/.yarn/bin/yarn
    npm: 7.24.0 - /usr/local/bin/npm
    Watchman: 2021.11.01.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 97.0.4692.99
    Firefox: 95.0.2
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-vue: ^1.9.4 => 1.10.2 
    @vitejs/plugin-vue-jsx: ^1.2.0 => 1.3.3 
    vite: ^2.6.4 => 2.7.10

Used Package Manager

pnpm

Logs

❯ pnpm build
.....
success

❯ pnpm preview

> arco-design-pro-vue@1.0.0 preview /Users/guanwei/code/company/cbj_pro/cbj_app
> vite preview --config ./config/vite.config.preview.ts

error when starting preview server:
TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at proxyMiddleware (/Users/guanwei/code/company/cbj_pro/cbj_app/node_modules/.pnpm/registry.npmmirror.com+vite@2.7.10_less@4.1.2/node_modules/vite/dist/node/chunks/dep-76613303.js:50062:12)
    at Object.preview (/Users/guanwei/code/company/cbj_pro/cbj_app/node_modules/.pnpm/registry.npmmirror.com+vite@2.7.10_less@4.1.2/node_modules/vite/dist/node/chunks/dep-76613303.js:73391:17)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async CAC.<anonymous> (/Users/guanwei/code/company/cbj_pro/cbj_app/node_modules/.pnpm/registry.npmmirror.com+vite@2.7.10_less@4.1.2/node_modules/vite/dist/node/cli.js:782:24)
 ELIFECYCLE  Command failed with exit code 1.

Validations

bluwy commented 2 years ago

This is the same as #7414 and is fixed in #7604. Also credits to #6666 which had implemented the same fix but we had missed out on merging it.