epicweb-dev / cachified

🤑 wrap virtually everything that can store by key to act as cache with ttl/max-age, stale-while-validate, parallel fetch protection and type-safety support
MIT License
916 stars 26 forks source link

Cachified doesn't work with Astro.build #27

Closed charliematters closed 1 year ago

charliematters commented 1 year ago

I'd like to use cachified in an Astro app, which I believe has its own compiler. It works fine when running it in dev mode, but when you try to build and run it, there is an import error. I can resolve that by adding "type": "module" to the cachified package.json. From my experience of getting CJS and ESM to work together, I know that it gets very complicated very quickly, but I'm not sure where else to raise this.

The error is:

(node:61186) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/charlie/dev/astro-cachified/node_modules/cachified/dist/index.js:4

...

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)

You can reproduce this by installing the default astro project, and then import and use cachified

Xiphe commented 1 year ago

Thanks for reporting this. I hoped that exposing the module export would be enough. Will look into this in the next days.

charliematters commented 1 year ago

Thanks for this. I agree, it looks like the package.json has the right stuff, but getting help from Astro didn't seem very fast

Xiphe commented 1 year ago

I know that feel. Will see what I can do

Xiphe commented 1 year ago

Hey @charliematters I've never worked with astro and by "installing the default astro project" (I did npm create astro@latest) I don't see where I would want to use cachified. Could you provide me a repo where I could see what you're trying to do or more detailed steps what I need to do to reproduce the issue?

Nevermind... the issue seems to not be astro related but you can not load cachified in any "type": "module" project 😭

Xiphe commented 1 year ago

Could you check if the issue persists with v3.0.2?

charliematters commented 1 year ago

That works great - thanks!

Xiphe commented 1 year ago

thanks for making me aware of this 👍