Created
October 17, 2015 16:53
-
-
Save philopon/53a4ab862c093a5e94f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var requireAsync = require('./require-async.js'); | |
| // デカいモジュールを先読みしておく | |
| var modulePromise = requireAsync('name'); | |
| // モジュールの読み込みを待たずに実行される。 | |
| console.log('foo'); | |
| modulePromise.then(function(largeModule){ | |
| // 読み込み完了次第実行される | |
| console.log(largeModule); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var fs = require('fs'); | |
| var Module = module.constructor; | |
| module.exports = (name) => { | |
| return new Promise((resolve, reject) => { | |
| var m = new Module(); | |
| var file = require.resolve(name); | |
| fs.readFile(file, (err, data) => { | |
| if(err) reject(err); | |
| m._compile(data.toString(), file); | |
| resolve(m.exports); | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment