-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (49 loc) · 1.47 KB
/
index.js
File metadata and controls
58 lines (49 loc) · 1.47 KB
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
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
var assign = require('object-assign');
var getCLIOptions = function () {
var modules = [];
process.argv.forEach(function (val, index, array) {
if (val.indexOf('--link') > -1) {
var args = val.split('=')[1];
if (args) { modules = args.split(',') }
}
});
return modules;
};
var formatSettings = function (option) {
if (option.constructor !== Array) return { path: option };
return {
path: option[0],
loader: assign(
{
test: option[1].test,
loader: option[1].loader,
},
{ include: [option[1].entry || option[0]] }
),
}
}
var outputStatus = function (module, path) {
console.log(
'\x1b[0;33m', 'Linked', module, 'to local path', path, '\x1b[0m'
);
}
function WebpackLinkPlugin(options) {
this.options = options;
this.modules = getCLIOptions();
}
WebpackLinkPlugin.prototype.apply = function(compiler) {
var options = this.options;
var modules = this.modules;
if (!modules || !options || modules.length === 0) { return }
for (var key in options) {
if (!options.hasOwnProperty(key)) continue;
if (modules.indexOf(key) < 0) continue;
var settings = formatSettings(options[key]);
compiler.options.resolve.alias[key] = settings.path;
outputStatus(key, settings.path);
if (!settings.loader) continue;
compiler.options.module.loaders = compiler.options.module.loaders || []
compiler.options.module.loaders.push(settings.loader);
}
};
module.exports = WebpackLinkPlugin;