Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
garden-loader
Advanced tools
A minimal ES6 System and AMD compatible module loader.
This package implements a partial System and Asynchronous Module Definition (AMD) polyfill. It is meant to be used in the browser, rather than node.js. It is based off the ES6 micro-loader project.
This loader doesn't implement the entire ES6 loader specification. It is meant to be a light weight loader that is suitable for development and production for developers willing to prepare their modules for easier distribution.
Use the garden loader if:
Don't use the garden loader if:
The loader is available in dist/garden-loader.js
. Include it in your page before loading any files, usually as the first script on your page. The only configuration option is the System.baseURL
property which tells the loader where your scripts are. To load scripts call System.import()
or System.importAll()
. Both methods return a Promise object.
Note: The garden loader depends on the relatively new Promise class being present. If you want to support older browsers the include a promise polyfill before the garden loader. One is included in the dist folder, but any promise polyfill library should work.
Here is a basic example:
<html>
<head>
<!-- Include a promise polyfill for old IE. -->
<script src="path/to/dist/promise.js"></script>
<script src="path/to/dist/garden-loader.js"></script>
<script>
System.baseURL = "js";
System.import('juery').then(function ($) {
// jQuery will load and be available.
});
</script>
</head>
...
</html>
Both AMD and the System module formats have the concept of named and anonymous modules.
Anonymous modules are defined without a name, rather their name is taken from their filename. It's generally recommended that you code anonymous modules so that you can move your files around during development. Modules coded in ES6 are all anonymous.
Named modules are defined with a name. Usually, modules are named during a build process so that you can combine several modules into one file for production.
A good rule of thumb is to code anonymous modules and then name them in your build.
The loader is all contained in the System
global namespace. It also declares the global define()
function to support AMD modules.
Define an AMD module.
define('react', ['react-dom'], function (ReactDOM) {
// ...
});
Use this function for backwards compatibility with RequireJS. If want to use it you should define it in the global scope:
window.require = System.amdRequire;
The base URL of your Javascript files. This will be prepended to module names when loading.
Get a module that has already been loaded.
var module = System.get('module-name')
Returns whether or not a given module has been loaded.
if (System.has('module-name')) {
// ...
}
Import a module by name. This function will return the module from its cache if it exists or load the module asynchronously.
System.import('jquery').then(function($) {
// jQuery was loaded asynchronously and is now available.
});
This is a convenience method for importing multiple modules. It's commonly used with the Promise.spread()
method that is provided with the garden loader. This method can reduce a lot of boilerplate code if you need to import several dependencies to get your application going.
System.importAll('react', 'jquery', 'app').spread(
function(React, $, Application) {
}
);
Define a new module in the System format. This format is a bit complicated and is usually only the result of transpiling ES6 code.
Manually set a module in the registry. This is a useful for making modules that have already been loaded available to the loader so that they can me used as dependencies.
<script src="js/moment.js"></script>
<script src="js/garden-loader.min.js"></script>
<script>
// Moment.js was loaded prior to garden loader, so manually install it.
System.set('moment', moment);
</script>
If you are familiar with Javascript modules then the garden loader should be pretty straightforward. However there are a few gotchas with module loaders that you should be aware of.
If you are used to including a library and then accessing it through a global variable you might be puzzled that the global variable no longer exists. This is because most modules look for a module loader and if they find it will instead define themselves as a module. If you still need the module to be accessed globally you'll need to write a little bit of code.
<script src="js/garden-loader.min.js"></script>
<script src="js/react.js"></script>
<script>
if (React == undefined) {
// React is not available globally because it was installed as a module.
React = System.get('react').default;
}
</script>
If you manually include a module that is registered anonymously then the module loader will not recognize it. Make sure that you only include anonymous modules with System.import()
or System.importAll()
.
This can especially be an issue with popular Javascript frameworks because most of them use anonymous modules. You can use a tool such as grunt AMD tamer. To name anonymous modules before including them manually.
FAQs
A minimal ES6 System and AMD compatible module loader.
We found that garden-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.