|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } |
| 4 | + |
| 5 | +var fs = _interopDefault(require('fs')); |
| 6 | +var Purgecss = _interopDefault(require('purgecss')); |
| 7 | +var webpackSources = require('webpack-sources'); |
| 8 | +var path = _interopDefault(require('path')); |
| 9 | + |
| 10 | +const entryPaths = paths => { |
| 11 | + const ret = paths || []; |
| 12 | + |
| 13 | + // Convert possible string to an array |
| 14 | + if (typeof ret === 'string') { |
| 15 | + return [ret]; |
| 16 | + } |
| 17 | + |
| 18 | + return ret; |
| 19 | +}; |
| 20 | + |
| 21 | +const flatten = paths => Array.isArray(paths) ? paths : Object.keys(paths).reduce((acc, val) => [...acc, ...paths[val]], []); |
| 22 | + |
| 23 | +const entries = (paths, chunkName) => { |
| 24 | + if (Array.isArray(paths)) { |
| 25 | + return paths; |
| 26 | + } |
| 27 | + |
| 28 | + if (!(chunkName in paths)) { |
| 29 | + return []; |
| 30 | + } |
| 31 | + |
| 32 | + const ret = paths[chunkName]; |
| 33 | + |
| 34 | + return Array.isArray(ret) ? ret : [ret]; |
| 35 | +}; |
| 36 | + |
| 37 | +const assets = (assets = [], extensions = []) => Object.keys(assets).map(name => { |
| 38 | + return extensions.indexOf(path.extname(name.indexOf('?') >= 0 ? name.split('?').slice(0, -1).join('') : name)) >= 0 && { name, asset: assets[name] }; |
| 39 | +}).filter(a => a); |
| 40 | + |
| 41 | +const files = (modules = {}, extensions = [], getter = a => a) => Object.keys(modules).map(name => { |
| 42 | + const file = getter(modules[name]); |
| 43 | + |
| 44 | + if (!file) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + return extensions.indexOf(path.extname(file)) >= 0 && file; |
| 49 | +}).filter(a => a); |
| 50 | + |
| 51 | +class PurgecssPlugin { |
| 52 | + constructor(options) { |
| 53 | + this.options = options; |
| 54 | + } |
| 55 | + |
| 56 | + apply(compiler) { |
| 57 | + compiler.plugin('this-compilation', compilation => { |
| 58 | + const entryPaths$$1 = entryPaths(this.options.paths); |
| 59 | + |
| 60 | + flatten(entryPaths$$1).forEach(p => { |
| 61 | + if (!fs.existsSync(p)) throw new Error(`Path ${p} does not exist.`); |
| 62 | + }); |
| 63 | + |
| 64 | + // Output debug information through a callback pattern |
| 65 | + // to avoid unnecessary processing |
| 66 | + const output = this.options.verbose ? messageCb => console.info(...messageCb()) : () => {}; |
| 67 | + |
| 68 | + compilation.plugin('additional-assets', cb => { |
| 69 | + // Go through chunks and purify as configured |
| 70 | + compilation.chunks.forEach(({ name: chunkName, files: files$$1, modules }) => { |
| 71 | + console.log('chunkForEach'); |
| 72 | + const assetsToPurify = assets(compilation.assets, ['.css']).filter(asset => files$$1.indexOf(asset.name) >= 0); |
| 73 | + |
| 74 | + output(() => ['Assets to purify:', assetsToPurify.map(({ name }) => name).join(', ')]); |
| 75 | + console.log(assetsToPurify); |
| 76 | + |
| 77 | + assetsToPurify.forEach(({ name, asset }) => { |
| 78 | + console.log('assetsToPurify'); |
| 79 | + const filesToSearch = entries(entryPaths$$1, chunkName).concat(files(modules, this.options.moduleExtensions || [], file => file.resource)); |
| 80 | + |
| 81 | + output(() => ['Files to search for used rules:', filesToSearch.join(', ')]); |
| 82 | + |
| 83 | + // Compile through Purify and attach to output. |
| 84 | + // This loses sourcemaps should there be any! |
| 85 | + const purgecss = new Purgecss({ |
| 86 | + content: [filesToSearch], |
| 87 | + css: [asset.source], |
| 88 | + stdin: true, |
| 89 | + info: this.options.verbose, |
| 90 | + minify: this.options.minimize |
| 91 | + }); |
| 92 | + console.log(purgecss.purge()); |
| 93 | + compilation.assets[name] = new webpackSources.ConcatSource(purgecss.purge()[0].css); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + cb(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = PurgecssPlugin; |
0 commit comments