@caweb/css-audit-webpack-plugin 1.1.0 → 2.0.1

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.
@@ -0,0 +1,25 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+
5
+ export function hasOwnProperty( obj, prop ) {
6
+ return Object.prototype.hasOwnProperty.call( obj, prop );
7
+ }
8
+
9
+
10
+ export default {
11
+ format: 'html',
12
+ filename: 'index',
13
+ outputFolder: '/audits/css',
14
+ audits: [
15
+ 'colors',
16
+ 'important',
17
+ 'display-none',
18
+ 'selectors',
19
+ 'media-queries',
20
+ 'typography',
21
+ ['property-values', 'font-size'],
22
+ ['property-values', 'padding,padding-top,padding-bottom,padding-right,padding-left'],
23
+ ['property-values', 'margin,margin-top,marin-bottom,marin-right,marin-left'],
24
+ ]
25
+ }
package/index.js CHANGED
@@ -15,7 +15,7 @@ import chalk from 'chalk';
15
15
  import { fileURLToPath, URL } from 'url';
16
16
 
17
17
  // default configuration
18
- import {default as DefaultConfig} from './default.config.js';
18
+ import {default as DefaultConfig} from './css-audit.config.js';
19
19
 
20
20
  const boldWhite = chalk.bold.white;
21
21
  const boldGreen = chalk.bold.green;
