Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic import fails with variables and aliases #10460

Open
7 tasks done
hassanzohdy opened this issue Oct 13, 2022 · 9 comments
Open
7 tasks done

Dynamic import fails with variables and aliases #10460

hassanzohdy opened this issue Oct 13, 2022 · 9 comments

Comments

@hassanzohdy
Copy link

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

@Dunqing
Copy link
Contributor

Dunqing commented Oct 14, 2022

It looks like this plugin will solve your problem

@hassanzohdy
Copy link
Author

@Dunqing Thanks for the share but this does not actually help as it in somehow transforms it to relative paths. looking for something similar to how CRA handle it

@hassanzohdy
Copy link
Author

Any update about this

@harshithmullapudi
Copy link

Hey any update here?

@hassanzohdy
Copy link
Author

@harshithmullapudi Nope, but i've worked with relative paths to resolve this issue, so absolute paths with dynamic imports won't work.

If you're facing a similar situation here, i may help.

@seniordev-ca
Copy link

seniordev-ca commented Oct 28, 2022

Any updates here?

This works:

import loadable from '@loadable/component'
...
const Icon = loadable.lib(() => import(`react-icons/fa`))

but this doesn't

import loadable from '@loadable/component'
...
const Icon = loadable.lib(() => import(`react-icons/${'fa'}`))

@SebastianBoergers
Copy link

I think dynamic imports with variables have to start with "./" or "../" and also include the file ending.

@caoxiemeihao
Copy link
Contributor

const aliases = {
- apps: "/src/apps",
+ apps: path.join(__dirname, "/src/apps"),
};

Because "/src/apps" will be treated as an absolute path. So you must ensure that the absolute path is correct.

@Farnsi
Copy link

Farnsi commented May 9, 2023

Possible that this plugin vite-plugin-virtual-resource could do the job.
You can define a custom resolver to load / include your file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants