Skip to content

Commit

Permalink
feat: js side export plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 10, 2024
1 parent 558cf00 commit 29c9ebe
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
11 changes: 11 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export enum BuiltinPluginName {
RuntimeChunkPlugin = 'RuntimeChunkPlugin',
SizeLimitsPlugin = 'SizeLimitsPlugin',
NoEmitOnErrorsPlugin = 'NoEmitOnErrorsPlugin',
ContextReplacementPlugin = 'ContextReplacementPlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down Expand Up @@ -470,11 +471,14 @@ export interface JsContextModuleFactoryAfterResolveData {
context: string
request: string
regExp?: RawRegex
recursive: boolean
}

export interface JsContextModuleFactoryBeforeResolveData {
context: string
request?: string
regExp?: RawRegex
recursive: boolean
}

export interface JsCreateData {
Expand Down Expand Up @@ -1125,6 +1129,13 @@ export interface RawContainerReferencePluginOptions {
enhanced: boolean
}

export interface RawContextReplacementPluginOptions {
resourceRegExp: RawRegex
newContentResource?: string
newContentRecursive?: boolean
newContentRegExp?: RawRegex
}

export interface RawCopyGlobOptions {
caseSensitiveMatch?: boolean
dot?: boolean
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ class Compiler {
: undefined,
request: bindingData.request,
context: bindingData.context,
recursive: bindingData.recursive,
// TODO: Dependencies are not fully supported yet; this is a placeholder to prevent errors in moment-locales-webpack-plugin.
dependencies: []
} satisfies ContextModuleFactoryAfterResolveResult)
Expand All @@ -1181,7 +1182,8 @@ class Compiler {
source: ret.regExp.source,
flags: ret.regExp.flags
}
: undefined
: undefined,
recursive: ret.recursive
} satisfies binding.JsContextModuleFactoryAfterResolveData)
: false;
return result;
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type ContextModuleFactoryAfterResolveResult =
context: string;
request: string;
regExp?: RegExp;
recursive: boolean;
dependencies: Array<any>;
};

Expand Down
56 changes: 56 additions & 0 deletions packages/rspack/src/builtin-plugin/ContextReplacementPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
BuiltinPluginName,
type RawContextReplacementPluginOptions
} from "@rspack/binding";

import { create } from "./base";

export const ContextReplacementPlugin = create(
BuiltinPluginName.ContextReplacementPlugin,
(
resourceRegExp: RegExp,
newContentResource?: any,
newContentRecursive?: any,
newContentRegExp?: any
) => {
const rawOptions: RawContextReplacementPluginOptions = {
resourceRegExp
};
if (typeof newContentResource === "function") {
// rawOptions.newContentCallback = newContentResource;
} else if (
typeof newContentResource === "string" &&
typeof newContentRecursive === "object"
) {
rawOptions.newContentResource = newContentResource;
// rawOptions.newContentCreateContextMap = (fs, callback) => {
// callback(null, newContentRecursive);
// };
} else if (
typeof newContentResource === "string" &&
typeof newContentRecursive === "function"
) {
rawOptions.newContentResource = newContentResource;
// rawOptions.newContentCreateContextMap = newContentRecursive;
} else {
if (typeof newContentResource !== "string") {
// biome-ignore lint/style/noParameterAssign: based on webpack's logic
newContentRegExp = newContentRecursive;
// biome-ignore lint/style/noParameterAssign: based on webpack's logic
newContentRecursive = newContentResource;
// biome-ignore lint/style/noParameterAssign: based on webpack's logic
newContentResource = undefined;
}
if (typeof newContentRecursive !== "boolean") {
// biome-ignore lint/style/noParameterAssign: based on webpack's logic
newContentRegExp = newContentRecursive;
// biome-ignore lint/style/noParameterAssign: based on webpack's logic
newContentRecursive = undefined;
}
rawOptions.newContentResource = newContentResource;
rawOptions.newContentRecursive = newContentRecursive;
rawOptions.newContentRegExp = newContentRegExp;
}
return rawOptions;
}
);
1 change: 1 addition & 0 deletions packages/rspack/src/builtin-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ export * from "./WebWorkerTemplatePlugin";
export * from "./WorkerPlugin";
export * from "./FetchCompileAsyncWasmPlugin";
export * from "./NoEmitOnErrorsPlugin";
export * from "./ContextReplacementPlugin";
1 change: 1 addition & 0 deletions packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export { SourceMapDevToolPlugin } from "./builtin-plugin";
export { EvalSourceMapDevToolPlugin } from "./builtin-plugin";
export { EvalDevToolModulePlugin } from "./builtin-plugin";
export { CssExtractRspackPlugin } from "./builtin-plugin";
export { ContextReplacementPlugin } from "./builtin-plugin";

///// Rspack Postfixed Internal Loaders /////
export type {
Expand Down

0 comments on commit 29c9ebe

Please sign in to comment.