Skip to content

Commit 36f58cb

Browse files
committed
chore: Enhance Webpack configuration and remove unused dependencies
- Added `inline-script-csp-html-webpack-plugin` for improved CSP management with inline script support. - Removed unused dependencies and cleaned up `package-lock.json`. - Updated SCSS for better modularity and code readability. - Merged `linkedData.ejs` into `index.ejs` to simplify JSON-LD implementation. - Enhanced `HtmlWebpackPlugin` minification settings and added support for processing JSON-LD scripts. - Improved CSP configuration with stricter hashing and enabled `script-src` and `style-src` hashing. - Optimized Terser configuration for better compression with additional passes.
1 parent 078baff commit 36f58cb

File tree

7 files changed

+164
-253
lines changed

7 files changed

+164
-253
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const cheerio = require('cheerio');
2+
const CspHtmlWebpackPlugin = require('csp-html-webpack-plugin');
3+
const flatten = require('lodash/flatten');
4+
const get = require('lodash/get');
5+
6+
/**
7+
* InlineScriptCspHtmlWebpackPlugin
8+
* An extension of CspHtmlWebpackPlugin to handle Content Security Policies (CSPs).
9+
* This class only modifies a single property (`_useHtmlParser2`) in the Cheerio configuration
10+
* to customize how HTML is parsed.
11+
*/
12+
class InlineScriptCspHtmlWebpackPlugin extends CspHtmlWebpackPlugin {
13+
/**
14+
* Constructor
15+
* Calls the base class constructor to set up the plugin with user-defined or default policies and options.
16+
* @param {object} policy - CSP policy object, typically defining 'script-src' and 'style-src'.
17+
* @param {object} additionalOpts - Additional options for nonce/hash generation and processing.
18+
*/
19+
constructor(policy = {}, additionalOpts = {}) {
20+
super(policy, additionalOpts);
21+
}
22+
23+
/**
24+
* Processes HtmlWebpackPlugin's HTML output to inject the Content Security Policy.
25+
* The key difference from the base class is setting `_useHtmlParser2: false` in the Cheerio configuration.
26+
* @param {object} compilation - Webpack's compilation object.
27+
* @param {object} htmlPluginData - Data object from HtmlWebpackPlugin containing the generated HTML.
28+
* @param {function} compileCb - Callback to continue Webpack's compilation process.
29+
*/
30+
processCsp(compilation, htmlPluginData, compileCb) {
31+
const $ = cheerio.load(htmlPluginData.html, {
32+
decodeEntities: false,
33+
_useHtmlParser2: false, // *** Changed from 'true' in the base class to 'false' ***
34+
xmlMode: get(htmlPluginData, 'plugin.options.xhtml', false),
35+
});
36+
37+
// if not enabled, remove the empty tag
38+
if (!this.isEnabled(htmlPluginData)) {
39+
return compileCb(null, htmlPluginData);
40+
}
41+
42+
// get all nonces for script and style tags
43+
const scriptNonce = this.setNonce($, 'script-src', 'script[src]');
44+
const styleNonce = this.setNonce($, 'style-src', 'link[rel="stylesheet"]');
45+
46+
// get all shas for script and style tags
47+
const scriptShas = this.getShas($, 'script-src', 'script:not([src])');
48+
const styleShas = this.getShas($, 'style-src', 'style:not([href])');
49+
50+
const builtPolicy = this.buildPolicy({
51+
...this.policy,
52+
'script-src': flatten([this.policy['script-src']]).concat(
53+
scriptShas,
54+
scriptNonce
55+
),
56+
'style-src': flatten([this.policy['style-src']]).concat(
57+
styleShas,
58+
styleNonce
59+
),
60+
});
61+
62+
this.processFn(builtPolicy, htmlPluginData, $, compilation);
63+
64+
return compileCb(null, htmlPluginData);
65+
}
66+
}
67+
68+
module.exports = InlineScriptCspHtmlWebpackPlugin;

package-lock.json

Lines changed: 3 additions & 171 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Personal website and portfolio",
55
"main": "src/index.html",
66
"scripts": {
7-
"build": "webpack --mode production --optimization-minimize --config webpack.config.js",
7+
"build": "webpack --mode production --config webpack.config.js",
88
"start": "npx webpack serve --mode production --optimization-minimize --config webpack.config.js --stats verbose"
99
},
1010
"repository": {
@@ -28,7 +28,6 @@
2828
"html-inline-css-webpack-plugin": "^1.11.1",
2929
"html-inline-script-webpack-plugin": "^3.2.1",
3030
"html-loader": "^5.1.0",
31-
"html-minifier": "^4.0.0",
3231
"html-minimizer-webpack-plugin": "^5.0.0",
3332
"html-webpack-plugin": "^5.6.3",
3433
"image-minimizer-webpack-plugin": "^4.1.1",

0 commit comments

Comments
 (0)