@@ -27,41 +27,64 @@ class CSSAuditPlugin {
27
27
  config = {};
28
28
 
29
29
  constructor(opts = {}) {
30
-
31
- // if no outputFolder is defined fallback to the default path
32
- if( ! opts.outputFolder ){
33
- opts.outputFolder = path.join(process.cwd(), 'audits' )
34
- // if outputfolder is set and not absolute
35
- }else if( ! path.isAbsolute(opts.outputFolder) ){
36
- opts.outputFolder = path.join(process.cwd(), opts.outputFolder );
37
- }
30
+ // the default publicPath is always the outputFolder
31
+ DefaultConfig.publicPath = DefaultConfig.outputFolder;
32
+
33
+ // the default output folder is always relative to the current working directory.
34
+ DefaultConfig.outputFolder = path.join( process.cwd(), DefaultConfig.outputFolder );
35
+
36
+ // if opts.outputFolder is defined
37
+ if( opts.outputFolder && ! path.isAbsolute(opts.outputFolder) ){
38
+ opts.publicPath = opts.outputFolder;
38
39
 
40
+ // we join the current working directory with the opts.outputFolder
41
+ opts.outputFolder = path.join( process.cwd(), opts.outputFolder );
42
+ }
43
+
39
44
  this.config = deepmerge(DefaultConfig, opts);
40
45
  }
41
46
 
42
47
  apply(compiler) {
43
48
  const staticDir = {
44
49
  directory: this.config.outputFolder,
50
+ publicPath: encodeURI(this.config.publicPath).replace(':', ''),
45
51
  watch: true
46
52
  }
53
+
47
54
  let { devServer } = compiler.options;
48
55
  let auditUrl = `${devServer.server}://${devServer.host}:${devServer.port}`;
56
+ let nodeModulePath = encodeURI(this.config.publicPath).replace(':', '') + '/node_modules';
57
+ let pathRewrite = {};
58
+ pathRewrite[`^${nodeModulePath}`] = '';
49
59
 
50
- // if dev server allows for multiple pages to be opened
51
- // add css-audit.html to open property.
52
- if( Array.isArray(devServer.open) ){
53
- devServer.open.push(`${auditUrl}/${this.config.filename}.html`)
54
- }else if( 'object' === typeof devServer.open && Array.isArray(devServer.open.target) ){
55
- devServer.open.target.push(`${auditUrl}/${this.config.filename}.html`)
60
+ let proxy = {
61
+ context: [ nodeModulePath ],
62
+ target: auditUrl,
63
+ pathRewrite,
64
+ };
65
+
66
+ // we add the proxy to the devServer
67
+ if( Array.isArray(devServer.proxy) ){
68
+ devServer.proxy.push(proxy)
69
+ }else{
70
+ devServer.proxy = [].concat(devServer.proxy, proxy );
56
71
  }
57
72
 
58
- // add our static directory
73
+ // add our static directory to the devServer
59
74
  if( Array.isArray(devServer.static) ){
60
75
  devServer.static.push(staticDir)
61
76
  }else{
62
77
  devServer.static = [].concat(devServer.static, staticDir );
63
78
  }
64
-
79
+
80
+ // if dev server allows for multiple pages to be opened
81
+ // add filename.html to open property.
82
+ if( Array.isArray(devServer.open) ){
83
+ devServer.open.push(`${staticDir.publicPath}/${this.config.filename}.html`)
84
+ }else if( 'object' === typeof devServer.open && Array.isArray(devServer.open.target) ){
85
+ devServer.open.target.push(`${staticDir.publicPath}/${this.config.filename}.html`)
86
+ }
87
+
65
88
  // we always make sure the output folder exists
66
89
  fs.mkdirSync( staticDir.directory, { recursive: true } );
67
90
 
@@ -128,7 +151,29 @@ class CSSAuditPlugin {
128
151
  })
129
152
  console.log(`<i> ${boldGreen('[webpack-dev-middleware] Running CSS Audit...')}`);
130
153
 
131
- let result = this.audit(files, this.config );
154
+ let audits = {};
155
+ this.config.audits.forEach( (audit) => {
156
+ let key = 'string' === typeof audit ? audit : audit[0];
157
+ let value = 'string' === typeof audit ? true : audit[1];
158
+
159
+ // fix key
160
+ key = key.replace(/-\w/g, (m) => m[1].toUpperCase());
161
+
162
+ // if key already exists, push value to array
163
+ if( audits.hasOwnProperty(key) ){
164
+ audits[key].push(value);
165
+ }else{
166
+ // otherwise, if the audit is an array create a new array with the value
167
+ audits[key] = ! Array.isArray(audit) ? value : [value];
168
+ }
169
+ });
170
+
171
+ let result = this.audit(files, {
172
+ format: this.config.format,
173
+ filename: this.config.filename,
174
+ outputFolder: this.config.outputFolder,
175
+ ...audits,
176
+ } );
132
177
 
133
178
  if( result ){
134
179
  // we have to inject the css-audit.update.js file into the head in order for the webpack-dev-server scripts to load.
@@ -140,7 +185,7 @@ class CSSAuditPlugin {
140
185
  )
141
186
  }
142
187
 
143
- console.log(`<i> ${boldGreen('[webpack-dev-middleware] CSS Audit can be viewed at')} ${ boldBlue(new URL(`${auditUrl}/${this.config.filename}.html`).toString()) }`);
188
+ console.log(`<i> ${boldGreen('[webpack-dev-middleware] CSS Audit can be viewed at')} ${ boldBlue(new URL(`${auditUrl}${staticDir.publicPath}/${this.config.filename}.html`).toString()) }`);
144
189
 
145
190
  callback();
146
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caweb/css-audit-webpack-plugin",
3
- "version": "1.1.0",
3
+ "version": "2.0.1",
4
4
  "description": "CAWebPublishing Webpack Plugin to run WordPress CSS Audit",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -10,8 +10,7 @@
10
10
  "files": [
11
11
  "bin",
12
12
  "sample",
13
- "css-audit.config.cjs",
14
- "default.config.js",
13
+ "css-audit.config.js",
15
14
  "index.js",
16
15
  "README.md"
17
16
  ],
@@ -48,7 +47,7 @@
48
47
  "twig": "^1.17.1"
49
48
  },
50
49
  "devDependencies": {
51
- "webpack": "^5.99.9",
50
+ "webpack": "^5.101.0",
52
51
  "webpack-cli": "^6.0.1"
53
52
  }
54
53
  }
@@ -1,5 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
-
5
- module.exports = {};
package/default.config.js DELETED
@@ -1,19 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
-
5
- export default {
6
- format: 'html',
7
- filename: 'css-audit',
8
- colors: true,
9
- important: true,
10
- displayNone: true,
11
- selectors: true,
12
- mediaQueries: true,
13
- typography: true,
14
- propertyValues: [
15
- 'font-size',
16
- 'padding,padding-top,padding-bottom,padding-right,padding-left' ,
17
- 'property-values', 'margin,margin-top,marin-bottom,marin-right,marin-left',
18
- ]
19
- };