UNPKG

3.41 kBJavaScriptView Raw
1var async = require('async');
2var assert = require('assert');
3
4exports.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 * Extract the set of loaders (one or more) from a given a "module.loaders"
20 * webpack config entry.
21 *
22 * Example inputs/outputs:
23 *
24 * // a loader chain
25 * {
26 * loaders: [ 'style', 'css' ]
27 * }
28 * // => [ "style", "css" ]
29 *
30 * // another loader chain
31 * {
32 * loaders: [ 'style!css' ]
33 * }
34 * // => [ "style!css" ]
35 *
36 * // a single loader, no query:
37 * {
38 * loader: 'babel'
39 * }
40 * // => [ "babel" ]
41 *
42 * // a single loader with inline query:
43 * {
44 * loader: 'babel?presets[]=react'
45 * }
46 * // => [ "babel?presets[]=react" ]
47 *
48 * // a single loader with a query object:
49 * {
50 * loader: 'babel',
51 * query: { presets: [ 'react' ] }
52 * }
53 * // => [ "babel?presets[]=react" ]
54 *
55 * @param {Object} entry
56 * @param {Array.<String>?} entry.loaders
57 * @param {String?} entry.loader
58 * @param {Object?} entry.query
59 *
60 * @return {Array.<String>}
61 */
62exports.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
84exports.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 // webpack2 has changed the signature for the resolve method, but there's
90 // no way to tell the current compiler version so we need to try/catch
91 // and wack-a-mole
92 //
93 // fixes #23
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
120exports.applyLoaders = require('./applyLoaders');
121
122exports.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
133function 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}