Open akdor1154 opened 3 years ago
There is some weird behavior going on since the changes in 3.1.0. I'm having similar issues with react-cosmos: https://github.com/react-cosmos/react-cosmos/issues/1320
I've tried to look into this (I really want to make it work with react-cosmos) but sadly this seems like correct behavior - we are telling snowpack to just not touch the import when transpiling the sources but it would be natural to expect that we will provide this dependency on runtime (I've recreated the issue and the error happens when we actually request the dependency from the dev server).
Let's look at skypack for the antlr4
and similar dependency that I'm fighting with:
https://cdn.skypack.dev/-/antlr4@v4.9.2-JifkgbNObkYOM5jksEE6/dist=es2020,mode=imports/optimized/antlr4.js
https://cdn.skypack.dev/-/xmlhttprequest-ssl@v1.6.3-ZnvqsXZiURMNnfCeQd23/dist=es2020,mode=imports/optimized/xmlhttprequest-ssl.js
As you can see we are importing something weird:
import "/error/node:fs?from=antlr4";
The comment explains quite a lot:
/*
* [Package Error] [Package Error] "fs" does not exist. (Imported by "antlr4").
*
* The package "antlr4" depends on this Node.js built-in module.
* Node.js built-in modules (like "fs", "http", etc.) are Node.js-specific, and do
* not exist in non-Node environments like Deno or the web browser. Skypack CDN polyfills
* most of these modules for you automatically, but this one could not be polyfilled.
*
* How to fix:
* - Let the package author know that you'd like to run their package in the browser.
* - Use https://skypack.dev/ to find a web-friendly alternative to find another package.
*/
I've checked by trying the streaming imports and importing it directly as es module. I assume that snowpack should have similar to skypack when using local
option.
So it seems that polyfillNode: true
is actually the correct solution (or dependency maintainer could separate web/node code and provide additional info in exports
in package.json
). If someone smarter than me could confirm (or deny) this I would be happy :)
Bug Report Quick Checklist
Describe the bug
Even though I set
external: ['fs']
, I still get an error when runningsnowpack dev
, and I can't build my project unless I setpolyfillNode: true
. This only appears in 3.1 versions, 3.0 seems to work ok.To Reproduce
We can't fix bugs that we can't see for ourselves. Issues often need to be closed if this section is skipped.
npm init snowpack-app --template @snowpack/app-template-blank
npm add -D snowpack@3.1.2
npm add antlr4@4.9.2
npx snowpack dev
polyfillNode: true
but this isn't really a solution.Expected behavior
No error