Skip to content

Commit

Permalink
fix: error when used integrity with complex root publicPath, #45
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenBlake committed Nov 1, 2023
1 parent f7e5e04 commit 105fbad
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 2.15.2 (2023-10-31)

- fix: error when used integrity with complex root publicPath, #45 ( @SteffenBlake )

## 2.15.1 (2023-10-28)

- fix: error when used integrity with root publicPath, #42, #43
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-bundler-webpack-plugin",
"version": "2.15.1",
"version": "2.15.2",
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
"keywords": [
"html",
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ class Collection {
if (Options.isAutoPublicPath()) {
pathname = path.join(entryDirname, pathname);
} else if (Options.isRootPublicPath()) {
pathname = pathname.slice(1);
pathname = pathname.slice(Options.getPublicPath().length);
}

const assetContent = compilation.assets[pathname].source();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2 {
color: green;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(">> main");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(">> ignore integrity");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(">> vendor");
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>Test</title>

<!-- load source style -->
<link href="/test/assets/css/style.bundle.css" rel="stylesheet" integrity="sha384-gaDmgJjLpipN1Jmuc98geFnDjVqWn1fixlG0Ab90qFyUIJ4ARXlKBsMGumxTSu7E" crossorigin="use-credentials">

<!-- load source script -->
<script src="/test/assets/js/main.bundle.js" defer="defer" integrity="sha384-E4IoJ3Xutt/6tUVDjvtPwDTTlCfU5oG199UoqWShFCNx6mb4tdpcPLu7sLzNc8Pe" crossorigin="use-credentials"></script>

<!-- test: mixed order of css and js files -->
<link href="/test/assets/css/vendor.bundle.css" rel="stylesheet" integrity="sha384-S5He9W/ycWxLrmqRI+6B4V1TOIe0rK0NKdUpD8M61yjawdrgmpxiC/EwmNGkUkcd" crossorigin="use-credentials">
<script src="/test/assets/js/vendor.bundle.js" defer="defer" integrity="sha384-KOHLjGo0rQ+C8SOiWbOw8KN2r6NlfN+E6Jlre5dqkZJLv2NCk3EzaQClYi7qJBtc" crossorigin="use-credentials"></script>

<!-- test: already exists integrity attribute -->
<script src="/test/assets/js/no-integrity.bundle.js" integrity="ignore me" defer="defer"></script>

<!-- test: not processed script, because the attribute is invalid -->
<script x-src="./not-processed-script.js" defer="defer"></script>

<!-- test: not processed script, because it's loaded via url -->
<script src="//cdn-script.js" defer="defer"></script>
</head>
<body>
<h1>Hello World!</h1>

<!-- load source image -->
<img src="/test/assets/img/image.697ef306.png">
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions test/cases/integrity-publicPath-root-complex/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>Test</title>

<!-- load source style -->
<link href="./style.css" rel="stylesheet">

<!-- load source script -->
<script src="./main.js" defer="defer"></script>

<!-- test: mixed order of css and js files -->
<link href="./vendor.css" rel="stylesheet">
<script src="./vendor.js" defer="defer"></script>

<!-- test: already exists integrity attribute -->
<script src="./no-integrity.js" integrity="ignore me" defer="defer"></script>

<!-- test: not processed script, because the attribute is invalid -->
<script x-src="./not-processed-script.js" defer="defer"></script>

<!-- test: not processed script, because it's loaded via url -->
<script src="//cdn-script.js" defer="defer"></script>
</head>
<body>
<h1>Hello World!</h1>

<!-- load source image -->
<img src="./image.png">
</body>
</html>
1 change: 1 addition & 0 deletions test/cases/integrity-publicPath-root-complex/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('>> main');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('>> ignore integrity');
3 changes: 3 additions & 0 deletions test/cases/integrity-publicPath-root-complex/src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
3 changes: 3 additions & 0 deletions test/cases/integrity-publicPath-root-complex/src/vendor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2 {
color: green;
}
1 change: 1 addition & 0 deletions test/cases/integrity-publicPath-root-complex/src/vendor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('>> vendor');
51 changes: 51 additions & 0 deletions test/cases/integrity-publicPath-root-complex/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const path = require('path');
const HtmlBundlerPlugin = require('../../../');

module.exports = {
//mode: 'development',
mode: 'production',
target: 'web',

output: {
publicPath: '/test/', // test the root path
path: path.join(__dirname, 'dist/'),
crossOriginLoading: 'use-credentials', // required for test Subresource Integrity
},

plugins: [
new HtmlBundlerPlugin({
entry: {
//jquery: 'https://code.jquery.com/ui/1.13.2/jquery-ui.min.js',
//test: the relative asset file must be the same as pathname of `compilation.assets`
'pages/index': './src/index.html',
},

js: {
filename: 'assets/js/[name].bundle.js',
},

css: {
filename: 'assets/css/[name].bundle.css',
},

integrity: true, // test the `true` value
}),
],

module: {
rules: [
{
test: /\.css$/,
use: ['css-loader'],
},

{
test: /\.(png|jpe?g|ico|svg)$/,
type: 'asset/resource',
generator: {
filename: 'assets/img/[name].[hash:8][ext]',
},
},
],
},
};
1 change: 1 addition & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ describe('integrity', () => {

test('script, link, publicPath=""', () => compareFiles('integrity-publicPath-empty'));
test('script, link, publicPath="/"', () => compareFiles('integrity-publicPath-root'));
test('script, link, publicPath="/test/"', () => compareFiles('integrity-publicPath-root-complex'));
test('script, link, publicPath="auto"', () => compareFiles('integrity-publicPath-auto'));

test('split chunks', () => compareFiles('integrity-split-chunks'));
Expand Down

0 comments on commit 105fbad

Please sign in to comment.