main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[79a0317] | 1 | var path = require('path');
|
---|
| 2 |
|
---|
| 3 | var extension = require('./lib/extension');
|
---|
| 4 | var normalize = require('./lib/normalize');
|
---|
| 5 | var register = require('./lib/register');
|
---|
| 6 |
|
---|
| 7 | exports.prepare = function (extensions, filepath, cwd, nothrow) {
|
---|
| 8 | var config, usedExtension, err, option, attempt, error;
|
---|
| 9 | var attempts = [];
|
---|
| 10 | var onlyErrors = true;
|
---|
| 11 | var exts = extension(filepath);
|
---|
| 12 |
|
---|
| 13 | if (exts) {
|
---|
| 14 | exts.some(function (ext) {
|
---|
| 15 | usedExtension = ext;
|
---|
| 16 | config = normalize(extensions[ext]);
|
---|
| 17 | return !!config;
|
---|
| 18 | });
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | if (Object.keys(require.extensions).indexOf(usedExtension) !== -1) {
|
---|
| 22 | return true;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | if (!config) {
|
---|
| 26 | if (nothrow) {
|
---|
| 27 | return;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | throw new Error('No module loader found for "' + usedExtension + '".');
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | if (!cwd) {
|
---|
| 34 | cwd = path.dirname(path.resolve(filepath));
|
---|
| 35 | }
|
---|
| 36 | if (!Array.isArray(config)) {
|
---|
| 37 | config = [config];
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | for (var i in config) {
|
---|
| 41 | option = config[i];
|
---|
| 42 | attempt = register(cwd, option.module, option.register);
|
---|
| 43 | error = attempt instanceof Error ? attempt : null;
|
---|
| 44 | if (error) {
|
---|
| 45 | attempt = null;
|
---|
| 46 | }
|
---|
| 47 | attempts.push({
|
---|
| 48 | moduleName: option.module,
|
---|
| 49 | module: attempt,
|
---|
| 50 | error: error,
|
---|
| 51 | });
|
---|
| 52 | if (!error) {
|
---|
| 53 | onlyErrors = false;
|
---|
| 54 | break;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | if (onlyErrors) {
|
---|
| 58 | err = new Error(
|
---|
| 59 | 'Unable to use specified module loaders for "' + usedExtension + '".'
|
---|
| 60 | );
|
---|
| 61 | err.failures = attempts;
|
---|
| 62 | if (nothrow) {
|
---|
| 63 | return err;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | throw err;
|
---|
| 67 | }
|
---|
| 68 | return attempts;
|
---|
| 69 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.