Skip to content

Commit

Permalink
Merge 742698e into 914fdd9
Browse files Browse the repository at this point in the history
  • Loading branch information
sowtame committed Aug 24, 2023
2 parents 914fdd9 + 742698e commit e52ce10
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 3 deletions.
3 changes: 3 additions & 0 deletions 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
4 changes: 4 additions & 0 deletions examples/webpack-module-federaition-host/bootstrap.js
@@ -0,0 +1,4 @@
// mock dependency
require("lodash");

console.log("ok");
1 change: 1 addition & 0 deletions examples/webpack-module-federaition-host/index.js
@@ -0,0 +1 @@
import("./bootstrap");
17 changes: 17 additions & 0 deletions 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"
}
}
47 changes: 47 additions & 0 deletions 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-");
});
});
},
},
],
};
3 changes: 3 additions & 0 deletions examples/webpack-module-federaition-remote/README.md
@@ -0,0 +1,3 @@
# With Webpack module federation

Usage of With Webpack module federation as remote app
4 changes: 4 additions & 0 deletions examples/webpack-module-federaition-remote/bootstrap.js
@@ -0,0 +1,4 @@
// mock dependency
require("lodash");

console.log("ok");
1 change: 1 addition & 0 deletions examples/webpack-module-federaition-remote/index.js
@@ -0,0 +1 @@
import("./bootstrap");
17 changes: 17 additions & 0 deletions examples/webpack-module-federaition-remote/package.json
@@ -0,0 +1,17 @@
{
"name": "webpack-module-federaition-remote",
"description": "Ensure integrity works 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"
}
}
43 changes: 43 additions & 0 deletions 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-");
});
});
},
},
],
};
7 changes: 5 additions & 2 deletions webpack-subresource-integrity/src/manifest.ts
Expand Up @@ -17,6 +17,7 @@ import {
allChunksInChunkIterable,
allChunksInPrimaryChunkIterable,
sriHashVariableReference,
wmfSharedChunk,
} from "./util";
import { createDAGfromGraph } from "./scc";
import { RuntimeModule, Template, Chunk } from "webpack";
Expand Down Expand Up @@ -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<Chunk>());
for (const childChunk of allChunksInChunkIterable(vertex)) {
edges.get(vertex)?.add(childChunk);
if (!wmfSharedChunk(childChunk)) {
edges.get(vertex)?.add(childChunk);
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion webpack-subresource-integrity/src/util.ts
Expand Up @@ -87,6 +87,11 @@ export function addIfNotExist<T>(set: Set<T>, 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 findChunks(chunk: Chunk): Set<Chunk> {
const allChunks = new Set<Chunk>();
Expand All @@ -99,7 +104,9 @@ export function findChunks(chunk: Chunk): Set<Chunk> {
group.childrenIterable.forEach(recurseGroup);
}

if (addIfNotExist(allChunks, childChunk)) return;
if (wmfSharedChunk(childChunk) || addIfNotExist(allChunks, childChunk)) {
return;
}
Array.from(childChunk.groupsIterable).forEach(recurseGroup);
})(chunk);

Expand Down
26 changes: 26 additions & 0 deletions yarn.lock
Expand Up @@ -9574,6 +9574,32 @@ __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"
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"
Expand Down

0 comments on commit e52ce10

Please sign in to comment.