preProcessPattern.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = preProcessPattern;
  6. var _path = require('path');
  7. var _path2 = _interopRequireDefault(_path);
  8. var _isGlob = require('is-glob');
  9. var _isGlob2 = _interopRequireDefault(_isGlob);
  10. var _escape = require('./utils/escape');
  11. var _escape2 = _interopRequireDefault(_escape);
  12. var _isObject = require('./utils/isObject');
  13. var _isObject2 = _interopRequireDefault(_isObject);
  14. var _promisify = require('./utils/promisify');
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. // https://www.debuggex.com/r/VH2yS2mvJOitiyr3
  17. var isTemplateLike = /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(:\d+)?\])|(\[(\w+:)?hash(:\w+)?(:\d+)?\])|(\[\d+\])/;
  18. function preProcessPattern(globalRef, pattern) {
  19. var info = globalRef.info,
  20. debug = globalRef.debug,
  21. warning = globalRef.warning,
  22. context = globalRef.context,
  23. inputFileSystem = globalRef.inputFileSystem,
  24. fileDependencies = globalRef.fileDependencies,
  25. contextDependencies = globalRef.contextDependencies,
  26. compilation = globalRef.compilation;
  27. pattern = typeof pattern === 'string' ? {
  28. from: pattern
  29. } : Object.assign({}, pattern);
  30. pattern.to = pattern.to || '';
  31. pattern.context = pattern.context || context;
  32. if (!_path2.default.isAbsolute(pattern.context)) {
  33. pattern.context = _path2.default.join(context, pattern.context);
  34. }
  35. pattern.ignore = globalRef.ignore.concat(pattern.ignore || []);
  36. info('processing from: \'' + pattern.from + '\' to: \'' + pattern.to + '\'');
  37. switch (true) {
  38. case !!pattern.toType:
  39. // if toType already exists
  40. break;
  41. case isTemplateLike.test(pattern.to):
  42. pattern.toType = 'template';
  43. break;
  44. case _path2.default.extname(pattern.to) === '' || pattern.to.slice(-1) === '/':
  45. pattern.toType = 'dir';
  46. break;
  47. default:
  48. pattern.toType = 'file';
  49. }
  50. debug('determined \'' + pattern.to + '\' is a \'' + pattern.toType + '\'');
  51. // If we know it's a glob, then bail early
  52. if ((0, _isObject2.default)(pattern.from) && pattern.from.glob) {
  53. pattern.fromType = 'glob';
  54. var fromArgs = Object.assign({}, pattern.from);
  55. delete fromArgs.glob;
  56. pattern.fromArgs = fromArgs;
  57. pattern.glob = (0, _escape2.default)(pattern.context, pattern.from.glob);
  58. pattern.absoluteFrom = _path2.default.resolve(pattern.context, pattern.from.glob);
  59. return Promise.resolve(pattern);
  60. }
  61. if (_path2.default.isAbsolute(pattern.from)) {
  62. pattern.absoluteFrom = pattern.from;
  63. } else {
  64. pattern.absoluteFrom = _path2.default.resolve(pattern.context, pattern.from);
  65. }
  66. debug('determined \'' + pattern.from + '\' to be read from \'' + pattern.absoluteFrom + '\'');
  67. var noStatsHandler = function noStatsHandler() {
  68. // If from doesn't appear to be a glob, then log a warning
  69. if ((0, _isGlob2.default)(pattern.from) || pattern.from.indexOf('*') !== -1) {
  70. pattern.fromType = 'glob';
  71. pattern.glob = (0, _escape2.default)(pattern.context, pattern.from);
  72. } else {
  73. var msg = 'unable to locate \'' + pattern.from + '\' at \'' + pattern.absoluteFrom + '\'';
  74. var warningMsg = '[copy-webpack-plugin] ' + msg;
  75. // only display the same message once
  76. if (compilation.errors.indexOf(warningMsg) === -1) {
  77. warning(msg);
  78. compilation.errors.push(warningMsg);
  79. }
  80. pattern.fromType = 'nonexistent';
  81. }
  82. };
  83. return (0, _promisify.stat)(inputFileSystem, pattern.absoluteFrom).catch(function () {
  84. return noStatsHandler();
  85. }).then(function (stat) {
  86. if (!stat) {
  87. noStatsHandler();
  88. return pattern;
  89. }
  90. if (stat.isDirectory()) {
  91. pattern.fromType = 'dir';
  92. pattern.context = pattern.absoluteFrom;
  93. contextDependencies.push(pattern.absoluteFrom);
  94. pattern.glob = (0, _escape2.default)(pattern.absoluteFrom, '**/*');
  95. pattern.absoluteFrom = _path2.default.join(pattern.absoluteFrom, '**/*');
  96. pattern.fromArgs = {
  97. dot: true
  98. };
  99. } else if (stat.isFile()) {
  100. pattern.fromType = 'file';
  101. pattern.context = _path2.default.dirname(pattern.absoluteFrom);
  102. pattern.glob = (0, _escape2.default)(pattern.absoluteFrom);
  103. pattern.fromArgs = {
  104. dot: true
  105. };
  106. fileDependencies.push(pattern.absoluteFrom);
  107. } else if (!pattern.fromType) {
  108. info('Unrecognized file type for ' + pattern.from);
  109. }
  110. return pattern;
  111. });
  112. }