UNPKG

3.66 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = loader;
7exports.raw = void 0;
8
9var _path = _interopRequireDefault(require("path"));
10
11var _loaderUtils = require("loader-utils");
12
13var _schemaUtils = require("schema-utils");
14
15var _mimeTypes = _interopRequireDefault(require("mime-types"));
16
17var _normalizeFallback = _interopRequireDefault(require("./utils/normalizeFallback"));
18
19var _options = _interopRequireDefault(require("./options.json"));
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function shouldTransform(limit, size) {
24 if (typeof limit === 'boolean') {
25 return limit;
26 }
27
28 if (typeof limit === 'string') {
29 return size <= parseInt(limit, 10);
30 }
31
32 if (typeof limit === 'number') {
33 return size <= limit;
34 }
35
36 return true;
37}
38
39function getMimetype(mimetype, resourcePath) {
40 if (typeof mimetype === 'boolean') {
41 if (mimetype) {
42 const resolvedMimeType = _mimeTypes.default.contentType(_path.default.extname(resourcePath));
43
44 if (!resolvedMimeType) {
45 return '';
46 }
47
48 return resolvedMimeType.replace(/;\s+charset/i, ';charset');
49 }
50
51 return '';
52 }
53
54 if (typeof mimetype === 'string') {
55 return mimetype;
56 }
57
58 const resolvedMimeType = _mimeTypes.default.contentType(_path.default.extname(resourcePath));
59
60 if (!resolvedMimeType) {
61 return '';
62 }
63
64 return resolvedMimeType.replace(/;\s+charset/i, ';charset');
65}
66
67function getEncoding(encoding) {
68 if (typeof encoding === 'boolean') {
69 return encoding ? 'base64' : '';
70 }
71
72 if (typeof encoding === 'string') {
73 return encoding;
74 }
75
76 return 'base64';
77}
78
79function getEncodedData(generator, mimetype, encoding, content, resourcePath) {
80 if (generator) {
81 return generator(content, mimetype, encoding, resourcePath);
82 }
83
84 return `data:${mimetype}${encoding ? `;${encoding}` : ''},${content.toString( // eslint-disable-next-line no-undefined
85 encoding || undefined)}`;
86}
87
88function loader(content) {
89 // Loader Options
90 const options = (0, _loaderUtils.getOptions)(this) || {};
91 (0, _schemaUtils.validate)(_options.default, options, {
92 name: 'URL Loader',
93 baseDataPath: 'options'
94 }); // No limit or within the specified limit
95
96 if (shouldTransform(options.limit, content.length)) {
97 const {
98 resourcePath
99 } = this;
100 const mimetype = getMimetype(options.mimetype, resourcePath);
101 const encoding = getEncoding(options.encoding);
102
103 if (typeof content === 'string') {
104 // eslint-disable-next-line no-param-reassign
105 content = Buffer.from(content);
106 }
107
108 const encodedData = getEncodedData(options.generator, mimetype, encoding, content, resourcePath);
109 const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true;
110 return `${esModule ? 'export default' : 'module.exports ='} ${JSON.stringify(encodedData)}`;
111 } // Normalize the fallback.
112
113
114 const {
115 loader: fallbackLoader,
116 options: fallbackOptions
117 } = (0, _normalizeFallback.default)(options.fallback, options); // Require the fallback.
118 // eslint-disable-next-line global-require, import/no-dynamic-require
119
120 const fallback = require(fallbackLoader); // Call the fallback, passing a copy of the loader context. The copy has the query replaced. This way, the fallback
121 // loader receives the query which was intended for it instead of the query which was intended for url-loader.
122
123
124 const fallbackLoaderContext = Object.assign({}, this, {
125 query: fallbackOptions
126 });
127 return fallback.call(fallbackLoaderContext, content);
128} // Loader Mode
129
130
131const raw = true;
132exports.raw = raw;
\No newline at end of file