aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.34k stars 50 forks source link

Vite fails to find `index' files on aliased paths when resolving Web Worker modules #145

Closed haakonjackfloat closed 4 months ago

haakonjackfloat commented 4 months ago

Given a directory structure:

app/
├─ moduleA/
│  └─ index.ts
└─ workerModule.ts

and a TS alias configured so that @app points to the top level app directory.

Say workerModule is a WebWorker which is imported with the workerModule?worker syntax, and workerModule has an import like so:

import { something } from '@app/moduleA'

Vite will give an error like:

[vite:worker] [vite]: Rollup failed to resolve import "@app/moduleA" from "app/workerModule.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
file: app/workerModule.ts?worker

The error will be resolved if we change the import like so:

import { something } from '@app/moduleA/index'

// OR

import { something } from '../moduleA'

Versions:

vite@5.3.3 vite-tsconfig-paths@4.3.2

haakonjackfloat commented 4 months ago

I realize now that the issue is because Vite plugins are not automatically applied to Web-Workers.

Resolved the issue by updating vite.config with:

{
  worker: {
    plugins: [
      tsconfigPaths(),
    ],
  },
}