index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loader;
  6. exports.raw = void 0;
  7. var _loaderUtils = require("loader-utils");
  8. var _schemaUtils = _interopRequireDefault(require("schema-utils"));
  9. var _mime = _interopRequireDefault(require("mime"));
  10. var _normalizeFallback = _interopRequireDefault(require("./utils/normalizeFallback"));
  11. var _options = _interopRequireDefault(require("./options.json"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /* eslint-disable
  14. global-require,
  15. no-param-reassign,
  16. prefer-destructuring,
  17. import/no-dynamic-require,
  18. */
  19. function shouldTransform(limit, size) {
  20. if (typeof limit === 'boolean') {
  21. return limit;
  22. }
  23. if (typeof limit === 'string') {
  24. return size <= parseInt(limit, 10);
  25. }
  26. if (typeof limit === 'number') {
  27. return size <= limit;
  28. }
  29. return true;
  30. }
  31. function loader(src) {
  32. // Loader Options
  33. const options = (0, _loaderUtils.getOptions)(this) || {};
  34. (0, _schemaUtils.default)(_options.default, options, {
  35. name: 'URL Loader',
  36. baseDataPath: 'options'
  37. }); // No limit or within the specified limit
  38. if (shouldTransform(options.limit, src.length)) {
  39. const file = this.resourcePath; // Get MIME type
  40. const mimetype = options.mimetype || _mime.default.getType(file);
  41. if (typeof src === 'string') {
  42. src = Buffer.from(src);
  43. }
  44. return `${options.esModules ? 'export default' : 'module.exports ='} ${JSON.stringify(`data:${mimetype || ''};base64,${src.toString('base64')}`)}`;
  45. } // Normalize the fallback.
  46. const {
  47. loader: fallbackLoader,
  48. options: fallbackOptions
  49. } = (0, _normalizeFallback.default)(options.fallback, options); // Require the fallback.
  50. const fallback = require(fallbackLoader); // Call the fallback, passing a copy of the loader context. The copy has the query replaced. This way, the fallback
  51. // loader receives the query which was intended for it instead of the query which was intended for url-loader.
  52. const fallbackLoaderContext = Object.assign({}, this, {
  53. query: fallbackOptions
  54. });
  55. return fallback.call(fallbackLoaderContext, src);
  56. } // Loader Mode
  57. const raw = true;
  58. exports.raw = raw;