Skip to content

Commit

Permalink
Merge branch 'main' into fix_call_undefined_delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Jul 18, 2023
2 parents 8a0f95f + 5c3b115 commit d657030
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 115 deletions.
11 changes: 11 additions & 0 deletions packages/nextjs-mf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [7.0.5](https://github.com/module-federation/nextjs-mf/compare/nextjs-mf-7.0.4...nextjs-mf-7.0.5) (2023-07-18)

### Dependency Updates

* `utils` updated to version `2.0.2`
* `node` updated to version `1.0.4`
* `utils` updated to version `2.0.2`
* `utils` updated to version `2.0.2`
* `node` updated to version `1.0.4`


## [7.0.4](https://github.com/module-federation/nextjs-mf/compare/nextjs-mf-7.0.3...nextjs-mf-7.0.4) (2023-07-17)

### Dependency Updates
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-mf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@module-federation/nextjs-mf",
"version": "7.0.4",
"version": "7.0.5",
"license": "MIT",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [1.0.4](https://github.com/module-federation/nextjs-mf/compare/node-1.0.3...node-1.0.4) (2023-07-18)

### Dependency Updates

* `utils` updated to version `2.0.2`
* `utils` updated to version `2.0.2`


## [1.0.3](https://github.com/module-federation/nextjs-mf/compare/node-1.0.2...node-1.0.3) (2023-07-17)

### Dependency Updates
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"public": true,
"name": "@module-federation/node",
"version": "1.0.3",
"version": "1.0.4",
"type": "commonjs",
"main": "src/index.js",
"exports": {
Expand Down
4 changes: 4 additions & 0 deletions packages/utilities/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).

## [2.0.2](https://github.com/module-federation/nextjs-mf/compare/utils-2.0.1...utils-2.0.2) (2023-07-18)



## [2.0.1](https://github.com/module-federation/nextjs-mf/compare/utils-2.0.0...utils-2.0.1) (2023-07-17)


Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@module-federation/utilities",
"version": "2.0.1",
"version": "2.0.2",
"type": "commonjs",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down
159 changes: 50 additions & 109 deletions packages/utilities/src/plugins/DelegateModulesPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,113 +1,54 @@
import DelegateModulesPlugin from './DelegateModulesPlugin';
import { Compilation } from 'webpack';
import { RawSource } from 'webpack-sources';

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

// Mock a minimal Webpack Compiler
function createMockCompiler(): any {
return {
options: {
name: 'test-compiler',
},
hooks: {
compilation: {
tap: jest.fn(),
},
},
};
}

// Mock a minimal Webpack Compilation
function createMockCompilation(): Compilation {
return {
hooks: {
optimizeChunks: {
tap: jest.fn(),
},
finishModules: {
tapAsync: jest.fn(),
},
it('should add and remove delegate modules correctly', () => {
const plugin = new DelegateModulesPlugin({
runtime: 'runtime',
container: 'container',
remotes: {
remote1: 'internal /path/to/remote1',
remote2: 'internal /path/to/remote2',
},
chunkGraph: {
isModuleInChunk: jest.fn(),
connectChunkAndModule: jest.fn(),
disconnectChunkAndModule: jest.fn(),
},
} as unknown as Compilation;
}

describe('DelegateModulesPlugin', () => {
it('should be an instance of DelegateModulesPlugin', () => {
const plugin = new DelegateModulesPlugin({});
expect(plugin).toBeInstanceOf(DelegateModulesPlugin);
});

it('should apply the plugin to the compiler and call the hooks', () => {
const plugin = new DelegateModulesPlugin({});
const compiler = createMockCompiler();

plugin.apply(compiler);

expect(compiler.hooks.compilation.tap).toHaveBeenCalledWith(
'DelegateModulesPlugin',
expect.any(Function)
);
});

it('should add and remove delegate modules correctly', () => {
const plugin = new DelegateModulesPlugin({
runtime: 'runtime',
container: 'container',
remotes: {
remote1: 'internal /path/to/remote1',
remote2: 'internal /path/to/remote2',
},
});

const compiler = createMockCompiler();
const compilation = createMockCompilation();

plugin.apply(compiler);

// Call the compilation tap function
(compiler.hooks.compilation.tap as jest.Mock).mock.calls[0][1](compilation);

// Call the finishModules tap function
(compilation.hooks.finishModules.tapAsync as jest.Mock).mock.calls[0][1](
[
createMockModule('/path/to/remote1'),
createMockModule('/path/to/remote2'),
createMockModule('/path/to/non-delegate-module'),
],
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {}
);

// Check if delegate modules are added
expect(plugin['_delegateModules'].size).toBe(2);

// Call the optimizeChunks tap function
(compilation.hooks.optimizeChunks.tap as jest.Mock).mock.calls[0][1]([
{ name: 'runtime', hasRuntime: () => true },
{ name: 'container', hasRuntime: () => false },
]);

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

// Check if disconnectChunkAndModule was called
expect(
compilation.chunkGraph.disconnectChunkAndModule
).toHaveBeenCalledTimes(2);
});
const compiler = createMockCompiler();
const compilation = createMockCompilation();

// Mock getChunkByName
jest.spyOn(plugin, 'getChunkByName').mockImplementation((chunks, name) => chunks.find(chunk => chunk.name === name));

plugin.apply(compiler);

// Call the compilation tap function
(compiler.hooks.compilation.tap as jest.Mock).mock.calls[0][1](compilation);

// Call the finishModules tap function
(compilation.hooks.finishModules.tapAsync as jest.Mock).mock.calls[0][1](
[
createMockModule('/path/to/remote1'),
createMockModule('/path/to/remote2'),
createMockModule('/path/to/non-delegate-module'),
],
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {}
);

// Check if delegate modules are added
expect(plugin['_delegateModules'].size).toBe(2);

// Call the optimizeChunks tap function
(compilation.hooks.optimizeChunks.tap as jest.Mock).mock.calls[0][1]([
{ name: 'runtime', hasRuntime: () => true },
{ name: 'container', hasRuntime: () => false },
]);

// Check if getChunkByName was called
expect((plugin.getChunkByName as jest.Mock)).toHaveBeenCalledTimes(2);

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

// Check if disconnectChunkAndModule was called
expect(
compilation.chunkGraph.disconnectChunkAndModule
).toHaveBeenCalledTimes(2);
});
3 changes: 1 addition & 2 deletions tools/tsconfig.tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"rootDir": ".",
"module": "commonjs",
"target": "es5",
"types": ["node"],
"importHelpers": false
"types": ["node"]
},
"include": ["**/*.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"importHelpers": false,
"target": "es2021",
"module": "esnext",
"lib": ["es2021", "dom"],
Expand Down

0 comments on commit d657030

Please sign in to comment.