Skip to content

Commit

Permalink
fix: Memory leak in watch mode and use Set for performance (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesherov authored and joshwiens committed Sep 29, 2017
1 parent 79f30ab commit de46fde
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
const debugLevelIndex = debugLevels.indexOf(options.debug);
function log(msg, level) {
if (level === 0) {
msg = `WARNING - ${msg}`;
msg = `WARNING - ${msg}`;
} else {
level = level || 1;
}
Expand All @@ -42,8 +42,8 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
}

const apply = (compiler) => {
const fileDependencies = [];
const contextDependencies = [];
let fileDependencies;
let contextDependencies;
const written = {};

compiler.plugin('emit', (compilation, cb) => {
Expand All @@ -53,6 +53,9 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
cb();
};

fileDependencies = [];
contextDependencies = [];

const globalRef = {
info,
debug,
Expand Down Expand Up @@ -95,9 +98,12 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
cb();
};

const compilationFileDependencies = new Set(compilation.fileDependencies);
const compilationContextDependencies = new Set(compilation.contextDependencies);

// Add file dependencies if they're not already tracked
_.forEach(fileDependencies, (file) => {
if (_.includes(compilation.fileDependencies, file)) {
if (compilationFileDependencies.has(file)) {
debug(`not adding ${file} to change tracking, because it's already tracked`);
} else {
debug(`adding ${file} to change tracking`);
Expand All @@ -107,7 +113,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) {

// Add context dependencies if they're not already tracked
_.forEach(contextDependencies, (context) => {
if (_.includes(compilation.contextDependencies, context)) {
if (compilationContextDependencies.has(context)) {
debug(`not adding ${context} to change tracking, because it's already tracked`);
} else {
debug(`adding ${context} to change tracking`);
Expand All @@ -118,7 +124,7 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
callback();
});
};

return {
apply
};
Expand Down

0 comments on commit de46fde

Please sign in to comment.