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

Commit 4c25f08

Browse files
committed
rollup config & build
1 parent 15ec540 commit 4c25f08

File tree

3 files changed

+213
-15
lines changed

3 files changed

+213
-15
lines changed

lib/purgecss-webpack-plugin.es.js

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

lib/purgecss-webpack-plugin.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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;

rollup.config.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
import babel from "rollup-plugin-babel"
2-
import commonjs from "rollup-plugin-commonjs"
3-
import resolve from "rollup-plugin-node-resolve"
1+
import babel from 'rollup-plugin-babel'
2+
import commonjs from 'rollup-plugin-commonjs'
3+
import resolve from 'rollup-plugin-node-resolve'
44

55
export default {
6-
entry: "./src/index.js",
6+
entry: './src/index.js',
77
targets: [
88
{
9-
dest: "./lib/purgecss-webpack-plugin.es.js",
10-
format: "es"
9+
dest: './lib/purgecss-webpack-plugin.es.js',
10+
format: 'es'
1111
},
1212
{
13-
dest: "./lib/purgecss-webpack-plugin.js",
14-
format: "cjs"
13+
dest: './lib/purgecss-webpack-plugin.js',
14+
format: 'cjs'
1515
}
1616
],
17-
plugins: [
18-
resolve(), commonjs(), babel()
19-
],
20-
external: [
21-
"purgecss"
22-
]
23-
}
17+
plugins: [resolve(), commonjs(), babel()],
18+
external: ['purgecss', 'webpack-sources']
19+
}

0 commit comments

Comments
 (0)