vitejs/vite

require.resolve (called at runtime) is not transformed

Closed this issue · 1 comments

Describe the bug

The require.resolve('foo') calls do not get transformed and throw errors at runtime.

Reproduction

https://github.com/SpadarShut/vite-require-resolve

import { getThemeVariables } from 'antd/dist/theme';

getThemeVariables(); // throws Uncaught TypeError: __require.resolve is not a function

// getThemeVariables looks like this:

function getThemeVariables(options = {}) {
  let themeVar = {
    'hack': `true;@import "${require.resolve('antd/lib/style/color/colorPalette.less')}";`,
    ...defaultTheme
  };
  // ...
}

I believe vite should transform such require calls into resolved paths.

System Info

System:
    OS: macOS 11.4
    CPU: (4) x64 Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
    Memory: 258.35 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.4.0 - /usr/local/bin/node
    npm: 7.19.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 91.0.4472.164
    Safari: 14.1.1
  npmPackages:
    vite: ^2.3.8 => 2.4.3

Used Package Manager

npm

Logs

No response

Validations

require.resolve is a Node.js only API and esbuild doesn't support it. This is a wont fix. Antd should not rely on require.resolve in client-oriented code. AFAIK webpack is the only bundler that supports it - basically this means Antd will only work in webpack - which, IMO, is a bad decision.