Skip to content

Commit 0000283

Browse files
add info about data imports (#1692)
Co-authored-by: Bartek Iwańczuk <[email protected]>
1 parent 873ec71 commit 0000283

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

runtime/fundamentals/modules.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,43 @@ This is the only import attribute type currently supported in Deno. Support for
8080
currently waiting on the
8181
[Module Harmony proposal](https://github.com/whatwg/html/issues/9444).
8282

83+
## Data URL imports
84+
85+
Deno supports importing of data URLs, which allows you to import content that
86+
isn't in a separate file. This is useful for testing, prototyping, or when you
87+
need to programmatically generate modules.
88+
89+
You can create modules on the fly using the `data:` URL scheme:
90+
91+
```ts
92+
// Import a simple JavaScript module from a data URL
93+
import * as module from "data:application/javascript;base64,ZXhwb3J0IGNvbnN0IG1lc3NhZ2UgPSAiSGVsbG8gZnJvbSBkYXRhIFVSTCI7";
94+
console.log(module.message); // Outputs: Hello from data URL
95+
96+
// You can also use the non-base64 format
97+
const plainModule = await import(
98+
"data:application/javascript,export function greet() { return 'Hi there!'; }"
99+
);
100+
console.log(plainModule.greet()); // Outputs: Hi there!
101+
102+
// A simpler example with text content
103+
const textModule = await import(
104+
"data:text/plain,export default 'This is plain text'"
105+
);
106+
console.log(textModule.default); // Outputs: This is plain text
107+
```
108+
109+
The data URL format follows this pattern:
110+
111+
```sh
112+
data:[<media type>][;base64],<data>
113+
```
114+
115+
For JavaScript modules, use `application/javascript` as the media type.
116+
TypeScript is also supported with `application/typescript`. This feature is
117+
particularly useful for testing modules in isolation and creeating mock modules
118+
during tests.
119+
83120
## Importing third party modules and libraries
84121

85122
When working with third-party modules in Deno, use the same `import` syntax as

0 commit comments

Comments
 (0)