index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 _options = _interopRequireDefault(require("./options.json"));
  11. var _utils = require("./utils");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function loader(content) {
  14. const options = (0, _loaderUtils.getOptions)(this);
  15. (0, _schemaUtils.validate)(_options.default, options, {
  16. name: 'File Loader',
  17. baseDataPath: 'options'
  18. });
  19. const context = options.context || this.rootContext;
  20. const name = options.name || '[contenthash].[ext]';
  21. const url = (0, _loaderUtils.interpolateName)(this, name, {
  22. context,
  23. content,
  24. regExp: options.regExp
  25. });
  26. let outputPath = url;
  27. if (options.outputPath) {
  28. if (typeof options.outputPath === 'function') {
  29. outputPath = options.outputPath(url, this.resourcePath, context);
  30. } else {
  31. outputPath = _path.default.posix.join(options.outputPath, url);
  32. }
  33. }
  34. let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
  35. if (options.publicPath) {
  36. if (typeof options.publicPath === 'function') {
  37. publicPath = options.publicPath(url, this.resourcePath, context);
  38. } else {
  39. publicPath = `${options.publicPath.endsWith('/') ? options.publicPath : `${options.publicPath}/`}${url}`;
  40. }
  41. publicPath = JSON.stringify(publicPath);
  42. }
  43. if (options.postTransformPublicPath) {
  44. publicPath = options.postTransformPublicPath(publicPath);
  45. }
  46. if (typeof options.emitFile === 'undefined' || options.emitFile) {
  47. const assetInfo = {};
  48. if (typeof name === 'string') {
  49. let normalizedName = name;
  50. const idx = normalizedName.indexOf('?');
  51. if (idx >= 0) {
  52. normalizedName = normalizedName.substr(0, idx);
  53. }
  54. const isImmutable = /\[([^:\]]+:)?(hash|contenthash)(:[^\]]+)?]/gi.test(normalizedName);
  55. if (isImmutable === true) {
  56. assetInfo.immutable = true;
  57. }
  58. }
  59. assetInfo.sourceFilename = (0, _utils.normalizePath)(_path.default.relative(this.rootContext, this.resourcePath));
  60. this.emitFile(outputPath, content, null, assetInfo);
  61. }
  62. const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true;
  63. return `${esModule ? 'export default' : 'module.exports ='} ${publicPath};`;
  64. }
  65. const raw = true;
  66. exports.raw = raw;