Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/enhanced/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "warn",
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-restricted-imports": [
"error",
{
"paths": [
{
"name": "webpack",
"message": "Please use require(normalizeWebpackPath('webpack')) instead.",
"allowTypeImports": true
}
],
"patterns": [
{
"group": ["webpack/lib/*"],
"message": "Please use require(normalizeWebpackPath('webpack')) instead.",
"allowTypeImports": true
}
]
}
]
}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
"files": ["*.js", "*.jsx"]
}
]
}
39 changes: 30 additions & 9 deletions packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import Compiler from 'webpack/lib/Compiler';
import Compilation from 'webpack/lib/Compilation';
import Chunk, { Source } from 'webpack/lib/Chunk';
import RuntimeGlobals from 'webpack/lib/RuntimeGlobals';
import SortableSet from 'webpack/lib/util/SortableSet';
import Module from 'webpack/lib/Module';
import { StartupRenderContext } from 'webpack/lib/javascript/JavascriptModulesPlugin';
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import type {
Compiler,
Compilation,
Chunk,
sources,
Module,
RuntimeGlobals,
javascript,
} from 'webpack';
import type { SyncWaterfallHook } from 'tapable';

const SortableSet = require(
normalizeWebpackPath('webpack/lib/util/SortableSet'),
) as typeof import('webpack/lib/util/SortableSet');

type CompilationHooksJavascriptModulesPlugin = ReturnType<
typeof javascript.JavascriptModulesPlugin.getCompilationHooks
>;
type RenderStartup = CompilationHooksJavascriptModulesPlugin['renderStartup'];

type InferStartupRenderContext<T> = T extends SyncWaterfallHook<
[infer Source, infer Module, infer StartupRenderContext]
>
? StartupRenderContext
: never;

type StartupRenderContext = InferStartupRenderContext<RenderStartup>;

