Skip to content

Dynamic import fails with variables and aliases #10460

@hassanzohdy

Description

@hassanzohdy

Describe the bug

I added in vite.config.ts aliases list as follows

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

const aliases = {
  apps: "/src/apps",
};

const aliasesList = [];

for (const [key, value] of Object.entries(aliases)) {
  aliasesList.push({
    find: key,
    replacement: value,
  });
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: aliasesList,
  },
});

Now when i try to make a dynamic import like this

import('apps/my-file').then(output => {
    // works fine
});

This works fine, but when i try to do something like this, it does not work at all

const path = 'my-file';
import('apps/' + path).then(output => {
    // does not work
});

See the example for reproducing.

Any idea on how to make this works.

Thanks

Reproduction

https://codesandbox.io/s/angry-snyder-7vsypo?file=/src/main.tsx

Steps to reproduce

No response

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz   
    Memory: 6.77 GB / 15.85 GB
  Binaries:
    Node: 16.14.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.6.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (106.0.1370.42)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @vitejs/plugin-react: ^2.1.0 => 2.1.0
    vite: ^3.1.0 => 3.1.8

Used Package Manager

yarn

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions