Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use browser-resolve to ignore fs inlining #523

Merged
merged 1 commit into from
Jan 14, 2018

Conversation

fathyb
Copy link
Contributor

@fathyb fathyb commented Jan 9, 2018

browser-resolve has an option to skip fs and other modules. This PR makes the fs visitor check if fs is ignored before processing it.

This makes aws-sdk-js usable amongst other libraries using this feature.

Fixes #496 and maybe #474

Copy link

@olivoil olivoil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the PR in a project importing aws-amplify which depends on aws-sdk-js. Seems to fix the issue for me. Thanks @fathyb !

@devongovett
Copy link
Member

Cool! another option would be to just not error on non-static file paths (e.g. with variables), and just skip inlining. The only thing I'd be concerned about would be that it might reduce debuggability for legit coding errors. thoughts?

@fathyb
Copy link
Contributor Author

fathyb commented Jan 12, 2018

I agree on the debuggability point, it could be frustrating to not understand why it does not inline. What about passing an additional option to fs.readFileSync, something like :

fs.readFileSync(Math.random().toString(), {
  parcel: false
})

That way it can be ignored on node.js and can hint the visitor if the call wasn't meant to be parsed. It might not be applicable on certain use case tho.

@devongovett
Copy link
Member

I don't think we should be changing the API. Also that won't work for existing node_modules that are already broken with Parcel.

@fathyb
Copy link
Contributor Author

fathyb commented Jan 13, 2018

Alright, then I'd recommend to display a warning when it happens. #448 could be useful here to silent warnings on some files.

@SheetJSDev
Copy link

Encountered the Could not statically evaluate fs call error while trying to import the xlsx module. It uses the browser field of package.json to suppress for browserify.

another option would be to just not error on non-static file paths (e.g. with variables), and just skip inlining.

@devongovett even if a specific fs call could be statically evaluated, this patch is still important. false entries in package.json browser field unambiguously communicate a desire to suppress behavior in a browser-based deployment. The general expectation from the perspective of library authors is that require("fs") would either error or evaluate to {} if suppressed.

Considering prior art, neither browserify nor webpack attach special meaning to the fs module; the filesystem meaning is conferred by modules like brfs. You can manually verify this with a basic fs.readFileSync example in browserify and webpack.

@fathyb I personally like the concise expression

let ignore = (((asset||{}).package||{}).browser||{}).fs === false

but the explicit form is probably clearer.

@devongovett devongovett merged commit bd9fd9f into parcel-bundler:master Jan 14, 2018
@sterproton
Copy link

sterproton commented Dec 4, 2018

i have problem that after use browser-resolve and it skip fs module
but i still want to use the fs module and it just become {}?? and after parcel build, the prod code throw error because fs module become {}

i use parcel to bundler my node.js server code

maxlath added a commit to inventaire/handlebars.js that referenced this pull request Oct 5, 2020
to work around parcel-bundler "Cannot statically evaluate fs argument" issue
see parcel-bundler/parcel#523 (comment)
maxlath added a commit to inventaire/inventaire-client that referenced this pull request Oct 6, 2020
to prevent Parcel to crash with:
"node_modules/handlebars/lib/index.js:18:39: Cannot statically evaluate fs argument"
due to Handlebars use of 'fs.readFileSync', when used without precompile:
somehow, Parcel doesn't figure-out that we won't need this piece of code
and thus tries to inline the code to prevent to rely on fs.readFileSync, which
isn't available in the browser

See parcel-bundler/parcel#496
and parcel-bundler/parcel#523
maxlath added a commit to inventaire/inventaire-client that referenced this pull request Oct 6, 2020
to prevent Parcel to crash with:
"node_modules/handlebars/lib/index.js:18:39: Cannot statically evaluate fs argument"
due to Handlebars use of 'fs.readFileSync', when used without precompile:
somehow, Parcel doesn't figure-out that we won't need this piece of code
and thus tries to inline the code to prevent to rely on fs.readFileSync, which
isn't available in the browser

See parcel-bundler/parcel#496
and parcel-bundler/parcel#523
maxlath added a commit to inventaire/inventaire-client that referenced this pull request Oct 9, 2020
to prevent Parcel to crash with:
"node_modules/handlebars/lib/index.js:18:39: Cannot statically evaluate fs argument"
due to Handlebars use of 'fs.readFileSync', when used without precompile:
somehow, Parcel doesn't figure-out that we won't need this piece of code
and thus tries to inline the code to prevent to rely on fs.readFileSync, which
isn't available in the browser

See parcel-bundler/parcel#496
and parcel-bundler/parcel#523
maxlath added a commit to inventaire/inventaire-client that referenced this pull request Oct 9, 2020
to prevent Parcel to crash with:
"node_modules/handlebars/lib/index.js:18:39: Cannot statically evaluate fs argument"
due to Handlebars use of 'fs.readFileSync', when used without precompile:
somehow, Parcel doesn't figure-out that we won't need this piece of code
and thus tries to inline the code to prevent to rely on fs.readFileSync, which
isn't available in the browser

See parcel-bundler/parcel#496
and parcel-bundler/parcel#523
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Could not statically evaluate fs call
5 participants