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.
Dynamically loads ES6 modules in browsers and [NodeJS](#nodejs-usage) supporting custom module resolution and the loading of existing module formats from ES6 modules via a hookable pipeline.
3
+
Dynamically loads ES6 modules in browsers and [NodeJS](#nodejs-usage) with support for loading existing and custom module formats through loader hooks.
4
+
5
+
This project implements dynamic module loading through `System` exactly to the previous ES6-specified loader API at [2014-08-24 ES6 Specification Draft Rev 27, Section 15](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts#august_24_2014_draft_rev_27) and will continue to track this API as it is re-drafted as a browser specification (currently most likely to be at https://github.com/whatwg/loader).
4
6
5
7
* Provides an asynchronous loader (`System.import`) to [dynamically load ES6 modules](#basic-use).
6
8
* Uses [Traceur](https://github.com/google/traceur-compiler) for compiling ES6 modules and syntax into ES5 in the browser with source map support.
7
-
* Fully supports [ES6 circular references and bindings](#circular-references--bindings).
9
+
* Fully supports [ES6 circular references and live bindings](#circular-references--bindings).
8
10
* Polyfills ES6 Promises in the browser with an optionally bundled ES6 promise implementation.
9
11
* Supports ES6 module loading in IE8+. Other ES6 features only supported by Traceur in IE9+.
10
12
* The complete combined polyfill, including ES6 promises, comes to 9KB minified and gzipped, making it suitable for production use, provided that modules are [built into ES5 making them independent of Traceur](#moving-to-production).
@@ -17,14 +19,6 @@ For an example of a universal module loader based on this polyfill for loading A
17
19
18
20
_The current version is tested against **[Traceur 0.0.79](https://github.com/google/traceur-compiler/tree/0.0.79)**._
19
21
20
-
### Background
21
-
22
-
The ES6 specification defines a module system in JavaScript using `import` and `export` syntax. This syntax will be supported within the `<script type="module">` tag in the browser. For dynamically loading modules, a dynamic browser module loader was initially proposed for the ES6 module specification, and is now being developed as a browser specification.
23
-
24
-
The proposed dynamic loader API uses a global loader at `window.System` for importing modules dynamically and allows customisation of the ES6 lookup process including loading from other module formats.
25
-
26
-
This project implements the dynamic module loading through `System` exactly to the previous ES6-specified API at [2014-08-24 ES6 Specification Draft Rev 27](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts#august_24_2014_draft_rev_27), Section 15, and will continue to track changes as it is re-drafted as a browser specification (currently most likely to be at https://github.com/whatwg/loader).
27
-
28
22
### Basic Use
29
23
30
24
Download both [es6-module-loader.js](https://raw.githubusercontent.com/ModuleLoader/es6-module-loader/v0.11.0/dist/es6-module-loader.js) and traceur.js into the same folder.
@@ -59,7 +53,7 @@ We can then load the module with the dynamic loader:
59
53
60
54
The dynamic loader returns a `Module` object, which contains getters for the named exports (in this case, `q`).
61
55
62
-
[Read the wiki on overview of ES6 modules and syntax](https://github.com/ModuleLoader/es6-module-loader/wiki/A-Brief-ES6-Modules-Overview).
56
+
[Read an overview of ES6 modules and syntax](https://github.com/ModuleLoader/es6-module-loader/wiki/A-Brief-ES6-Modules-Overview).
A simple analog to the module tag is provided with:
68
+
As well as defining `window.System`, this polyfill provides support for the `<script type="module">` tag:
75
69
76
70
```html
77
71
<scripttype="module">
@@ -82,11 +76,7 @@ A simple analog to the module tag is provided with:
82
76
</script>
83
77
```
84
78
85
-
Ideally this should be based on polyfilling the `<module>` tag, as `<script type="module">` is not in the spec.
86
-
87
-
As such this approach is not really suitable for anything more than experimentation.
88
-
89
-
See an overview of the specification module tag features here - https://github.com/dherman/web-modules/blob/master/explainer.md.
79
+
Because it is only possible to load ES6 modules with this tag, it is not suitable for production use in this way.
90
80
91
81
### baseURL
92
82
@@ -127,14 +117,22 @@ It is also possible to define wildcard paths rules. The most specific rule will
127
117
128
118
### Circular References & Bindings
129
119
130
-
All [AMD](http://requirejs.org/docs/api.html#circular), [CommonJS](http://nodejs.org/api/modules.html#modules_cycles), and [ES6](https://github.com/ModuleLoader/es6-module-loader#circular-references--bindings) treat circular dependencies differently.
120
+
#### Zebra Striping
121
+
122
+
All [AMD](http://requirejs.org/docs/api.html#circular), [CommonJS](http://nodejs.org/api/modules.html#modules_cycles), and [ES6](https://github.com/ModuleLoader/es6-module-loader#circular-references--bindings) treat circular dependencies differently.
123
+
Handling this problem is one of the major innovations in the loader spec, using a technique called **zebra striping**. This involves analyzing the dependency tree and forming alternate layers of ES6 / non-ES6 modules with circular references in each layer for linking.
124
+
The layers are then individually linked, with the appropriate circular reference handling being done within each layer. This allows CommonJS circular references to interact with ES6 circular references. Inter-format circular references are not supported as they
125
+
would be across layers.
131
126
132
-
Circular references and live bindings are fully supported identically to ES6 in this polyfill.
127
+
This loader implementation handles zebra-striping automatically, allowing a loader like [SystemJS](https://github.com/systemjs/systemjs) to support all module formats with exact circular reference support.
128
+
129
+
#### ES6 Circular References & Bindings
130
+
131
+
ES6 circular references and bindings behave in the following way:
133
132
134
-
That is:
135
133
* Bindings are set up before module execution.
136
134
* Execution is run from depth-first left to right on the module tree stopping at circular references.
137
-
* Bindings are live - an adjustment to an export of one module affects all modules importing it.
135
+
* Bindings are live - an adjustment to an export of one module affects all modules importing it, but it can only be modified in the defining module.
138
136
139
137
even.js
140
138
```javascript
@@ -166,25 +164,21 @@ odd.js
166
164
});
167
165
```
168
166
169
-
When using [SystemJS](https://github.com/systemjs/systemjs) loader, AMD and CommonJS preserve their own circular reference behavior.
170
-
171
167
### Moving to Production
172
168
173
169
When in production, it is not suitable to load ES6 modules and syntax in the browser.
174
170
171
+
#### System.register Output
172
+
175
173
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)
176
174
from [SystemJS](https://github.com/systemjs/systemjs).
177
175
178
176
The benefit of this output is that it provides full support for circular references and live module bindings.
179
177
180
178
This output format is explained here - https://github.com/ModuleLoader/es6-module-loader/wiki/System.register-Explained.
181
179
182
-
Alternatively, Traceur can also output `amd` or `cjs` as well.
183
-
184
180
A basic example of using this extension with a build would be the following:
185
181
186
-
#### Building all files into one bundle
187
-
188
182
1. Build all ES6 modules into ES5 System.register form:
189
183
190
184
```
@@ -231,6 +225,12 @@ We can also build separate files with:
231
225
232
226
With the above, we can load from the separate files identical to loading ES6.
233
227
228
+
#### Building across module formats
229
+
230
+
If using a loader like [SystemJS](https://github.com/systemjs/systemjs) to load different module formats, then a build can also be performed across module formats as well.
231
+
232
+
See [SystemJS builder](https://github.com/systemjs/builder) for this combined approach.
233
+
234
234
### NodeJS Usage
235
235
236
236
```
@@ -259,56 +259,10 @@ Running the application:
259
259
NodeJS test
260
260
```
261
261
262
-
### Tracing API
263
-
264
-
This is not in the specification, but is provided since it is such a natural extension of loading and not much code at all.
265
-
266
-
Enable tracing and start importing modules:
267
-
268
-
```javascript
269
-
loader.trace=true;
270
-
loader.execute=true; // optional, disables execution of module bodies
271
-
272
-
loader.import('some/module').then(function() {
273
-
/*
274
-
Now we have:
275
-
276
-
loader.loads['some/module'] == {
277
-
name: 'some/module',
278
-
deps: ['./unnormalized', 'deps'],
279
-
depMap: {
280
-
'./unnormalized': 'normalized',
281
-
'deps': 'deps'
282
-
},
283
-
address: '/resolvedURL',
284
-
metadata: { metadata object from load },
285
-
source: 'translated source code string',
286
-
kind: 'dynamic' (instantiated) or 'declarative' (ES6 module pipeline)
287
-
}
288
-
289
-
With the dependency load records
290
-
loader.loads['normalized']
291
-
loader.loads['deps']
292
-
also set.
293
-
*/
294
-
});
295
-
```
296
-
297
-
Then start importing modules
298
-
299
-
### Extending the Loader
300
-
301
-
The loader in its default state provides only ES6 loading.
302
-
303
-
We can extend it to load AMD, CommonJS and global scripts as well as various other custom functionality through the loader hooks.
304
-
305
-
[Read the wiki on extending the loader here](https://github.com/ModuleLoader/es6-module-loader/wiki/Extending-the-ES6-Loader).
306
-
307
-
### Specification Notes
308
-
309
-
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.
262
+
### Further Documentation
310
263
311
-
To follow the current the specification changes, see the marked issues https://github.com/ModuleLoader/es6-module-loader/issues?labels=specification&page=1&state=open.
264
+
*[Extending the loader through loader hooks](https://github.com/ModuleLoader/es6-module-loader/wiki/Extending-the-ES6-Loader)
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt).
0 commit comments