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 importing multiple different exports with the same name from different modules (namespaced) #140

Closed
elbojoloco opened this issue Aug 17, 2023 · 3 comments

Comments

@elbojoloco
Copy link

elbojoloco commented Aug 17, 2023

This error occurred only after switching from the regular react plugin to the react-swc plugin. The exact error is:
Uncaught SyntaxError: The requested module '/src/types/devices.ts?t=1692279660309' does not provide an export named 'Device' (at AddDevicePopup.tsx:11:10)

In my scenario I have a React component that imports a type and a styled component with the same name:

AddDevicePopup.tsx

import { Device } from '@types/devices';
import * as S from "./AddDevicePopup.styled"; // includes S.Device

devices.ts

export type Device = {};

AddDevicePopup.styled

export const Device = styled.button;

It seems that even though the styled component Device is imported under the namespace S it somehow collides with the exported type Device and breaks my app. When renaming either the exported type or the styled component it works fine. So the workaround is easy but this seems like an issue that should resolved.

@ArnaudBarre
Copy link
Member

ArnaudBarre commented Aug 20, 2023

My guess from the code snippets is the file doesn't contains enough information to decide if import { Device } from '@types/devices'; is a type import or not and Babel default to type import and SWC to runtime import which fails.

A good solution to avoid that is to enable the new compiler flag verbatimModuleSyntax which forces you to be explicit on type imports which simplify the transpilation by other tools that can safely drop import statements

@ArnaudBarre ArnaudBarre closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
@elbojoloco
Copy link
Author

Sorry for the late response, got lost in the notifications.

So you suggest I start using the following syntax to import types to avoid these edge cases?

import type { Device } from '@types/devices';

@ArnaudBarre
Copy link
Member

Yes this is the simplest solution.
If it's used in the file, this is probably an SWC bug. If this just re-exported, this is best to change the other importers to import directly from @types/devices. This is better for Vite HMR because when you edit a file the is more explicit which which modules depends on it.

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

2 participants