index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 = _interopRequireDefault(require("loader-utils"));
  9. var _schemaUtils = _interopRequireDefault(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 = _loaderUtils.default.getOptions(this) || {};
  14. (0, _schemaUtils.default)(_options.default, options, {
  15. name: 'File Loader',
  16. baseDataPath: 'options'
  17. });
  18. const context = options.context || this.rootContext;
  19. const url = _loaderUtils.default.interpolateName(this, options.name || '[contenthash].[ext]', {
  20. context,
  21. content,
  22. regExp: options.regExp
  23. });
  24. let outputPath = url;
  25. if (options.outputPath) {
  26. if (typeof options.outputPath === 'function') {
  27. outputPath = options.outputPath(url, this.resourcePath, context);
  28. } else {
  29. outputPath = _path.default.posix.join(options.outputPath, url);
  30. }
  31. }
  32. let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
  33. if (options.publicPath) {
  34. if (typeof options.publicPath === 'function') {
  35. publicPath = options.publicPath(url, this.resourcePath, context);
  36. } else {
  37. publicPath = `${options.publicPath.endsWith('/') ? options.publicPath : `${options.publicPath}/`}${url}`;
  38. }
  39. publicPath = JSON.stringify(publicPath);
  40. }
  41. if (options.postTransformPublicPath) {
  42. publicPath = options.postTransformPublicPath(publicPath);
  43. }
  44. if (typeof options.emitFile === 'undefined' || options.emitFile) {
  45. this.emitFile(outputPath, content);
  46. }
  47. return `${options.esModules ? 'export default' : 'module.exports ='} ${publicPath};`;
  48. }
  49. const raw = true;
  50. exports.raw = raw;