From 105fbadf85e1978dc5bbccfb2a79fdca930693ac Mon Sep 17 00:00:00 2001 From: Steffen Cole Blake Date: Wed, 1 Nov 2023 00:07:36 -0600 Subject: [PATCH] fix: error when used integrity with complex root publicPath, #45 --- CHANGELOG.md | 4 ++ package.json | 2 +- src/Plugin/Collection.js | 2 +- .../expected/assets/css/style.bundle.css | 3 ++ .../expected/assets/css/vendor.bundle.css | 3 ++ .../expected/assets/img/image.697ef306.png | Bin 0 -> 1250 bytes .../expected/assets/js/main.bundle.js | 1 + .../expected/assets/js/no-integrity.bundle.js | 1 + .../expected/assets/js/vendor.bundle.js | 1 + .../expected/pages/index.html | 31 +++++++++++ .../src/image.png | Bin 0 -> 1250 bytes .../src/index.html | 31 +++++++++++ .../src/main.js | 1 + .../src/no-integrity.js | 1 + .../src/style.css | 3 ++ .../src/vendor.css | 3 ++ .../src/vendor.js | 1 + .../webpack.config.js | 51 ++++++++++++++++++ test/integration.test.js | 1 + 19 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 test/cases/integrity-publicPath-root-complex/expected/assets/css/style.bundle.css create mode 100644 test/cases/integrity-publicPath-root-complex/expected/assets/css/vendor.bundle.css create mode 100644 test/cases/integrity-publicPath-root-complex/expected/assets/img/image.697ef306.png create mode 100644 test/cases/integrity-publicPath-root-complex/expected/assets/js/main.bundle.js create mode 100644 test/cases/integrity-publicPath-root-complex/expected/assets/js/no-integrity.bundle.js create mode 100644 test/cases/integrity-publicPath-root-complex/expected/assets/js/vendor.bundle.js create mode 100644 test/cases/integrity-publicPath-root-complex/expected/pages/index.html create mode 100644 test/cases/integrity-publicPath-root-complex/src/image.png create mode 100644 test/cases/integrity-publicPath-root-complex/src/index.html create mode 100644 test/cases/integrity-publicPath-root-complex/src/main.js create mode 100644 test/cases/integrity-publicPath-root-complex/src/no-integrity.js create mode 100644 test/cases/integrity-publicPath-root-complex/src/style.css create mode 100644 test/cases/integrity-publicPath-root-complex/src/vendor.css create mode 100644 test/cases/integrity-publicPath-root-complex/src/vendor.js create mode 100644 test/cases/integrity-publicPath-root-complex/webpack.config.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d1f9d9a..09854a48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index 341668dd..1104251f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Plugin/Collection.js b/src/Plugin/Collection.js index 1a7c17f2..91291352 100644 --- a/src/Plugin/Collection.js +++ b/src/Plugin/Collection.js @@ -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(); diff --git a/test/cases/integrity-publicPath-root-complex/expected/assets/css/style.bundle.css b/test/cases/integrity-publicPath-root-complex/expected/assets/css/style.bundle.css new file mode 100644 index 00000000..adc68fa6 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/expected/assets/css/style.bundle.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/test/cases/integrity-publicPath-root-complex/expected/assets/css/vendor.bundle.css b/test/cases/integrity-publicPath-root-complex/expected/assets/css/vendor.bundle.css new file mode 100644 index 00000000..ad4f3016 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/expected/assets/css/vendor.bundle.css @@ -0,0 +1,3 @@ +h2 { + color: green; +} diff --git a/test/cases/integrity-publicPath-root-complex/expected/assets/img/image.697ef306.png b/test/cases/integrity-publicPath-root-complex/expected/assets/img/image.697ef306.png new file mode 100644 index 0000000000000000000000000000000000000000..29d95462457487e7347780175d75d743c4f07642 GIT binary patch literal 1250 zcmV<81ReW{P)004pr0{{R3_H{EN0001KP)t-s_xJbq z_V)Gl_4M@g^Yioa^78TV@$m5Q@9*#K?(XgF?d+9?4>gws~>FDU_=jZ3<=H}() z<>ch#32auvuF;-u{rG5l|fJ}NUM|Oz~kfXaf$tcfB?nL zax(0;KSv7dIZhhb4r5W5cUYt+9lNs%y!vxh^ahta!#g4uh` zMePFOf_r8$^drMEO4!C1EG{B=ZI&a`wBLr_A1NwWwsKlp;po(I`n(js3UXWG=)7IK zxHN(`fneDNyrmTYgmyOIg5Q1};e6W=)cr-U!wE`%Lip+sq`%aH(qCW}8@_c2NYY5%G{pTJfFok*lVK{`b7ODrh;{V?3NoKi5}pWr#IV7xye2SMpCPeJJ~XF=&N zCqZq0=>?^~JO!n{oCT%7ECr>%j2AR=-({wPFt?ny;KE6NnF=l)3zOg^I0;UIf281I z1VO;87j(}FE?jsqPYD9fcKa8zEuOn?hF;F}T>d*ug1#*P1z}pyJ|g&rqf==FuiYVb zjlq#ITmQSDIR&`@I&4Pf;J6b!n5ZCbA#8#A{~+~805?D zO^+ZhY~Zw9E9?`b#&J??jXi<{Xd0Jr!_ck`0^Ai8sQ%<+T$Ut$Q9-1)=3+gMj&INJ zrS82xsGP5x&B!!$We{|NfU@(5fd20RFa-dh5X_B9a1xvZC&5YZFK{PSsxrUwVE_OC M07*qoM6N<$g7<`XtpET3 literal 0 HcmV?d00001 diff --git a/test/cases/integrity-publicPath-root-complex/expected/assets/js/main.bundle.js b/test/cases/integrity-publicPath-root-complex/expected/assets/js/main.bundle.js new file mode 100644 index 00000000..1ce9c884 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/expected/assets/js/main.bundle.js @@ -0,0 +1 @@ +console.log(">> main"); \ No newline at end of file diff --git a/test/cases/integrity-publicPath-root-complex/expected/assets/js/no-integrity.bundle.js b/test/cases/integrity-publicPath-root-complex/expected/assets/js/no-integrity.bundle.js new file mode 100644 index 00000000..4fbdad65 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/expected/assets/js/no-integrity.bundle.js @@ -0,0 +1 @@ +console.log(">> ignore integrity"); \ No newline at end of file diff --git a/test/cases/integrity-publicPath-root-complex/expected/assets/js/vendor.bundle.js b/test/cases/integrity-publicPath-root-complex/expected/assets/js/vendor.bundle.js new file mode 100644 index 00000000..5615af0f --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/expected/assets/js/vendor.bundle.js @@ -0,0 +1 @@ +console.log(">> vendor"); \ No newline at end of file diff --git a/test/cases/integrity-publicPath-root-complex/expected/pages/index.html b/test/cases/integrity-publicPath-root-complex/expected/pages/index.html new file mode 100644 index 00000000..0666d596 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/expected/pages/index.html @@ -0,0 +1,31 @@ + + + + Test + + + + + + + + + + + + + + + + + + + + + +

