purgecss-webpack-plugin 6.0.0-alpha.0 → 7.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1,140 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { PurgeCSS, defaultOptions } from 'purgecss';
|
|
4
|
+
import { sources } from 'webpack';
|
|
5
|
+
|
|
6
|
+
const styleExtensions = [".css", ".scss", ".styl", ".sass", ".less"];
|
|
7
|
+
const pluginName = "PurgeCSS";
|
|
8
|
+
/**
|
|
9
|
+
* Get the filename without ?hash
|
|
10
|
+
*
|
|
11
|
+
* @param fileName - file name
|
|
12
|
+
*/
|
|
13
|
+
function getFormattedFilename(fileName) {
|
|
14
|
+
if (fileName.includes("?")) {
|
|
15
|
+
return fileName.split("?").slice(0, -1).join("");
|
|
16
|
+
}
|
|
17
|
+
return fileName;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns true if the filename is of types of one of the specified extensions
|
|
21
|
+
*
|
|
22
|
+
* @param filename - file name
|
|
23
|
+
* @param extensions - extensions
|
|
24
|
+
*/
|
|
25
|
+
function isFileOfTypes(filename, extensions) {
|
|
26
|
+
const extension = path.extname(getFormattedFilename(filename));
|
|
27
|
+
return extensions.includes(extension);
|
|
28
|
+
}
|
|
29
|
+
function getPurgeCSSOptions(pluginOptions, filesToSearch, asset, fileName, sourceMap) {
|
|
30
|
+
const options = {
|
|
31
|
+
...defaultOptions,
|
|
32
|
+
...pluginOptions,
|
|
33
|
+
content: filesToSearch,
|
|
34
|
+
css: [
|
|
35
|
+
{
|
|
36
|
+
raw: asset.source().toString(),
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
if (typeof options.safelist === "function") {
|
|
41
|
+
options.safelist = options.safelist();
|
|
42
|
+
}
|
|
43
|
+
if (typeof options.blocklist === "function") {
|
|
44
|
+
options.blocklist = options.blocklist();
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
content: options.content,
|
|
48
|
+
css: options.css,
|
|
49
|
+
defaultExtractor: options.defaultExtractor,
|
|
50
|
+
extractors: options.extractors,
|
|
51
|
+
fontFace: options.fontFace,
|
|
52
|
+
keyframes: options.keyframes,
|
|
53
|
+
output: options.output,
|
|
54
|
+
rejected: options.rejected,
|
|
55
|
+
variables: options.variables,
|
|
56
|
+
safelist: options.safelist,
|
|
57
|
+
blocklist: options.blocklist,
|
|
58
|
+
sourceMap: sourceMap ? { inline: false, to: fileName } : false,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create the Source instance result of PurgeCSS
|
|
63
|
+
*
|
|
64
|
+
* @param name - asset name
|
|
65
|
+
* @param asset - webpack asset
|
|
66
|
+
* @param purgeResult - result of PurgeCSS purge method
|
|
67
|
+
* @param sourceMap - wether sourceMap is enabled
|
|
68
|
+
* @returns the new Source
|
|
69
|
+
*/
|
|
70
|
+
function createSource(name, asset, purgeResult, sourceMap) {
|
|
71
|
+
if (!sourceMap || !purgeResult.sourceMap) {
|
|
72
|
+
return new sources.RawSource(purgeResult.css);
|
|
73
|
+
}
|
|
74
|
+
const { source, map } = asset.sourceAndMap();
|
|
75
|
+
return new sources.SourceMapSource(purgeResult.css, name, purgeResult.sourceMap, source.toString(), map, false);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
class PurgeCSSPlugin {
|
|
81
|
+
constructor(options) {
|
|
82
|
+
this.purgedStats = {};
|
|
83
|
+
this.options = options;
|
|
84
|
+
}
|
|
85
|
+
apply(compiler) {
|
|
86
|
+
compiler.hooks.compilation.tap(pluginName, this.initializePlugin.bind(this));
|
|
87
|
+
}
|
|
88
|
+
initializePlugin(compilation) {
|
|
89
|
+
compilation.hooks.additionalAssets.tapPromise(pluginName, async () => {
|
|
90
|
+
let configFileOptions;
|
|
91
|
+
try {
|
|
92
|
+
const t = path.resolve(process.cwd(), "purgecss.config.js");
|
|
93
|
+
configFileOptions = await import(t);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// no config file present
|
|
97
|
+
}
|
|
98
|
+
this.options = {
|
|
99
|
+
...(configFileOptions ? configFileOptions : {}),
|
|
100
|
+
...this.options,
|
|
101
|
+
};
|
|
102
|
+
const entryPaths = typeof this.options.paths === "function"
|
|
103
|
+
? this.options.paths()
|
|
104
|
+
: this.options.paths;
|
|
105
|
+
entryPaths.forEach((p) => {
|
|
106
|
+
if (!fs.existsSync(p))
|
|
107
|
+
throw new Error(`Path ${p} does not exist.`);
|
|
108
|
+
});
|
|
109
|
+
return this.runPluginHook(compilation, entryPaths);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async runPluginHook(compilation, entryPaths) {
|
|
113
|
+
const assetsFromCompilation = Object.entries(compilation.assets).filter(([name]) => {
|
|
114
|
+
return isFileOfTypes(name, [".css"]);
|
|
115
|
+
});
|
|
116
|
+
for (const chunk of compilation.chunks) {
|
|
117
|
+
const assetsToPurge = assetsFromCompilation.filter(([name]) => {
|
|
118
|
+
if (this.options.only) {
|
|
119
|
+
if (!this.options.only.some((only) => name.includes(only))) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return chunk.files.has(name);
|
|
124
|
+
});
|
|
125
|
+
for (const [name, asset] of assetsToPurge) {
|
|
126
|
+
const filesToSearch = entryPaths.filter((v) => !styleExtensions.some((ext) => v.endsWith(ext)));
|
|
127
|
+
const sourceMapEnabled = !!compilation.compiler.options.devtool;
|
|
128
|
+
const purgeCSSOptions = getPurgeCSSOptions(this.options, filesToSearch, asset, name, sourceMapEnabled);
|
|
129
|
+
const purgecss = await new PurgeCSS().purge(purgeCSSOptions);
|
|
130
|
+
const purged = purgecss[0];
|
|
131
|
+
if (purged.rejected) {
|
|
132
|
+
this.purgedStats[name] = purged.rejected;
|
|
133
|
+
}
|
|
134
|
+
compilation.updateAsset(name, createSource(name, asset, purged, sourceMapEnabled));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { PurgeCSSPlugin };
|
|
@@ -1 +1,162 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
var purgecss = require('purgecss');
|
|
6
|
+
var webpack = require('webpack');
|
|
7
|
+
|
|
8
|
+
function _interopNamespaceDefault(e) {
|
|
9
|
+
var n = Object.create(null);
|
|
10
|
+
if (e) {
|
|
11
|
+
Object.keys(e).forEach(function (k) {
|
|
12
|
+
if (k !== 'default') {
|
|
13
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return e[k]; }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
n.default = e;
|
|
22
|
+
return Object.freeze(n);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
26
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
27
|
+
|
|
28
|
+
const styleExtensions = [".css", ".scss", ".styl", ".sass", ".less"];
|
|
29
|
+
const pluginName = "PurgeCSS";
|
|
30
|
+
/**
|
|
31
|
+
* Get the filename without ?hash
|
|
32
|
+
*
|
|
33
|
+
* @param fileName - file name
|
|
34
|
+
*/
|
|
35
|
+
function getFormattedFilename(fileName) {
|
|
36
|
+
if (fileName.includes("?")) {
|
|
37
|
+
return fileName.split("?").slice(0, -1).join("");
|
|
38
|
+
}
|
|
39
|
+
return fileName;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns true if the filename is of types of one of the specified extensions
|
|
43
|
+
*
|
|
44
|
+
* @param filename - file name
|
|
45
|
+
* @param extensions - extensions
|
|
46
|
+
*/
|
|
47
|
+
function isFileOfTypes(filename, extensions) {
|
|
48
|
+
const extension = path__namespace.extname(getFormattedFilename(filename));
|
|
49
|
+
return extensions.includes(extension);
|
|
50
|
+
}
|
|
51
|
+
function getPurgeCSSOptions(pluginOptions, filesToSearch, asset, fileName, sourceMap) {
|
|
52
|
+
const options = {
|
|
53
|
+
...purgecss.defaultOptions,
|
|
54
|
+
...pluginOptions,
|
|
55
|
+
content: filesToSearch,
|
|
56
|
+
css: [
|
|
57
|
+
{
|
|
58
|
+
raw: asset.source().toString(),
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
if (typeof options.safelist === "function") {
|
|
63
|
+
options.safelist = options.safelist();
|
|
64
|
+
}
|
|
65
|
+
if (typeof options.blocklist === "function") {
|
|
66
|
+
options.blocklist = options.blocklist();
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
content: options.content,
|
|
70
|
+
css: options.css,
|
|
71
|
+
defaultExtractor: options.defaultExtractor,
|
|
72
|
+
extractors: options.extractors,
|
|
73
|
+
fontFace: options.fontFace,
|
|
74
|
+
keyframes: options.keyframes,
|
|
75
|
+
output: options.output,
|
|
76
|
+
rejected: options.rejected,
|
|
77
|
+
variables: options.variables,
|
|
78
|
+
safelist: options.safelist,
|
|
79
|
+
blocklist: options.blocklist,
|
|
80
|
+
sourceMap: sourceMap ? { inline: false, to: fileName } : false,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create the Source instance result of PurgeCSS
|
|
85
|
+
*
|
|
86
|
+
* @param name - asset name
|
|
87
|
+
* @param asset - webpack asset
|
|
88
|
+
* @param purgeResult - result of PurgeCSS purge method
|
|
89
|
+
* @param sourceMap - wether sourceMap is enabled
|
|
90
|
+
* @returns the new Source
|
|
91
|
+
*/
|
|
92
|
+
function createSource(name, asset, purgeResult, sourceMap) {
|
|
93
|
+
if (!sourceMap || !purgeResult.sourceMap) {
|
|
94
|
+
return new webpack.sources.RawSource(purgeResult.css);
|
|
95
|
+
}
|
|
96
|
+
const { source, map } = asset.sourceAndMap();
|
|
97
|
+
return new webpack.sources.SourceMapSource(purgeResult.css, name, purgeResult.sourceMap, source.toString(), map, false);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
class PurgeCSSPlugin {
|
|
103
|
+
constructor(options) {
|
|
104
|
+
this.purgedStats = {};
|
|
105
|
+
this.options = options;
|
|
106
|
+
}
|
|
107
|
+
apply(compiler) {
|
|
108
|
+
compiler.hooks.compilation.tap(pluginName, this.initializePlugin.bind(this));
|
|
109
|
+
}
|
|
110
|
+
initializePlugin(compilation) {
|
|
111
|
+
compilation.hooks.additionalAssets.tapPromise(pluginName, async () => {
|
|
112
|
+
let configFileOptions;
|
|
113
|
+
try {
|
|
114
|
+
const t = path__namespace.resolve(process.cwd(), "purgecss.config.js");
|
|
115
|
+
configFileOptions = await import(t);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// no config file present
|
|
119
|
+
}
|
|
120
|
+
this.options = {
|
|
121
|
+
...(configFileOptions ? configFileOptions : {}),
|
|
122
|
+
...this.options,
|
|
123
|
+
};
|
|
124
|
+
const entryPaths = typeof this.options.paths === "function"
|
|
125
|
+
? this.options.paths()
|
|
126
|
+
: this.options.paths;
|
|
127
|
+
entryPaths.forEach((p) => {
|
|
128
|
+
if (!fs__namespace.existsSync(p))
|
|
129
|
+
throw new Error(`Path ${p} does not exist.`);
|
|
130
|
+
});
|
|
131
|
+
return this.runPluginHook(compilation, entryPaths);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async runPluginHook(compilation, entryPaths) {
|
|
135
|
+
const assetsFromCompilation = Object.entries(compilation.assets).filter(([name]) => {
|
|
136
|
+
return isFileOfTypes(name, [".css"]);
|
|
137
|
+
});
|
|
138
|
+
for (const chunk of compilation.chunks) {
|
|
139
|
+
const assetsToPurge = assetsFromCompilation.filter(([name]) => {
|
|
140
|
+
if (this.options.only) {
|
|
141
|
+
if (!this.options.only.some((only) => name.includes(only))) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return chunk.files.has(name);
|
|
146
|
+
});
|
|
147
|
+
for (const [name, asset] of assetsToPurge) {
|
|
148
|
+
const filesToSearch = entryPaths.filter((v) => !styleExtensions.some((ext) => v.endsWith(ext)));
|
|
149
|
+
const sourceMapEnabled = !!compilation.compiler.options.devtool;
|
|
150
|
+
const purgeCSSOptions = getPurgeCSSOptions(this.options, filesToSearch, asset, name, sourceMapEnabled);
|
|
151
|
+
const purgecss$1 = await new purgecss.PurgeCSS().purge(purgeCSSOptions);
|
|
152
|
+
const purged = purgecss$1[0];
|
|
153
|
+
if (purged.rejected) {
|
|
154
|
+
this.purgedStats[name] = purged.rejected;
|
|
155
|
+
}
|
|
156
|
+
compilation.updateAsset(name, createSource(name, asset, purged, sourceMapEnabled));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
exports.PurgeCSSPlugin = PurgeCSSPlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "purgecss-webpack-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.1-alpha.0",
|
|
4
4
|
"description": "PurgeCSS plugin for webpack - Remove unused css",
|
|
5
5
|
"author": "Ffloriel",
|
|
6
6
|
"homepage": "https://purgecss.com",
|
|
@@ -37,16 +37,16 @@
|
|
|
37
37
|
"test": "jest"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"purgecss": "^
|
|
41
|
-
"webpack": ">=5.
|
|
40
|
+
"purgecss": "^7.0.1-alpha.0",
|
|
41
|
+
"webpack": ">=5.95.0"
|
|
42
42
|
},
|
|
43
43
|
"bugs": {
|
|
44
44
|
"url": "https://github.com/FullHuman/purgecss/issues"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@types/webpack-sources": "^3.2.
|
|
48
|
-
"css-loader": "^
|
|
49
|
-
"mini-css-extract-plugin": "^2.
|
|
47
|
+
"@types/webpack-sources": "^3.2.3",
|
|
48
|
+
"css-loader": "^7.1.2",
|
|
49
|
+
"mini-css-extract-plugin": "^2.9.1"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"webpack": ">=5.0.0"
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"access": "public",
|
|
56
56
|
"registry": "https://registry.npmjs.org/"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "5a51b05a191ba48b5e55440ac2e66822f9e182fe"
|
|
59
59
|
}
|