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

RollupError: [type] is not exported by [file] - failure for relative imports of types #17538

Closed
7 tasks done
donmccurdy opened this issue Jun 20, 2024 · 2 comments
Closed
7 tasks done

Comments

@donmccurdy
Copy link

Describe the bug

When running Vite in library mode, it fails to build if there are types imported from relative paths. The issue has previously been raised in a discussion, but I believe it's likely to be a bug and would like to report it for visibility here. Thanks!

Reproduction

https://github.com/donmccurdy/2024-06-20_vite_lib_ts

Steps to reproduce

// index.js
export { MyClass, MyType } from "./impl.js";

// impl.js
export class MyClass {}
export type MyType = string;

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M2 Pro
    Memory: 296.55 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.11.0/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm
    pnpm: 9.4.0 - ~/.nvm/versions/node/v20.11.0/bin/pnpm
  Browsers:
    Chrome: 126.0.6478.62
    Edge: 126.0.2592.61
    Safari: 17.5
    Safari Technology Preview: 17.4
  npmPackages:
    vite: ^5.3.1 => 5.3.1

Used Package Manager

yarn

Logs

$ vite build --config vite.config.ts
vite v5.3.1 building for production...
✓ 2 modules transformed.
x Build failed in 16ms
error during build:
src/index.ts (1:18): "MyType" is not exported by "src/impl.ts", imported by "src/index.ts".
file: /Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/src/index.ts:1:18

1: export { MyClass, MyType } from "./impl.js";
                     ^

    at getRollupError (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
    at error (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/parseAst.js:392:42)
    at Module.error (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:13858:16)
    at Module.getVariableForExportName (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:14022:29)
    at Module.includeAllExports (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:14098:37)
    at Graph.includeStatements (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:20033:36)
    at Graph.build (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:19969:14)
    at async file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:20611:13
    at async catchUnfinishedHookActions (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:20119:16)
    at async rollupInternal (file:///Users/donmccurdy/Demos/2024-06-20_vite_lib_ts/node_modules/rollup/dist/es/shared/node-entry.js:20606:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Validations

@hi-ogawa
Copy link
Collaborator

I think this is working intended and Vite requires type import/export to be explicitly flagged as export { type MyType } since each ts file is transpiled independently.

Such invalid usage can be caught when enabling tsconfig's isolatedModules https://vitejs.dev/guide/features.html#isolatedmodules and typescript tells this error:

src/index.ts:1:19 - error TS1205: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'.

1 export { MyClass, MyType } from "./impl.js";
                    ~~~~~~

@donmccurdy
Copy link
Author

Thanks @hi-ogawa! Indeed, that fixed the issue. I wasn't aware of the isolatedModules property.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants