-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (28 loc) · 1.1 KB
/
index.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
30
31
32
33
34
35
36
var path = require('path')
var loaderUtils = require('loader-utils');
var utils = require('./libs/utils')
var assign = utils.assign, isObject = utils.isObject
// /(?:import|export)\s+\{?(?:([\w_*$]+),)*(?:[\w_*$]+)\}?\s+from/
var IMPORT_REG = /(?:@import|require|from|import)(?:\(|\s*)(\'|\")([^\s\1]+)\1(?:\)|\s*);?/gmi
module.exports = function(source) {
this.cacheable();
var query = assign(loaderUtils.parseQuery(this.resourceQuery || '?'), loaderUtils.parseQuery(this.query || '?'));
var alias = query && query.alias || (this.options.resolve || {}).alias
if (!isObject(alias)) {
return source
}
var result, resolved = '', lastIndex = 0
while(result = IMPORT_REG.exec(source)) {
var s = result[2], m = result[0]
for(var k in alias) {
if(alias.hasOwnProperty(k) && s.indexOf(k) === 0) {
resolved = resolved + source.slice(lastIndex, result.index)
lastIndex = result.index + m.length
resolved = resolved + m.replace(k, path.relative(this.context, alias[k]))
break
}
}
}
resolved = resolved + source.slice(lastIndex)
return resolved
}