Skip to content

Commit

Permalink
fix: inconsistent behavior with lazy compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nio-o committed Nov 29, 2023
1 parent 6bea445 commit c4d800a
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 188 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@
"typedoc": "^0.23.9",
"typescript": "^4.8.4",
"uvu": "^0.5.6",
"webpack": "^5.67.0"
"webpack": "^5.89.0"
},
"peerDependencies": {
"webpack": "^5.67.0"
"webpack": "^5.89.0"
},
"repository": {
"type": "git",
Expand Down
46 changes: 19 additions & 27 deletions src/main/ts/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ModuleFederationManifest } from './schema'
import type { Hooks } from './hooks'
import type { ModuleFederationManifest } from './schema'

import webpack from 'webpack'
import { AsyncSeriesHook } from 'tapable'
import webpack, { type Chunk, type Module } from 'webpack'

import {
exposedModuleParser,
remoteModuleParser,
consumedModuleParser,
exposedModuleParser,
providedModuleParser,
remoteModuleParser,
} from './identifier-parsers'

type ModuleFederationPluginOptions = ConstructorParameters<typeof webpack.container.ModuleFederationPlugin>[0]
Expand Down Expand Up @@ -50,7 +50,7 @@ export class ModuleFederationManifestPlugin {
name: pluginName,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
async () => this.processWebpackAssets(compilation),
async () => await this.processWebpackAssets(compilation),
)
})
}
Expand All @@ -69,23 +69,20 @@ export class ModuleFederationManifestPlugin {

private createManifest(
publicPath: string,
entryChunk: webpack.StatsChunk | undefined,
modules: webpack.StatsModule[],
entryChunk: Chunk | undefined,
modules: Set<Module>,
): ModuleFederationManifest {
const entry: ModuleFederationManifest['entry'] =
entryChunk && entryChunk.files
? {
path: entryChunk.files[0],
}
: undefined
const entry: ModuleFederationManifest['entry'] = entryChunk?.files && {
path: Array.from(entryChunk.files)[0],
}

const remotes: ModuleFederationManifest['remotes'] = {}
const consumes: ModuleFederationManifest['consumes'] = {}
const provides: ModuleFederationManifest['provides'] = {}
const exposes: ModuleFederationManifest['exposes'] = {}

for (const module of modules) {
const { identifier } = module
for (const module of Array.from(modules)) {
const identifier = module.identifier()

if (!identifier) {
continue
Expand Down Expand Up @@ -146,23 +143,18 @@ export class ModuleFederationManifestPlugin {
}

private async processWebpackAssets(compilation: webpack.Compilation): Promise<void> {
const liveStats = compilation.getStats()
const stats = liveStats.toJson({
all: false,
chunks: true,
publicPath: true,
modules: true,
})

const remoteEntryChunk = this.getRemoteEntryChunk(stats)
const manifest = this.createManifest(
compilation.outputOptions.publicPath as string,
this.getRemoteEntryChunk(compilation.chunks),
compilation.modules,
)

const manifest = this.createManifest(stats.publicPath as string, remoteEntryChunk, stats.modules || [])
await ModuleFederationManifestPlugin.getHooks(compilation).onManifestCreated.promise(manifest)

this.emitManifestAsset(compilation, manifest)
}

private getRemoteEntryChunk(stats: webpack.StatsCompilation) {
return stats.chunks?.find((chunk) => chunk.names?.find((name) => name === this.federationPluginOptions.name))
private getRemoteEntryChunk(chunks: Set<Chunk>): Chunk | undefined {
return Array.from(chunks).find((chunk) => chunk.name === this.federationPluginOptions.name)
}
}
8 changes: 4 additions & 4 deletions src/test/ts/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ModuleFederationManifestPlugin } from '../../main/ts'
import { test } from 'uvu'
import { buildAndAssert, buildFixture, getManifestPath } from './helpers/build-and-assert'
import { expect } from 'earljs'
import fs from 'node:fs'
import { test } from 'uvu'
import { ModuleFederationManifestPlugin } from '../../main/ts'
import { buildAndAssert, buildFixture, getManifestPath } from './helpers/build-and-assert'

test('multiple package versions', async (t) => {
await buildAndAssert(t, 'multiple-package-versions', {
Expand Down Expand Up @@ -86,7 +86,7 @@ test('remotes and exposed kitchensink', async (t) => {
modules: ['app'],
},
remote2: {
modules: ['app', 'helpers'],
modules: ['helpers', 'app'],
},
},
})
Expand Down
Loading

0 comments on commit c4d800a

Please sign in to comment.