About imports
Hi, I'm new to Deno and looking for answers regarding imports.
1.
2. The documentation at https://deno.land/std@0.224.0 suggests that I should avoid usingHowever, since version 1.5, Deno does tree-shaking, making this seem irrelevant. Is this correct?
3. Should I avoid or update imports from
4. The documentation mentions the
5. Is there a benefit to importing modules directly in the source code rather than using an import map in general?
deno add @org/package
registers the package at deno.jsonc.imports
even if import_maps.json
is present. Can this be configured, or is Deno moving away from placing import aliases in a separate file? On one hand, having a separate file seems maintainable; on the other, the import map is kind of a Deno "feature."
2. The documentation at https://deno.land/std@0.224.0 suggests that I should avoid using
*
when importing only one function (import * as fs from "https://deno.land/std@0.224.0/fs/mod.ts";
). 3. Should I avoid or update imports from
deno.land/
and use jsr
packages exclusively? The latter seems friendlier to me because I find it cumbersome to look up which file I need within a package (sometimes it's specific like .../assert.ts
, sometimes .../mod.ts
). Are there any important packages/code that aren't on jsr
but are present on deno.land
?
4. The documentation mentions the
$
convention for std (only), yet I didn't find any packages, even official ones, using it. Is my iteration on $
confusing? ("$assert": "jsr:@std/assert@^1.0.0"
)
5. Is there a benefit to importing modules directly in the source code rather than using an import map in general?
2 Replies
2. Where did you read that Deno does treeshaking? Anyway, even if it does, Deno favors explicit, clear code. When you read
import *...
, you can't tell what's being imported and used, whereas with import { delay } from...
, you know what's being imported and used. But you're still free to use *
if you like
3. Deno team recommends using JSR, avoiding URL imports and migrating from URL to JSR. But the community is still quite splitted regarding JSR. Many prefer URL imports. So it's up to you to compare the pros and cons and decide which way to go. It's a long discussion.
I believe there are packages that are only on deno.land
and not on JSR, but the Deno team is pushing everyone to migrate their packages to JSR. The latest versions of the Deno Standard Library are available only on JSR, although you can still import the latest version via jsdelivr
4. Where did you read that? $
was a common pattern to be used with import maps, but I'm not sure if it still holds true after JSR was introduced
5. Yes, but I think the advantage is generally not worth it.
For libraries, if you don't use an import map, it's easier to distribute your library because users don't need to care about the import map. An app can't load multiple import maps, so users of your library who already use an import map will have some trouble.
For applications, if you import the module directly in the source code, it's easier to read your code because you know where everything is coming from without looking at another file. Also, your code becomes "self contained" in the sense that each file has everything needed to run. It doesn't need anything else. It's a single-file application- "deno does tree shaking" : No, it doesnt, i misread a post, it was about
deno bundle
and not Deno
- The $
convention is mentioned here as a code comment
- I missed the last section that talks about Import Maps are for Applications
Im thankful for sharing me answers and details!