Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix call undefined delegate #1149

Merged
merged 12 commits into from
Jul 19, 2023
1 change: 1 addition & 0 deletions apps/3000-home/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const nextConfig = {
antd: {},
},
extraOptions: {
debug:false,
exposePages: true,
enableImageLoaderFix: true,
enableUrlLoaderFix: true,
Expand Down
4 changes: 1 addition & 3 deletions apps/3000-home/remote-delegate.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {importDelegatedModule} from "@module-federation/utilities/src/utils/importDelegatedModule";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing eager import which should break application

/* eslint-disable no-undef */
// eslint-disable-next-line no-async-promise-executor
module.exports = new Promise(async (resolve, reject) => {
//eslint-disable-next-line
const currentRequest = new URLSearchParams(__resourceQuery).get('remote');

const [global, url] = currentRequest.split('@');
const { importDelegatedModule } = await import(
'@module-federation/utilities/src/utils/importDelegatedModule'
);

importDelegatedModule({
global,
Expand Down
189 changes: 189 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"prod": "nx build:libs && nx run-many --target=build --verbose --output-style=stream",
"commit": "cz",
"git:pruneTags": "git tag -l | xargs git tag -d && git fetch --all --prune --tags",
"release:normal": "nx run-many --target=version --projects=utils,node,nextjs-mf --maxParallel=1 --allowEmptyRelease=true",
"prerelease:all": "nx run-many --target=version --projects=utils,node,nextjs-mf --releaseAs=prerelease --preid=rc --maxParallel=1 --allowEmptyRelease=true",
"release:node": "nx run node:version",
"release:next": "nx run nextjs-mf:version",
Expand Down
7 changes: 2 additions & 5 deletions packages/nextjs-mf/src/default-delegate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { importDelegatedModule } from '@module-federation/utilities/src/utils/importDelegatedModule'

// eslint-disable-next-line no-async-promise-executor
module.exports = new Promise(async (resolve, reject) => {
// @ts-ignore
const { importDelegatedModule } = await import(
// @ts-ignore
'@module-federation/utilities/src/utils/importDelegatedModule'
);
// eslint-disable-next-line no-undef
const currentRequest = new URLSearchParams(__resourceQuery).get('remote');
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RemoveEagerModulesFromRuntimePlugin {
);
return;
}
//return
//return
compiler.hooks.thisCompilation.tap(
'RemoveEagerModulesFromRuntimePlugin',
(compilation: Compilation) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/node/src/plugins/ChunkCorrelationPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ class FederationStatsPlugin {
// Iterate over each dependency within the block.
for (const dep of blockmodule.dependencies) {
// Get the module that corresponds to the dependency.
const { module } = compilation.moduleGraph.getConnection(dep);
const connection = compilation.moduleGraph.getConnection(dep);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only search valid module connections to relocate in graph

if(!connection) continue;
const { module } = connection;
const moduleChunks =
compilation.chunkGraph.getModuleChunksIterable(module);
// Iterate over each chunk associated with the module.
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/plugins/NodeFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type { Compiler, container } from 'webpack';
import type { ModuleFederationPluginOptions } from '../types';
import { extractUrlAndGlobal } from '@module-federation/utilities';
import { extractUrlAndGlobal } from '@module-federation/utilities/src/utils/pure';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move pure utils to pure folder for smaller footprint of code moved into runtime module as eager


interface NodeFederationOptions extends ModuleFederationPluginOptions {
experiments?: Record<string, unknown>;
Expand Down
25 changes: 6 additions & 19 deletions packages/utilities/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/utilities",
"main": "packages/utilities/src/index.ts",
"tsConfig": "packages/utilities/tsconfig.lib.json",
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true,
"assets": [
"packages/utilities/*.md"
]
"assets": ["packages/utilities/*.md"]
}
},
"publish": {
Expand All @@ -33,20 +29,14 @@
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": [
"{options.outputFile}"
],
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/utilities/**/*.ts"
]
"lintFilePatterns": ["packages/utilities/**/*.ts"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": [
"{workspaceRoot}/coverage/packages/utilities"
],
"outputs": ["{workspaceRoot}/coverage/packages/utilities"],
"options": {
"jestConfig": "packages/utilities/jest.config.ts",
"passWithNoTests": true
Expand All @@ -55,10 +45,7 @@
"version": {
"executor": "@jscutlery/semver:version",
"options": {
"postTargets": [
"utils:npm",
"utils:github"
],
"postTargets": ["utils:npm", "utils:github"],
"trackDeps": true,
"push": true
}
Expand Down
31 changes: 28 additions & 3 deletions packages/utilities/src/plugins/DelegateModulesPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ import DelegateModulesPlugin from './DelegateModulesPlugin';
import { Compilation } from 'webpack';
import { RawSource } from 'webpack-sources';

function createMockModuleDependency(resource: string): any {
return {
resource,
identifier: () => resource,
source: () => new RawSource(''),
dependencies: [],
};
}
const dependency = createMockModuleDependency('dependency');
// Mock a minimal Webpack Module
function createMockModule(resource: string): any {
return {
resource,
identifier: () => resource,
source: () => new RawSource(''),
dependencies: [dependency],
};
}

Expand All @@ -25,6 +35,8 @@ function createMockCompiler(): any {
};
}

const chunkMap = {};

// Mock a minimal Webpack Compilation
function createMockCompilation(): Compilation {
return {
Expand All @@ -37,10 +49,23 @@ function createMockCompilation(): Compilation {
},
},
chunkGraph: {
isModuleInChunk: jest.fn(),
connectChunkAndModule: jest.fn(),
//@ts-ignore
isModuleInChunk: jest.fn((module, chunk) => {
//@ts-ignore
return !!chunkMap?.[chunk.name]?.[module.identifier()];
}),
// @ts-ignore
connectChunkAndModule: jest.fn((chunk, module) => {
//@ts-ignore
chunkMap[chunk.name] = {};
//@ts-ignore
chunkMap[chunk.name][module.identifier()] = module;
}),
disconnectChunkAndModule: jest.fn(),
},
moduleGraph: {
getModule: jest.fn(() => dependency),
},
} as unknown as Compilation;
}

Expand Down Expand Up @@ -102,7 +127,7 @@ describe('DelegateModulesPlugin', () => {

// Check if connectChunkAndModule was called
expect(compilation.chunkGraph.connectChunkAndModule).toHaveBeenCalledTimes(
4
8
);

// Check if disconnectChunkAndModule was called
Expand Down
Loading