1 | var async = require('async');
|
2 | var assert = require('assert');
|
3 |
|
4 | exports.disectLoaderString = function disectLoaderString(string) {
|
5 | if (typeof string === 'object' && string && string.path) {
|
6 | return string;
|
7 | }
|
8 |
|
9 | var loaderStrings = string.split('!');
|
10 | if (loaderStrings.length === 1) {
|
11 | return extractPathAndQuery(loaderStrings[0]);
|
12 | }
|
13 | else {
|
14 | return loaderStrings.map(disectLoaderString);
|
15 | }
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | exports.extractLoaders = function extractLoaders(entry) {
|
63 | if (entry.loaders) {
|
64 | return entry.loaders;
|
65 | }
|
66 |
|
67 | var query = '';
|
68 |
|
69 | if (entry.query) {
|
70 | if (typeof entry.query === 'object') {
|
71 | query = '?' + JSON.stringify(entry.query);
|
72 | }
|
73 | else if (typeof entry.query === 'string') {
|
74 | query = entry.query[0] === '?' ? '' : '?';
|
75 | query += entry.query;
|
76 | }
|
77 | }
|
78 |
|
79 | return [
|
80 | entry.loader + query
|
81 | ];
|
82 | };
|
83 |
|
84 | exports.resolveLoaders = function(compiler, loaders, done) {
|
85 | async.parallel(loaders.map(function(loader) {
|
86 | assert(!!loader, "Invalid loader string to resolve!!! " + JSON.stringify(loaders));
|
87 |
|
88 | return function(callback) {
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | var callArgs = [ compiler.context, loader, function(err, result) {
|
95 | if (err) {
|
96 | return callback(err);
|
97 | }
|
98 |
|
99 | callback(null, extractPathAndQuery(result));
|
100 | }];
|
101 |
|
102 | try {
|
103 | compiler.resolvers.loader.resolve.apply(compiler.resolvers.loader, callArgs);
|
104 | }
|
105 | catch(e) {
|
106 | if (e.toString().match(/Signature changed: context parameter added/)) {
|
107 | compiler.resolvers.loader.resolve.apply(
|
108 | compiler.resolvers.loader,
|
109 | [ compiler.context ].concat(callArgs)
|
110 | );
|
111 | }
|
112 | else {
|
113 | throw e;
|
114 | }
|
115 | }
|
116 | };
|
117 | }), done);
|
118 | };
|
119 |
|
120 | exports.applyLoaders = require('./applyLoaders');
|
121 |
|
122 | exports.convertLoadersToObjects = function(loaders) {
|
123 | return loaders.map(function(loader) {
|
124 | if (typeof loader === 'string') {
|
125 | return extractPathAndQuery(loader);
|
126 | }
|
127 | else if (typeof loader === 'object') {
|
128 | return loader;
|
129 | }
|
130 | })
|
131 | }
|
132 |
|
133 | function extractPathAndQuery(string) {
|
134 | var fragments = string.split('?');
|
135 |
|
136 | if (fragments.length === 1) {
|
137 | return { path: string };
|
138 | }
|
139 |
|
140 | return { path: fragments[0], query: '?' + fragments[1] };
|
141 | }
|