Skip to content

Commit

Permalink
fix library
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Nov 22, 2023
1 parent a0aba2a commit 80552c9
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 67 deletions.
6 changes: 3 additions & 3 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Compiler {
webpack = rspack;
// @ts-expect-error
compilation: Compilation;
builtinPlugins: RspackBuiltinPlugin[];
builtinPlugins: binding.BuiltinPlugin[];
root: Compiler;
running: boolean;
idle: boolean;
Expand Down Expand Up @@ -241,7 +241,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),
Expand Down Expand Up @@ -1101,7 +1101,7 @@ class Compiler {
return source.buffer();
}

__internal__registerBuiltinPlugin(plugin: RspackBuiltinPlugin) {
__internal__registerBuiltinPlugin(plugin: binding.BuiltinPlugin) {
this.builtinPlugins.push(plugin);
}
}
Expand Down
35 changes: 0 additions & 35 deletions packages/rspack/src/builtin-plugin/ContainerPlugin.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/rspack/src/builtin-plugin/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/rspack/src/builtin-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
75 changes: 75 additions & 0 deletions packages/rspack/src/container/ContainerPlugin.ts
Original file line number Diff line number Diff line change
@@ -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
};
}
}
3 changes: 1 addition & 2 deletions packages/rspack/src/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
21 changes: 0 additions & 21 deletions packages/rspack/src/container/options.ts
Original file line number Diff line number Diff line change
@@ -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<T> =
| (string | Record<string, string | string[] | T>)[]
| Record<string, string | string[] | T>;
Expand Down
10 changes: 9 additions & 1 deletion packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,17 @@ export const webworker = { WebWorkerTemplatePlugin };
import { LimitChunkCountPlugin } from "./builtin-plugin";
export const optimize = { LimitChunkCountPlugin };

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 };

///// Rspack Postfixed Internal Plugins /////
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ContainerPlugin } = require("../../../../").container;
const { ContainerPlugin } = require("../../../../packages/rspack").container;

/** @type {import("@rspack/core").Configuration} */
module.exports = {
Expand Down

0 comments on commit 80552c9

Please sign in to comment.