Skip to content

Commit

Permalink
refactor index
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Sep 25, 2023
1 parent a757197 commit 7f206f5
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 163 deletions.
2 changes: 0 additions & 2 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,5 +954,3 @@ export class Compilation {
}
}
}

export type { JsAssetInfo };
110 changes: 3 additions & 107 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* https://github.com/webpack/webpack/blob/main/LICENSE
*/
import type * as binding from "@rspack/binding";
import * as rspackIndex from "./index";
import fs from "fs";
import path from "path";
import * as tapable from "tapable";
Expand All @@ -31,14 +32,13 @@ import { createThreadsafeNodeFSFromRaw } from "./fileSystem";
import Cache from "./lib/Cache";
import { makePathsRelative } from "./util/identifier";
import CacheFacade from "./lib/CacheFacade";
import ModuleFilenameHelpers from "./lib/ModuleFilenameHelpers";
import { runLoader } from "./loader-runner";
import { Logger } from "./logging/Logger";
import { NormalModuleFactory } from "./NormalModuleFactory";
import { WatchFileSystem } from "./util/fs";
import { getScheme } from "./util/scheme";
import { checkVersion } from "./util/bindingVersionCheck";
import Watching from "./Watching";
import { Watching } from "./Watching";
import { NormalModule } from "./NormalModule";
import { normalizeJsModule } from "./util/normalization";
import {
Expand All @@ -50,7 +50,7 @@ import { optionsApply_compat } from "./rspackOptionsApply";
class Compiler {
#_instance?: binding.Rspack;

webpack: any;
webpack = rspackIndex;
// @ts-expect-error
compilation: Compilation;
builtinPlugins: RspackBuiltinPlugin[];
Expand Down Expand Up @@ -117,110 +117,6 @@ class Compiler {
this.cache = new Cache();
this.compilerPath = "";
this.builtinPlugins = [];
// to workaround some plugin access webpack, we may change dev-server to avoid this hack in the future
this.webpack = {
NormalModule,
get sources(): typeof import("webpack-sources") {
return require("webpack-sources");
},
Compilation,
get version() {
return require("../package.json").webpackVersion; // this is a hack to be compatible with plugin which detect webpack's version
},
get rspackVersion() {
return require("../package.json").version;
},
get BannerPlugin() {
return require("./builtin-plugin").BannerPlugin;
},
get DefinePlugin() {
return require("./builtin-plugin").DefinePlugin;
},
get ProgressPlugin() {
return require("./builtin-plugin").ProgressPlugin;
},
get ProvidePlugin() {
return require("./builtin-plugin").ProvidePlugin;
},
get EntryPlugin() {
return require("./builtin-plugin").EntryPlugin;
},
get ExternalsPlugin() {
return require("./builtin-plugin").ExternalsPlugin;
},
get HotModuleReplacementPlugin() {
return require("./builtin-plugin").HotModuleReplacementPlugin;
},
get LoaderOptionsPlugin() {
return require("./lib/LoaderOptionsPlugin").LoaderOptionsPlugin;
},
get LoaderTargetPlugin() {
return require("./lib/LoaderTargetPlugin").LoaderTargetPlugin;
},
WebpackError: Error,
ModuleFilenameHelpers,
javascript: {
get EnableChunkLoadingPlugin() {
return require("./builtin-plugin").EnableChunkLoadingPlugin;
}
},
node: {
get NodeTargetPlugin() {
return require("./builtin-plugin").NodeTargetPlugin;
},
get NodeTemplatePlugin() {
return require("./node/NodeTemplatePlugin").default;
}
},
electron: {
get ElectronTargetPlugin() {
return require("./builtin-plugin").ElectronTargetPlugin;
}
},
wasm: {
get EnableWasmLoadingPlugin() {
return require("./builtin-plugin").EnableWasmLoadingPlugin;
}
},
library: {
get EnableLibraryPlugin() {
return require("./builtin-plugin").EnableLibraryPlugin;
}
},
util: {
get createHash() {
return require("./util/createHash").createHash;
},
get cleverMerge() {
return require("./util/cleverMerge").cachedCleverMerge;
}
// get comparators() {
// return require("./util/comparators");
// },
// get runtime() {
// return require("./util/runtime");
// },
// get serialization() {
// return require("./util/serialization");
// },
// get LazySet() {
// return require("./util/LazySet");
// }
},
// XxxRspackPlugin, Rspack-only plugins
get HtmlRspackPlugin() {
return require("./builtin-plugin").HtmlRspackPlugin;
},
get SwcJsMinimizerRspackPlugin() {
return require("./builtin-plugin").SwcJsMinimizerRspackPlugin;
},
get SwcCssMinimizerRspackPlugin() {
return require("./builtin-plugin").SwcCssMinimizerRspackPlugin;
},
get CopyRspackPlugin() {
return require("./builtin-plugin").CopyRspackPlugin;
}
};
this.root = this;
this.ruleSet = new RuleSetCompiler();
this.running = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/MultiCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from ".";
import ResolverFactory from "./ResolverFactory";
import { WatchFileSystem } from "./util/fs";
import Watching from "./Watching";
import { Watching } from "./Watching";
import tapable, {
AsyncSeriesHook,
Callback,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/MultiStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* https://github.com/webpack/webpack/blob/main/LICENSE
*/

import { KnownCreateStatsOptionsContext } from ".";
import { KnownCreateStatsOptionsContext } from "./Compilation";
import { SnapshotOptions } from "./config";
import { StatsCompilation } from "./stats/statsFactoryUtils";
import { Stats } from "./Stats";
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/MultiWatching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { MultiCompiler } from "./MultiCompiler";
import Watching from "./Watching";
import { Watching } from "./Watching";
import asyncLib from "neo-async";
import { Callback } from "tapable";

Expand Down
4 changes: 1 addition & 3 deletions packages/rspack/src/Watching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { WatchOptions } from "./config";
import { FileSystemInfoEntry, Watcher } from "./util/fs";
import assert from "assert";

class Watching {
export class Watching {
watcher?: Watcher;
pausedWatcher?: Watcher;
compiler: Compiler;
Expand Down Expand Up @@ -346,5 +346,3 @@ class Watching {
}
}
}

export default Watching;
130 changes: 85 additions & 45 deletions packages/rspack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
import type Watching from "./Watching";
export * from "./Compiler";
export * from "./MultiCompiler";
export * from "./Compilation";
import { rspack } from "./rspack";
export default rspack;
export { rspack };
export { rspack as webpack };

import { version as rspackVersion, webpackVersion } from "../package.json";
export { webpackVersion as version, rspackVersion };

export { Compiler } from "./Compiler";

export { Compilation } from "./Compilation";
export type {
Asset,
AssetInfo,
Assets,
LogEntry,
CompilationParams
} from "./Compilation";

export { MultiCompiler } from "./MultiCompiler";
export type { MultiCompilerOptions, MultiRspackOptions } from "./MultiCompiler";

export * from "./config";
export * from "./rspack";

export { RuntimeGlobals } from "./RuntimeGlobals";
export * from "./Stats";
export * from "./MultiStats";
export * from "./ChunkGroup";
export * from "./NormalModuleFactory";
export { cachedCleverMerge as cleverMerge } from "./util/cleverMerge";
export { EnvironmentPlugin } from "./lib/EnvironmentPlugin";

export { Stats } from "./Stats";
export type { MultiStats } from "./MultiStats";

export type { ChunkGroup } from "./ChunkGroup";

export type { NormalModuleFactory } from "./NormalModuleFactory";

export { NormalModule } from "./NormalModule";

export { default as ModuleFilenameHelpers } from "./lib/ModuleFilenameHelpers";

export const WebpackError = Error;

export * as sources from "webpack-sources";

export { BannerPlugin } from "./builtin-plugin";
export type { BannerPluginArgument } from "./builtin-plugin";

export { ProvidePlugin } from "./builtin-plugin";
export type { ProvidePluginOptions } from "./builtin-plugin";

export { DefinePlugin } from "./builtin-plugin";
export type { DefinePluginOptions } from "./builtin-plugin";

export { ProgressPlugin } from "./builtin-plugin";
export type { ProgressPluginArgument } from "./builtin-plugin";

export { EntryPlugin } from "./builtin-plugin";
export type { EntryOptions } from "./builtin-plugin";

export { ExternalsPlugin } from "./builtin-plugin";

export { HotModuleReplacementPlugin } from "./builtin-plugin";

export { LoaderOptionsPlugin } from "./lib/LoaderOptionsPlugin";

export { LoaderTargetPlugin } from "./lib/LoaderTargetPlugin";
export {
registerGlobalTrace as experimental_registerGlobalTrace,
cleanupGlobalTrace as experimental_cleanupGlobalTrace
} from "@rspack/binding";
import { Configuration } from "./config";
// TODO(hyf0): should remove this re-export when we cleanup the exports of `@rspack/core`
export type OptimizationSplitChunksOptions = NonNullable<
Configuration["optimization"]
>["splitChunks"];
export {
BannerPlugin,
DefinePlugin,
ProvidePlugin,
ProgressPlugin,
HtmlRspackPlugin,
SwcJsMinimizerRspackPlugin,
SwcCssMinimizerRspackPlugin,
CopyRspackPlugin,
EntryPlugin,
ExternalsPlugin,
EnableChunkLoadingPlugin,
HotModuleReplacementPlugin
} from "./builtin-plugin";
export type {
BannerPluginArgument,
DefinePluginOptions,
ProvidePluginOptions,
ProgressPluginArgument,
HtmlRspackPluginOptions,
SwcJsMinimizerRspackPluginOptions,
CopyRspackPluginOptions,
EntryOptions
} from "./builtin-plugin";

export { EnvironmentPlugin } from "./lib/EnvironmentPlugin";

import NodeTemplatePlugin from "./node/NodeTemplatePlugin";
import { NodeTargetPlugin } from "./builtin-plugin";
Expand All @@ -60,4 +77,27 @@ export const library = { EnableLibraryPlugin };
import { EnableWasmLoadingPlugin } from "./builtin-plugin";
export const wasm = { EnableWasmLoadingPlugin };

export { Watching };
import { EnableChunkLoadingPlugin } from "./builtin-plugin";
export const javascript = { EnableChunkLoadingPlugin };

export type { Watching } from "./Watching";

import { createHash } from "./util/createHash";
import { cachedCleverMerge as cleverMerge } from "./util/cleverMerge";
export const util = { createHash, cleverMerge };

export {
registerGlobalTrace as experimental_registerGlobalTrace,
cleanupGlobalTrace as experimental_cleanupGlobalTrace
} from "@rspack/binding";

export { HtmlRspackPlugin } from "./builtin-plugin";
export type { HtmlRspackPluginOptions } from "./builtin-plugin";

export { SwcJsMinimizerRspackPlugin } from "./builtin-plugin";
export type { SwcJsMinimizerRspackPluginOptions } from "./builtin-plugin";

export { SwcCssMinimizerRspackPlugin } from "./builtin-plugin";

export { CopyRspackPlugin } from "./builtin-plugin";
export type { CopyRspackPluginOptions } from "./builtin-plugin";
4 changes: 2 additions & 2 deletions packages/rspack/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"rootDir": ".",
"allowJs": true,
"strict": true
},
"include": ["src"],
"include": ["src", "package.json"],
"ts-node": {
"transpileOnly": true
},
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"declarationMap": true,
"composite": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
"skipLibCheck": true,
"resolveJsonModule": true
}
}

0 comments on commit 7f206f5

Please sign in to comment.