Repository: gaearon/promise-loader Branch: master Commit: 7e6e311e3923 Files: 4 Total size: 3.5 KB Directory structure: gitextract_k4fh87rh/ ├── .gitignore ├── README.md ├── index.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules .DS_Store ================================================ FILE: README.md ================================================ ## A sister of [bundle-loader](https://github.com/webpack/bundle-loader) with promise API ### Usage [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) This is a ripoff of [bundle-loader](https://github.com/webpack/bundle-loader) that uses promises instead of callbacks. 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. `require: (string) -> () -> Promise` It's up to you to specify your Promise library of choice as a parameter. ``` javascript // Assuming you use Bluebird var load = require("promise?bluebird!./file.js"); // The chunk is not requested until you call the load function load().then(function(file) { }); ``` If a promise library is already loaded externally you can specify 'global'. You can optionally specify [a name for your chunk](http://webpack.github.io/docs/code-splitting.html#named-chunks) after a comma: ```javascript var load = require("promise?bluebird,editor!./editor.js"); ``` 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. 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: ```javascript { test: /\.i18n\.json$/, loader: 'promise?global,[name].i18n' } ``` ### License MIT (http://www.opensource.org/licenses/mit-license.php) ================================================ FILE: index.js ================================================ /* MIT License http://www.opensource.org/licenses/mit-license.php Author Dan Abramov Shamelessly based on bundle-loader by Tobias Koppers @sokra */ var path = require('path'); module.exports = function () {}; module.exports.pitch = function (remainingRequest) { this.cacheable && this.cacheable(); var query = this.query.substring(1).split(','), promiseLib = query[0], bundleName = query[1] || ''; var filename = path.basename(remainingRequest); var name = path.basename(remainingRequest, path.extname(filename)); bundleName = bundleName.replace(/\[filename\]/g, filename).replace(/\[name\]/g, name); if (!promiseLib) { throw new Error('You need to specify your Promise library of choice, e.g. require("promise?bluebird!./file.js")'); } var result = [ (promiseLib !== 'global') ? 'var Promise = require(' + JSON.stringify(promiseLib) + ');\n' : '', 'module.exports = function () {\n', ' return new Promise(function (resolve) {\n', ' require.ensure([], function (require) {\n', ' resolve(require(', JSON.stringify('!!' + remainingRequest), '));\n', ' }' + (bundleName && (', ' + JSON.stringify(bundleName))) + ');\n', ' });\n', '}' ]; return result.join(''); }; ================================================ FILE: package.json ================================================ { "name": "promise-loader", "version": "1.0.0", "author": { "name": "Dan Abramov" }, "description": "a webpack bundle-loader ripoff with promise interface", "repository": { "type": "git", "url": "git://github.com/gaearon/promise-loader.git" }, "licenses": [ { "type": "MIT", "url": "http://www.opensource.org/licenses/mit-license.php" } ] }