Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 03d8512

Browse files
committed
readme prep for 0.6
1 parent a14ed75 commit 03d8512

File tree

1 file changed

+105
-45
lines changed

1 file changed

+105
-45
lines changed

README.md

Lines changed: 105 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Dynamically loads ES6 modules in NodeJS and current browsers.
55
* Implemented exactly to the April 27 2014 ES6 specification draft.
66
* Provides an asynchronous loader (`System.import`) to [dynamically load ES6 modules](#basic-use).
77
* Uses [Traceur](https://github.com/google/traceur-compiler) for compiling ES6 modules and syntax into ES5 in the browser with source map support.
8-
* Fully supports [ES6 circular references and bindings](#circular-references-&-bindings).
8+
* Fully supports [ES6 circular references and bindings](#circular-references--bindings).
99
* Polyfills ES6 Promises in the browser with a bundled [es6-promise](https://github.com/jakearchibald/es6-promise) implementation.
1010
* [Compatible with NodeJS](#nodejs-support) allowing for server-side module loading and tracing extensions.
1111
* Supports ES6 module loading in IE9+, and dynamic module formats in IE8+.
@@ -23,7 +23,7 @@ _Note the ES6 module specification is still in draft, and subject to change._
2323

2424
### Basic Use
2525

26-
Download both [es6-module-loader.js](https://raw.githubusercontent.com/ModuleLoader/es6-module-loader/v0.5.4/dist/es6-module-loader.js) and [traceur.js](https://raw.githubusercontent.com/google/traceur-compiler/[email protected].32/bin/traceur.js) into the same folder.
26+
Download both [es6-module-loader.js](https://raw.githubusercontent.com/ModuleLoader/es6-module-loader/v0.6.0/dist/es6-module-loader.js) and [traceur.js](https://raw.githubusercontent.com/google/traceur-compiler/[email protected].41/bin/traceur.js) into the same folder.
2727

2828
If using ES6 syntax (optional), include `traceur.js` in the page first then include `es6-module-loader.js`:
2929

@@ -63,6 +63,62 @@ _Because the loader is promise-based we need to add a catch handler in order to
6363

6464
[Read the wiki on overview of ES6 modules and syntax](https://github.com/ModuleLoader/es6-module-loader/wiki/A-Brief-ES6-Modules-Overview).
6565

66+
### Module Tag
67+
68+
A simple analog to the module tag is provided with:
69+
70+
```html
71+
<script type="module">
72+
// loads the 'q' export from 'mymodule.js' in the same path as the page
73+
import { q } from 'mymodule';
74+
75+
new q(); // -> 'this is an es6 class!'
76+
</script>
77+
```
78+
79+
Ideally this should be based on polyfilling the `<module>` tag, as `<script type="module">` is not in the spec.
80+
81+
As such this approach is not really suitable for anything more than experimentation.
82+
83+
See an overview of the specification module tag features here - https://github.com/dherman/web-modules/blob/master/module-tag/explainer.md.
84+
85+
### baseURL
86+
87+
All modules are loaded relative to the `baseURL`, which by default is set to the current page path.
88+
89+
We can alter this with:
90+
91+
```javascript
92+
System.baseURL = '/js/lib';
93+
System.import('module'); // now loads "/js/lib/module.js"
94+
```
95+
96+
### Paths Implementation
97+
98+
_Note: This is a specification under discussion and not confirmed. This implementation will likely change._
99+
100+
The System loader provides paths rules used by the standard `locate` function.
101+
102+
For example, we might want to load `jquery` from a CDN location. For this we can provide a paths rule:
103+
104+
```javascript
105+
System.paths['jquery'] = '//code.jquery.com/jquery-1.10.2.min.js';
106+
System.import('jquery').then(function($) {
107+
// ...
108+
});
109+
```
110+
111+
Any reference to `jquery` in other modules will also use this same version.
112+
113+
It is also possible to define wildcard paths rules. The most specific rule will be used:
114+
115+
```javascript
116+
System.paths['lodash/*'] = '/js/lodash/*.js'
117+
System.import('lodash/map').then(function(map) {
118+
// ...
119+
});
120+
```
121+
66122
### Circular References & Bindings
67123

68124
Circular references and live bindings are fully supported identically to ES6 in this polyfill.
@@ -102,67 +158,63 @@ odd.js
102158
});
103159
```
104160

105-
### Extending the Loader
161+
### Moving to Production
106162

107-
The loader in its default state provides only ES6 loading.
163+
When in production, it is not suitable to load ES6 modules and syntax in the browser.
108164

109-
We can extend it to load AMD, CommonJS and global scripts as well as various other custom functionality through the loader hooks.
165+
There is a `modules=instantiate` build output in Traceur that can be used with the ES6 Module Loader, provided it has the [System.register extension](https://github.com/systemjs/systemjs/blob/master/lib/extension-register.js)
166+
from [SystemJS](https://github.com/systemjs/systemjs).
110167

111-
[Read the wiki on extending the loader here](https://github.com/ModuleLoader/es6-module-loader/wiki/Extending-the-ES6-Loader).
168+
The benefit of this output is that it provides full support for circular references and live module bindings.
112169

113-
### Module Tag
170+
Alternatively, Traceur can also output `amd` or `cjs` as well.
114171

115-
A simple analog to the module tag is provided with:
172+
A basic example of using this extension with a build would be the following:
116173

117-
```html
118-
<script type="module">
119-
// loads the 'q' export from 'mymodule.js' in the same path as the page
120-
import { q } from 'mymodule';
174+
#### Building all files into one bundle
121175

122-
new q(); // -> 'this is an es6 class!'
123-
</script>
124-
```
125-
126-
Ideally this should be based on polyfilling the `<module>` tag, as `<script type="module">` is not in the spec.
127-
128-
As such this approach is not really suitable for anything more than experimentation.
176+
1. Build all ES6 modules into ES5 System.register form:
129177

130-
See an overview of the specification module tag features here - https://github.com/dherman/web-modules/blob/master/module-tag/explainer.md.
178+
```
179+
traceur --out app-build.js app/app.js --modules=instantiate
180+
```
131181

132-
### Paths Implementation
182+
2. Load [`traceur-runtime.js`](https://raw.githubusercontent.com/google/traceur-compiler/[email protected]/bin/traceur.js), `es6-module-loader.js` and then apply the register extension before doing the import or loading the bundle as a script:
133183

134-
_Note: This is a specification under discussion and not confirmed. This implementation will likely change._
184+
```html
185+
<script src="traceur-runtime.js"></script>
186+
<script src="es6-module-loader.js"></script>
187+
<script>
188+
/*
189+
* This should be a separate external script
190+
* Register function is included from https://github.com/systemjs/systemjs/blob/master/lib/extension-register.js
191+
*/
192+
function register(loader) {
193+
// ...
194+
}
135195
136-
The System loader provides paths rules used by the standard `locate` function.
196+
// this needs to be added to apply the extension
197+
register(System);
198+
</script>
137199

138-
For example, we might want to load `jquery` from a CDN location. For this we can provide a paths rule:
200+
<!-- now include the bundle -->
201+
<script src="app-build.js"></script>
139202

140-
```javascript
141-
System.paths['jquery'] = '//code.jquery.com/jquery-1.10.2.min.js';
142-
System.import('jquery').then(function($) {
143-
// ...
144-
});
145-
```
203+
<!-- now we can import and get modules from the bundle -->
204+
<script>
205+
System.import('app/app');
206+
</script>
207+
```
146208

147-
Any reference to `jquery` in other modules will also use this same version.
209+
#### Building into separate files
148210

149-
It is also possible to define wildcard paths rules. The most specific rule will be used:
211+
We can also build separate files with:
150212

151-
```javascript
152-
System.paths['lodash/*'] = '/js/lodash/*.js'
153-
System.import('lodash/map').then(function(map) {
154-
// ...
155-
});
213+
```
214+
traceur --dir app app-build --modules=instantiate
156215
```
157216

158-
<a name="moving-to-production">
159-
### Moving to Production
160-
161-
When in production, one wouldn't want to load ES6 modules and syntax in the browser. Rather the modules would be built into ES5 and AMD to be loaded.
162-
163-
Additionally, suitable bundling would need to be used.
164-
165-
Traceur provides build outputs that can be loaded with extensions to the module loader including AMD, CommonJS and a System.register build.
217+
With the above, we can load from the separate files identical to loading ES6.
166218

167219
### NodeJS Usage
168220

@@ -192,6 +244,14 @@ Running the application:
192244
NodeJS test
193245
```
194246

247+
### Extending the Loader
248+
249+
The loader in its default state provides only ES6 loading.
250+
251+
We can extend it to load AMD, CommonJS and global scripts as well as various other custom functionality through the loader hooks.
252+
253+
[Read the wiki on extending the loader here](https://github.com/ModuleLoader/es6-module-loader/wiki/Extending-the-ES6-Loader).
254+
195255
### Specification Notes
196256

197257
See the source of https://github.com/ModuleLoader/es6-module-loader/blob/master/lib/es6-module-loader.js, which contains comments detailing the exact specification notes and design decisions.

0 commit comments

Comments
 (0)