processPattern.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = processPattern;
  6. var _globby = require('globby');
  7. var _globby2 = _interopRequireDefault(_globby);
  8. var _pLimit = require('p-limit');
  9. var _pLimit2 = _interopRequireDefault(_pLimit);
  10. var _isGlob = require('is-glob');
  11. var _isGlob2 = _interopRequireDefault(_isGlob);
  12. var _path = require('path');
  13. var _path2 = _interopRequireDefault(_path);
  14. var _minimatch = require('minimatch');
  15. var _minimatch2 = _interopRequireDefault(_minimatch);
  16. var _writeFile = require('./writeFile');
  17. var _writeFile2 = _interopRequireDefault(_writeFile);
  18. var _isObject = require('./utils/isObject');
  19. var _isObject2 = _interopRequireDefault(_isObject);
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. function processPattern(globalRef, pattern) {
  22. var info = globalRef.info,
  23. debug = globalRef.debug,
  24. output = globalRef.output,
  25. concurrency = globalRef.concurrency,
  26. contextDependencies = globalRef.contextDependencies;
  27. var globArgs = Object.assign({
  28. cwd: pattern.context
  29. }, pattern.fromArgs || {});
  30. if (pattern.fromType === 'nonexistent') {
  31. return Promise.resolve();
  32. }
  33. var limit = (0, _pLimit2.default)(concurrency || 100);
  34. info('begin globbing \'' + pattern.glob + '\' with a context of \'' + pattern.context + '\'');
  35. return (0, _globby2.default)(pattern.glob, globArgs).then(function (paths) {
  36. return Promise.all(paths.map(function (from) {
  37. return limit(function () {
  38. var file = {
  39. force: pattern.force,
  40. absoluteFrom: _path2.default.resolve(pattern.context, from)
  41. };
  42. file.relativeFrom = _path2.default.relative(pattern.context, file.absoluteFrom);
  43. if (pattern.flatten) {
  44. file.relativeFrom = _path2.default.basename(file.relativeFrom);
  45. }
  46. // This is so webpack is able to watch the directory and when
  47. // a new file is added it triggeres a rebuild
  48. var contextPath = _path2.default.dirname(_path2.default.resolve(from));
  49. if (contextDependencies.indexOf(contextPath) === -1 && (0, _isGlob2.default)(pattern.glob)) {
  50. contextDependencies.push(contextPath);
  51. }
  52. debug('found ' + from);
  53. // Check the ignore list
  54. var il = pattern.ignore.length;
  55. while (il--) {
  56. var ignoreGlob = pattern.ignore[il];
  57. var globParams = {
  58. dot: true,
  59. matchBase: true
  60. };
  61. var glob = void 0;
  62. if (typeof ignoreGlob === 'string') {
  63. glob = ignoreGlob;
  64. } else if ((0, _isObject2.default)(ignoreGlob)) {
  65. glob = ignoreGlob.glob || '';
  66. var ignoreGlobParams = Object.assign({}, ignoreGlob);
  67. delete ignoreGlobParams.glob;
  68. // Overwrite minimatch defaults
  69. globParams = Object.assign(globParams, ignoreGlobParams);
  70. } else {
  71. glob = '';
  72. }
  73. debug('testing ' + glob + ' against ' + file.relativeFrom);
  74. if ((0, _minimatch2.default)(file.relativeFrom, glob, globParams)) {
  75. info('ignoring \'' + file.relativeFrom + '\', because it matches the ignore glob \'' + glob + '\'');
  76. return Promise.resolve();
  77. } else {
  78. debug(glob + ' doesn\'t match ' + file.relativeFrom);
  79. }
  80. }
  81. // Change the to path to be relative for webpack
  82. if (pattern.toType === 'dir') {
  83. file.webpackTo = _path2.default.join(pattern.to, file.relativeFrom);
  84. } else if (pattern.toType === 'file') {
  85. file.webpackTo = pattern.to || file.relativeFrom;
  86. } else if (pattern.toType === 'template') {
  87. file.webpackTo = pattern.to;
  88. file.webpackToRegExp = pattern.test;
  89. }
  90. if (_path2.default.isAbsolute(file.webpackTo)) {
  91. if (output === '/') {
  92. throw '[copy-webpack-plugin] Using older versions of webpack-dev-server, devServer.outputPath must be defined to write to absolute paths';
  93. }
  94. file.webpackTo = _path2.default.relative(output, file.webpackTo);
  95. }
  96. // ensure forward slashes
  97. file.webpackTo = file.webpackTo.replace(/\\/g, '/');
  98. info('determined that \'' + from + '\' should write to \'' + file.webpackTo + '\'');
  99. return (0, _writeFile2.default)(globalRef, pattern, file);
  100. });
  101. }));
  102. });
  103. }