Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #688. The tricky part here is
FileAttachment
. Unlike everything else, we can’t strictly know statically which method you’re going to call to load the file, and therefore which library we need to load. The association is:FileAttachment.csv
npm:d3-dsv
FileAttachment.tsv
npm:d3-dsv
FileAttachment.arrow
npm:apache-arrow
FileAttachment.parquet
npm:apache-arrow
andnpm:parquet-wasm
FileAttachment.sqlite
npm:@observablehq/sqlite
FileAttachment.xlsx
npm:@observablehq/xlsx
FileAttachment.zip
npm:@observablehq/zip
It’s not the end of the world if we don’t preload
npm:d3-dsv
for example (at least until we want to support self-hosting of libraries #20), but it’s still nice to preload it if we know you’re going to load a CSV file. But if we don’t detect that you neednpm:@observablehq/zip
for example, then your built site will be broken, because that library doesn’t actually exist (yet) on npm — it’s built locally and self-hosted. (We do plan on publishing it to npm eventually, but it’s a little tricky as it depends on releasing a new version of@observablehq/stdlib
and I’m not in a rush to do a major version bump there until we’re a little farther along in bringing Observable notebooks up to parity with Observable Framework.)So, here are some options:
We could always include
sqlite
,xlsx
, andzip
. That’s not ideal but it ensures things work. But we wouldn’t want to preload these always, so it means we also don’t have any library preloading based on what files you’re using, which is a slight bummer.We infer which libraries you need based on the file names. The file names are statically analyzable so this should work pretty well as long as the file extensions strongly imply how you will load them. For example we assume that a
.csv
file will be loaded withFileAttachment.csv
and hence needsnpm:d3-dsv
. I think this would work pretty well, although for SQLite at least there isn’t a strong convention around the file extension. I guess we would assume that.db
and.sqlite
(and some others) is for SQLite? This could also result in a false positive for example if you load a.csv
file as text, but that wouldn’t be terrible.We infer which libraries you need based on the methods you call. But, you can declare a
FileAttachment
in one place and then call the method in another place, so we can’t simply look for instances of e.g.FileAttachment(name).csv(…)
. I think this basically means that if you have any member expression of the formobject.csv(…)
then we have to suppose theobject
might be a file attachment and hence you neednpm:d3-dsv
.I think I’m inclined to take option (2) because it’s the most predictable, and we can just document what our expectations are around file extensions?