diff --git a/packages/enhanced/.eslintrc.json b/packages/enhanced/.eslintrc.json index afb3e80a75f..6164350a932 100644 --- a/packages/enhanced/.eslintrc.json +++ b/packages/enhanced/.eslintrc.json @@ -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"] } ] } diff --git a/packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts b/packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts index 32444127af2..d73b8ab2c78 100644 --- a/packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts +++ b/packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts @@ -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 extends SyncWaterfallHook< + [infer Source, infer Module, infer StartupRenderContext] +> + ? StartupRenderContext + : never; + +type StartupRenderContext = InferStartupRenderContext; interface Options { eager?: RegExp | ((module: Module) => boolean); @@ -52,7 +73,7 @@ class AsyncEntryStartupPlugin { ).renderStartup.tap( 'AsyncEntryStartupPlugin', ( - source: Source, + source: sources.Source, _renderContext: Module, upperContext: StartupRenderContext, ) => { @@ -267,7 +288,7 @@ class AsyncEntryStartupPlugin { initialEntryModules: string[], shared: string, remotes: string, - source: Source, + source: sources.Source, ) { const { Template } = compiler.webpack; if ( diff --git a/packages/enhanced/src/lib/container/ContainerEntryDependency.ts b/packages/enhanced/src/lib/container/ContainerEntryDependency.ts index 6991a9464d2..0f77f07aa08 100644 --- a/packages/enhanced/src/lib/container/ContainerEntryDependency.ts +++ b/packages/enhanced/src/lib/container/ContainerEntryDependency.ts @@ -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; diff --git a/packages/enhanced/src/lib/container/ContainerEntryModule.ts b/packages/enhanced/src/lib/container/ContainerEntryModule.ts index dd184bf4c21..d73dd63a5cc 100644 --- a/packages/enhanced/src/lib/container/ContainerEntryModule.ts +++ b/packages/enhanced/src/lib/container/ContainerEntryModule.ts @@ -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, @@ -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']); @@ -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: ( @@ -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, @@ -186,7 +197,6 @@ class ContainerEntryModule extends Module { RuntimeGlobals.exports, ]); const getters = []; - //@ts-ignore for (const block of this.blocks) { const { dependencies } = block; @@ -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 { @@ -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 { @@ -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', diff --git a/packages/enhanced/src/lib/container/ContainerEntryModuleFactory.ts b/packages/enhanced/src/lib/container/ContainerEntryModuleFactory.ts index c6251182c9d..cbf1c0fcea9 100644 --- a/packages/enhanced/src/lib/container/ContainerEntryModuleFactory.ts +++ b/packages/enhanced/src/lib/container/ContainerEntryModuleFactory.ts @@ -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, @@ -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), }); } diff --git a/packages/enhanced/src/lib/container/ContainerExposedDependency.ts b/packages/enhanced/src/lib/container/ContainerExposedDependency.ts index d09ffc90a2b..263f88abe67 100644 --- a/packages/enhanced/src/lib/container/ContainerExposedDependency.ts +++ b/packages/enhanced/src/lib/container/ContainerExposedDependency.ts @@ -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; diff --git a/packages/enhanced/src/lib/container/ContainerPlugin.ts b/packages/enhanced/src/lib/container/ContainerPlugin.ts index ccf60bdea8c..75db244ad8d 100644 --- a/packages/enhanced/src/lib/container/ContainerPlugin.ts +++ b/packages/enhanced/src/lib/container/ContainerPlugin.ts @@ -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'), @@ -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; @@ -95,8 +103,8 @@ class ContainerPlugin { PLUGIN_NAME, (compilation: Compilation, { normalModuleFactory }) => { compilation.dependencyFactories.set( - //@ts-ignore ContainerEntryDependency, + //@ts-ignore new ContainerEntryModuleFactory(), ); diff --git a/packages/enhanced/src/lib/container/ContainerReferencePlugin.ts b/packages/enhanced/src/lib/container/ContainerReferencePlugin.ts index 5953dc2e899..e64a39770cf 100644 --- a/packages/enhanced/src/lib/container/ContainerReferencePlugin.ts +++ b/packages/enhanced/src/lib/container/ContainerReferencePlugin.ts @@ -2,10 +2,13 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy */ +import type { Compiler } from 'webpack'; +import { + getWebpackPath, + normalizeWebpackPath, +} from '@module-federation/sdk/normalize-webpack-path'; -import type Compiler from 'webpack/lib/Compiler'; -import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import createSchemaValidation from 'webpack/lib/util/create-schema-validation'; +// import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; import FallbackDependency from './FallbackDependency'; import FallbackItemDependency from './FallbackItemDependency'; import FallbackModuleFactory from './FallbackModuleFactory'; @@ -13,16 +16,20 @@ import RemoteModule from './RemoteModule'; import RemoteRuntimeModule from './RemoteRuntimeModule'; import RemoteToExternalDependency from './RemoteToExternalDependency'; import { parseOptions } from './options'; -import ExternalsPlugin from 'webpack/lib/ExternalsPlugin'; -import type Compilation from 'webpack/lib/Compilation'; -import type { ResolveData } from 'webpack/lib/NormalModuleFactory'; -import NormalModuleFactory from 'webpack/lib/NormalModuleFactory'; import { ExternalsType, ContainerReferencePluginOptions, RemotesConfig, } from '../../declarations/plugins/container/ContainerReferencePlugin'; +const { ExternalsPlugin } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); + +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/ContainerReferencePlugin.check.js'), @@ -65,6 +72,9 @@ class ContainerReferencePlugin { * @returns {void} */ apply(compiler: Compiler): void { + process.env['FEDERATION_WEBPACK_PATH'] = + process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler); + const { _remotes: remotes, _remoteType: remoteType } = this; /** @type {Record} */ @@ -83,15 +93,11 @@ class ContainerReferencePlugin { } } const Externals = compiler.webpack.ExternalsPlugin || ExternalsPlugin; - //@ts-ignore new Externals(remoteType, remoteExternals).apply(compiler); compiler.hooks.compilation.tap( 'ContainerReferencePlugin', - ( - compilation: Compilation, - { normalModuleFactory }: { normalModuleFactory: NormalModuleFactory }, - ) => { + (compilation, { normalModuleFactory }) => { compilation.dependencyFactories.set( RemoteToExternalDependency, normalModuleFactory, @@ -104,13 +110,14 @@ class ContainerReferencePlugin { compilation.dependencyFactories.set( FallbackDependency, + // @ts-ignore new FallbackModuleFactory(), ); normalModuleFactory.hooks.factorize.tap( 'ContainerReferencePlugin', - //@ts-ignore - (data: ResolveData): Module => { + // @ts-ignore + (data) => { if (!data.request.includes('!')) { for (const [key, config] of remotes) { if ( @@ -139,13 +146,13 @@ class ContainerReferencePlugin { ); compilation.hooks.runtimeRequirementInTree - .for(RuntimeGlobals.ensureChunkHandlers) + .for(compiler.webpack.RuntimeGlobals.ensureChunkHandlers) .tap('ContainerReferencePlugin', (chunk, set) => { - set.add(RuntimeGlobals.module); - set.add(RuntimeGlobals.moduleFactoriesAddOnly); - set.add(RuntimeGlobals.hasOwnProperty); - set.add(RuntimeGlobals.initializeSharing); - set.add(RuntimeGlobals.shareScopeMap); + set.add(compiler.webpack.RuntimeGlobals.module); + set.add(compiler.webpack.RuntimeGlobals.moduleFactoriesAddOnly); + set.add(compiler.webpack.RuntimeGlobals.hasOwnProperty); + set.add(compiler.webpack.RuntimeGlobals.initializeSharing); + set.add(compiler.webpack.RuntimeGlobals.shareScopeMap); compilation.addRuntimeModule(chunk, new RemoteRuntimeModule()); }); }, diff --git a/packages/enhanced/src/lib/container/FallbackDependency.ts b/packages/enhanced/src/lib/container/FallbackDependency.ts index 1512dbccbe9..a2bb7c68daf 100644 --- a/packages/enhanced/src/lib/container/FallbackDependency.ts +++ b/packages/enhanced/src/lib/container/FallbackDependency.ts @@ -9,8 +9,14 @@ import type { ObjectDeserializerContext, ObjectSerializerContext, } from 'webpack/lib/Dependency'; -import Dependency from 'webpack/lib/Dependency'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; +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 FallbackDependency extends Dependency { requests: string[]; @@ -47,10 +53,6 @@ class FallbackDependency extends Dependency { super.serialize(context); } - /** - * @param {ObjectDeserializerContext} context context - * @returns {FallbackDependency} deserialize fallback dependency - */ static deserialize(context: ObjectDeserializerContext): FallbackDependency { const { read } = context; const obj = new FallbackDependency(read()); diff --git a/packages/enhanced/src/lib/container/FallbackItemDependency.ts b/packages/enhanced/src/lib/container/FallbackItemDependency.ts index 5092861cc65..f25c5de81cd 100644 --- a/packages/enhanced/src/lib/container/FallbackItemDependency.ts +++ b/packages/enhanced/src/lib/container/FallbackItemDependency.ts @@ -2,10 +2,16 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ -import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; -class FallbackItemDependency extends ModuleDependency { +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); +const { dependencies } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); + +class FallbackItemDependency extends dependencies.ModuleDependency { /** * @param {string} request request */ diff --git a/packages/enhanced/src/lib/container/FallbackModule.ts b/packages/enhanced/src/lib/container/FallbackModule.ts index 00c7cfcb90c..1b5f6a83723 100644 --- a/packages/enhanced/src/lib/container/FallbackModule.ts +++ b/packages/enhanced/src/lib/container/FallbackModule.ts @@ -5,7 +5,8 @@ 'use strict'; -import { RawSource } from 'webpack-sources'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { ChunkGraph, Chunk } from 'webpack'; import type { RequestShortener, LibIdentOptions, @@ -20,15 +21,21 @@ import type { ObjectDeserializerContext, ObjectSerializerContext, } from 'webpack/lib/Module'; -import Module from 'webpack/lib/Module'; -import type ChunkGraph from 'webpack/lib/ChunkGraph'; -import Chunk from 'webpack/lib/Chunk'; -import { WEBPACK_MODULE_TYPE_FALLBACK } from 'webpack/lib/ModuleTypeConstants'; -import RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import Template from 'webpack/lib/Template'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; import FallbackItemDependency from './FallbackItemDependency'; +const { sources: webpackSources } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const { Template, Module, RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); +const { WEBPACK_MODULE_TYPE_FALLBACK } = require( + normalizeWebpackPath('webpack/lib/ModuleTypeConstants'), +) as typeof import('webpack/lib/ModuleTypeConstants'); + const TYPES = new Set(['javascript']); const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]); @@ -87,6 +94,7 @@ class FallbackModule extends Module { * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild * @returns {void} */ + // @ts-ignore override needBuild( context: NeedBuildContext, callback: (error: WebpackError | null, result?: boolean) => void, @@ -102,6 +110,7 @@ class FallbackModule extends Module { * @param {function(WebpackError=): void} callback callback function * @returns {void} */ + // @ts-ignore override build( options: WebpackOptions, compilation: Compilation, @@ -140,6 +149,7 @@ class FallbackModule extends Module { * @param {CodeGenerationContext} context context for code generation * @returns {CodeGenerationResult} result */ + // @ts-ignore override codeGeneration({ runtimeTemplate, moduleGraph, @@ -172,7 +182,7 @@ class FallbackModule extends Module { 'module.exports = loop();', ]); const sources = new Map(); - sources.set('javascript', new RawSource(code)); + sources.set('javascript', new webpackSources.RawSource(code)); return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS }; } diff --git a/packages/enhanced/src/lib/container/FallbackModuleFactory.ts b/packages/enhanced/src/lib/container/FallbackModuleFactory.ts index 35013c97cdd..f6a9b68b956 100644 --- a/packages/enhanced/src/lib/container/FallbackModuleFactory.ts +++ b/packages/enhanced/src/lib/container/FallbackModuleFactory.ts @@ -4,13 +4,16 @@ */ 'use strict'; -import { +import type { ModuleFactoryCreateData, ModuleFactoryResult, } from 'webpack/lib/ModuleFactory'; -import ModuleFactory from 'webpack/lib/ModuleFactory'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; import FallbackModule from './FallbackModule'; -import FallbackDependency from './FallbackDependency'; + +const ModuleFactory = require( + normalizeWebpackPath('webpack/lib/ModuleFactory'), +) as typeof import('webpack/lib/ModuleFactory'); export default class FallbackModuleFactory extends ModuleFactory { /** @@ -22,8 +25,9 @@ export default class FallbackModuleFactory extends ModuleFactory { data: ModuleFactoryCreateData, callback: (error: Error | null, result?: ModuleFactoryResult) => void, ): void { - const dependency = data.dependencies[0] as FallbackDependency; + const dependency = data.dependencies[0]; callback(null, { + // @ts-ignore module: new FallbackModule(dependency.requests), }); } diff --git a/packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts b/packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts index dfd94770630..bb252159cbc 100644 --- a/packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts +++ b/packages/enhanced/src/lib/container/HoistContainerReferencesPlugin.ts @@ -1,4 +1,4 @@ -import { +import type { Compiler, Module, Chunk, @@ -7,13 +7,6 @@ import { WebpackPluginInstance, } from 'webpack'; -/** - * @typedef {import("webpack").Compiler} Compiler - * @typedef {import("webpack").Compilation} Compilation - * @typedef {import("webpack").Chunk} Chunk - * @typedef {import("webpack").Module} Module - */ - /** * This class is used to hoist container references in the code. * @constructor diff --git a/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts b/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts index d2becfee96b..cbda154a36e 100644 --- a/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts +++ b/packages/enhanced/src/lib/container/ModuleFederationPlugin.ts @@ -5,16 +5,22 @@ 'use strict'; -import type Compiler from 'webpack/lib/Compiler'; +import type { Compiler } from 'webpack'; +import { + getWebpackPath, + normalizeWebpackPath, +} from '@module-federation/sdk/normalize-webpack-path'; import isValidExternalsType from 'webpack/schemas/plugins/container/ExternalsType.check.js'; import type { ModuleFederationPluginOptions } from './ModuleFederationPluginTypes'; import SharePlugin from '../sharing/SharePlugin'; -import createSchemaValidation from 'webpack/lib/util/create-schema-validation'; import ContainerPlugin from './ContainerPlugin'; import ContainerReferencePlugin from './ContainerReferencePlugin'; import checkOptions from 'webpack/schemas/plugins/container/ModuleFederationPlugin.check.js'; import schema from '../../schemas/container/ModuleFederationPlugin'; +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 checkOptions, @@ -42,6 +48,9 @@ class ModuleFederationPlugin { * @returns {void} */ apply(compiler: Compiler): void { + process.env['FEDERATION_WEBPACK_PATH'] = + process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler); + const { _options: options } = this; const library = options.library || { type: 'var', name: options.name }; const remoteType = diff --git a/packages/enhanced/src/lib/container/RemoteModule.ts b/packages/enhanced/src/lib/container/RemoteModule.ts index 7329aca1c4c..51f36490722 100644 --- a/packages/enhanced/src/lib/container/RemoteModule.ts +++ b/packages/enhanced/src/lib/container/RemoteModule.ts @@ -2,8 +2,8 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ - -import { RawSource } from 'webpack-sources'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { Compilation, WebpackOptionsNormalized } from 'webpack'; import type { CodeGenerationContext, CodeGenerationResult, @@ -11,19 +11,26 @@ import type { NeedBuildContext, WebpackError, } from 'webpack/lib/Module'; -import Module from 'webpack/lib/Module'; -import RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; import FallbackDependency from './FallbackDependency'; import RemoteToExternalDependency from './RemoteToExternalDependency'; -import type Compilation from 'webpack/lib/Compilation'; import type { ResolverWithOptions } from 'webpack/lib/ResolverFactory'; import type { InputFileSystem } from 'webpack/lib/FileSystemInfo'; import type { RequestShortener } from 'webpack/lib/RuntimeModule'; -import { WEBPACK_MODULE_TYPE_REMOTE } from 'webpack/lib/ModuleTypeConstants'; -import type { WebpackOptionsNormalized } from 'webpack/lib/WebpackOptionsDefaulter'; import type { ObjectDeserializerContext } from 'webpack/lib/serialization/ObjectMiddleware'; +const { sources: webpackSources } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const { Module, RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const { WEBPACK_MODULE_TYPE_REMOTE } = require( + normalizeWebpackPath('webpack/lib/ModuleTypeConstants'), +) as typeof import('webpack/lib/ModuleTypeConstants'); +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); + const TYPES: Set = new Set(['remote', 'share-init']); const RUNTIME_REQUIREMENTS: Set = new Set([RuntimeGlobals.module]); @@ -86,6 +93,7 @@ class RemoteModule extends Module { * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild * @returns {void} */ + // @ts-ignore override needBuild( context: NeedBuildContext, callback: (err: WebpackError | null, needsRebuild?: boolean) => void, @@ -101,6 +109,7 @@ class RemoteModule extends Module { * @param {function(WebpackError=): void} callback callback function * @returns {void} */ + // @ts-ignore override build( options: WebpackOptionsNormalized, compilation: Compilation, @@ -151,14 +160,16 @@ class RemoteModule extends Module { * @param {CodeGenerationContext} context context for code generation * @returns {CodeGenerationResult} result */ + // @ts-ignore override codeGeneration( context: CodeGenerationContext, ): CodeGenerationResult { const { runtimeTemplate, moduleGraph, chunkGraph } = context; + // @ts-ignore const module = moduleGraph.getModule(this.dependencies[0]); const id = module && chunkGraph.getModuleId(module); const sources = new Map(); - sources.set('remote', new RawSource('')); + sources.set('remote', new webpackSources.RawSource('')); const data = new Map(); data.set('share-init', [ { diff --git a/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts b/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts index 4b24ce4ae3e..92f59623080 100644 --- a/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts +++ b/packages/enhanced/src/lib/container/RemoteRuntimeModule.ts @@ -2,11 +2,12 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ -import RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import type Compilation from 'webpack/lib/Compilation'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { Compilation } from 'webpack'; import RemoteModule from './RemoteModule'; -import RuntimeModule from 'webpack/lib/RuntimeModule'; -import Template from 'webpack/lib/Template'; +const { Template, RuntimeModule, RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); class RemoteRuntimeModule extends RuntimeModule { constructor() { @@ -36,8 +37,9 @@ class RemoteRuntimeModule extends RuntimeModule { // @ts-ignore const remotes = (chunkToRemotesMapping[chunk.id] = []); for (const m of modules) { - const module: RemoteModule = m as RemoteModule; + const module: RemoteModule = m as unknown as RemoteModule; const name = module.internalRequest; + // @ts-ignore const id = chunkGraph ? chunkGraph.getModuleId(module) : undefined; const { shareScope } = module; const dep = module.dependencies[0]; diff --git a/packages/enhanced/src/lib/container/RemoteToExternalDependency.d.ts b/packages/enhanced/src/lib/container/RemoteToExternalDependency.d.ts index c0daff1543e..68eea3c3e10 100644 --- a/packages/enhanced/src/lib/container/RemoteToExternalDependency.d.ts +++ b/packages/enhanced/src/lib/container/RemoteToExternalDependency.d.ts @@ -1,3 +1,4 @@ export default RemoteToExternalDependency; declare class RemoteToExternalDependency extends ModuleDependency {} +// eslint-disable-next-line @typescript-eslint/no-restricted-imports import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency'; diff --git a/packages/enhanced/src/lib/container/RemoteToExternalDependency.ts b/packages/enhanced/src/lib/container/RemoteToExternalDependency.ts index 280aa39c008..eca572e8fe1 100644 --- a/packages/enhanced/src/lib/container/RemoteToExternalDependency.ts +++ b/packages/enhanced/src/lib/container/RemoteToExternalDependency.ts @@ -4,11 +4,16 @@ */ 'use strict'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; -import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); +const { dependencies } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); -class RemoteToExternalDependency extends ModuleDependency { +class RemoteToExternalDependency extends dependencies.ModuleDependency { /** * @param {string} request request */ diff --git a/packages/enhanced/src/lib/sharing/ConsumeSharedFallbackDependency.ts b/packages/enhanced/src/lib/sharing/ConsumeSharedFallbackDependency.ts index db780de0045..a38dd89e6e3 100644 --- a/packages/enhanced/src/lib/sharing/ConsumeSharedFallbackDependency.ts +++ b/packages/enhanced/src/lib/sharing/ConsumeSharedFallbackDependency.ts @@ -4,11 +4,15 @@ */ 'use strict'; +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 ModuleDependency from 'webpack/lib/dependencies/ModuleDependency'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; - -class ConsumeSharedFallbackDependency extends ModuleDependency { +class ConsumeSharedFallbackDependency extends dependencies.ModuleDependency { /** * @param {string} request the request */ diff --git a/packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts b/packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts index 03ef2e1ce8b..47f86d558dc 100644 --- a/packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts +++ b/packages/enhanced/src/lib/sharing/ConsumeSharedModule.ts @@ -4,10 +4,7 @@ */ 'use strict'; - -import { RawSource } from 'webpack-sources'; -//@ts-ignore -import AsyncDependenciesBlock from 'webpack/lib/AsyncDependenciesBlock'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; import type { WebpackOptions, Compilation, @@ -24,14 +21,24 @@ import type { Hash, InputFileSystem, } from 'webpack/lib/Module'; -//@ts-ignore -import Module from 'webpack/lib/Module'; -import { WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE } from 'webpack/lib/ModuleTypeConstants'; -import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -//@ts-ignore -import makeSerializable from 'webpack/lib/util/makeSerializable'; -import { rangeToString, stringifyHoley } from 'webpack/lib/util/semver'; import ConsumeSharedFallbackDependency from './ConsumeSharedFallbackDependency'; + +const { rangeToString, stringifyHoley } = require( + normalizeWebpackPath('webpack/lib/util/semver'), +) as typeof import('webpack/lib/util/semver'); +const { AsyncDependenciesBlock, Module, RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const { WEBPACK_MODULE_TYPE_CONSUME_SHARED_MODULE } = require( + normalizeWebpackPath('webpack/lib/ModuleTypeConstants'), +) as typeof import('webpack/lib/ModuleTypeConstants'); +const { sources: webpackSources } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); + export type ConsumeOptions = { /** * fallback request @@ -160,6 +167,7 @@ class ConsumeSharedModule extends Module { * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild * @returns {void} */ + // @ts-ignore override needBuild( context: NeedBuildContext, callback: (error?: WebpackError | null, needsRebuild?: boolean) => void, @@ -175,6 +183,7 @@ class ConsumeSharedModule extends Module { * @param {function(WebpackError=): void} callback callback function * @returns {void} */ + // @ts-ignore override build( options: WebpackOptions, compilation: Compilation, @@ -217,8 +226,10 @@ class ConsumeSharedModule extends Module { * @param {UpdateHashContext} context context * @returns {void} */ + // @ts-ignore override updateHash(hash: Hash, context: UpdateHashContext): void { hash.update(JSON.stringify(this.options)); + // @ts-ignore super.updateHash(hash, context); } @@ -226,6 +237,7 @@ class ConsumeSharedModule extends Module { * @param {CodeGenerationContext} context context for code generation * @returns {CodeGenerationResult} result */ + // @ts-ignore override codeGeneration({ chunkGraph, moduleGraph, @@ -246,6 +258,7 @@ class ConsumeSharedModule extends Module { if (eager) { const dep = this.dependencies[0]; fallbackCode = runtimeTemplate.syncModuleFactory({ + // @ts-ignore dependency: dep, chunkGraph, runtimeRequirements, @@ -254,6 +267,7 @@ class ConsumeSharedModule extends Module { } else { const block = this.blocks[0]; fallbackCode = runtimeTemplate.asyncModuleFactory({ + // @ts-ignore block, chunkGraph, runtimeRequirements, @@ -283,7 +297,7 @@ class ConsumeSharedModule extends Module { } const code = runtimeTemplate.returningFunction(`${fn}(${args.join(', ')})`); const sources = new Map(); - sources.set('consume-shared', new RawSource(code)); + sources.set('consume-shared', new webpackSources.RawSource(code)); return { runtimeRequirements, sources, diff --git a/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts b/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts index b4244c73603..cf2a5489c3a 100644 --- a/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts +++ b/packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts @@ -4,11 +4,14 @@ */ 'use strict'; - +import { + getWebpackPath, + normalizeWebpackPath, +} from '@module-federation/sdk/normalize-webpack-path'; +import type { Compiler } from 'webpack'; import { parseOptions } from '../container/options'; import { ConsumeOptions } from './ConsumeSharedModule'; import { ConsumeSharedPluginOptions } from '../../declarations/plugins/sharing/ConsumeSharedPlugin'; -import { parseRange } from 'webpack/lib/util/semver'; import { resolveMatchedConfigs } from './resolveMatchedConfigs'; import { isRequiredVersion, @@ -20,18 +23,26 @@ import ConsumeSharedFallbackDependency from './ConsumeSharedFallbackDependency'; import ConsumeSharedModule from './ConsumeSharedModule'; import ConsumeSharedRuntimeModule from './ConsumeSharedRuntimeModule'; import ProvideForSharedDependency from './ProvideForSharedDependency'; -//@ts-ignore -import ModuleNotFoundError from 'webpack/lib/ModuleNotFoundError'; -import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -//@ts-ignore -import WebpackError from 'webpack/lib/WebpackError'; -//@ts-ignore -import Compiler from 'webpack/lib/Compiler'; -//@ts-ignore -import LazySet from 'webpack/lib/util/LazySet'; -//@ts-ignore -import createSchemaValidation from 'webpack/lib/util/create-schema-validation'; -import { SemVerRange } from 'webpack/lib/util/semver'; +import type { SemVerRange } from 'webpack/lib/util/semver'; + +const { parseRange } = require( + normalizeWebpackPath('webpack/lib/util/semver'), +) as typeof import('webpack/lib/util/semver'); +const ModuleNotFoundError = require( + normalizeWebpackPath('webpack/lib/ModuleNotFoundError'), +) as typeof import('webpack/lib/ModuleNotFoundError'); +const { RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const LazySet = require( + normalizeWebpackPath('webpack/lib/util/LazySet'), +) as typeof import('webpack/lib/util/LazySet'); +const WebpackError = require( + normalizeWebpackPath('webpack/lib/WebpackError'), +) as typeof import('webpack/lib/WebpackError'); +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 @@ -108,6 +119,9 @@ class ConsumeSharedPlugin { } apply(compiler: Compiler): void { + process.env['FEDERATION_WEBPACK_PATH'] = + process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler); + compiler.hooks.thisCompilation.tap( PLUGIN_NAME, (compilation, { normalModuleFactory }) => { @@ -129,6 +143,7 @@ class ConsumeSharedPlugin { const resolver = compilation.resolverFactory.get( 'normal', + //@ts-ignore RESOLVE_OPTIONS, ); @@ -142,6 +157,7 @@ class ConsumeSharedPlugin { `No required version specified and unable to automatically determine one. ${details}`, ); error.file = `shared module ${request}`; + //@ts-ignore compilation.warnings.push(error); }; const directFallback = @@ -172,6 +188,7 @@ class ConsumeSharedPlugin { ); if (err) { compilation.errors.push( + //@ts-ignore new ModuleNotFoundError(null, err, { name: `resolving fallback for shared module ${request}`, }), @@ -287,6 +304,7 @@ class ConsumeSharedPlugin { ); normalModuleFactory.hooks.createModule.tapPromise( PLUGIN_NAME, + //@ts-ignore ({ resource }, { context, dependencies }) => { if ( dependencies[0] instanceof ConsumeSharedFallbackDependency || @@ -314,6 +332,7 @@ class ConsumeSharedPlugin { set.add(RuntimeGlobals.hasOwnProperty); compilation.addRuntimeModule( chunk, + //@ts-ignore new ConsumeSharedRuntimeModule(set), ); }, diff --git a/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts b/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts index d76004d37d5..212a2f28e70 100644 --- a/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts +++ b/packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts @@ -2,22 +2,23 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; -import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import Template from 'webpack/lib/Template'; -import { +import type { Module, ChunkGraph, Compilation, Chunk } from 'webpack'; +import ConsumeSharedModule from './ConsumeSharedModule'; +import { Source } from 'webpack-sources'; + +const { Template, RuntimeGlobals, RuntimeModule } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const { parseVersionRuntimeCode, versionLtRuntimeCode, rangeToStringRuntimeCode, satisfyRuntimeCode, -} from 'webpack/lib/util/semver'; -import RuntimeModule from 'webpack/lib/RuntimeModule'; -import Module from 'webpack/lib/Module'; -import ConsumeSharedModule from './ConsumeSharedModule'; -import type ChunkGraph from 'webpack/lib/ChunkGraph'; -import type Compilation from 'webpack/lib/Compilation'; -import type Chunk from 'webpack/lib/Chunk'; -import { Source } from 'webpack-sources'; +} = require( + normalizeWebpackPath('webpack/lib/util/semver'), +) as typeof import('webpack/lib/util/semver'); class ConsumeSharedRuntimeModule extends RuntimeModule { private _runtimeRequirements: ReadonlySet; @@ -52,12 +53,15 @@ class ConsumeSharedRuntimeModule extends RuntimeModule { list: (string | number)[], ) => { for (const m of modules) { - const module: ConsumeSharedModule = m as ConsumeSharedModule; + const module: ConsumeSharedModule = m as unknown as ConsumeSharedModule; + // @ts-ignore const id = chunkGraph.getModuleId(module); list.push(id); moduleIdToSourceMapping.set( id, + // @ts-ignore codeGenerationResults.getSource( + // @ts-ignore module, chunk.runtime, 'consume-shared', diff --git a/packages/enhanced/src/lib/sharing/ProvideForSharedDependency.ts b/packages/enhanced/src/lib/sharing/ProvideForSharedDependency.ts index 0e9c8db7c86..1763324ed73 100644 --- a/packages/enhanced/src/lib/sharing/ProvideForSharedDependency.ts +++ b/packages/enhanced/src/lib/sharing/ProvideForSharedDependency.ts @@ -2,11 +2,16 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; -import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); +const { dependencies } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); -class ProvideForSharedDependency extends ModuleDependency { +class ProvideForSharedDependency extends dependencies.ModuleDependency { /** * * @param request request string diff --git a/packages/enhanced/src/lib/sharing/ProvideSharedDependency.ts b/packages/enhanced/src/lib/sharing/ProvideSharedDependency.ts index 4c652ea3e72..482be7cb6b9 100644 --- a/packages/enhanced/src/lib/sharing/ProvideSharedDependency.ts +++ b/packages/enhanced/src/lib/sharing/ProvideSharedDependency.ts @@ -3,8 +3,15 @@ Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ -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'), +) as typeof import('webpack/lib/util/makeSerializable'); +const { Dependency } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); + import { ObjectDeserializerContext, ObjectSerializerContext, diff --git a/packages/enhanced/src/lib/sharing/ProvideSharedModule.ts b/packages/enhanced/src/lib/sharing/ProvideSharedModule.ts index e62fa771534..334520bc2d7 100644 --- a/packages/enhanced/src/lib/sharing/ProvideSharedModule.ts +++ b/packages/enhanced/src/lib/sharing/ProvideSharedModule.ts @@ -2,14 +2,9 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy */ - -import AsyncDependenciesBlock from 'webpack/lib/AsyncDependenciesBlock'; -import Module from 'webpack/lib/Module'; -import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import makeSerializable from 'webpack/lib/util/makeSerializable'; -import type Compilation from 'webpack/lib/Compilation'; -import WebpackError from 'webpack/lib/WebpackError'; -import { WEBPACK_MODULE_TYPE_PROVIDE } from 'webpack/lib/ModuleTypeConstants'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { Compilation } from 'webpack'; +import type WebpackError from 'webpack/lib/WebpackError'; import type { CodeGenerationContext, CodeGenerationResult, @@ -20,9 +15,19 @@ import type { ObjectDeserializerContext, ObjectSerializerContext, } from 'webpack/lib/Module'; -import { InputFileSystem } from 'webpack/lib/util/fs'; +import type { InputFileSystem } from 'webpack/lib/util/fs'; import ProvideForSharedDependency from './ProvideForSharedDependency'; -import { WebpackOptionsNormalized as WebpackOptions } from 'webpack/declarations/WebpackOptions'; +import type { WebpackOptionsNormalized as WebpackOptions } from 'webpack/declarations/WebpackOptions'; + +const { AsyncDependenciesBlock, Module, RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const makeSerializable = require( + normalizeWebpackPath('webpack/lib/util/makeSerializable'), +) as typeof import('webpack/lib/util/makeSerializable'); +const { WEBPACK_MODULE_TYPE_PROVIDE } = require( + normalizeWebpackPath('webpack/lib/ModuleTypeConstants'), +) as typeof import('webpack/lib/ModuleTypeConstants'); const TYPES = new Set(['share-init']); @@ -92,6 +97,7 @@ class ProvideSharedModule extends Module { * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild * @returns {void} */ + // @ts-ignore override needBuild( context: NeedBuildContext, callback: (error?: WebpackError | null, needsRebuild?: boolean) => void, @@ -107,6 +113,7 @@ class ProvideSharedModule extends Module { * @param {function(WebpackError=): void} callback callback function * @returns {void} */ + // @ts-ignore override build( options: WebpackOptions, compilation: Compilation, @@ -151,6 +158,7 @@ class ProvideSharedModule extends Module { * @param {CodeGenerationContext} context context for code generation * @returns {CodeGenerationResult} result */ + // @ts-ignore override codeGeneration({ runtimeTemplate, moduleGraph, @@ -162,12 +170,14 @@ class ProvideSharedModule extends Module { )}, ${ this._eager ? runtimeTemplate.syncModuleFactory({ + // @ts-ignore dependency: this.dependencies[0], chunkGraph, request: this._request, runtimeRequirements, }) : runtimeTemplate.asyncModuleFactory({ + // @ts-ignore block: this.blocks[0], chunkGraph, request: this._request, diff --git a/packages/enhanced/src/lib/sharing/ProvideSharedModuleFactory.ts b/packages/enhanced/src/lib/sharing/ProvideSharedModuleFactory.ts index 595e0e7cafd..82d19c663ca 100644 --- a/packages/enhanced/src/lib/sharing/ProvideSharedModuleFactory.ts +++ b/packages/enhanced/src/lib/sharing/ProvideSharedModuleFactory.ts @@ -2,8 +2,8 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy */ -//@ts-ignore -import ModuleFactory from 'webpack/lib/ModuleFactory'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; + import ProvideSharedModule from './ProvideSharedModule'; import type { ModuleFactoryCreateData, @@ -11,6 +11,10 @@ import type { } from 'webpack/lib/ModuleFactory'; import ProvideSharedDependency from './ProvideSharedDependency'; +const ModuleFactory = require( + normalizeWebpackPath('webpack/lib/ModuleFactory'), +) as typeof import('webpack/lib/ModuleFactory'); + class ProvideSharedModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object @@ -21,9 +25,11 @@ class ProvideSharedModuleFactory extends ModuleFactory { data: ModuleFactoryCreateData, callback: (error: Error | null, result?: ModuleFactoryResult) => void, ): void { + // @ts-ignore const dep: ProvideSharedDependency = data .dependencies[0] as ProvideSharedDependency; callback(null, { + // @ts-ignore module: new ProvideSharedModule( dep.shareScope, dep.name, diff --git a/packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts b/packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts index 835ac9e1db8..66b09aae6ea 100644 --- a/packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts +++ b/packages/enhanced/src/lib/sharing/ProvideSharedPlugin.ts @@ -4,17 +4,24 @@ */ 'use strict'; - +import { + getWebpackPath, + normalizeWebpackPath, +} from '@module-federation/sdk/normalize-webpack-path'; +import type { Compiler, Compilation } from 'webpack'; import { parseOptions } from '../container/options'; -import createSchemaValidation from 'webpack/lib/util/create-schema-validation'; -import WebpackError from 'webpack/lib/WebpackError'; import ProvideForSharedDependency from './ProvideForSharedDependency'; import ProvideSharedDependency from './ProvideSharedDependency'; import ProvideSharedModuleFactory from './ProvideSharedModuleFactory'; -import type Compiler from 'webpack/lib/Compiler'; -import type Compilation from 'webpack/lib/Compilation'; import type { ProvideSharedPluginOptions } from '../../declarations/plugins/sharing/ProvideSharedPlugin'; +const createSchemaValidation = require( + normalizeWebpackPath('webpack/lib/util/create-schema-validation'), +) as typeof import('webpack/lib/util/create-schema-validation'); +const WebpackError = require( + normalizeWebpackPath('webpack/lib/WebpackError'), +) as typeof import('webpack/lib/WebpackError'); + export type ProvideOptions = { shareKey: string; shareScope: string; @@ -92,6 +99,9 @@ class ProvideSharedPlugin { * @returns {void} */ apply(compiler: Compiler): void { + process.env['FEDERATION_WEBPACK_PATH'] = + process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler); + const compilationData: WeakMap = new WeakMap(); @@ -151,6 +161,7 @@ class ProvideSharedPlugin { `No version specified and unable to automatically determine one. ${details}`, ); error.file = `shared module ${key} -> ${resource}`; + // @ts-ignore compilation.warnings.push(error); } } @@ -243,6 +254,7 @@ class ProvideSharedPlugin { compilation.dependencyFactories.set( //@ts-ignore ProvideSharedDependency, + // @ts-ignore new ProvideSharedModuleFactory(), ); }, diff --git a/packages/enhanced/src/lib/sharing/SharePlugin.ts b/packages/enhanced/src/lib/sharing/SharePlugin.ts index 43ba63a73c7..dec19036a45 100644 --- a/packages/enhanced/src/lib/sharing/SharePlugin.ts +++ b/packages/enhanced/src/lib/sharing/SharePlugin.ts @@ -4,7 +4,7 @@ */ 'use strict'; - +import type { Compiler } from 'webpack'; import { parseOptions } from '../container/options'; import ConsumeSharedPlugin from './ConsumeSharedPlugin'; import ProvideSharedPlugin from './ProvideSharedPlugin'; @@ -14,8 +14,8 @@ import type { SharedConfig, } from '../../declarations/plugins/sharing/SharePlugin'; import type { ConsumesConfig } from '../../declarations/plugins/sharing/ConsumeSharedPlugin'; -import type Compiler from 'webpack/lib/Compiler'; import type { ProvidesConfig } from '../../declarations/plugins/sharing/ProvideSharedPlugin'; +import { getWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; class SharePlugin { private _shareScope: string; @@ -77,6 +77,9 @@ class SharePlugin { * @returns {void} */ apply(compiler: Compiler) { + process.env['FEDERATION_WEBPACK_PATH'] = + process.env['FEDERATION_WEBPACK_PATH'] || getWebpackPath(compiler); + new ConsumeSharedPlugin({ shareScope: this._shareScope, consumes: this._consumes, diff --git a/packages/enhanced/src/lib/sharing/ShareRuntimeModule.ts b/packages/enhanced/src/lib/sharing/ShareRuntimeModule.ts index 98aec40c8d2..0ee1f6d2ad9 100644 --- a/packages/enhanced/src/lib/sharing/ShareRuntimeModule.ts +++ b/packages/enhanced/src/lib/sharing/ShareRuntimeModule.ts @@ -4,16 +4,15 @@ */ 'use strict'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { Compilation, ChunkGraph } from 'webpack'; -import * as RuntimeGlobals from 'webpack/lib/RuntimeGlobals'; -import RuntimeModule from 'webpack/lib/RuntimeModule'; -import Template from 'webpack/lib/Template'; -import Compilation from 'webpack/lib/Compilation'; -import ChunkGraph from 'webpack/lib/ChunkGraph'; -import { - compareModulesByIdentifier, - compareStrings, -} from 'webpack/lib/util/comparators'; +const { Template, RuntimeGlobals, RuntimeModule } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); +const { compareModulesByIdentifier, compareStrings } = require( + normalizeWebpackPath('webpack/lib/util/comparators'), +) as typeof import('webpack/lib/util/comparators'); class ShareRuntimeModule extends RuntimeModule { constructor() { @@ -45,6 +44,7 @@ class ShareRuntimeModule extends RuntimeModule { const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType( chunk, 'share-init', + // @ts-ignore compareModulesByIdentifier, ); if (!modules) continue; diff --git a/packages/enhanced/src/lib/sharing/resolveMatchedConfigs.ts b/packages/enhanced/src/lib/sharing/resolveMatchedConfigs.ts index df2315c187e..228bb95b676 100644 --- a/packages/enhanced/src/lib/sharing/resolveMatchedConfigs.ts +++ b/packages/enhanced/src/lib/sharing/resolveMatchedConfigs.ts @@ -2,10 +2,16 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy */ -import type Compilation from 'webpack/lib/Compilation'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { Compilation } from 'webpack'; import type { ResolveOptionsWithDependencyType } from 'webpack/lib/ResolverFactory'; -import ModuleNotFoundError from 'webpack/lib/ModuleNotFoundError'; -import LazySet from 'webpack/lib/util/LazySet'; + +const ModuleNotFoundError = require( + normalizeWebpackPath('webpack/lib/ModuleNotFoundError'), +) as typeof import('webpack/lib/ModuleNotFoundError'); +const LazySet = require( + normalizeWebpackPath('webpack/lib/util/LazySet'), +) as typeof import('webpack/lib/util/LazySet'); interface MatchedConfigs { resolved: Map; @@ -29,6 +35,7 @@ export async function resolveMatchedConfigs( contextDependencies: new LazySet(), missingDependencies: new LazySet(), }; + // @ts-ignore const resolver = compilation.resolverFactory.get('normal', RESOLVE_OPTIONS); const context = compilation.compiler.context; @@ -47,6 +54,7 @@ export async function resolveMatchedConfigs( if (err || result === false) { err = err || new Error(`Can't resolve ${request}`); compilation.errors.push( + // @ts-ignore new ModuleNotFoundError(null, err, { name: `shared module ${request}`, }), diff --git a/packages/enhanced/src/lib/sharing/utils.ts b/packages/enhanced/src/lib/sharing/utils.ts index eeaf46e42b5..3c049fd3747 100644 --- a/packages/enhanced/src/lib/sharing/utils.ts +++ b/packages/enhanced/src/lib/sharing/utils.ts @@ -2,8 +2,11 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ - -import { join, dirname, readJson, InputFileSystem } from 'webpack/lib/util/fs'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { InputFileSystem } from 'webpack/lib/util/fs'; +const { join, dirname, readJson } = require( + normalizeWebpackPath('webpack/lib/util/fs'), +) as typeof import('webpack/lib/util/fs'); // Extreme shorthand only for github. eg: foo/bar const RE_URL_GITHUB_EXTREME_SHORT: RegExp = diff --git a/packages/enhanced/src/runtime/AddRuntimeRequirementToPromiseExternalPlugin.ts b/packages/enhanced/src/runtime/AddRuntimeRequirementToPromiseExternalPlugin.ts index 2bcd01a8fed..25adad06143 100644 --- a/packages/enhanced/src/runtime/AddRuntimeRequirementToPromiseExternalPlugin.ts +++ b/packages/enhanced/src/runtime/AddRuntimeRequirementToPromiseExternalPlugin.ts @@ -1,4 +1,4 @@ -import { Compiler } from 'webpack'; +import type { Compiler } from 'webpack'; export class AddRuntimeRequirementToPromiseExternal { apply(compiler: Compiler) { diff --git a/packages/enhanced/src/runtime/ModuleInfoRuntimeModule.ts b/packages/enhanced/src/runtime/ModuleInfoRuntimeModule.ts index 48faec289ed..9797baeabaf 100644 --- a/packages/enhanced/src/runtime/ModuleInfoRuntimeModule.ts +++ b/packages/enhanced/src/runtime/ModuleInfoRuntimeModule.ts @@ -1,7 +1,9 @@ 'use strict'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; -import { RuntimeGlobals, RuntimeModule, Template } from 'webpack'; - +const { Template, RuntimeGlobals, RuntimeModule } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); /** * Class representing a runtime module for federation module info. * @extends RuntimeModule diff --git a/packages/enhanced/src/runtime/ModuleInfoRuntimePlugin.ts b/packages/enhanced/src/runtime/ModuleInfoRuntimePlugin.ts index 229177c58af..e48fe7e6893 100644 --- a/packages/enhanced/src/runtime/ModuleInfoRuntimePlugin.ts +++ b/packages/enhanced/src/runtime/ModuleInfoRuntimePlugin.ts @@ -1,5 +1,9 @@ -import { Compilation, RuntimeGlobals, Compiler } from 'webpack'; +import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path'; +import type { Compiler, Compilation } from 'webpack'; import { ModuleInfoRuntimeModule } from './ModuleInfoRuntimeModule'; +const { RuntimeGlobals } = require( + normalizeWebpackPath('webpack'), +) as typeof import('webpack'); class ModuleInfoRuntimePlugin { apply(compiler: Compiler) { diff --git a/packages/nextjs-mf/.eslintrc.json b/packages/nextjs-mf/.eslintrc.json index 192c5f356b6..d234ae24295 100644 --- a/packages/nextjs-mf/.eslintrc.json +++ b/packages/nextjs-mf/.eslintrc.json @@ -14,13 +14,19 @@ "@typescript-eslint/no-restricted-imports": [ "error", { - "patterns": ["webpack/lib/*"], "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 + } ] } ] diff --git a/packages/node/.eslintrc.json b/packages/node/.eslintrc.json index c924aa24d9e..07fe19291a2 100644 --- a/packages/node/.eslintrc.json +++ b/packages/node/.eslintrc.json @@ -10,28 +10,35 @@ "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} - }, - { - "files": ["*.ts", "*.tsx"], "rules": { - "no-var": "off", - "@typescript-eslint/no-var-requires": "off", "@typescript-eslint/no-restricted-imports": [ "error", { - "patterns": ["webpack/lib/*"], "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": ["*.ts", "*.tsx"], + "rules": { + "no-var": "off", + "@typescript-eslint/no-var-requires": "off" + } + }, { "files": ["*.js", "*.jsx"], "rules": {}