├── .gitignore ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## A sister of [bundle-loader](https://github.com/webpack/bundle-loader) with promise API 2 | 3 | ### Usage 4 | 5 | [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) 6 | 7 | This is a ripoff of [bundle-loader](https://github.com/webpack/bundle-loader) that uses promises instead of callbacks. 8 | It only implements so-called `lazy` `bundle-loader` mode—that is, `require` returns a function that, when invoked, returns a promise that resolves to the module. 9 | 10 | `require: (string) -> () -> Promise` 11 | 12 | It's up to you to specify your Promise library of choice as a parameter. 13 | 14 | ``` javascript 15 | // Assuming you use Bluebird 16 | var load = require("promise?bluebird!./file.js"); 17 | 18 | // The chunk is not requested until you call the load function 19 | load().then(function(file) { 20 | 21 | }); 22 | ``` 23 | 24 | If a promise library is already loaded externally you can specify 'global'. 25 | 26 | 27 | You can optionally specify [a name for your chunk](http://webpack.github.io/docs/code-splitting.html#named-chunks) after a comma: 28 | 29 | ```javascript 30 | var load = require("promise?bluebird,editor!./editor.js"); 31 | ``` 32 | 33 | This can be useful for [single-page apps](http://webpack.github.io/docs/optimization.html#single-page-app) because you can later extract filenames from [Webpack-generated stats](https://github.com/webpack/docs/wiki/node.js-api#stats) and pre-load specific bundles if you know user's going to hit them. 34 | 35 | The bundle name may include `[filename]`, which will be replaced with the filename, and `[name]`, which omits the extension. This is useful for when you want to configure loaders in Webpack configuration without specifying precise filenames—for example, by a suffix: 36 | 37 | ```javascript 38 | { 39 | test: /\.i18n\.json$/, 40 | loader: 'promise?global,[name].i18n' 41 | } 42 | ``` 43 | 44 | ### License 45 | 46 | MIT (http://www.opensource.org/licenses/mit-license.php) 47 | 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License http://www.opensource.org/licenses/mit-license.php 3 | Author Dan Abramov 4 | Shamelessly based on bundle-loader by Tobias Koppers @sokra 5 | */ 6 | 7 | var path = require('path'); 8 | 9 | module.exports = function () {}; 10 | module.exports.pitch = function (remainingRequest) { 11 | this.cacheable && this.cacheable(); 12 | var query = this.query.substring(1).split(','), 13 | promiseLib = query[0], 14 | bundleName = query[1] || ''; 15 | var filename = path.basename(remainingRequest); 16 | var name = path.basename(remainingRequest, path.extname(filename)); 17 | 18 | bundleName = bundleName.replace(/\[filename\]/g, filename).replace(/\[name\]/g, name); 19 | 20 | if (!promiseLib) { 21 | throw new Error('You need to specify your Promise library of choice, e.g. require("promise?bluebird!./file.js")'); 22 | } 23 | 24 | var result = [ 25 | (promiseLib !== 'global') ? 'var Promise = require(' + JSON.stringify(promiseLib) + ');\n' : '', 26 | 'module.exports = function () {\n', 27 | ' return new Promise(function (resolve) {\n', 28 | ' require.ensure([], function (require) {\n', 29 | ' resolve(require(', JSON.stringify('!!' + remainingRequest), '));\n', 30 | ' }' + (bundleName && (', ' + JSON.stringify(bundleName))) + ');\n', 31 | ' });\n', 32 | '}' 33 | ]; 34 | 35 | return result.join(''); 36 | }; 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promise-loader", 3 | "version": "1.0.0", 4 | "author": { 5 | "name": "Dan Abramov" 6 | }, 7 | "description": "a webpack bundle-loader ripoff with promise interface", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/gaearon/promise-loader.git" 11 | }, 12 | "licenses": [ 13 | { 14 | "type": "MIT", 15 | "url": "http://www.opensource.org/licenses/mit-license.php" 16 | } 17 | ] 18 | } 19 | --------------------------------------------------------------------------------