-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (54 loc) · 1.62 KB
/
index.js
File metadata and controls
57 lines (54 loc) · 1.62 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
const resolve = require('import-sub');
function WebpackImportSubPlugin(options) {
const config = {};
if (!options) {
throw new Error('WebpackImportSub plugin requires at least one import rule.');
}
if (typeof options !== 'object') {
throw new Error('WebpackImportSub plugin options must ba an object or an array.');
}
if (options instanceof Array) {
Object.assign(config, {
rules: options,
});
} else {
Object.assign(config, options);
}
if (!(config.rules instanceof Array)) {
throw new Error('WebpackImportSub plugin requires option `sub`');
}
this.resolve = typeof config.resolve === 'function' ? config.resolve : null,
this.root = config.root || process.cwd();
this.rules = config.rules;
}
WebpackImportSubPlugin.prototype = {
constructor: WebpackImportSubPlugin,
apply: function (compiler) {
compiler.plugin("normal-module-factory", (nmf) => {
nmf.plugin("before-resolve", (result, callback) => {
if(!result) return callback();
const base = result.context;
const request = result.request;
/**
* Define holders and calculate two general properties [id] and [folder]
*/
return resolve(this.rules, {
base: base,
request: request,
root: this.root,
resolve: this.resolve,
})
.then(function(request) {
return callback(null, Object.assign({},
result,
{
request: request instanceof Array ? request[0] : request,
}
));
})
.catch(console.log);
});
});
}
}
module.exports = WebpackImportSubPlugin;