forked from deepsweet/baggage-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (94 loc) · 3.91 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* eslint-disable consistent-return */
'use strict';
var path = require('path');
var fs = require('fs');
var loaderUtils = require('loader-utils');
var SourceMap = require('source-map');
var legacyLoader = require('./compat/legacy-loader.js');
var util = require('./lib/util');
var injectBanner = '\n/* injects from baggage-loader */\n';
module.exports = function(source, sourceMap) {
// parseQuery will always give us an object, for back-compat we
// want to know if we're working with JSON query or query string
if (!util.isJSONString(this.query.replace('?', ''))) {
return legacyLoader.call(this, source, sourceMap);
}
var query = loaderUtils.parseQuery(this.query);
// /foo/bar/file.js
var srcFilepath = this.resourcePath;
// /foo/bar/file.js -> file
var srcFilename = path.basename(srcFilepath, path.extname(srcFilepath));
// /foo/bar/file.js -> /foo/bar
var srcDirpath = path.dirname(srcFilepath);
// /foo/bar -> bar
var srcDirname = srcDirpath.split(path.sep).pop();
if (this.cacheable) {
this.cacheable();
}
var filePaths = Object.keys(query);
var injections = [];
if (filePaths.length) {
injections = filePaths.map(function(filePath) {
var varName;
var loadersForFile = '';
var inject = null;
if (typeof query[filePath] === 'object') {
var fileConfig = query[filePath];
var loaderStringForFile = fileConfig.loaders || '';
if (loaderStringForFile) {
loadersForFile = loaderStringForFile.replace(/\*/g, '!') + '!';
}
varName = fileConfig.varName;
}
filePath = util.applyPlaceholders(filePath, srcDirname, srcFilename);
if (varName) {
varName = util.applyPlaceholders(varName, srcDirname, srcFilename);
}
// @todo support mandatory/optional requires via config
try {
// check if absoluted from srcDirpath + baggageFile path exists
var stats = fs.statSync(path.resolve(srcDirpath, filePath));
if (stats.isFile()) {
inject = '';
if (varName) {
inject = 'var ' + varName + ' = ';
}
inject += 'require(\'' + loadersForFile + './' + filePath + '\');\n';
}
} catch (e) {}
return inject;
});
injections.filter(function(inject) {
return typeof inject === 'string';
});
if (injections.length) {
var srcInjection = injectBanner + injections.join('\n');
// support existing SourceMap
// https://github.com/mozilla/source-map#sourcenode
// https://github.com/webpack/imports-loader/blob/master/index.js#L34-L44
// https://webpack.github.io/docs/loaders.html#writing-a-loader
if (sourceMap) {
var currentRequest = loaderUtils.getCurrentRequest(this);
var SourceNode = SourceMap.SourceNode;
var SourceMapConsumer = SourceMap.SourceMapConsumer;
var sourceMapConsumer = new SourceMapConsumer(sourceMap);
var node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer);
node.prepend(srcInjection);
var result = node.toStringWithSourceMap({
file: currentRequest
});
this.callback(null, result.code, result.map.toJSON());
return;
}
// prepend collected inject at the top of file
return srcInjection + source;
}
}
// return the original source and sourceMap
if (sourceMap) {
this.callback(null, source, sourceMap);
return;
}
// return the original source
return source;
};