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

Error when using alias in vite.config.js #39

Open
6 tasks done
ericdmachado opened this issue Sep 14, 2024 · 3 comments
Open
6 tasks done

Error when using alias in vite.config.js #39

ericdmachado opened this issue Sep 14, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@ericdmachado
Copy link

Describe the bug

I am using aliases to make it easier to import modules and components into my project, however, there seems to be a problem when searching for the directory that causes it to get lost during the import. Ex.:

{ resolve: { alias: { '@assets': path.resolve(__dirname, 'src/assets/'), '@styles': path.resolve(__dirname, 'src/styles/'), '@components': path.resolve(__dirname, 'src/components/'), } } }

Reproduction

https://github.com/ericdmachado/react-server-test

Steps to reproduce

No response

System Info

Node 21.7.3 e npm 10.8.3

Used Package Manager

pnpm

Logs

No response

Validations

@ericdmachado ericdmachado added the pending triage Pending triage label Sep 14, 2024
@lazarv lazarv added bug Something isn't working and removed pending triage Pending triage labels Sep 15, 2024
@lazarv lazarv self-assigned this Sep 15, 2024
@lazarv
Copy link
Owner

lazarv commented Sep 15, 2024

Thanks for reporting @ericdmachado! resolve.alias objects are now supported, please upgrade the framework to the latest version.

Keep in mind, that you still need to use Vite configuration in ESM or CommonJS formats, but don't mix these.

When using CommonJS to support __dirname usage, you need to use module.exports in vite.config.js:

// vite.config.js
const { join } = require("path");

module.exports = {
  resolve: {
    alias: {
      "@/": join(__dirname, "src/"),
    },
  },
};

When using the ESM format, you can use export default, but you can't use __dirname. You need to name your configuration file vite.config.mjs and you need to construct your path alias using new URL and import.meta.url:

// vite.config.mjs
import { defineConfig } from "vite";

export default defineConfig({
  resolve: {
    alias: {
      "@/": new URL("src/", import.meta.url).pathname,
    },
  },
});

@mrose
Copy link

mrose commented Nov 14, 2024

@ericdmachado curious is the fix working for you? thanks

@lazarv
Copy link
Owner

lazarv commented Nov 14, 2024

@mrose is it working for you? The Photos example uses this at https://github.com/lazarv/react-server/blob/main/examples/photos/vite.config.mjs. But let me know if there's still an issue in this topic that needs my attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants