From 63c9d8af59fec1787023acefcec175f6c7da1e31 Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Wed, 23 Aug 2023 18:01:34 +0700 Subject: [PATCH 01/10] feat(*): add wmfSharedChunk check --- webpack-subresource-integrity/src/manifest.ts | 7 +++++-- webpack-subresource-integrity/src/util.ts | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/webpack-subresource-integrity/src/manifest.ts b/webpack-subresource-integrity/src/manifest.ts index 079624c..c83623e 100644 --- a/webpack-subresource-integrity/src/manifest.ts +++ b/webpack-subresource-integrity/src/manifest.ts @@ -17,6 +17,7 @@ import { allChunksInChunkIterable, allChunksInPrimaryChunkIterable, sriHashVariableReference, + wmfSharedChunk, } from "./util"; import { createDAGfromGraph } from "./scc"; import { RuntimeModule, Template, Chunk } from "webpack"; @@ -52,13 +53,15 @@ function buildTopologicallySortedChunkGraph( // Chunks should have *all* chunks, not simply entry chunks for (const vertex of chunks) { - if (addIfNotExist(vertices, vertex)) { + if (wmfSharedChunk(vertex) || addIfNotExist(vertices, vertex)) { continue; } edges.set(vertex, new Set()); for (const childChunk of allChunksInChunkIterable(vertex)) { - edges.get(vertex)?.add(childChunk); + if (wmfSharedChunk(childChunk)) { + edges.get(vertex)?.add(childChunk); + } } } diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index d07f2d8..8354b5a 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -87,6 +87,9 @@ export function addIfNotExist(set: Set, item: T): boolean { set.add(item); return false; } +export function wmfSharedChunk(chunk: Chunk): boolean { + return chunk.chunkReason === "split chunk (cache group: default)"; +} export function findChunks(chunk: Chunk): Set { const allChunks = new Set(); @@ -99,7 +102,9 @@ export function findChunks(chunk: Chunk): Set { group.childrenIterable.forEach(recurseGroup); } - if (addIfNotExist(allChunks, childChunk)) return; + if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)){ + return; + } Array.from(childChunk.groupsIterable).forEach(recurseGroup); })(chunk); From db5f61bc24ab4c1ae77fa58d1504372048bd46cb Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Wed, 23 Aug 2023 19:06:07 +0700 Subject: [PATCH 02/10] feat(*): fit lint --- .vscode/settings.json | 3 +- package.json | 1 + .../module-federation/src/index.js | 2 + .../src/__tests__/integration.test.ts | 37 ++++++++++++++++++- webpack-subresource-integrity/src/manifest.ts | 2 +- webpack-subresource-integrity/src/util.ts | 2 +- 6 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js diff --git a/.vscode/settings.json b/.vscode/settings.json index 4aa23f2..b0f2c13 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,5 +5,6 @@ }, "prettier.prettierPath": ".yarn/sdks/prettier/index.js", "typescript.tsdk": ".yarn/sdks/typescript/lib", - "typescript.enablePromptUseWorkspaceTsdk": true + "typescript.enablePromptUseWorkspaceTsdk": true, + "editor.defaultFormatter": "dbaeumer.vscode-eslint" } diff --git a/package.json b/package.json index 0b52af5..c3ebb38 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "lint": "yarn eslint .", "test:jest": "(cd wsi-test-helper && yarn tsc) && (cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit)", "test:unit": "cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit unit.test.ts", + "test:unit2": "cd webpack-subresource-integrity && yarn jest webpack-subresource-integrity/src/__tests__/examples.test.ts", "test:examples": "cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit examples.test.ts", "test:integration": "cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit integration.test.ts", "test:smoke": "cd webpack-subresource-integrity && yarn prepublish && yarn pack --filename ../smoke-test/webpack-subresource-integrity.tgz && cd ../smoke-test && rm -rf yarn.lock && touch yarn.lock && YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn && yarn test", diff --git a/webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js b/webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js new file mode 100644 index 0000000..f98b2e6 --- /dev/null +++ b/webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js @@ -0,0 +1,2 @@ +import("module/remoteEntry.js"); +console.log("ok"); diff --git a/webpack-subresource-integrity/src/__tests__/integration.test.ts b/webpack-subresource-integrity/src/__tests__/integration.test.ts index 8256995..2585f83 100644 --- a/webpack-subresource-integrity/src/__tests__/integration.test.ts +++ b/webpack-subresource-integrity/src/__tests__/integration.test.ts @@ -10,13 +10,19 @@ import { StatsAsset, WebpackError, WebpackOptionsNormalized, + // container, } from "webpack"; import { resolve } from "path"; import tmp from "tmp-promise"; -import { SubresourceIntegrityPlugin } from ".."; +import { + SubresourceIntegrityPlugin, + // SubresourceIntegrityPluginOptions, +} from ".."; import { runWebpack } from "./test-utils"; import merge from "lodash/merge"; +// const { ModuleFederationPlugin } = container; + jest.unmock("html-webpack-plugin"); async function runWebpackForSimpleProject( @@ -36,6 +42,35 @@ async function runWebpackForSimpleProject( ); } +// async function runWebpackForModuleFederationProject( +// options: Partial = {} +// ): Promise { +// const tmpDir = await tmp.dir({ unsafeCleanup: true }); +// return await runWebpack({ +// mode: "production", +// output: { path: tmpDir.path, crossOriginLoading: "anonymous" }, +// entry: resolve(__dirname, "./__fixtures__/module-federation/src/index.js"), +// plugins: [ +// new SubresourceIntegrityPlugin(options), +// new ModuleFederationPlugin({ +// name: "remote", +// shared: ["lodash"], +// }), +// ], +// }); +// } + +// test.only("dont inlcude webpack_share module federation", async () => { +// const mainAsset = await runWebpackForModuleFederationProject(); +// expect(mainAsset.compilation.warnings).toHaveLength(0); +// }); + +// test("can skip chunks", async () => { +// const stats = await runWebpackForModuleFederationProject(); + +// expect(stats.compilation.warnings).toHaveLength(0); +// }); + test("enabled with webpack mode=production", async () => { const mainAsset = (await runWebpackForSimpleProject()) .toJson() diff --git a/webpack-subresource-integrity/src/manifest.ts b/webpack-subresource-integrity/src/manifest.ts index c83623e..5bc5cea 100644 --- a/webpack-subresource-integrity/src/manifest.ts +++ b/webpack-subresource-integrity/src/manifest.ts @@ -59,7 +59,7 @@ function buildTopologicallySortedChunkGraph( edges.set(vertex, new Set()); for (const childChunk of allChunksInChunkIterable(vertex)) { - if (wmfSharedChunk(childChunk)) { + if (!wmfSharedChunk(childChunk)) { edges.get(vertex)?.add(childChunk); } } diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index 8354b5a..6b17bcd 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -102,7 +102,7 @@ export function findChunks(chunk: Chunk): Set { group.childrenIterable.forEach(recurseGroup); } - if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)){ + if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)) { return; } Array.from(childChunk.groupsIterable).forEach(recurseGroup); From bda82d50f54aad43bbeaa8b633ca71f6eb826943 Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Thu, 24 Aug 2023 14:53:55 +0700 Subject: [PATCH 03/10] remove integration test --- .../module-federation/src/index.js | 2 - .../src/__tests__/integration.test.ts | 37 +------------------ 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js diff --git a/webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js b/webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js deleted file mode 100644 index f98b2e6..0000000 --- a/webpack-subresource-integrity/src/__tests__/__fixtures__/module-federation/src/index.js +++ /dev/null @@ -1,2 +0,0 @@ -import("module/remoteEntry.js"); -console.log("ok"); diff --git a/webpack-subresource-integrity/src/__tests__/integration.test.ts b/webpack-subresource-integrity/src/__tests__/integration.test.ts index 2585f83..8256995 100644 --- a/webpack-subresource-integrity/src/__tests__/integration.test.ts +++ b/webpack-subresource-integrity/src/__tests__/integration.test.ts @@ -10,19 +10,13 @@ import { StatsAsset, WebpackError, WebpackOptionsNormalized, - // container, } from "webpack"; import { resolve } from "path"; import tmp from "tmp-promise"; -import { - SubresourceIntegrityPlugin, - // SubresourceIntegrityPluginOptions, -} from ".."; +import { SubresourceIntegrityPlugin } from ".."; import { runWebpack } from "./test-utils"; import merge from "lodash/merge"; -// const { ModuleFederationPlugin } = container; - jest.unmock("html-webpack-plugin"); async function runWebpackForSimpleProject( @@ -42,35 +36,6 @@ async function runWebpackForSimpleProject( ); } -// async function runWebpackForModuleFederationProject( -// options: Partial = {} -// ): Promise { -// const tmpDir = await tmp.dir({ unsafeCleanup: true }); -// return await runWebpack({ -// mode: "production", -// output: { path: tmpDir.path, crossOriginLoading: "anonymous" }, -// entry: resolve(__dirname, "./__fixtures__/module-federation/src/index.js"), -// plugins: [ -// new SubresourceIntegrityPlugin(options), -// new ModuleFederationPlugin({ -// name: "remote", -// shared: ["lodash"], -// }), -// ], -// }); -// } - -// test.only("dont inlcude webpack_share module federation", async () => { -// const mainAsset = await runWebpackForModuleFederationProject(); -// expect(mainAsset.compilation.warnings).toHaveLength(0); -// }); - -// test("can skip chunks", async () => { -// const stats = await runWebpackForModuleFederationProject(); - -// expect(stats.compilation.warnings).toHaveLength(0); -// }); - test("enabled with webpack mode=production", async () => { const mainAsset = (await runWebpackForSimpleProject()) .toJson() From ff683a217cdf5283dab4168f8a8277d320280da5 Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Thu, 24 Aug 2023 14:59:06 +0700 Subject: [PATCH 04/10] fix for dev and prod --- webpack-subresource-integrity/src/util.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index 6b17bcd..3f3ff22 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -88,7 +88,9 @@ export function addIfNotExist(set: Set, item: T): boolean { return false; } export function wmfSharedChunk(chunk: Chunk): boolean { - return chunk.chunkReason === "split chunk (cache group: default)"; + return Boolean( + chunk.chunkReason?.includes("split chunk (cache group: default)") + ); } export function findChunks(chunk: Chunk): Set { From 3f1ed4a47a06329c7d3bff00def1051ae99f2ada Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Thu, 24 Aug 2023 15:02:02 +0700 Subject: [PATCH 05/10] add wmf remote example --- .../README.md | 3 ++ .../bootstrap.js | 4 ++ .../index.js | 1 + .../package.json | 21 +++++++++ .../webpack.config.js | 43 +++++++++++++++++++ yarn.lock | 13 ++++++ 6 files changed, 85 insertions(+) create mode 100644 examples/webpack-module-federaition-remote/README.md create mode 100644 examples/webpack-module-federaition-remote/bootstrap.js create mode 100644 examples/webpack-module-federaition-remote/index.js create mode 100644 examples/webpack-module-federaition-remote/package.json create mode 100644 examples/webpack-module-federaition-remote/webpack.config.js diff --git a/examples/webpack-module-federaition-remote/README.md b/examples/webpack-module-federaition-remote/README.md new file mode 100644 index 0000000..e412fce --- /dev/null +++ b/examples/webpack-module-federaition-remote/README.md @@ -0,0 +1,3 @@ +# With Webpack module federation + +Usage of With Webpack module federation as remote app diff --git a/examples/webpack-module-federaition-remote/bootstrap.js b/examples/webpack-module-federaition-remote/bootstrap.js new file mode 100644 index 0000000..1c45e61 --- /dev/null +++ b/examples/webpack-module-federaition-remote/bootstrap.js @@ -0,0 +1,4 @@ +// mock dependency +require("lodash"); + +console.log("ok"); diff --git a/examples/webpack-module-federaition-remote/index.js b/examples/webpack-module-federaition-remote/index.js new file mode 100644 index 0000000..50599f7 --- /dev/null +++ b/examples/webpack-module-federaition-remote/index.js @@ -0,0 +1 @@ +import("./bootstrap"); diff --git a/examples/webpack-module-federaition-remote/package.json b/examples/webpack-module-federaition-remote/package.json new file mode 100644 index 0000000..92171a6 --- /dev/null +++ b/examples/webpack-module-federaition-remote/package.json @@ -0,0 +1,21 @@ +{ + "name": "webpack-module-federaition-remote", + "description": "Usage of With Webpack module federation as remote app", + "version": "1.0.0", + "license": "MIT", + "private": true, + "devDependencies": { + "expect": "^26.6.2", + "nyc": "*", + "webpack": "^5.44.0", + "webpack-cli": "4", + "webpack-subresource-integrity": "*" + }, + "dependencies": { + "lodash": "^4.17.21" + }, + "scripts": { + "build": "webpack --mode production", + "dev": "webpack --mode development" + } +} diff --git a/examples/webpack-module-federaition-remote/webpack.config.js b/examples/webpack-module-federaition-remote/webpack.config.js new file mode 100644 index 0000000..a55ebb3 --- /dev/null +++ b/examples/webpack-module-federaition-remote/webpack.config.js @@ -0,0 +1,43 @@ +const { SubresourceIntegrityPlugin } = require("webpack-subresource-integrity"); +const expect = require("expect"); +const container = require("webpack").container; + +const { ModuleFederationPlugin } = container; + +module.exports = { + entry: { + index: "./index.js", + }, + output: { + crossOriginLoading: "anonymous", + }, + plugins: [ + new SubresourceIntegrityPlugin({ + hashFuncNames: ["sha256"], + enabled: true, + }), + new ModuleFederationPlugin({ + name: "remote", + filename: "remoteEntry.js", + exposes: { + "./remote": "./bootstrap", + }, + shared: { + lodash: { + singleton: true, + }, + }, + }), + { + apply: (compiler) => { + compiler.hooks.done.tap("wsi-test", (stats) => { + const assets = stats.toJson().assets; + + assets.forEach((asset) => { + expect(asset).not.toContain("*-*-*-CHUNK-SRI-HASH-"); + }); + }); + }, + }, + ], +}; diff --git a/yarn.lock b/yarn.lock index b489c0d..06dd513 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9574,6 +9574,19 @@ __metadata: languageName: node linkType: hard +"webpack-module-federaition-remote@workspace:examples/webpack-module-federaition-remote": + version: 0.0.0-use.local + resolution: "webpack-module-federaition-remote@workspace:examples/webpack-module-federaition-remote" + dependencies: + expect: ^26.6.2 + lodash: ^4.17.21 + nyc: "*" + webpack: ^5.44.0 + webpack-cli: 4 + webpack-subresource-integrity: "*" + languageName: unknown + linkType: soft + "webpack-preload@workspace:examples/webpack-preload": version: 0.0.0-use.local resolution: "webpack-preload@workspace:examples/webpack-preload" From 641fb5e87114a05582efbdebf6cb1d52bf394609 Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Thu, 24 Aug 2023 15:15:17 +0700 Subject: [PATCH 06/10] add wmf host example --- .../webpack-module-federaition-host/README.md | 3 ++ .../bootstrap.js | 4 ++ .../webpack-module-federaition-host/index.js | 1 + .../package.json | 17 +++++++ .../webpack.config.js | 47 +++++++++++++++++++ .../package.json | 6 +-- package.json | 1 - yarn.lock | 13 +++++ 8 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 examples/webpack-module-federaition-host/README.md create mode 100644 examples/webpack-module-federaition-host/bootstrap.js create mode 100644 examples/webpack-module-federaition-host/index.js create mode 100644 examples/webpack-module-federaition-host/package.json create mode 100644 examples/webpack-module-federaition-host/webpack.config.js diff --git a/examples/webpack-module-federaition-host/README.md b/examples/webpack-module-federaition-host/README.md new file mode 100644 index 0000000..1af3245 --- /dev/null +++ b/examples/webpack-module-federaition-host/README.md @@ -0,0 +1,3 @@ +# With Webpack module federation + +Usage of With Webpack module federation as host app and eager dependency diff --git a/examples/webpack-module-federaition-host/bootstrap.js b/examples/webpack-module-federaition-host/bootstrap.js new file mode 100644 index 0000000..1c45e61 --- /dev/null +++ b/examples/webpack-module-federaition-host/bootstrap.js @@ -0,0 +1,4 @@ +// mock dependency +require("lodash"); + +console.log("ok"); diff --git a/examples/webpack-module-federaition-host/index.js b/examples/webpack-module-federaition-host/index.js new file mode 100644 index 0000000..50599f7 --- /dev/null +++ b/examples/webpack-module-federaition-host/index.js @@ -0,0 +1 @@ +import("./bootstrap"); diff --git a/examples/webpack-module-federaition-host/package.json b/examples/webpack-module-federaition-host/package.json new file mode 100644 index 0000000..b0e73ad --- /dev/null +++ b/examples/webpack-module-federaition-host/package.json @@ -0,0 +1,17 @@ +{ + "name": "webpack-module-federaition-host", + "description": "Ensure integrity works with webpack module federation as host app", + "version": "1.0.0", + "license": "MIT", + "private": true, + "devDependencies": { + "expect": "^26.6.2", + "nyc": "*", + "webpack": "^5.44.0", + "webpack-cli": "4", + "webpack-subresource-integrity": "*" + }, + "dependencies": { + "lodash": "^4.17.21" + } +} diff --git a/examples/webpack-module-federaition-host/webpack.config.js b/examples/webpack-module-federaition-host/webpack.config.js new file mode 100644 index 0000000..9199353 --- /dev/null +++ b/examples/webpack-module-federaition-host/webpack.config.js @@ -0,0 +1,47 @@ +const { SubresourceIntegrityPlugin } = require("webpack-subresource-integrity"); +const expect = require("expect"); +const container = require("webpack").container; + +const { ModuleFederationPlugin } = container; + +module.exports = { + entry: { + index: "./index.js", + }, + output: { + crossOriginLoading: "anonymous", + }, + plugins: [ + new SubresourceIntegrityPlugin({ + hashFuncNames: ["sha256"], + enabled: true, + }), + new ModuleFederationPlugin({ + name: "host", + filename: "remoteEntry.js", + remotes: { + app1: "app1@http://localhost:3001/remoteEntry.js", + }, + exposes: { + "./remote": "./bootstrap", + }, + shared: { + lodash: { + singleton: true, + eager: true, + }, + }, + }), + { + apply: (compiler) => { + compiler.hooks.done.tap("wsi-test", (stats) => { + const assets = stats.toJson().assets; + + assets.forEach((asset) => { + expect(asset).not.toContain("*-*-*-CHUNK-SRI-HASH-"); + }); + }); + }, + }, + ], +}; diff --git a/examples/webpack-module-federaition-remote/package.json b/examples/webpack-module-federaition-remote/package.json index 92171a6..e5607c1 100644 --- a/examples/webpack-module-federaition-remote/package.json +++ b/examples/webpack-module-federaition-remote/package.json @@ -1,6 +1,6 @@ { "name": "webpack-module-federaition-remote", - "description": "Usage of With Webpack module federation as remote app", + "description": "Ensure integrity works with webpack module federation as remote app", "version": "1.0.0", "license": "MIT", "private": true, @@ -13,9 +13,5 @@ }, "dependencies": { "lodash": "^4.17.21" - }, - "scripts": { - "build": "webpack --mode production", - "dev": "webpack --mode development" } } diff --git a/package.json b/package.json index c3ebb38..0b52af5 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "lint": "yarn eslint .", "test:jest": "(cd wsi-test-helper && yarn tsc) && (cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit)", "test:unit": "cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit unit.test.ts", - "test:unit2": "cd webpack-subresource-integrity && yarn jest webpack-subresource-integrity/src/__tests__/examples.test.ts", "test:examples": "cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit examples.test.ts", "test:integration": "cd webpack-subresource-integrity && yarn tsc && yarn jest --detectOpenHandles --forceExit integration.test.ts", "test:smoke": "cd webpack-subresource-integrity && yarn prepublish && yarn pack --filename ../smoke-test/webpack-subresource-integrity.tgz && cd ../smoke-test && rm -rf yarn.lock && touch yarn.lock && YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn && yarn test", diff --git a/yarn.lock b/yarn.lock index 06dd513..da73407 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9574,6 +9574,19 @@ __metadata: languageName: node linkType: hard +"webpack-module-federaition-host@workspace:examples/webpack-module-federaition-host": + version: 0.0.0-use.local + resolution: "webpack-module-federaition-host@workspace:examples/webpack-module-federaition-host" + dependencies: + expect: ^26.6.2 + lodash: ^4.17.21 + nyc: "*" + webpack: ^5.44.0 + webpack-cli: 4 + webpack-subresource-integrity: "*" + languageName: unknown + linkType: soft + "webpack-module-federaition-remote@workspace:examples/webpack-module-federaition-remote": version: 0.0.0-use.local resolution: "webpack-module-federaition-remote@workspace:examples/webpack-module-federaition-remote" From 742698e722b4c4ec09acd70c526e7d311984b475 Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Thu, 24 Aug 2023 15:15:31 +0700 Subject: [PATCH 07/10] lint --- .vscode/settings.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b0f2c13..4aa23f2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,5 @@ }, "prettier.prettierPath": ".yarn/sdks/prettier/index.js", "typescript.tsdk": ".yarn/sdks/typescript/lib", - "typescript.enablePromptUseWorkspaceTsdk": true, - "editor.defaultFormatter": "dbaeumer.vscode-eslint" + "typescript.enablePromptUseWorkspaceTsdk": true } From eab0ac240c80f9a815c2ba0fd0878314f10c535b Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Fri, 6 Oct 2023 13:36:36 +0300 Subject: [PATCH 08/10] checkForFiles --- webpack-subresource-integrity/src/manifest.ts | 6 +++--- webpack-subresource-integrity/src/util.ts | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/webpack-subresource-integrity/src/manifest.ts b/webpack-subresource-integrity/src/manifest.ts index 5bc5cea..9a0c60d 100644 --- a/webpack-subresource-integrity/src/manifest.ts +++ b/webpack-subresource-integrity/src/manifest.ts @@ -17,7 +17,7 @@ import { allChunksInChunkIterable, allChunksInPrimaryChunkIterable, sriHashVariableReference, - wmfSharedChunk, + checkForFiles, } from "./util"; import { createDAGfromGraph } from "./scc"; import { RuntimeModule, Template, Chunk } from "webpack"; @@ -53,13 +53,13 @@ function buildTopologicallySortedChunkGraph( // Chunks should have *all* chunks, not simply entry chunks for (const vertex of chunks) { - if (wmfSharedChunk(vertex) || addIfNotExist(vertices, vertex)) { + if (checkForFiles(vertex) || addIfNotExist(vertices, vertex)) { continue; } edges.set(vertex, new Set()); for (const childChunk of allChunksInChunkIterable(vertex)) { - if (!wmfSharedChunk(childChunk)) { + if (!checkForFiles(childChunk)) { edges.get(vertex)?.add(childChunk); } } diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index 3f3ff22..943271c 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -87,10 +87,8 @@ export function addIfNotExist(set: Set, item: T): boolean { set.add(item); return false; } -export function wmfSharedChunk(chunk: Chunk): boolean { - return Boolean( - chunk.chunkReason?.includes("split chunk (cache group: default)") - ); +export function checkForFiles(chunk: Chunk): boolean { + return chunk.files?.size === 0; } export function findChunks(chunk: Chunk): Set { @@ -104,7 +102,7 @@ export function findChunks(chunk: Chunk): Set { group.childrenIterable.forEach(recurseGroup); } - if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)) { + if (checkForFiles(childChunk) || addIfNotExist(allChunks, childChunk)) { return; } Array.from(childChunk.groupsIterable).forEach(recurseGroup); From 71788522f3387426a8a39cc1fe088d9c569db6ed Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Fri, 6 Oct 2023 18:49:12 +0300 Subject: [PATCH 09/10] feat(deploy_kube_feature): revert --- webpack-subresource-integrity/src/manifest.ts | 6 +++--- webpack-subresource-integrity/src/util.ts | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/webpack-subresource-integrity/src/manifest.ts b/webpack-subresource-integrity/src/manifest.ts index 9a0c60d..5bc5cea 100644 --- a/webpack-subresource-integrity/src/manifest.ts +++ b/webpack-subresource-integrity/src/manifest.ts @@ -17,7 +17,7 @@ import { allChunksInChunkIterable, allChunksInPrimaryChunkIterable, sriHashVariableReference, - checkForFiles, + wmfSharedChunk, } from "./util"; import { createDAGfromGraph } from "./scc"; import { RuntimeModule, Template, Chunk } from "webpack"; @@ -53,13 +53,13 @@ function buildTopologicallySortedChunkGraph( // Chunks should have *all* chunks, not simply entry chunks for (const vertex of chunks) { - if (checkForFiles(vertex) || addIfNotExist(vertices, vertex)) { + if (wmfSharedChunk(vertex) || addIfNotExist(vertices, vertex)) { continue; } edges.set(vertex, new Set()); for (const childChunk of allChunksInChunkIterable(vertex)) { - if (!checkForFiles(childChunk)) { + if (!wmfSharedChunk(childChunk)) { edges.get(vertex)?.add(childChunk); } } diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index 943271c..3f3ff22 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -87,8 +87,10 @@ export function addIfNotExist(set: Set, item: T): boolean { set.add(item); return false; } -export function checkForFiles(chunk: Chunk): boolean { - return chunk.files?.size === 0; +export function wmfSharedChunk(chunk: Chunk): boolean { + return Boolean( + chunk.chunkReason?.includes("split chunk (cache group: default)") + ); } export function findChunks(chunk: Chunk): Set { @@ -102,7 +104,7 @@ export function findChunks(chunk: Chunk): Set { group.childrenIterable.forEach(recurseGroup); } - if (checkForFiles(childChunk) || addIfNotExist(allChunks, childChunk)) { + if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)) { return; } Array.from(childChunk.groupsIterable).forEach(recurseGroup); From 00f4ac530cb8b5b1159b3ee907f59a56500ad816 Mon Sep 17 00:00:00 2001 From: Stepan Ivankov Date: Mon, 9 Oct 2023 16:39:29 +0300 Subject: [PATCH 10/10] filter --- webpack-subresource-integrity/src/index.ts | 2 +- webpack-subresource-integrity/src/manifest.ts | 20 ++++++++++------- webpack-subresource-integrity/src/plugin.ts | 4 ++-- webpack-subresource-integrity/src/util.ts | 22 ++++++++++++++----- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/webpack-subresource-integrity/src/index.ts b/webpack-subresource-integrity/src/index.ts index 3fda2fd..66313fe 100644 --- a/webpack-subresource-integrity/src/index.ts +++ b/webpack-subresource-integrity/src/index.ts @@ -167,7 +167,7 @@ export class SubresourceIntegrityPlugin { const allChunks = this.options.hashLoading === "lazy" ? plugin.getChildChunksToAddToChunkManifest(chunk) - : findChunks(chunk); + : findChunks(chunk, compilation); const includedChunks = chunk.getChunkMaps(false).hash; if (Object.keys(includedChunks).length > 0) { diff --git a/webpack-subresource-integrity/src/manifest.ts b/webpack-subresource-integrity/src/manifest.ts index 5bc5cea..04a2fb5 100644 --- a/webpack-subresource-integrity/src/manifest.ts +++ b/webpack-subresource-integrity/src/manifest.ts @@ -20,7 +20,7 @@ import { wmfSharedChunk, } from "./util"; import { createDAGfromGraph } from "./scc"; -import { RuntimeModule, Template, Chunk } from "webpack"; +import { RuntimeModule, Template, Chunk, Compilation } from "webpack"; // This implementation assumes a directed acyclic graph (such as one produced by createDAGfromGraph), // and does not attempt to detect cycles @@ -42,7 +42,8 @@ function topologicalSort({ vertices, edges }: Graph): T[] { } function buildTopologicallySortedChunkGraph( - chunks: Iterable + chunks: Iterable, + compilation: Compilation ): [ sortedVertices: StronglyConnectedComponent[], sccGraph: Graph>, @@ -53,13 +54,16 @@ function buildTopologicallySortedChunkGraph( // Chunks should have *all* chunks, not simply entry chunks for (const vertex of chunks) { - if (wmfSharedChunk(vertex) || addIfNotExist(vertices, vertex)) { + if ( + wmfSharedChunk(vertex, compilation) || + addIfNotExist(vertices, vertex) + ) { continue; } edges.set(vertex, new Set()); for (const childChunk of allChunksInChunkIterable(vertex)) { - if (!wmfSharedChunk(childChunk)) { + if (!wmfSharedChunk(childChunk, compilation)) { edges.get(vertex)?.add(childChunk); } } @@ -85,9 +89,9 @@ class ChunkToManifestMapBuilder { // or its parents regardless of the tree traversal used from the roots private hashesByChunkGroupAndParents = new Map>(); - constructor(chunks: Iterable) { + constructor(compilation: Compilation) { const [sortedVertices, , chunkToSccMap] = - buildTopologicallySortedChunkGraph(chunks); + buildTopologicallySortedChunkGraph(compilation.chunks, compilation); this.sortedVertices = sortedVertices; this.chunkToSccMap = chunkToSccMap; this.manifest = this.createManifest(); @@ -189,12 +193,12 @@ class ChunkToManifestMapBuilder { } export function getChunkToManifestMap( - chunks: Iterable + compilation: Compilation ): [ sortedVertices: StronglyConnectedComponent[], chunkManifest: Map> ] { - return new ChunkToManifestMapBuilder(chunks).build(); + return new ChunkToManifestMapBuilder(compilation).build(); } export class AddLazySriRuntimeModule extends RuntimeModule { diff --git a/webpack-subresource-integrity/src/plugin.ts b/webpack-subresource-integrity/src/plugin.ts index 8d363b4..d7dac97 100644 --- a/webpack-subresource-integrity/src/plugin.ts +++ b/webpack-subresource-integrity/src/plugin.ts @@ -150,7 +150,7 @@ export class Plugin { chunk: Chunk, assets: Record ): void => { - Array.from(findChunks(chunk)) + Array.from(findChunks(chunk, this.compilation)) .reverse() .forEach((chunk) => this.processChunkAssets(chunk, assets)); }; @@ -296,7 +296,7 @@ export class Plugin { beforeRuntimeRequirements = (): void => { if (this.options.hashLoading === "lazy") { const [sortedSccChunks, chunkManifest] = getChunkToManifestMap( - this.compilation.chunks + this.compilation ); this.sortedSccChunks = sortedSccChunks; this.chunkManifest = chunkManifest; diff --git a/webpack-subresource-integrity/src/util.ts b/webpack-subresource-integrity/src/util.ts index 3f3ff22..ae2913c 100644 --- a/webpack-subresource-integrity/src/util.ts +++ b/webpack-subresource-integrity/src/util.ts @@ -87,13 +87,22 @@ export function addIfNotExist(set: Set, item: T): boolean { set.add(item); return false; } -export function wmfSharedChunk(chunk: Chunk): boolean { - return Boolean( - chunk.chunkReason?.includes("split chunk (cache group: default)") +/** + * Filter out shared module that are not used in the build + */ +export function wmfSharedChunk( + chunk: Chunk, + compilation: Compilation +): boolean { + const rootModules = compilation.chunkGraph.getChunkRootModules(chunk); + const isSharedModule = rootModules.some( + (module) => module.type === "consume-shared-module" ); + + return isSharedModule && rootModules.length === 1; } -export function findChunks(chunk: Chunk): Set { +export function findChunks(chunk: Chunk, compilation: Compilation): Set { const allChunks = new Set(); const groupsVisited = new Set(); @@ -104,7 +113,10 @@ export function findChunks(chunk: Chunk): Set { group.childrenIterable.forEach(recurseGroup); } - if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)) { + if ( + wmfSharedChunk(childChunk, compilation) || + addIfNotExist(allChunks, childChunk) + ) { return; } Array.from(childChunk.groupsIterable).forEach(recurseGroup);