Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
es6-module-loader
Advanced tools
Dynamically loads ES6 modules in browsers and NodeJS with support for loading existing and custom module formats through loader hooks.
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 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).
System.import
) to dynamically load ES6 modules.baseURL
and paths
implementations.For an overview of build workflows, see the production guide.
For an example of a universal module loader based on this polyfill for loading AMD, CommonJS and globals, see SystemJS.
If using ES6 syntax (optional), include traceur.js
or babel.js
in the page first then include es6-module-loader.js
:
<script src="traceur.js"></script>
<script src="es6-module-loader.js"></script>
To use Babel, set the transpiler to babel
with the loader configuration:
<script>
System.transpiler = 'babel';
</script>
Then we can write any ES6 module:
mymodule.js:
export class q {
constructor() {
console.log('this is an es6 class!');
}
}
and load the module dynamically in the browser
<script>
System.import('mymodule').then(function(m) {
new m.q();
});
</script>
The dynamic loader returns a Module
object, which contains getters for the named exports (in this case, q
).
If using Traceur, these can be set with:
System.traceurOptions = {...};
Or with Babel:
System.babelOptions = {...};
As well as defining window.System
, this polyfill provides support for the <script type="module">
tag:
<script type="module">
// loads the 'q' export from 'mymodule.js' in the same path as the page
import { q } from 'mymodule';
new q(); // -> 'this is an es6 class!'
</script>
Because it is only possible to load ES6 modules with this tag, it is not suitable for production use in this way.
See the demo folder in this repo for a working example demonstrating module loading in the browser both with System.import
and with the module-type script tag.
npm install es6-module-loader
For use in NodeJS, the Loader
and System
globals are provided as exports:
index.js:
var System = require('es6-module-loader').System;
/*
* Include:
* System.transpiler = 'babel';
* to use Babel instead of Traceur
*/
System.import('some-module').then(function(m) {
console.log(m.p);
});
some-module.js:
export var p = 'NodeJS test';
Running the application:
> node index.js
NodeJS test
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.
Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "lib" subdirectory!
npm run test:node
will use node to to run the testsnpm run test:browser
will run npm run test:browser-babel
and npm run test:browser-traceur
npm run test:browser-[transpiler]
use karma to run the tests with Traceur or Babel.npm run test:browser:perf
will use karma to run benchmarksnpm run test:browser-[transpiler]
supports options after a double dash (--
) :
You can use the --polyfill
option to test the code with polyfill.
You can use the --coverage
option to test and extract coverage info.
You can use the --ie8
option to test the code in the ie8 scope only.
You can use the --saucelabs
option to use karma and saucelabs to run the tests in various browsers.
Note: you will need to export your username and key to launch it.
export SAUCE_USERNAME={your user name} && export SAUCE_ACCESS_KEY={the access key that you see once logged in}
npm run test:browsers -- --saucelabs
Copyright (c) 2015 Luke Hoban, Addy Osmani, Guy Bedford
Licensed under the MIT license.
FAQs
An ES6 Module Loader shim
The npm package es6-module-loader receives a total of 5,732 weekly downloads. As such, es6-module-loader popularity was classified as popular.
We found that es6-module-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.