Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit b5843d7

Browse files
author
Andy
authored
Merge pull request #329 from Microsoft/more_umd
Add description of UMD modules to `Modules.md` in addition to `Writin…
2 parents 819891d + a574809 commit b5843d7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pages/Modules.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,35 @@ import x, {y} from "hot-new-module";
517517
x(y);
518518
```
519519

520+
### UMD modules
521+
522+
Some libraries are designed to be used in many module loaders, or with no module loading (global variables).
523+
These are known as [UMD](https://github.com/umdjs/umd) or [Isomorphic](http://isomorphic.net) modules.
524+
These libraries can be accessed through either an import or a global variable.
525+
For example:
526+
527+
##### math-lib.d.ts
528+
529+
```ts
530+
export const isPrime(x: number): boolean;'
531+
export as namespace mathLib;
532+
```
533+
534+
The library can then be used as an import within modules:
535+
536+
```ts
537+
import { isPrime } from "math-lib";
538+
isPrime(2);
539+
mathLib.isPrime(2); // ERROR: can't use the global definition from inside a module
540+
```
541+
542+
It can also be used as a global variable, but only inside of a script.
543+
(A script is a file with no imports or exports.)
544+
545+
```ts
546+
mathLib.isPrime(2);
547+
```
548+
520549
# Guidance for structuring modules
521550

522551
## Export as close to top-level as possible

0 commit comments

Comments
 (0)