From 777d18dcd3846bc7ab32679a59f550eeee72a7f4 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Mon, 20 Nov 2023 20:25:20 +0800 Subject: [PATCH] fix library --- packages/rspack/src/Compiler.ts | 6 +- .../src/builtin-plugin/ContainerPlugin.ts | 35 --------- packages/rspack/src/builtin-plugin/base.ts | 4 +- packages/rspack/src/builtin-plugin/index.ts | 1 - .../rspack/src/container/ContainerPlugin.ts | 75 +++++++++++++++++++ .../src/container/ModuleFederationPlugin.ts | 3 +- packages/rspack/src/container/options.ts | 21 ------ packages/rspack/src/index.ts | 10 ++- .../container/container-entry/test.filter.js | 1 - .../container-entry/webpack.config.js | 2 +- 10 files changed, 91 insertions(+), 67 deletions(-) delete mode 100644 packages/rspack/src/builtin-plugin/ContainerPlugin.ts create mode 100644 packages/rspack/src/container/ContainerPlugin.ts delete mode 100644 webpack-test/configCases/container/container-entry/test.filter.js diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index f30b5b4d5323..f800a3b09822 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -55,7 +55,7 @@ class Compiler { webpack: any; // @ts-expect-error compilation: Compilation; - builtinPlugins: RspackBuiltinPlugin[]; + builtinPlugins: binding.BuiltinPlugin[]; root: Compiler; running: boolean; idle: boolean; @@ -360,7 +360,7 @@ class Compiler { this.#_instance = new instanceBinding.Rspack( rawOptions, - this.builtinPlugins.map(bp => bp.raw()), + this.builtinPlugins, { beforeCompile: this.#beforeCompile.bind(this), afterCompile: this.#afterCompile.bind(this), @@ -1152,7 +1152,7 @@ class Compiler { return source.buffer(); } - __internal__registerBuiltinPlugin(plugin: RspackBuiltinPlugin) { + __internal__registerBuiltinPlugin(plugin: binding.BuiltinPlugin) { this.builtinPlugins.push(plugin); } } diff --git a/packages/rspack/src/builtin-plugin/ContainerPlugin.ts b/packages/rspack/src/builtin-plugin/ContainerPlugin.ts deleted file mode 100644 index 5dd99fd655b3..000000000000 --- a/packages/rspack/src/builtin-plugin/ContainerPlugin.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { RawContainerPluginOptions } from "@rspack/binding"; -import { BuiltinPluginName, create } from "./base"; -import { getRawEntryRuntime, getRawLibrary } from "../config"; -import { isNil } from "../util"; -import { ContainerPluginOptions, parseOptions } from "../container/options"; - -export const ContainerPlugin = create( - BuiltinPluginName.ContainerPlugin, - (options: ContainerPluginOptions): RawContainerPluginOptions => { - const library = options.library || { - type: "var", - name: options.name - }; - const runtime = options.runtime; - - return { - name: options.name, - shareScope: options.shareScope || "default", - library: getRawLibrary(library), - runtime: !isNil(runtime) ? getRawEntryRuntime(runtime) : undefined, - filename: options.filename, - exposes: parseOptions( - options.exposes, - item => ({ - import: Array.isArray(item) ? item : [item], - name: undefined - }), - item => ({ - import: Array.isArray(item.import) ? item.import : [item.import], - name: item.name || undefined - }) - ).map(([key, r]) => ({ key, ...r })) - }; - } -); diff --git a/packages/rspack/src/builtin-plugin/base.ts b/packages/rspack/src/builtin-plugin/base.ts index 1a2c47d538ff..3b748585e87c 100644 --- a/packages/rspack/src/builtin-plugin/base.ts +++ b/packages/rspack/src/builtin-plugin/base.ts @@ -30,10 +30,10 @@ export enum BuiltinPluginName { } export abstract class RspackBuiltinPlugin implements RspackPluginInstance { - abstract raw(): binding.BuiltinPlugin; + abstract raw(compiler: Compiler): binding.BuiltinPlugin; abstract name: BuiltinPluginName; apply(compiler: Compiler) { - compiler.__internal__registerBuiltinPlugin(this); + compiler.__internal__registerBuiltinPlugin(this.raw(compiler)); } } diff --git a/packages/rspack/src/builtin-plugin/index.ts b/packages/rspack/src/builtin-plugin/index.ts index db398931199e..01e5a9811ab6 100644 --- a/packages/rspack/src/builtin-plugin/index.ts +++ b/packages/rspack/src/builtin-plugin/index.ts @@ -19,7 +19,6 @@ export * from "./HotModuleReplacementPlugin"; export * from "./WebWorkerTemplatePlugin"; export * from "./LimitChunkCountPlugin"; export * from "./MergeDuplicateChunksPlugin"; -export * from "./ContainerPlugin"; export * from "./HtmlRspackPlugin"; export * from "./CopyRspackPlugin"; diff --git a/packages/rspack/src/container/ContainerPlugin.ts b/packages/rspack/src/container/ContainerPlugin.ts new file mode 100644 index 000000000000..1a837d7303ef --- /dev/null +++ b/packages/rspack/src/container/ContainerPlugin.ts @@ -0,0 +1,75 @@ +import { RawContainerPluginOptions, BuiltinPlugin } from "@rspack/binding"; +import { BuiltinPluginName, RspackBuiltinPlugin } from "../builtin-plugin/base"; +import { + EntryRuntime, + Filename, + LibraryOptions, + getRawEntryRuntime, + getRawLibrary +} from "../config"; +import { isNil } from "../util"; +import { parseOptions } from "../container/options"; +import { Compiler } from "../Compiler"; + +export type ContainerPluginOptions = { + exposes: Exposes; + filename?: Filename; + library?: LibraryOptions; + name: string; + runtime?: EntryRuntime; + shareScope?: string; +}; +export type Exposes = (ExposesItem | ExposesObject)[] | ExposesObject; +export type ExposesItem = string; +export type ExposesItems = ExposesItem[]; +export type ExposesObject = { + [k: string]: ExposesConfig | ExposesItem | ExposesItems; +}; +export type ExposesConfig = { + import: ExposesItem | ExposesItems; + name?: string; +}; + +export class ContainerPlugin extends RspackBuiltinPlugin { + name = BuiltinPluginName.ContainerPlugin; + _options: RawContainerPluginOptions; + _library; + + constructor(options: ContainerPluginOptions) { + super(); + const library = (this._library = options.library || { + type: "var", + name: options.name + }); + const runtime = options.runtime; + this._options = { + name: options.name, + shareScope: options.shareScope || "default", + library: getRawLibrary(library), + runtime: !isNil(runtime) ? getRawEntryRuntime(runtime) : undefined, + filename: options.filename, + exposes: parseOptions( + options.exposes, + item => ({ + import: Array.isArray(item) ? item : [item], + name: undefined + }), + item => ({ + import: Array.isArray(item.import) ? item.import : [item.import], + name: item.name || undefined + }) + ).map(([key, r]) => ({ key, ...r })) + }; + } + + raw(compiler: Compiler): BuiltinPlugin { + const library = this._library; + if (!compiler.options.output.enabledLibraryTypes!.includes(library.type)) { + compiler.options.output.enabledLibraryTypes!.push(library.type); + } + return { + name: this.name as any, + options: this._options + }; + } +} diff --git a/packages/rspack/src/container/ModuleFederationPlugin.ts b/packages/rspack/src/container/ModuleFederationPlugin.ts index 77877f7c8604..a44ae1eeef6e 100644 --- a/packages/rspack/src/container/ModuleFederationPlugin.ts +++ b/packages/rspack/src/container/ModuleFederationPlugin.ts @@ -1,7 +1,6 @@ import { Compiler } from "../Compiler"; -import { ContainerPlugin } from "../builtin-plugin"; +import { ContainerPlugin, Exposes } from "./ContainerPlugin"; import { LibraryOptions, EntryRuntime } from "../config"; -import { Exposes } from "./options"; export interface ModuleFederationPluginOptions { exposes?: Exposes; diff --git a/packages/rspack/src/container/options.ts b/packages/rspack/src/container/options.ts index 55ec93c1dd5d..8c1557b0ff20 100644 --- a/packages/rspack/src/container/options.ts +++ b/packages/rspack/src/container/options.ts @@ -1,24 +1,3 @@ -import { EntryRuntime, Filename, LibraryOptions } from "../config"; - -export type ContainerPluginOptions = { - exposes: Exposes; - filename?: Filename; - library?: LibraryOptions; - name: string; - runtime?: EntryRuntime; - shareScope?: string; -}; -export type Exposes = (ExposesItem | ExposesObject)[] | ExposesObject; -export type ExposesItem = string; -export type ExposesItems = ExposesItem[]; -export type ExposesObject = { - [k: string]: ExposesConfig | ExposesItem | ExposesItems; -}; -export type ExposesConfig = { - import: ExposesItem | ExposesItems; - name?: string; -}; - type ContainerOptionsFormat = | (string | Record)[] | Record; diff --git a/packages/rspack/src/index.ts b/packages/rspack/src/index.ts index dbefcf2561bd..5f042cc3eb0c 100644 --- a/packages/rspack/src/index.ts +++ b/packages/rspack/src/index.ts @@ -65,9 +65,17 @@ export const optimize = { LimitChunkCountPlugin }; export const sources = require("webpack-sources"); -import { ContainerPlugin } from "./builtin-plugin"; +import { ContainerPlugin } from "./container/ContainerPlugin"; import { ModuleFederationPlugin } from "./container/ModuleFederationPlugin"; export type { ModuleFederationPluginOptions } from "./container/ModuleFederationPlugin"; +export type { + ContainerPluginOptions, + Exposes, + ExposesItem, + ExposesItems, + ExposesObject, + ExposesConfig +} from "./container/ContainerPlugin"; export const container = { ContainerPlugin, ModuleFederationPlugin }; export { Watching }; diff --git a/webpack-test/configCases/container/container-entry/test.filter.js b/webpack-test/configCases/container/container-entry/test.filter.js deleted file mode 100644 index 3be456dcd23c..000000000000 --- a/webpack-test/configCases/container/container-entry/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/webpack-test/configCases/container/container-entry/webpack.config.js b/webpack-test/configCases/container/container-entry/webpack.config.js index e9bfb1ce811b..bcc032caf689 100644 --- a/webpack-test/configCases/container/container-entry/webpack.config.js +++ b/webpack-test/configCases/container/container-entry/webpack.config.js @@ -1,4 +1,4 @@ -const { ContainerPlugin } = require("../../../../").container; +const { ContainerPlugin } = require("../../../../packages/rspack").container; /** @type {import("@rspack/core").Configuration} */ module.exports = {