Git Product home page Git Product logo

loader.js's Introduction

loader.js Build Status

Minimal AMD loader mostly stolen from @wycats.

No Conflict

To prevent the loader from overriding require, define, or requirejs you can instruct the loader to use no conflict mode by providing it an alternative name for the various globals that are normally used.

Example:

loader.noConflict({
  define: 'newDefine',
  require: 'newRequire'
});

Extra stuff

define.alias('new-name', 'old/path')

define.alias allows creation of a symlink from one module to another, for example:

define('foo/bar/baz', [], () => 'hi');
define.alias('foo', 'foo/bar/baz');

require('foo') // => 'hi';
require('foo') === require('foo/bar/baz');

require('require')

When within a module, one can require require. This provides a require scoped to the current module. Enabling dynamic, relatively path requires.

define('foo/apple', ['require'], function() { return 'apple'; });
define('foo/bar', ['require'], function(require){ return require('./apple'););

require('foo/bar'); // 'apple';

This scoped require also enables a module some reflection, in this case the ability for a module to see its own moduleId;

define('my/name/is', ['require'], function(require) {
  require.moduleId // => 'my/name/is';
});

define.exports('foo', {})

define.exports enables a fastpath for non-lazy dependency-less modules, for example:

Rather then:

define("my-foo-app/templates/application", ["exports"], function (exports) {
  "use strict";

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  exports.default = Ember.HTMLBars.template({ "id": "VVZNWoRm", "block": "{\"statements\":[[1,[26,[\"welcome-page\"]],false],[0,\"\\n\"],[0,\"\\n\"],[1,[26,[\"outlet\"]],false]],\"locals\":[],\"named\":[],\"yields\":[],\"hasPartials\":false}", "meta": { "moduleName": "my-foo-app/templates/application.hbs" } });
});

We can author:

define.exports('my-app/template/apple', { hbs: true, "id": "VVZNWoRm", "block": "{\"statements\":[[1,[26,[\"welcome-page\"]],false],[0,\"\\n\"],[0,\"\\n\"],[1,[26,[\"outlet\"]],false]],\"locals\":[],\"named\":[],\"yields\":[],\"hasPartials\":false}", "meta": { "moduleName": "my-foo-app/templates/application.hbs" }});

benefits:

  • less bytes
  • no reification step
  • no need to juggle pre-parse voodoo.

require.unsee('foo');

require.unsee allows one to unload a given module. note The side-effects of that module cannot be unloaded. This is quite useful, especially for test suites. Being able to unload run tests, mitigates many common memory leaks:

example:

define('my-app/tests/foo-test.js', ['qunit'], function(qunit) {
  let app;

  qunit.module('my module', {
    beforeEach() {
      app = new App();
    }

    // forgot to `null` out app in the afterEach
  });

  test('my app exists', function(assert) {
    assert.ok(app);
  })
})

Note: To be able to take advantage of alternate define method name, you will also need to ensure that your build tooling generates using the alternate. An example of this is done in the emberjs-build project in the babel-enifed-module-formatter plugin.

wrapModules

It is possible to hook loader to augment or transform the loaded code. wrapModules is an optional method on the loader that is called as each module is originally loaded. wrapModules must be a function of the form wrapModules(name, callback). The callback is the original AMD callback. The return value of wrapModules is then used in subsequent requests for name

This functionality is useful for instrumenting code, for instance in code coverage libraries.

loader.wrapModules = function(name, callback) {
            if (shouldTransform(name) {
                    return myTransformer(name, callback);
                }
            }
            return callback;
    };

makeDefaultExport

loader.js creates default exports for ember-cli amdStrict mode. If you do not need this behavior you can disable it like so:

loader.makeDefaultExport = false;

Tests

We use testem for running our test suite.

You may run them with:

npm test

You can also launch testem development mode with:

npm run test:dev

License

loader.js is MIT Licensed.

loader.js's People

Contributors

stefanpenner avatar rwjblue avatar abuiles avatar ef4 avatar chadhietala avatar krisselden avatar kratiahuja avatar mmun avatar tmquinn avatar statianzo avatar nathanhammond avatar bekzod avatar runspired avatar brzpegasus avatar jrowlingson avatar jschiq2 avatar olleolleolle avatar tricknotes avatar sivakumar-kailasam avatar turbo87 avatar yamadapc avatar

Watchers

James Cloos avatar 2hu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.