Skip to content

Commit

Permalink
fix: error when used integrity with root publicPath, #42, #43
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Oct 28, 2023
1 parent 6f8e15b commit f7e5e04
Show file tree
Hide file tree
Showing 50 changed files with 291 additions and 8 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.1 (2023-10-28)

- fix: error when used integrity with root publicPath, #42, #43

## 2.15.0 (2023-10-18)

- feat: add `parsedValue` argument as an array of parsed filenames w/o URL query, in the `filter()` function of the `sources`
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.0",
"version": "2.15.1",
"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
15 changes: 12 additions & 3 deletions src/Plugin/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ class Collection {

if (!rawSource) return;

const entryDirname = path.dirname(entryFilename);
const importedStyles = [];
let hasInlineSvg = false;
let content = rawSource.source();
Expand All @@ -843,8 +844,16 @@ class Collection {

// 1.1 compute CSS integrity
if (isIntegrity && !inline) {
const assetContent = compilation.assets[asset.assetFile].source();
asset.integrity = Integrity.getIntegrity(assetContent, asset.assetFile);
// path to asset relative by output.path
let pathname = asset.assetFile;
if (Options.isAutoPublicPath()) {
pathname = path.join(entryDirname, pathname);
} else if (Options.isRootPublicPath()) {
pathname = pathname.slice(1);
}

const assetContent = compilation.assets[pathname].source();
asset.integrity = Integrity.getIntegrity(assetContent, pathname);
assetIntegrity.set(asset.assetFile, asset.integrity);

if (!parseOptions.has(type)) {
Expand All @@ -864,7 +873,7 @@ class Collection {
if (!chunk.inline) {
const assetContent = compilation.assets[chunk.chunkFile].source();
chunk.integrity = Integrity.getIntegrity(assetContent, chunk.chunkFile);
assetIntegrity.set(chunk.chunkFile, chunk.integrity);
assetIntegrity.set(chunk.assetFile, chunk.integrity);

if (!parseOptions.has(type)) {
parseOptions.set(type, {
Expand Down
10 changes: 7 additions & 3 deletions src/Plugin/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class Options {

this.autoPublicPath = false;
this.isUrlPublicPath = false;
this.rootPublicPath = false;
this.isRelativePublicPath = false;
this.webpackPublicPath = publicPath;

Expand All @@ -231,6 +232,8 @@ class Options {
this.isUrlPublicPath = true;
} else if (!publicPath.startsWith('/')) {
this.isRelativePublicPath = true;
} else if (publicPath.startsWith('/')) {
this.rootPublicPath = true;
}
}

Expand Down Expand Up @@ -378,13 +381,14 @@ class Options {
return this.options.preload != null && this.options.preload !== false;
}

/**
* @return {boolean}
*/
static isAutoPublicPath() {
return this.autoPublicPath === true;
}

static isRootPublicPath() {
return this.rootPublicPath === true;
}

/**
* @return {string}
*/
Expand Down
31 changes: 31 additions & 0 deletions test/cases/integrity-publicPath-auto/expected/pages/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="../assets/css/style.bundle.css" rel="stylesheet" integrity="sha384-gaDmgJjLpipN1Jmuc98geFnDjVqWn1fixlG0Ab90qFyUIJ4ARXlKBsMGumxTSu7E" crossorigin="use-credentials">

<!-- load source script -->
<script src="../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="../assets/css/vendor.bundle.css" rel="stylesheet" integrity="sha384-S5He9W/ycWxLrmqRI+6B4V1TOIe0rK0NKdUpD8M61yjawdrgmpxiC/EwmNGkUkcd" crossorigin="use-credentials">
<script src="../assets/js/vendor.bundle.js" defer="defer" integrity="sha384-KOHLjGo0rQ+C8SOiWbOw8KN2r6NlfN+E6Jlre5dqkZJLv2NCk3EzaQClYi7qJBtc" crossorigin="use-credentials"></script>

<!-- test: already exists integrity attribute -->
<script src="../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="../assets/img/image.697ef306.png">
</body>
</html>
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions test/cases/integrity-publicPath-auto/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: 'auto', // test empty
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]',
},
},
],
},
};
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");
File renamed without changes.
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-empty/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-empty/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('>> main');
1 change: 1 addition & 0 deletions test/cases/integrity-publicPath-empty/src/no-integrity.js
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-empty/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-empty/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-empty/src/vendor.js
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
Expand Up @@ -7,6 +7,7 @@ module.exports = {
target: 'web',

output: {
publicPath: '', // test empty
path: path.join(__dirname, 'dist/'),
crossOriginLoading: 'use-credentials', // required for test Subresource Integrity
},
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");
31 changes: 31 additions & 0 deletions test/cases/integrity-publicPath-root/expected/pages/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="/assets/css/style.bundle.css" rel="stylesheet" integrity="sha384-gaDmgJjLpipN1Jmuc98geFnDjVqWn1fixlG0Ab90qFyUIJ4ARXlKBsMGumxTSu7E" crossorigin="use-credentials">

<!-- load source script -->
<script src="/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="/assets/css/vendor.bundle.css" rel="stylesheet" integrity="sha384-S5He9W/ycWxLrmqRI+6B4V1TOIe0rK0NKdUpD8M61yjawdrgmpxiC/EwmNGkUkcd" crossorigin="use-credentials">
<script src="/assets/js/vendor.bundle.js" defer="defer" integrity="sha384-KOHLjGo0rQ+C8SOiWbOw8KN2r6NlfN+E6Jlre5dqkZJLv2NCk3EzaQClYi7qJBtc" crossorigin="use-credentials"></script>

<!-- test: already exists integrity attribute -->
<script src="/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="/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/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/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('>> main');
1 change: 1 addition & 0 deletions test/cases/integrity-publicPath-root/src/no-integrity.js
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/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/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/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/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 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]',
},
},
],
},
};
Loading

0 comments on commit f7e5e04

Please sign in to comment.