1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loader;
|
7 | exports.raw = void 0;
|
8 |
|
9 | var _path = _interopRequireDefault(require("path"));
|
10 |
|
11 | var _loaderUtils = require("loader-utils");
|
12 |
|
13 | var _schemaUtils = require("schema-utils");
|
14 |
|
15 | var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
16 |
|
17 | var _normalizeFallback = _interopRequireDefault(require("./utils/normalizeFallback"));
|
18 |
|
19 | var _options = _interopRequireDefault(require("./options.json"));
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22 |
|
23 | function 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 |
|
39 | function 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 |
|
67 | function 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 |
|
79 | function 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 |
|
88 | function loader(content) {
|
89 |
|
90 | const options = (0, _loaderUtils.getOptions)(this) || {};
|
91 | (0, _schemaUtils.validate)(_options.default, options, {
|
92 | name: 'URL Loader',
|
93 | baseDataPath: 'options'
|
94 | });
|
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 |
|
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 | }
|
112 |
|
113 |
|
114 | const {
|
115 | loader: fallbackLoader,
|
116 | options: fallbackOptions
|
117 | } = (0, _normalizeFallback.default)(options.fallback, options);
|
118 |
|
119 |
|
120 | const fallback = require(fallbackLoader);
|
121 |
|
122 |
|
123 |
|
124 | const fallbackLoaderContext = Object.assign({}, this, {
|
125 | query: fallbackOptions
|
126 | });
|
127 | return fallback.call(fallbackLoaderContext, content);
|
128 | }
|
129 |
|
130 |
|
131 | const raw = true;
|
132 | exports.raw = raw; |
\ | No newline at end of file |