index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function loader(content) {
  13. const options = (0, _loaderUtils.getOptions)(this);
  14. (0, _schemaUtils.validate)(_options.default, options, {
  15. name: 'File Loader',
  16. baseDataPath: 'options'
  17. });
  18. const context = options.context || this.rootContext;
  19. const name = options.name || '[contenthash].[ext]';
  20. const immutable = /\[([^:\]]+:)?(hash|contenthash)(:[^\]]+)?\]/gi.test(name);
  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. this.emitFile(outputPath, content, null, {
  48. immutable
  49. });
  50. }
  51. const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true;
  52. return `${esModule ? 'export default' : 'module.exports ='} ${publicPath};`;
  53. }
  54. const raw = true;
  55. exports.raw = raw;