Hello World!

+ + + + + \ No newline at end of file diff --git a/test/cases/integrity-publicPath-root-complex/src/image.png b/test/cases/integrity-publicPath-root-complex/src/image.png new file mode 100644 index 0000000000000000000000000000000000000000..29d95462457487e7347780175d75d743c4f07642 GIT binary patch literal 1250 zcmV<81ReW{P)004pr0{{R3_H{EN0001KP)t-s_xJbq z_V)Gl_4M@g^Yioa^78TV@$m5Q@9*#K?(XgF?d+9?4>gws~>FDU_=jZ3<=H}() z<>ch#32auvuF;-u{rG5l|fJ}NUM|Oz~kfXaf$tcfB?nL zax(0;KSv7dIZhhb4r5W5cUYt+9lNs%y!vxh^ahta!#g4uh` zMePFOf_r8$^drMEO4!C1EG{B=ZI&a`wBLr_A1NwWwsKlp;po(I`n(js3UXWG=)7IK zxHN(`fneDNyrmTYgmyOIg5Q1};e6W=)cr-U!wE`%Lip+sq`%aH(qCW}8@_c2NYY5%G{pTJfFok*lVK{`b7ODrh;{V?3NoKi5}pWr#IV7xye2SMpCPeJJ~XF=&N zCqZq0=>?^~JO!n{oCT%7ECr>%j2AR=-({wPFt?ny;KE6NnF=l)3zOg^I0;UIf281I z1VO;87j(}FE?jsqPYD9fcKa8zEuOn?hF;F}T>d*ug1#*P1z}pyJ|g&rqf==FuiYVb zjlq#ITmQSDIR&`@I&4Pf;J6b!n5ZCbA#8#A{~+~805?D zO^+ZhY~Zw9E9?`b#&J??jXi<{Xd0Jr!_ck`0^Ai8sQ%<+T$Ut$Q9-1)=3+gMj&INJ zrS82xsGP5x&B!!$We{|NfU@(5fd20RFa-dh5X_B9a1xvZC&5YZFK{PSsxrUwVE_OC M07*qoM6N<$g7<`XtpET3 literal 0 HcmV?d00001 diff --git a/test/cases/integrity-publicPath-root-complex/src/index.html b/test/cases/integrity-publicPath-root-complex/src/index.html new file mode 100644 index 00000000..9cfdace5 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/src/index.html @@ -0,0 +1,31 @@ + + + + Test + + + + + + + + + + + + + + + + + + + + + +

Hello World!

+ + + + + \ No newline at end of file diff --git a/test/cases/integrity-publicPath-root-complex/src/main.js b/test/cases/integrity-publicPath-root-complex/src/main.js new file mode 100644 index 00000000..89ae7c12 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/src/main.js @@ -0,0 +1 @@ +console.log('>> main'); diff --git a/test/cases/integrity-publicPath-root-complex/src/no-integrity.js b/test/cases/integrity-publicPath-root-complex/src/no-integrity.js new file mode 100644 index 00000000..a1316638 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/src/no-integrity.js @@ -0,0 +1 @@ +console.log('>> ignore integrity'); diff --git a/test/cases/integrity-publicPath-root-complex/src/style.css b/test/cases/integrity-publicPath-root-complex/src/style.css new file mode 100644 index 00000000..adc68fa6 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/src/style.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/test/cases/integrity-publicPath-root-complex/src/vendor.css b/test/cases/integrity-publicPath-root-complex/src/vendor.css new file mode 100644 index 00000000..ad4f3016 --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/src/vendor.css @@ -0,0 +1,3 @@ +h2 { + color: green; +} diff --git a/test/cases/integrity-publicPath-root-complex/src/vendor.js b/test/cases/integrity-publicPath-root-complex/src/vendor.js new file mode 100644 index 00000000..a4549a7e --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/src/vendor.js @@ -0,0 +1 @@ +console.log('>> vendor'); diff --git a/test/cases/integrity-publicPath-root-complex/webpack.config.js b/test/cases/integrity-publicPath-root-complex/webpack.config.js new file mode 100644 index 00000000..f7309f8b --- /dev/null +++ b/test/cases/integrity-publicPath-root-complex/webpack.config.js @@ -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]', + }, + }, + ], + }, +}; diff --git a/test/integration.test.js b/test/integration.test.js index ecd7e422..d5a78c51 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -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'));