Coder Social home page Coder Social logo

async-module-loader's Introduction

async-module-loader for webpack

Based on https://github.com/webpack/bundle-loader with improvements of error handling

npm install async-module-loader

Usage

webpack documentation: Using loaders

Also you will need to use AsyncModulePlugin.

Basic usage

async-module-loader returns function which accepts 2 callbacks: for success and for fail Exports of the requested module are passed into success callback as a first argument

require('async-module-loader!./file.js')(function onLoad(mod) {
  mod.doSomething();
}, function onError() {
  // error happened
});

Also you can use Promises with promise option specified, like this:

require('async-module-loader?promise!./file.js').then(function onLoad(mod) {
  mod.doSomething();
}, function onError() {
  // error happened
});

Specifying a chunk name

require('async-module-loader?name=my-chunk!./file.js')(function onLoad(mod) {
  mod.doSomething();
}, function onError() {
  // error happened
});

Delayed execution

If you do not want your module to be executed immediately (maybe because some animation is in play), then you can tell to async-module-loader to load a chunk, but not execute it. In such, a function will be passed to the success callback instead of a module.exports object of requested chunk. Call that function then you will need you chunk executed:

require('async-module-loader?noexec!./file.js')(function onLoad(executeChunk) {
  setTimeout(function() {
    var mod = executeChunk();
    mod.doSomething();
  }, 500);
}, function onError() {
  // error happened
});

Plugin

To make async-module-loader work correctly you need to add AsyncModulePlugin to your plugins.

// webpack.config.js

var AsyncModulePlugin = require('async-module-loader/plugin');

module.exports = {
  // ...

  plugins: [
    // ... other plugins

    new AsyncModulePlugin()
  ]
  // ...
}

Query parameters

  • name: Use this to specify output name for requested chunk. See webpack documentation

  • promise: Use this to return a promise from async-module-loader.

  • noexec: Use this to delay chunk execution

License

MIT (http://www.opensource.org/licenses/mit-license)

async-module-loader's People

Contributors

fernandoacorreia avatar gaearon avatar nekr avatar nickdima avatar shama avatar sokra avatar zxcabs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

async-module-loader's Issues

Bug with AsyncModulePlugin?

I was trying to use this loader, but encountered this issue: webpack-contrib#17

I have a work around (caching if the module loaded myself), but after debugging, its this plugin that causes the constant refetching, presumably because you want retries after failures. Is it possible to not cause a refetch if the bundle is present?

Also, could you explain the memory leak this helps prevent?

Thanks!

Always failing

I'm using your fork in a project. It's just what I needed, thanks for this.

But I'm having an issue, I'm always hitting the fail callback. Do the number of modules that return matter or something?

When I do this:

require.ensure(['layouts/admin/admin-layout'], function(require) {
    var component = require('layouts/admin/admin-layout');
    console.log(component);
}, 'admin');

I get what I expect, the exports object. But when I do this:

require('async-module?name=admin!layouts/admin/admin-layout')(

    function success(component) {
        console.log('success');
    },

    function fail() {
        console.log('fail');
    }

);

I always get the fail.

This is my webpack.config:

var webpack = require('webpack');
var StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin;
var AsyncModulePlugin = require('async-module-loader/plugin');

var plugins = [
    new AsyncModulePlugin(),
    new StatsWriterPlugin({
        fields: null
    })
];

// if (process.env.NODE_ENV === 'production' && !Meteor.isCordova) {
//   plugins.push(new webpack.optimize.CommonsChunkPlugin('common', 'common.web.js'));
// }

module.exports = {
    entry: './entry',
    plugins: plugins,
    output: {
        chunkFilename: '[name]-[id].non-entry-chunk.web.js'
    },
    module: {
        loaders: [
            { test: /\.jade/, loader: 'template-html-loader' }
        ]
    }
};

Also, the network tab in chrome shows that the chunk was requested, and the response is 200 OK, with this contents (no whitespace above or below), in both scenarios. I repeat, same response in both scenarios. That's what's killing me:

webpackJsonp([1],{

/***/ 96:
/***/ function(module, exports, __webpack_require__) {

    /**
     * Admin Layout
     *
     * Default markup shared by all admin entry points
     */

    //===== STYLE ================================================================



    //===== JS ===================================================================



    //===== DEFINE COMPONENT =====================================================

    module.exports = MD.Vue.extend({
        template: __webpack_require__(97)
    });


/***/ },

/***/ 97:
/***/ function(module, exports) {

    module.exports = "<div class=\"admin-layout\"><p>This works as the layout for the admin stuff</p><router-view><p>this is content that can be accessed in the routed\ncomponent through the slot tag</p></router-view></div>"

/***/ }

});
//# sourceMappingURL=admin-1.non-entry-chunk.web.js.map

I don't really know if this is expecte behaviour and I just don't get what bundle-loader should be doing, or there's some legitimate issue going on.

Hope it's something small. Again, thanks for your fork, really nice.

Webpack 2 support

Method require.ensure in webpack 2.1.0-beta.25 now return a Promise, so with this loader i get exception: Uncaught TypeError: Cannot read property 'catch' of undefined

how to load the cdn file use the loader

i want use the loader load the remote file
require('async-module!http://xx.min.js')(function onLoad(mod) { console.log(mod) // mod.doSomething(); }, function onError() { });
it's not working .

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.