From 7f206f56b929474d3bfd8872833d98df76f68aa9 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Mon, 25 Sep 2023 17:14:54 +0800 Subject: [PATCH] refactor index --- packages/rspack/src/Compilation.ts | 2 - packages/rspack/src/Compiler.ts | 110 +---------------------- packages/rspack/src/MultiCompiler.ts | 2 +- packages/rspack/src/MultiStats.ts | 2 +- packages/rspack/src/MultiWatching.ts | 2 +- packages/rspack/src/Watching.ts | 4 +- packages/rspack/src/index.ts | 130 +++++++++++++++++---------- packages/rspack/tsconfig.json | 4 +- tsconfig.base.json | 3 +- 9 files changed, 96 insertions(+), 163 deletions(-) diff --git a/packages/rspack/src/Compilation.ts b/packages/rspack/src/Compilation.ts index cf7c13b003d1..931005e5229d 100644 --- a/packages/rspack/src/Compilation.ts +++ b/packages/rspack/src/Compilation.ts @@ -954,5 +954,3 @@ export class Compilation { } } } - -export type { JsAssetInfo }; diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index 7c5f4168d0dd..0c41059ad0d4 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -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"; @@ -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 { @@ -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[]; @@ -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; diff --git a/packages/rspack/src/MultiCompiler.ts b/packages/rspack/src/MultiCompiler.ts index c4368fac2cbe..964ed6446c67 100644 --- a/packages/rspack/src/MultiCompiler.ts +++ b/packages/rspack/src/MultiCompiler.ts @@ -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, diff --git a/packages/rspack/src/MultiStats.ts b/packages/rspack/src/MultiStats.ts index 434341e925f7..b666ea17de81 100644 --- a/packages/rspack/src/MultiStats.ts +++ b/packages/rspack/src/MultiStats.ts @@ -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"; diff --git a/packages/rspack/src/MultiWatching.ts b/packages/rspack/src/MultiWatching.ts index 1abf5cda7fa4..ff2919c0f78b 100644 --- a/packages/rspack/src/MultiWatching.ts +++ b/packages/rspack/src/MultiWatching.ts @@ -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"; diff --git a/packages/rspack/src/Watching.ts b/packages/rspack/src/Watching.ts index 7299ce3801ab..3800aedc1e5c 100644 --- a/packages/rspack/src/Watching.ts +++ b/packages/rspack/src/Watching.ts @@ -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; @@ -346,5 +346,3 @@ class Watching { } } } - -export default Watching; diff --git a/packages/rspack/src/index.ts b/packages/rspack/src/index.ts index 4cc90f3238a5..c1a1ffa994f9 100644 --- a/packages/rspack/src/index.ts +++ b/packages/rspack/src/index.ts @@ -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"; @@ -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"; diff --git a/packages/rspack/tsconfig.json b/packages/rspack/tsconfig.json index eab53b28b8f7..7cfd6d816c28 100644 --- a/packages/rspack/tsconfig.json +++ b/packages/rspack/tsconfig.json @@ -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 }, diff --git a/tsconfig.base.json b/tsconfig.base.json index dfb9e5550eb0..cd14bf28c218 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -11,6 +11,7 @@ "declarationMap": true, "composite": true, "forceConsistentCasingInFileNames": true, - "skipLibCheck": true + "skipLibCheck": true, + "resolveJsonModule": true } }