forked from eBay/arc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-loader.js
29 lines (25 loc) · 870 Bytes
/
proxy-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
let js = JSON.stringify;
let loaderUtils = require('loader-utils');
try {
require.resolve('arc-server/proxy');
} catch (e) {
/* istanbul ignore next: give a helpful error if the user doesn't have arc-server installed */
throw new Error(
"Using proxy with `arc-webpack` requires that you have `arc-server` installed as a dependency of your project, but we can't find it."
);
}
module.exports = function(source) {
let options = loaderUtils.getOptions(this);
let matches = options.matches;
let code = `
let Proxy = require('arc-server/proxy');
let MatchSet = require('arc-resolver').MatchSet;
let matches = new MatchSet([${
Array.from(matches).map(({ value, flags }) => {
return `{ value:require(${js(value)}), flags:${js(flags)}}`;
}).join(',')
}]);
module.exports = new Proxy(matches);
`;
return code;
};