lilnasy / gratelets

A collection of purpose-built, small, third-party integrations for Astro.
The Unlicense
105 stars 6 forks source link

Development error: Could not resolve "astro-dynamic-import:internal" #96

Closed RATIU5 closed 6 months ago

RATIU5 commented 6 months ago

The Problem

I am using the astro-dynamic-import library. My project used to work in development. I built, and then I restarted my development server and I got the following error:

✘ [ERROR] Could not resolve "astro-dynamic-import:internal"

    node_modules/.pnpm/astro-dynamic-import@1.1.0_astro@4.7.0/node_modules/astro-dynamic-import/runtime/virtual-module.ts:13:52:
      13 │ import { srcDirName, lookupMap as _lookupMap } from "astro-dynamic-import:internal"
         ╵                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "astro-dynamic-import:internal" as external to exclude it from the bundle,
  which will remove this error and leave the unresolved path in the bundle.

08:00:31 [ERROR] [UnhandledRejection] Astro detected an unhandled rejection. Here's the stack trace:
Error: Build failed with 1 error:
node_modules/.pnpm/astro-dynamic-import@1.1.0_astro@4.7.0/node_modules/astro-dynamic-import/runtime/virtual-module.ts:13:52: ERROR: Could not resolve "astro-dynamic-import:internal"

Replication

If you need to replicate it, here is my StackBlitz project. Simply run npm install && npm run dev to see the error. An item to note:

Versions

Astro version: ^4.7.0 astro-dynamic-import version: ^1.1.0 pnpm version: 8.15.1

About the Error

I did some extensive testing and the only correlation I found was once I build the project and then run the dev server, this error triggers. For me, it happens once, and then I reload my page, and most things return to normal. Tailwind seems to fail whenever I reload with this error occurring. I tried deleting the dist folder, that did nothing.

I was able to avoid the error when I reverted back to an old commit, then reverted back to the new commit, and then ran the dev server without building the project. Oh, and the project builds successfully. I could not replicate this fix on the sandbox.

evertonadame commented 6 months ago

Same for me, in dev mode the error is triggered but when the project is build successfully the error does not appear

evertonadame commented 6 months ago

I believe it's something in vite when optimizing dependencies in dev mode

try this

code

export default defineConfig({
  integrations: [dynamicImport()],
    vite: {
      optimizeDeps: {
        exclude: ['astro-dynamic-import:internal'],
    },
 },
});

i guess that a good call should be using the octal numbers ids and virtual name for the imports like the doc says https://vitejs.dev/guide/api-plugin

const virtualModuleId = 'virtual:my-module' const resolvedVirtualModuleId = '\0' + virtualModuleId

lilnasy commented 6 months ago

Thanks for the reproduction. I see the error on startup of the dev server.

I believe it's something in vite when optimizing dependencies in dev mode

That's a good observation. I notice the error on page request in addition to startup, which is when vite optimization takes place, but this might be helpful in pointing in the right direction.

i guess that a good call should be using the octal numbers ids and virtual name for the imports like the doc says

Yeah, that's a good idea. It didn't make a difference for this error, however.

Tailwind seems to fail whenever I reload with this error occurring.

Do tailwind styles remain broken after page reloads?

RATIU5 commented 6 months ago

Do tailwind styles remain broken after page reloads?

Yes even after reloading the styles remained broken. Not sure if this actually has anything to do with this error, just something I noticed.

try this

That seemed to make the error go away, even after I built and then started the dev server again. Not sure how long it will last or if this is the best solution, but it works for the time being. Thanks.

lilnasy commented 6 months ago

I don't understand the root cause, but the fix is safe, it will not have any side-effects.

@evertonadame Do you want to open a PR? It can go here, inside the updateConfig() call:

https://github.com/lilnasy/gratelets/blob/8615d2a20a9489aedf6248de67af355fc2dff24e/packages/dynamic-import/integration.ts#L13-L17

evertonadame commented 6 months ago

Pull request opened in https://github.com/lilnasy/gratelets/pull/97