You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 13, 2020. It is now read-only.
@@ -5,7 +5,7 @@ Dynamically loads ES6 modules in NodeJS and current browsers.
5
5
* Implemented exactly to the April 27 2014 ES6 specification draft.
6
6
* Provides an asynchronous loader (`System.import`) to [dynamically load ES6 modules](#basic-use).
7
7
* 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).
9
9
* Polyfills ES6 Promises in the browser with a bundled [es6-promise](https://github.com/jakearchibald/es6-promise) implementation.
10
10
*[Compatible with NodeJS](#nodejs-support) allowing for server-side module loading and tracing extensions.
11
11
* 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._
23
23
24
24
### Basic Use
25
25
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.
27
27
28
28
If using ES6 syntax (optional), include `traceur.js` in the page first then include `es6-module-loader.js`:
29
29
@@ -63,6 +63,62 @@ _Because the loader is promise-based we need to add a catch handler in order to
63
63
64
64
[Read the wiki on overview of ES6 modules and syntax](https://github.com/ModuleLoader/es6-module-loader/wiki/A-Brief-ES6-Modules-Overview).
65
65
66
+
### Module Tag
67
+
68
+
A simple analog to the module tag is provided with:
69
+
70
+
```html
71
+
<scripttype="module">
72
+
// loads the 'q' export from 'mymodule.js' in the same path as the page
73
+
import { q } from'mymodule';
74
+
75
+
newq(); // -> '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:
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
+
66
122
### Circular References & Bindings
67
123
68
124
Circular references and live bindings are fully supported identically to ES6 in this polyfill.
@@ -102,67 +158,63 @@ odd.js
102
158
});
103
159
```
104
160
105
-
### Extending the Loader
161
+
### Moving to Production
106
162
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.
108
164
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).
110
167
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.
112
169
113
-
### Module Tag
170
+
Alternatively, Traceur can also output `amd` or `cjs` as well.
114
171
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:
116
173
117
-
```html
118
-
<scripttype="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
121
175
122
-
newq(); // -> '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:
129
177
130
-
See an overview of the specification module tag features here - https://github.com/dherman/web-modules/blob/master/module-tag/explainer.md.
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:
133
183
134
-
_Note: This is a specification under discussion and not confirmed. This implementation will likely change._
184
+
```html
185
+
<scriptsrc="traceur-runtime.js"></script>
186
+
<scriptsrc="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
+
functionregister(loader) {
193
+
// ...
194
+
}
135
195
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>
137
199
138
-
For example, we might want to load `jquery` from a CDN location. For this we can provide a paths rule:
<!-- now we can import and get modules from the bundle -->
204
+
<script>
205
+
System.import('app/app');
206
+
</script>
207
+
```
146
208
147
-
Any reference to `jquery` in other modules will also use this same version.
209
+
#### Building into separate files
148
210
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:
150
212
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
156
215
```
157
216
158
-
<aname="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.
166
218
167
219
### NodeJS Usage
168
220
@@ -192,6 +244,14 @@ Running the application:
192
244
NodeJS test
193
245
```
194
246
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
+
195
255
### Specification Notes
196
256
197
257
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