index.js 8.37 KB
Newer Older
YazhouChen's avatar
YazhouChen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
'use strict';

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _preProcessPattern = require('./preProcessPattern');

var _preProcessPattern2 = _interopRequireDefault(_preProcessPattern);

var _processPattern = require('./processPattern');

var _processPattern2 = _interopRequireDefault(_processPattern);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function CopyWebpackPlugin() {
    var patterns = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    if (!Array.isArray(patterns)) {
        throw new Error('[copy-webpack-plugin] patterns must be an array');
    }

    // Defaults debug level to 'warning'
    options.debug = options.debug || 'warning';

    // Defaults debugging to info if only true is specified
    if (options.debug === true) {
        options.debug = 'info';
    }

    var debugLevels = ['warning', 'info', 'debug'];
    var debugLevelIndex = debugLevels.indexOf(options.debug);
    function log(msg, level) {
        if (level === 0) {
            msg = 'WARNING - ' + msg;
        } else {
            level = level || 1;
        }
        if (level <= debugLevelIndex) {
            console.log('[copy-webpack-plugin] ' + msg); // eslint-disable-line no-console
        }
    }

    function warning(msg) {
        log(msg, 0);
    }

    function info(msg) {
        log(msg, 1);
    }

    function debug(msg) {
        log(msg, 2);
    }

    var apply = function apply(compiler) {
        var fileDependencies = void 0;
        var contextDependencies = void 0;
        var written = {};

        var context = void 0;

        if (!options.context) {
            context = compiler.options.context;
        } else if (!_path2.default.isAbsolute(options.context)) {
            context = _path2.default.join(compiler.options.context, options.context);
        } else {
            context = options.context;
        }

        var emit = function emit(compilation, cb) {
            debug('starting emit');
            var callback = function callback() {
                debug('finishing emit');
                cb();
            };

            fileDependencies = [];
            contextDependencies = [];

            var globalRef = {
                info: info,
                debug: debug,
                warning: warning,
                compilation: compilation,
                written: written,
                fileDependencies: fileDependencies,
                contextDependencies: contextDependencies,
                context: context,
                inputFileSystem: compiler.inputFileSystem,
                output: compiler.options.output.path,
                ignore: options.ignore || [],
                copyUnmodified: options.copyUnmodified,
                concurrency: options.concurrency
            };

            if (globalRef.output === '/' && compiler.options.devServer && compiler.options.devServer.outputPath) {
                globalRef.output = compiler.options.devServer.outputPath;
            }

            var tasks = [];

            patterns.forEach(function (pattern) {
                tasks.push(Promise.resolve().then(function () {
                    return (0, _preProcessPattern2.default)(globalRef, pattern);
                })
                // Every source (from) is assumed to exist here
                .then(function (pattern) {
                    return (0, _processPattern2.default)(globalRef, pattern);
                }));
            });

            Promise.all(tasks).catch(function (err) {
                compilation.errors.push(err);
            }).then(function () {
                return callback();
            });
        };

        var afterEmit = function afterEmit(compilation, cb) {
            debug('starting after-emit');
            var callback = function callback() {
                debug('finishing after-emit');
                cb();
            };

            var compilationFileDependencies = void 0;
            var addFileDependency = void 0;
            if (Array.isArray(compilation.fileDependencies)) {
                compilationFileDependencies = new Set(compilation.fileDependencies);
                addFileDependency = function addFileDependency(file) {
                    return compilation.fileDependencies.push(file);
                };
            } else {
                compilationFileDependencies = compilation.fileDependencies;
                addFileDependency = function addFileDependency(file) {
                    return compilation.fileDependencies.add(file);
                };
            }

            var compilationContextDependencies = void 0;
            var addContextDependency = void 0;
            if (Array.isArray(compilation.contextDependencies)) {
                compilationContextDependencies = new Set(compilation.contextDependencies);
                addContextDependency = function addContextDependency(file) {
                    return compilation.contextDependencies.push(file);
                };
            } else {
                compilationContextDependencies = compilation.contextDependencies;
                addContextDependency = function addContextDependency(file) {
                    return compilation.contextDependencies.add(file);
                };
            }

            // Add file dependencies if they're not already tracked
            var _iteratorNormalCompletion = true;
            var _didIteratorError = false;
            var _iteratorError = undefined;

            try {
                for (var _iterator = fileDependencies[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                    var file = _step.value;

                    if (compilationFileDependencies.has(file)) {
                        debug('not adding ' + file + ' to change tracking, because it\'s already tracked');
                    } else {
                        debug('adding ' + file + ' to change tracking');
                        addFileDependency(file);
                    }
                }

                // Add context dependencies if they're not already tracked
            } catch (err) {
                _didIteratorError = true;
                _iteratorError = err;
            } finally {
                try {
                    if (!_iteratorNormalCompletion && _iterator.return) {
                        _iterator.return();
                    }
                } finally {
                    if (_didIteratorError) {
                        throw _iteratorError;
                    }
                }
            }

            var _iteratorNormalCompletion2 = true;
            var _didIteratorError2 = false;
            var _iteratorError2 = undefined;

            try {
                for (var _iterator2 = contextDependencies[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                    var _context = _step2.value;

                    if (compilationContextDependencies.has(_context)) {
                        debug('not adding ' + _context + ' to change tracking, because it\'s already tracked');
                    } else {
                        debug('adding ' + _context + ' to change tracking');
                        addContextDependency(_context);
                    }
                }
            } catch (err) {
                _didIteratorError2 = true;
                _iteratorError2 = err;
            } finally {
                try {
                    if (!_iteratorNormalCompletion2 && _iterator2.return) {
                        _iterator2.return();
                    }
                } finally {
                    if (_didIteratorError2) {
                        throw _iteratorError2;
                    }
                }
            }

            callback();
        };

        if (compiler.hooks) {
            var plugin = { name: 'CopyPlugin' };

            compiler.hooks.emit.tapAsync(plugin, emit);
            compiler.hooks.afterEmit.tapAsync(plugin, afterEmit);
        } else {
            compiler.plugin('emit', emit);
            compiler.plugin('after-emit', afterEmit);
        }
    };

    return {
        apply: apply
    };
}

CopyWebpackPlugin['default'] = CopyWebpackPlugin;
module.exports = CopyWebpackPlugin;