Closed tex0l closed 4 months ago
Start a new pull request in StackBlitz Codeflow.
The bug is caused by the Vite configuration not handling TypeScript path aliases correctly. To solve this, we need to update the Vite configuration to include a plugin that resolves these aliases. Additionally, we need to ensure that the tsconfig.json
file is correctly configured with the necessary path aliases.
The bug is caused by the Vite configuration not recognizing the TypeScript path aliases defined in the tsconfig.json
file. This leads to an error when trying to import resources using these aliases. The vite.config.ts
file does not include any configuration to handle these path aliases, which is why the import fails.
To resolve the issue, follow these steps:
Install the vite-tsconfig-paths
Plugin:
This plugin will allow Vite to recognize and resolve the TypeScript path aliases defined in the tsconfig.json
file.
npm install vite-tsconfig-paths --save-dev
# or
yarn add vite-tsconfig-paths --dev
Update vite.config.ts
:
Modify the vite.config.ts
file to include the vite-tsconfig-paths
plugin.
import { defineConfig } from "vite";
import vitePluginArraybuffer from "./src/main";
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [
tsconfigPaths(),
vitePluginArraybuffer()
],
});
Ensure tsconfig.json
is Correctly Configured:
Make sure that the tsconfig.json
file includes the baseUrl
and paths
properties to define the path aliases.
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"strict": true,
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
},
"include": ["src"]
}
tsconfig.json
and vite.config.ts
files.tsconfig.json
file.pnpm install && pnpm start
.ENOENT: no such file or directory, open '~/resources/my-resource.png'
.By following the steps above, you should be able to replicate the bug. After applying the suggested solution, the import should work correctly without any errors.
This solution ensures that Vite can resolve TypeScript path aliases, thereby fixing the import issue described in the ticket.
Click here to create a Pull Request with the proposed solution
Files used for this task:
Astro has upgraded their vite so a few APIs have changed. I will fix it tomorrow
Fixed on vite-plugin-arraybuffer@0.0.8
Describe the bug I have a resource I want to import as
arraybufer
which is located within the directory./src/resources
from my base directory, which is aliased using thetsconfig.json
configuration like it is mandated by Astro:I import it from a file with:
To Reproduce See the stackblitz here: https://stackblitz.com/edit/github-5tpg4s?file=src%2Fpages%2Findex.astro
Try to
pnpm install && pnpm start
, it'll fail with the errorENOENT: no such file or directory, open '~/resources/my-resource.png'
If you roll back to 0.0.2, it'll work.
Expected behavior I would expect the alias to be respected like in 0.0.2.
Additional context Somehow, it works with Astro 3 with vite-plugin-arraybuffer 0.0.7, but not with Astro 4.