index.js 3.6 KB

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