index.js
3.57 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
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
123
124
125
126
127
128
129
130
131
132
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = loader;
exports.raw = void 0;
var _path = _interopRequireDefault(require("path"));
var _loaderUtils = require("loader-utils");
var _schemaUtils = require("schema-utils");
var _mimeTypes = _interopRequireDefault(require("mime-types"));
var _normalizeFallback = _interopRequireDefault(require("./utils/normalizeFallback"));
var _options = _interopRequireDefault(require("./options.json"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function shouldTransform(limit, size) {
if (typeof limit === 'boolean') {
return limit;
}
if (typeof limit === 'string') {
return size <= parseInt(limit, 10);
}
if (typeof limit === 'number') {
return size <= limit;
}
return true;
}
function getMimetype(mimetype, resourcePath) {
if (typeof mimetype === 'boolean') {
if (mimetype) {
const resolvedMimeType = _mimeTypes.default.contentType(_path.default.extname(resourcePath));
if (!resolvedMimeType) {
return '';
}
return resolvedMimeType.replace(/;\s+charset/i, ';charset');
}
return '';
}
if (typeof mimetype === 'string') {
return mimetype;
}
const resolvedMimeType = _mimeTypes.default.contentType(_path.default.extname(resourcePath));
if (!resolvedMimeType) {
return '';
}
return resolvedMimeType.replace(/;\s+charset/i, ';charset');
}
function getEncoding(encoding) {
if (typeof encoding === 'boolean') {
return encoding ? 'base64' : '';
}
if (typeof encoding === 'string') {
return encoding;
}
return 'base64';
}
function getEncodedData(generator, mimetype, encoding, content, resourcePath) {
if (generator) {
return generator(content, mimetype, encoding, resourcePath);
}
return `data:${mimetype}${encoding ? `;${encoding}` : ''},${content.toString( // eslint-disable-next-line no-undefined
encoding || undefined)}`;
}
function loader(content) {
// Loader Options
const options = (0, _loaderUtils.getOptions)(this) || {};
(0, _schemaUtils.validate)(_options.default, options, {
name: 'URL Loader',
baseDataPath: 'options'
}); // No limit or within the specified limit
if (shouldTransform(options.limit, content.length)) {
const {
resourcePath
} = this;
const mimetype = getMimetype(options.mimetype, resourcePath);
const encoding = getEncoding(options.encoding);
if (typeof content === 'string') {
// eslint-disable-next-line no-param-reassign
content = Buffer.from(content);
}
const encodedData = getEncodedData(options.generator, mimetype, encoding, content, resourcePath);
const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true;
return `${esModule ? 'export default' : 'module.exports ='} ${JSON.stringify(encodedData)}`;
} // Normalize the fallback.
const {
loader: fallbackLoader,
options: fallbackOptions
} = (0, _normalizeFallback.default)(options.fallback, options); // Require the fallback.
// eslint-disable-next-line global-require, import/no-dynamic-require
const fallback = require(fallbackLoader); // Call the fallback, passing a copy of the loader context. The copy has the query replaced. This way, the fallback
// loader receives the query which was intended for it instead of the query which was intended for url-loader.
const fallbackLoaderContext = Object.assign({}, this, {
query: fallbackOptions
});
return fallback.call(fallbackLoaderContext, content);
} // Loader Mode
const raw = true;
exports.raw = raw;