Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Commit f42d2b0

Browse files
committed
v0.18
1 parent 4ffaa71 commit f42d2b0

File tree

3 files changed

+225
-203
lines changed

3 files changed

+225
-203
lines changed

lib/purgecss-webpack-plugin.es.js

Lines changed: 112 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ var toConsumableArray = function (arr) {
9494
var entryPaths = function entryPaths(paths) {
9595
var ret = paths || [];
9696

97+
if (typeof ret === 'function') {
98+
ret = ret();
99+
}
100+
97101
// Convert possible string to an array
98102
if (typeof ret === 'string') {
99-
return [ret];
103+
ret = [ret];
100104
}
101105

102106
return ret;
@@ -123,119 +127,126 @@ var entries = function entries(paths, chunkName) {
123127
};
124128

125129
var assets = function assets() {
126-
var assets = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
127-
var extensions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
128-
return Object.keys(assets).map(function (name) {
129-
return extensions.indexOf(path.extname(name.indexOf('?') >= 0 ? name.split('?').slice(0, -1).join('') : name)) >= 0 && { name: name, asset: assets[name] };
130-
}).filter(function (a) {
131-
return a;
132-
});
130+
var assets = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
131+
var extensions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
132+
return Object.keys(assets).map(function (name) {
133+
return extensions.indexOf(path.extname(name.indexOf('?') >= 0 ? name.split('?').slice(0, -1).join('') : name)) >= 0 && { name: name, asset: assets[name] };
134+
}).filter(function (a) {
135+
return a;
136+
});
133137
};
134138

135139
var files = function files(chunk) {
136-
var extensions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
137-
var getter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (a) {
138-
return a;
139-
};
140-
return chunk.mapModules(function (module) {
141-
var file = getter(module);
142-
if (!file) return null;
143-
return extensions.indexOf(path.extname(file)) >= 0 && file;
144-
}).filter(function (a) {
145-
return a;
146-
});
140+
var extensions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
141+
var getter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (a) {
142+
return a;
143+
};
144+
return chunk.mapModules(function (module) {
145+
var file = getter(module);
146+
if (!file) return null;
147+
return extensions.indexOf(path.extname(file)) >= 0 && file;
148+
}).filter(function (a) {
149+
return a;
150+
});
147151
};
148152

149153
var styleExtensions = ['.css', '.scss', '.styl', '.sass', '.less'];
150154

151155
var PurgecssPlugin = function () {
152-
function PurgecssPlugin(options) {
153-
classCallCheck(this, PurgecssPlugin);
156+
function PurgecssPlugin(options) {
157+
classCallCheck(this, PurgecssPlugin);
154158

155-
this.options = options;
156-
}
159+
this.options = options;
160+
}
161+
162+
createClass(PurgecssPlugin, [{
163+
key: 'apply',
164+
value: function apply(compiler) {
165+
var _this = this;
157166

158-
createClass(PurgecssPlugin, [{
159-
key: 'apply',
160-
value: function apply(compiler) {
161-
var _this = this;
162-
163-
compiler.plugin('this-compilation', function (compilation) {
164-
var entryPaths$$1 = entryPaths(_this.options.paths);
165-
166-
flatten(entryPaths$$1).forEach(function (p) {
167-
if (!fs.existsSync(p)) throw new Error('Path ' + p + ' does not exist.');
168-
});
169-
170-
compilation.plugin('additional-assets', function (cb) {
171-
var assetsFromCompilation = assets(compilation.assets, ['.css']);
172-
// Go through chunks and purge as configured
173-
compilation.chunks.forEach(function (chunk) {
174-
var chunkName = chunk.name,
175-
files$$1 = chunk.files;
176-
177-
var assetsToPurge = assetsFromCompilation.filter(function (asset) {
178-
if (_this.options.only) {
179-
return [].concat(_this.options.only).some(function (only) {
180-
return asset.name.indexOf(only) >= 0;
167+
compiler.plugin('this-compilation', function (compilation) {
168+
var entryPaths$$1 = entryPaths(_this.options.paths);
169+
170+
flatten(entryPaths$$1).forEach(function (p) {
171+
if (!fs.existsSync(p)) throw new Error('Path ' + p + ' does not exist.');
181172
});
182-
} else {
183-
return files$$1.indexOf(asset.name) >= 0;
184-
}
185-
});
186173

187-
assetsToPurge.forEach(function (_ref) {
188-
var name = _ref.name,
189-
asset = _ref.asset;
190-
191-
var filesToSearch = entries(entryPaths$$1, chunkName).concat(files(chunk, _this.options.moduleExtensions || [], function (file) {
192-
return file.resource;
193-
})).filter(function (v) {
194-
var _iteratorNormalCompletion = true;
195-
var _didIteratorError = false;
196-
var _iteratorError = undefined;
197-
198-
try {
199-
for (var _iterator = styleExtensions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
200-
var ext = _step.value;
201-
202-
if (v.endsWith(ext)) return false;
203-
}
204-
} catch (err) {
205-
_didIteratorError = true;
206-
_iteratorError = err;
207-
} finally {
208-
try {
209-
if (!_iteratorNormalCompletion && _iterator.return) {
210-
_iterator.return();
211-
}
212-
} finally {
213-
if (_didIteratorError) {
214-
throw _iteratorError;
215-
}
216-
}
217-
}
218-
219-
return true;
220-
});
221-
222-
// Compile through Purgecss and attach to output.
223-
// This loses sourcemaps should there be any!
224-
var purgecss = new Purgecss(_extends({}, _this.options, {
225-
content: filesToSearch,
226-
css: [asset.source()],
227-
stdin: true
228-
}));
229-
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css);
174+
compilation.plugin('additional-assets', function (cb) {
175+
var assetsFromCompilation = assets(compilation.assets, ['.css']);
176+
// Go through chunks and purge as configured
177+
compilation.chunks.forEach(function (chunk) {
178+
var chunkName = chunk.name,
179+
files$$1 = chunk.files;
180+
181+
var assetsToPurge = assetsFromCompilation.filter(function (asset) {
182+
if (_this.options.only) {
183+
return [].concat(_this.options.only).some(function (only) {
184+
return asset.name.indexOf(only) >= 0;
185+
});
186+
} else {
187+
return files$$1.indexOf(asset.name) >= 0;
188+
}
189+
});
190+
191+
assetsToPurge.forEach(function (_ref) {
192+
var name = _ref.name,
193+
asset = _ref.asset;
194+
195+
var filesToSearch = entries(entryPaths$$1, chunkName).concat(files(chunk, _this.options.moduleExtensions || [], function (file) {
196+
return file.resource;
197+
})).filter(function (v) {
198+
var _iteratorNormalCompletion = true;
199+
var _didIteratorError = false;
200+
var _iteratorError = undefined;
201+
202+
try {
203+
for (var _iterator = styleExtensions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
204+
var ext = _step.value;
205+
206+
if (v.endsWith(ext)) return false;
207+
}
208+
} catch (err) {
209+
_didIteratorError = true;
210+
_iteratorError = err;
211+
} finally {
212+
try {
213+
if (!_iteratorNormalCompletion && _iterator.return) {
214+
_iterator.return();
215+
}
216+
} finally {
217+
if (_didIteratorError) {
218+
throw _iteratorError;
219+
}
220+
}
221+
}
222+
223+
return true;
224+
});
225+
226+
// Compile through Purgecss and attach to output.
227+
// This loses sourcemaps should there be any!
228+
var options = _extends({}, _this.options, {
229+
content: filesToSearch,
230+
css: [asset.source()],
231+
stdin: true
232+
});
233+
if (typeof options.whitelist === 'function') {
234+
options.whitelist = options.whitelist();
235+
}
236+
if (typeof options.whitelistPatterns === 'function') {
237+
options.whitelistPatterns = options.whitelistPatterns();
238+
}
239+
var purgecss = new Purgecss(options);
240+
compilation.assets[name] = new ConcatSource(purgecss.purge()[0].css);
241+
});
242+
});
243+
244+
cb();
245+
});
230246
});
231-
});
232-
233-
cb();
234-
});
235-
});
236-
}
237-
}]);
238-
return PurgecssPlugin;
247+
}
248+
}]);
249+
return PurgecssPlugin;
239250
}();
240251

241252
export default PurgecssPlugin;

0 commit comments

Comments
 (0)