You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.raw = undefined;
  6. exports.default = loader;
  7. var _path = require('path');
  8. var _path2 = _interopRequireDefault(_path);
  9. var _loaderUtils = require('loader-utils');
  10. var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
  11. var _schemaUtils = require('schema-utils');
  12. var _schemaUtils2 = _interopRequireDefault(_schemaUtils);
  13. var _options = require('./options.json');
  14. var _options2 = _interopRequireDefault(_options);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. /* eslint-disable
  17. multiline-ternary,
  18. */
  19. function loader(content) {
  20. if (!this.emitFile) throw new Error('File Loader\n\nemitFile is required from module system');
  21. const options = _loaderUtils2.default.getOptions(this) || {};
  22. (0, _schemaUtils2.default)(_options2.default, options, 'File Loader');
  23. const context = options.context || this.rootContext || this.options && this.options.context;
  24. const url = _loaderUtils2.default.interpolateName(this, options.name, {
  25. context,
  26. content,
  27. regExp: options.regExp
  28. });
  29. let outputPath = url;
  30. if (options.outputPath) {
  31. if (typeof options.outputPath === 'function') {
  32. outputPath = options.outputPath(url);
  33. } else {
  34. outputPath = _path2.default.posix.join(options.outputPath, url);
  35. }
  36. }
  37. if (options.useRelativePath) {
  38. const filePath = this.resourcePath;
  39. const issuer = options.context ? context : this._module && this._module.issuer && this._module.issuer.context;
  40. const relativeUrl = issuer && _path2.default.relative(issuer, filePath).split(_path2.default.sep).join('/');
  41. const relativePath = relativeUrl && `${_path2.default.dirname(relativeUrl)}/`;
  42. // eslint-disable-next-line no-bitwise
  43. if (~relativePath.indexOf('../')) {
  44. outputPath = _path2.default.posix.join(outputPath, relativePath, url);
  45. } else {
  46. outputPath = _path2.default.posix.join(relativePath, url);
  47. }
  48. }
  49. let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
  50. if (options.publicPath) {
  51. if (typeof options.publicPath === 'function') {
  52. publicPath = options.publicPath(url);
  53. } else if (options.publicPath.endsWith('/')) {
  54. publicPath = options.publicPath + url;
  55. } else {
  56. publicPath = `${options.publicPath}/${url}`;
  57. }
  58. publicPath = JSON.stringify(publicPath);
  59. }
  60. // eslint-disable-next-line no-undefined
  61. if (options.emitFile === undefined || options.emitFile) {
  62. this.emitFile(outputPath, content);
  63. }
  64. // TODO revert to ES2015 Module export, when new CSS Pipeline is in place
  65. return `module.exports = ${publicPath};`;
  66. }
  67. const raw = exports.raw = true;