index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.raw = undefined;
  6. exports.default = loader;
  7. var _loaderUtils = require('loader-utils');
  8. var _schemaUtils = require('schema-utils');
  9. var _schemaUtils2 = _interopRequireDefault(_schemaUtils);
  10. var _mime = require('mime');
  11. var _mime2 = _interopRequireDefault(_mime);
  12. var _normalizeFallback = require('./utils/normalizeFallback');
  13. var _normalizeFallback2 = _interopRequireDefault(_normalizeFallback);
  14. var _options = require('./options.json');
  15. var _options2 = _interopRequireDefault(_options);
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. // Loader Mode
  18. const raw = exports.raw = true; /* eslint-disable
  19. global-require,
  20. no-param-reassign,
  21. prefer-destructuring,
  22. import/no-dynamic-require,
  23. */
  24. function loader(src) {
  25. // Loader Options
  26. const options = (0, _loaderUtils.getOptions)(this) || {};
  27. (0, _schemaUtils2.default)(_options2.default, options, 'URL Loader');
  28. const file = this.resourcePath;
  29. // Set limit for resource inlining (file size)
  30. let limit = options.limit;
  31. if (limit) {
  32. limit = parseInt(limit, 10);
  33. }
  34. // Get MIME type
  35. const mimetype = options.mimetype || _mime2.default.getType(file);
  36. // No limit or within the specified limit
  37. if (!limit || src.length < limit) {
  38. if (typeof src === 'string') {
  39. src = Buffer.from(src);
  40. }
  41. return `module.exports = ${JSON.stringify(`data:${mimetype || ''};base64,${src.toString('base64')}`)}`;
  42. }
  43. // Normalize the fallback.
  44. const {
  45. loader: fallbackLoader,
  46. options: fallbackOptions
  47. } = (0, _normalizeFallback2.default)(options.fallback, options);
  48. // Require the fallback.
  49. const fallback = require(fallbackLoader);
  50. // 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. }