vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.89k stars 6.01k forks source link

Dynamic import doesn't work on Windows. #15258

Closed enessefak closed 8 months ago

enessefak commented 8 months ago

Describe the bug

I am doing...

const getPluginModule = (pluginPathName) => () => import(`../plugins/${pluginPathName}/index.jsx`)

const importPlugins = async () => {
  try {
    const [configs, schemas] = await Promise.all([
      import.meta.glob('@/plugins/*/config.js', { eager: true }),
      import.meta.glob('@/plugins/*/schema.js', { eager: true })
    ])

    const plugins = Object.entries(configs).reduce((acc, [configPathName, configModule]) => {
      const basePath = configPathName.replace(/\/config\.([jt]sx?)$/, '')
      const isExtjsPlugin = extJsPlugins.some((extJsPlugin) => basePath.includes(extJsPlugin))
      const schemaModule = schemas[basePath + '/schema.js']

      const dynamicModulePath = basePath.replace('/src/plugins/', '')

      const config = configModule?.default
      const schema = schemaModule?.default?.()

      return {
        ...acc,
        [config.namespace]:
          !withExtjs && isExtjsPlugin
            ? {
                config,
                schema,
                module: () => null
              }
            : {
                config,
                schema,
                module: getPluginModule(dynamicModulePath)
              }
      }
    }, {})

    return plugins
  } catch (err) {
    console.log(err)
    throw err
  }
}

What I expect is...

I expect it to work correctly on Windows as it does on macOS.

What actually happening is...

On Windows, the module is not found, and when I build it, object.assign({}) comes empty. It works flawlessly on macOS.

Reproduction

https://stackblitz.com/edit/vitejs-vite-fkfu4y?file=src%2FApp.jsx

Steps to reproduce

"start": "vite"

pnpm install
pnpm run start

System Info

### Working System
  :
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1
    Memory: 76.86 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 8.6.12 - ~/Library/pnpm/pnpm
    Watchman: 2023.08.07.00 - /opt/homebrew/bin/watchman
  Browsers:
    Safari: 17.0
  npmPackages:
    @vitejs/plugin-react: ^4.0.4 => 4.0.4 
    vite: ^4.4.9 => 4.4.9

### Not Working System

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
    Memory: 1.39 GB / 15.75 GB
  Binaries:
    Node: 14.18.1 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    [@vitejs](https://t.me/vitejs)/plugin-react: ^4.0.4 => 4.0.4
    vite: ^4.4.9 => 4.4.9

Used Package Manager

pnpm

Logs

No response

Validations

stackblitz[bot] commented 8 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

sapphi-red commented 8 months ago

I wasn't able to reproduce on my Windows machine. I had to change import(`../plugins/${pluginPathName}/index.jsx`); to import(`./plugins/${pluginPathName}/index.jsx`);. But module does exist both on dev and build. image image

sapphi-red commented 8 months ago

Closing due to lack of response.