index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use strict';
  2. var _path = require('path');
  3. var _path2 = _interopRequireDefault(_path);
  4. var _preProcessPattern = require('./preProcessPattern');
  5. var _preProcessPattern2 = _interopRequireDefault(_preProcessPattern);
  6. var _processPattern = require('./processPattern');
  7. var _processPattern2 = _interopRequireDefault(_processPattern);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function CopyWebpackPlugin() {
  10. var patterns = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  11. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  12. if (!Array.isArray(patterns)) {
  13. throw new Error('[copy-webpack-plugin] patterns must be an array');
  14. }
  15. // Defaults debug level to 'warning'
  16. options.debug = options.debug || 'warning';
  17. // Defaults debugging to info if only true is specified
  18. if (options.debug === true) {
  19. options.debug = 'info';
  20. }
  21. var debugLevels = ['warning', 'info', 'debug'];
  22. var debugLevelIndex = debugLevels.indexOf(options.debug);
  23. function log(msg, level) {
  24. if (level === 0) {
  25. msg = 'WARNING - ' + msg;
  26. } else {
  27. level = level || 1;
  28. }
  29. if (level <= debugLevelIndex) {
  30. console.log('[copy-webpack-plugin] ' + msg); // eslint-disable-line no-console
  31. }
  32. }
  33. function warning(msg) {
  34. log(msg, 0);
  35. }
  36. function info(msg) {
  37. log(msg, 1);
  38. }
  39. function debug(msg) {
  40. log(msg, 2);
  41. }
  42. var apply = function apply(compiler) {
  43. var fileDependencies = void 0;
  44. var contextDependencies = void 0;
  45. var written = {};
  46. var context = void 0;
  47. if (!options.context) {
  48. context = compiler.options.context;
  49. } else if (!_path2.default.isAbsolute(options.context)) {
  50. context = _path2.default.join(compiler.options.context, options.context);
  51. } else {
  52. context = options.context;
  53. }
  54. var emit = function emit(compilation, cb) {
  55. debug('starting emit');
  56. var callback = function callback() {
  57. debug('finishing emit');
  58. cb();
  59. };
  60. fileDependencies = [];
  61. contextDependencies = [];
  62. var globalRef = {
  63. info: info,
  64. debug: debug,
  65. warning: warning,
  66. compilation: compilation,
  67. written: written,
  68. fileDependencies: fileDependencies,
  69. contextDependencies: contextDependencies,
  70. context: context,
  71. inputFileSystem: compiler.inputFileSystem,
  72. output: compiler.options.output.path,
  73. ignore: options.ignore || [],
  74. copyUnmodified: options.copyUnmodified,
  75. concurrency: options.concurrency
  76. };
  77. if (globalRef.output === '/' && compiler.options.devServer && compiler.options.devServer.outputPath) {
  78. globalRef.output = compiler.options.devServer.outputPath;
  79. }
  80. var tasks = [];
  81. patterns.forEach(function (pattern) {
  82. tasks.push(Promise.resolve().then(function () {
  83. return (0, _preProcessPattern2.default)(globalRef, pattern);
  84. })
  85. // Every source (from) is assumed to exist here
  86. .then(function (pattern) {
  87. return (0, _processPattern2.default)(globalRef, pattern);
  88. }));
  89. });
  90. Promise.all(tasks).catch(function (err) {
  91. compilation.errors.push(err);
  92. }).then(function () {
  93. return callback();
  94. });
  95. };
  96. var afterEmit = function afterEmit(compilation, cb) {
  97. debug('starting after-emit');
  98. var callback = function callback() {
  99. debug('finishing after-emit');
  100. cb();
  101. };
  102. var compilationFileDependencies = void 0;
  103. var addFileDependency = void 0;
  104. if (Array.isArray(compilation.fileDependencies)) {
  105. compilationFileDependencies = new Set(compilation.fileDependencies);
  106. addFileDependency = function addFileDependency(file) {
  107. return compilation.fileDependencies.push(file);
  108. };
  109. } else {
  110. compilationFileDependencies = compilation.fileDependencies;
  111. addFileDependency = function addFileDependency(file) {
  112. return compilation.fileDependencies.add(file);
  113. };
  114. }
  115. var compilationContextDependencies = void 0;
  116. var addContextDependency = void 0;
  117. if (Array.isArray(compilation.contextDependencies)) {
  118. compilationContextDependencies = new Set(compilation.contextDependencies);
  119. addContextDependency = function addContextDependency(file) {
  120. return compilation.contextDependencies.push(file);
  121. };
  122. } else {
  123. compilationContextDependencies = compilation.contextDependencies;
  124. addContextDependency = function addContextDependency(file) {
  125. return compilation.contextDependencies.add(file);
  126. };
  127. }
  128. // Add file dependencies if they're not already tracked
  129. var _iteratorNormalCompletion = true;
  130. var _didIteratorError = false;
  131. var _iteratorError = undefined;
  132. try {
  133. for (var _iterator = fileDependencies[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  134. var file = _step.value;
  135. if (compilationFileDependencies.has(file)) {
  136. debug('not adding ' + file + ' to change tracking, because it\'s already tracked');
  137. } else {
  138. debug('adding ' + file + ' to change tracking');
  139. addFileDependency(file);
  140. }
  141. }
  142. // Add context dependencies if they're not already tracked
  143. } catch (err) {
  144. _didIteratorError = true;
  145. _iteratorError = err;
  146. } finally {
  147. try {
  148. if (!_iteratorNormalCompletion && _iterator.return) {
  149. _iterator.return();
  150. }
  151. } finally {
  152. if (_didIteratorError) {
  153. throw _iteratorError;
  154. }
  155. }
  156. }
  157. var _iteratorNormalCompletion2 = true;
  158. var _didIteratorError2 = false;
  159. var _iteratorError2 = undefined;
  160. try {
  161. for (var _iterator2 = contextDependencies[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  162. var _context = _step2.value;
  163. if (compilationContextDependencies.has(_context)) {
  164. debug('not adding ' + _context + ' to change tracking, because it\'s already tracked');
  165. } else {
  166. debug('adding ' + _context + ' to change tracking');
  167. addContextDependency(_context);
  168. }
  169. }
  170. } catch (err) {
  171. _didIteratorError2 = true;
  172. _iteratorError2 = err;
  173. } finally {
  174. try {
  175. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  176. _iterator2.return();
  177. }
  178. } finally {
  179. if (_didIteratorError2) {
  180. throw _iteratorError2;
  181. }
  182. }
  183. }
  184. callback();
  185. };
  186. if (compiler.hooks) {
  187. var plugin = { name: 'CopyPlugin' };
  188. compiler.hooks.emit.tapAsync(plugin, emit);
  189. compiler.hooks.afterEmit.tapAsync(plugin, afterEmit);
  190. } else {
  191. compiler.plugin('emit', emit);
  192. compiler.plugin('after-emit', afterEmit);
  193. }
  194. };
  195. return {
  196. apply: apply
  197. };
  198. }
  199. CopyWebpackPlugin['default'] = CopyWebpackPlugin;
  200. module.exports = CopyWebpackPlugin;