interface Options {
eager?: RegExp | ((module: Module) => boolean);
Expand Down Expand Up @@ -52,7 +73,7 @@ class AsyncEntryStartupPlugin {
).renderStartup.tap(
'AsyncEntryStartupPlugin',
(
source: Source,
source: sources.Source,
_renderContext: Module,
upperContext: StartupRenderContext,
) => {
Expand Down Expand Up @@ -267,7 +288,7 @@ class AsyncEntryStartupPlugin {
initialEntryModules: string[],
shared: string,
remotes: string,
source: Source,
source: sources.Source,
) {
const { Template } = compiler.webpack;
if (
Expand Down
10 changes: 8 additions & 2 deletions packages/enhanced/src/lib/container/ContainerEntryDependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
*/

import { ExposeOptions } from './ContainerEntryModule';
import makeSerializable from 'webpack/lib/util/makeSerializable';
import Dependency from 'webpack/lib/Dependency';
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';

const makeSerializable = require(
normalizeWebpackPath('webpack/lib/util/makeSerializable'),
);
const { Dependency } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');

class ContainerEntryDependency extends Dependency {
public name: string;
Expand Down
41 changes: 23 additions & 18 deletions packages/enhanced/src/lib/container/ContainerEntryModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
*/

'use strict';
import AsyncDependenciesBlock from 'webpack/lib/AsyncDependenciesBlock';
import Dependency from 'webpack/lib/Dependency';
import Template from 'webpack/lib/Template';
import Module from 'webpack/lib/Module';
import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals';
import { OriginalSource, RawSource } from 'webpack-sources';
import { JAVASCRIPT_MODULE_TYPE_DYNAMIC } from 'webpack/lib/ModuleTypeConstants';
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import type { Dependency, Compilation } from 'webpack';

const makeSerializable = require(
normalizeWebpackPath('webpack/lib/util/makeSerializable'),
) as typeof import('webpack/lib/util/makeSerializable');
const { sources: webpackSources } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');
const { AsyncDependenciesBlock, Template, Module, RuntimeGlobals } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');
const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require(
normalizeWebpackPath('webpack/lib/ModuleTypeConstants'),
) as typeof import('webpack/lib/ModuleTypeConstants');
const StaticExportsDependency = require(
normalizeWebpackPath('webpack/lib/dependencies/StaticExportsDependency'),
) as typeof import('webpack/lib/dependencies/StaticExportsDependency');

import ContainerExposedDependency from './ContainerExposedDependency';
import StaticExportsDependency from 'webpack/lib/dependencies/StaticExportsDependency';
import type Compilation from 'webpack/lib/Compilation';
import type {
LibIdentOptions,
NeedBuildContext,
Expand All @@ -25,7 +35,6 @@ import type {
ResolverWithOptions,
} from 'webpack/lib/Module';
import type WebpackError from 'webpack/lib/WebpackError';
import makeSerializable from 'webpack/lib/util/makeSerializable';

const SOURCE_TYPES = new Set(['javascript']);

Expand Down Expand Up @@ -106,6 +115,7 @@ class ContainerEntryModule extends Module {
* @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
* @returns {void}
*/
// @ts-ignore typeof webpack/lib !== typeof webpack/types
override needBuild(
context: NeedBuildContext,
callback: (
Expand All @@ -124,6 +134,7 @@ class ContainerEntryModule extends Module {
* @param {function(WebpackError): void} callback callback function
* @returns {void}
*/
// @ts-ignore typeof webpack/lib !== typeof webpack/types
override build(
options: WebpackOptions,
compilation: Compilation,
Expand Down Expand Up @@ -186,7 +197,6 @@ class ContainerEntryModule extends Module {
RuntimeGlobals.exports,
]);
const getters = [];
//@ts-ignore
for (const block of this.blocks) {
const { dependencies } = block;

Expand All @@ -200,10 +210,8 @@ class ContainerEntryModule extends Module {
});

let str;
//@ts-ignore
if (modules.some((m) => !m.module)) {
str = runtimeTemplate.throwMissingModuleErrorBlock({
//@ts-ignore
request: modules.map((m) => m.request).join(', '),
});
} else {
Expand Down Expand Up @@ -281,8 +289,8 @@ class ContainerEntryModule extends Module {
sources.set(
'javascript',
this.useSourceMap || this.useSimpleSourceMap
? new OriginalSource(source, 'webpack/container-entry')
: new RawSource(source),
? new webpackSources.OriginalSource(source, 'webpack/container-entry')
: new webpackSources.RawSource(source),
);

return {
Expand All @@ -301,17 +309,14 @@ class ContainerEntryModule extends Module {
/**
* @param {ObjectSerializerContext} context context
*/
//@ts-ignore
override serialize(context: ObjectSerializerContext): void {
const { write } = context;
write(this._name);
write(this._exposes);
write(this._shareScope);
//@ts-ignore
super.serialize(context);
}
}
//@ts-ignore
makeSerializable(
ContainerEntryModule,
'enhanced/lib/container/ContainerEntryModule',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

'use strict';

import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import ContainerEntryModule from './ContainerEntryModule';
import ContainerEntryDependency from './ContainerEntryDependency';
import ModuleFactory from 'webpack/lib/ModuleFactory';

const ModuleFactory = require(
normalizeWebpackPath('webpack/lib/ModuleFactory'),
) as typeof import('webpack/lib/ModuleFactory');
import type {
ModuleFactoryCreateData,
ModuleFactoryResult,
Expand All @@ -29,6 +33,7 @@ export default class ContainerEntryModuleFactory extends ModuleFactory {
const dep = containerDependencies[0];

callback(null, {
//@ts-ignore
module: new ContainerEntryModule(dep.name, dep.exposes, dep.shareScope),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
*/
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';

const makeSerializable = require(
normalizeWebpackPath('webpack/lib/util/makeSerializable'),
) as typeof import('webpack/lib/util/makeSerializable');
const { dependencies } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');

import type {
ObjectDeserializerContext,
ObjectSerializerContext,
} from 'webpack/lib/dependencies/ModuleDependency';
import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency';
import makeSerializable from 'webpack/lib/util/makeSerializable';

class ContainerExposedDependency extends ModuleDependency {
class ContainerExposedDependency extends dependencies.ModuleDependency {
exposedName: string;
override request: string;

Expand Down
18 changes: 13 additions & 5 deletions packages/enhanced/src/lib/container/ContainerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
*/
//@ts-ignore
import createSchemaValidation from 'webpack/lib/util/create-schema-validation';
import {
getWebpackPath,
normalizeWebpackPath,
} from '@module-federation/sdk/normalize-webpack-path';
import type { Compiler, Compilation } from 'webpack';
import ContainerEntryDependency from './ContainerEntryDependency';
import ContainerEntryModuleFactory from './ContainerEntryModuleFactory';
import ContainerExposedDependency from './ContainerExposedDependency';
import { parseOptions } from './options';
import type Compiler from 'webpack/lib/Compiler';
import type Compilation from 'webpack/lib/Compilation';
import type { ContainerPluginOptions } from '../../declarations/plugins/container/ContainerPlugin';

const createSchemaValidation = require(
normalizeWebpackPath('webpack/lib/util/create-schema-validation'),
) as typeof import('webpack/lib/util/create-schema-validation');

const validate = createSchemaValidation(
//eslint-disable-next-line
require('webpack/schemas/plugins/container/ContainerPlugin.check.js'),
Expand Down Expand Up @@ -56,6 +61,9 @@ class ContainerPlugin {
}

apply(compiler: Compiler): void {
process.env['FEDERATION_WEBPACK_PATH'] =
process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler);

const { name, exposes, shareScope, filename, library, runtime } =
this._options;

Expand Down Expand Up @@ -95,8 +103,8 @@ class ContainerPlugin {
PLUGIN_NAME,
(compilation: Compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(
//@ts-ignore
ContainerEntryDependency,
//@ts-ignore
new ContainerEntryModuleFactory(),
);

Expand Down
Loading