|
| 1 | +use enum_tag::EnumTag; |
| 2 | +use rspack_core::{ |
| 3 | + BoxPlugin, ChunkLoadingType, CompilerOptions, EntryOptions, ExternalItem, ExternalType, |
| 4 | + LibraryType, PluginExt as _, WasmLoadingType, |
| 5 | +}; |
| 6 | + |
| 7 | +/// Options of builtin plugins |
| 8 | +/// |
| 9 | +/// The order of this list is strictly ordered with respect to `rspackOptionsApply`. |
| 10 | +#[allow(clippy::enum_variant_names)] |
| 11 | +#[derive(Debug, EnumTag)] |
| 12 | +#[repr(u8)] |
| 13 | +pub(super) enum BuiltinPluginOptions { |
| 14 | + // External handling plugins |
| 15 | + ExternalsPlugin((ExternalType, Vec<ExternalItem>)), |
| 16 | + NodeTargetPlugin, |
| 17 | + ElectronTargetPlugin(rspack_plugin_externals::ElectronTargetContext), |
| 18 | + HttpExternalsRspackPlugin((bool /* css */, bool /* web_async */)), |
| 19 | + |
| 20 | + // Chunk format and loading plugins |
| 21 | + ChunkPrefetchPreloadPlugin, |
| 22 | + CommonJsChunkFormatPlugin, |
| 23 | + ArrayPushCallbackChunkFormatPlugin, |
| 24 | + ModuleChunkFormatPlugin, |
| 25 | + EnableChunkLoadingPlugin(ChunkLoadingType), |
| 26 | + EnableWasmLoadingPlugin(WasmLoadingType), |
| 27 | + |
| 28 | + // Runtime and error handling |
| 29 | + RuntimeChunkPlugin(rspack_plugin_runtime_chunk::RuntimeChunkOptions), |
| 30 | + NoEmitOnErrorsPlugin, |
| 31 | + |
| 32 | + // DevTool plugins |
| 33 | + SourceMapDevToolPlugin(rspack_plugin_devtool::SourceMapDevToolPluginOptions), |
| 34 | + EvalSourceMapDevToolPlugin(rspack_plugin_devtool::SourceMapDevToolPluginOptions), |
| 35 | + EvalDevToolModulePlugin(rspack_plugin_devtool::EvalDevToolModulePluginOptions), |
| 36 | + |
| 37 | + // Core module plugins |
| 38 | + JavascriptModulesPlugin, |
| 39 | + JsonModulesPlugin, |
| 40 | + AssetModulesPlugin, |
| 41 | + AsyncWebAssemblyModulesPlugin, |
| 42 | + CssModulesPlugin, |
| 43 | + |
| 44 | + // Entry and runtime plugins |
| 45 | + EntryPlugin((String /* entry request */, EntryOptions)), |
| 46 | + RuntimePlugin, |
| 47 | + // TODO: add bundler info plugin |
| 48 | + // BundlerInfoRspackPlugin, |
| 49 | + |
| 50 | + // Core functionality plugins |
| 51 | + InferAsyncModulesPlugin, |
| 52 | + APIPlugin, |
| 53 | + DataUriPlugin, |
| 54 | + FileUriPlugin, |
| 55 | + |
| 56 | + // Optimization plugins |
| 57 | + EnsureChunkConditionsPlugin, |
| 58 | + MergeDuplicateChunksPlugin, |
| 59 | + SideEffectsFlagPlugin, |
| 60 | + FlagDependencyExportsPlugin, |
| 61 | + FlagDependencyUsagePlugin(bool), |
| 62 | + ModuleConcatenationPlugin, |
| 63 | + MangleExportsPlugin(bool), |
| 64 | + |
| 65 | + // Experiments |
| 66 | + // TODO: support lazy compilation |
| 67 | + // LazyCompilationPlugin, |
| 68 | + |
| 69 | + // Output plugins |
| 70 | + EnableLibraryPlugin(LibraryType), |
| 71 | + // TODO: support split chunks |
| 72 | + // SplitChunksPlugin, |
| 73 | + RemoveEmptyChunksPlugin, |
| 74 | + RealContentHashPlugin, |
| 75 | + |
| 76 | + // Module and chunk ID plugins |
| 77 | + NamedModuleIdsPlugin, |
| 78 | + NaturalModuleIdsPlugin, |
| 79 | + DeterministicModuleIdsPlugin, |
| 80 | + NaturalChunkIdsPlugin, |
| 81 | + NamedChunkIdsPlugin, |
| 82 | + DeterministicChunkIdsPlugin, |
| 83 | + OccurrenceChunkIdsPlugin(rspack_ids::OccurrenceChunkIdsPluginOptions), |
| 84 | + |
| 85 | + // Define and optimization plugins |
| 86 | + DefinePlugin(rspack_plugin_javascript::define_plugin::DefineValue), |
| 87 | + AnyMinimizerRspackPlugin(BoxPlugin), |
| 88 | + |
| 89 | + // TODO: support performance |
| 90 | + // SizeLimitsPlugin, |
| 91 | + |
| 92 | + // Cache plugins |
| 93 | + // MemoryCachePlugin, |
| 94 | + |
| 95 | + // Worker plugins |
| 96 | + WorkerPlugin, |
| 97 | +} |
| 98 | + |
| 99 | +/// Context used to build plugins |
| 100 | +#[derive(Default, Debug)] |
| 101 | +pub struct BuilderContext { |
| 102 | + pub(super) plugins: Vec<BuiltinPluginOptions>, |
| 103 | +} |
| 104 | + |
| 105 | +impl BuilderContext { |
| 106 | + /// Take plugins from the context. |
| 107 | + /// |
| 108 | + /// The plugins are sorted by their tag. |
| 109 | + pub fn take_plugins(&mut self, compiler_options: &CompilerOptions) -> Vec<BoxPlugin> { |
| 110 | + self.plugins.sort_by_key(|p| p.tag()); |
| 111 | + let mut plugins = Vec::new(); |
| 112 | + self.plugins.drain(..).for_each(|plugin| match plugin { |
| 113 | + // External handling plugins |
| 114 | + BuiltinPluginOptions::ExternalsPlugin((external_type, externals)) => { |
| 115 | + plugins |
| 116 | + .push(rspack_plugin_externals::ExternalsPlugin::new(external_type, externals).boxed()); |
| 117 | + } |
| 118 | + BuiltinPluginOptions::NodeTargetPlugin => { |
| 119 | + plugins.push(rspack_plugin_externals::node_target_plugin()) |
| 120 | + } |
| 121 | + BuiltinPluginOptions::ElectronTargetPlugin(context) => { |
| 122 | + rspack_plugin_externals::electron_target_plugin(context, &mut plugins) |
| 123 | + } |
| 124 | + BuiltinPluginOptions::HttpExternalsRspackPlugin((css, web_async)) => { |
| 125 | + plugins.push(rspack_plugin_externals::http_externals_rspack_plugin( |
| 126 | + css, web_async, |
| 127 | + )); |
| 128 | + } |
| 129 | + |
| 130 | + // Chunk format and loading plugins |
| 131 | + BuiltinPluginOptions::ChunkPrefetchPreloadPlugin => { |
| 132 | + plugins.push(rspack_plugin_runtime::ChunkPrefetchPreloadPlugin::default().boxed()); |
| 133 | + } |
| 134 | + BuiltinPluginOptions::CommonJsChunkFormatPlugin => { |
| 135 | + plugins.push(rspack_plugin_runtime::CommonJsChunkFormatPlugin::default().boxed()); |
| 136 | + } |
| 137 | + BuiltinPluginOptions::ArrayPushCallbackChunkFormatPlugin => { |
| 138 | + plugins.push(rspack_plugin_runtime::ArrayPushCallbackChunkFormatPlugin::default().boxed()); |
| 139 | + } |
| 140 | + BuiltinPluginOptions::ModuleChunkFormatPlugin => { |
| 141 | + plugins.push(rspack_plugin_runtime::ModuleChunkFormatPlugin::default().boxed()); |
| 142 | + } |
| 143 | + BuiltinPluginOptions::EnableChunkLoadingPlugin(chunk_loading_type) => { |
| 144 | + rspack_plugin_runtime::enable_chunk_loading_plugin(chunk_loading_type, &mut plugins); |
| 145 | + } |
| 146 | + BuiltinPluginOptions::EnableWasmLoadingPlugin(wasm_loading_type) => { |
| 147 | + plugins.push(rspack_plugin_wasm::enable_wasm_loading_plugin( |
| 148 | + wasm_loading_type, |
| 149 | + )); |
| 150 | + } |
| 151 | + |
| 152 | + // Runtime and error handling plugins |
| 153 | + BuiltinPluginOptions::RuntimeChunkPlugin(options) => { |
| 154 | + plugins.push(rspack_plugin_runtime_chunk::RuntimeChunkPlugin::new(options).boxed()); |
| 155 | + } |
| 156 | + BuiltinPluginOptions::NoEmitOnErrorsPlugin => { |
| 157 | + plugins.push(rspack_plugin_no_emit_on_errors::NoEmitOnErrorsPlugin::default().boxed()); |
| 158 | + } |
| 159 | + |
| 160 | + // DevTool plugins |
| 161 | + BuiltinPluginOptions::SourceMapDevToolPlugin(options) => { |
| 162 | + plugins.push(rspack_plugin_devtool::SourceMapDevToolPlugin::new(options).boxed()); |
| 163 | + } |
| 164 | + BuiltinPluginOptions::EvalSourceMapDevToolPlugin(options) => { |
| 165 | + plugins.push(rspack_plugin_devtool::EvalSourceMapDevToolPlugin::new(options).boxed()); |
| 166 | + } |
| 167 | + BuiltinPluginOptions::EvalDevToolModulePlugin(options) => { |
| 168 | + plugins.push(rspack_plugin_devtool::EvalDevToolModulePlugin::new(options).boxed()); |
| 169 | + } |
| 170 | + |
| 171 | + // Core module plugins |
| 172 | + BuiltinPluginOptions::JavascriptModulesPlugin => { |
| 173 | + plugins.push(rspack_plugin_javascript::JsPlugin::default().boxed()); |
| 174 | + } |
| 175 | + BuiltinPluginOptions::JsonModulesPlugin => { |
| 176 | + plugins.push(rspack_plugin_json::JsonPlugin.boxed()); |
| 177 | + } |
| 178 | + BuiltinPluginOptions::AssetModulesPlugin => { |
| 179 | + plugins.push(rspack_plugin_asset::AssetPlugin::default().boxed()); |
| 180 | + } |
| 181 | + BuiltinPluginOptions::AsyncWebAssemblyModulesPlugin => { |
| 182 | + plugins.push(rspack_plugin_wasm::AsyncWasmPlugin::default().boxed()); |
| 183 | + } |
| 184 | + BuiltinPluginOptions::CssModulesPlugin => { |
| 185 | + plugins.push(rspack_plugin_css::CssPlugin::default().boxed()); |
| 186 | + } |
| 187 | + |
| 188 | + // Entry and runtime plugins |
| 189 | + BuiltinPluginOptions::EntryPlugin((entry_request, options)) => { |
| 190 | + plugins.push( |
| 191 | + rspack_plugin_entry::EntryPlugin::new( |
| 192 | + compiler_options.context.clone(), |
| 193 | + entry_request, |
| 194 | + options, |
| 195 | + ) |
| 196 | + .boxed(), |
| 197 | + ); |
| 198 | + } |
| 199 | + BuiltinPluginOptions::RuntimePlugin => { |
| 200 | + plugins.push(rspack_plugin_runtime::RuntimePlugin::default().boxed()) |
| 201 | + } |
| 202 | + // TODO: add bundler info plugin |
| 203 | + // BuiltinPluginOptions::BundlerInfoRspackPlugin => {} |
| 204 | + |
| 205 | + // Core functionality plugins |
| 206 | + BuiltinPluginOptions::InferAsyncModulesPlugin => { |
| 207 | + plugins.push(rspack_plugin_javascript::InferAsyncModulesPlugin::default().boxed()) |
| 208 | + } |
| 209 | + BuiltinPluginOptions::APIPlugin => { |
| 210 | + plugins.push(rspack_plugin_javascript::api_plugin::APIPlugin::default().boxed()) |
| 211 | + } |
| 212 | + BuiltinPluginOptions::DataUriPlugin => { |
| 213 | + plugins.push(rspack_plugin_schemes::DataUriPlugin::default().boxed()); |
| 214 | + } |
| 215 | + BuiltinPluginOptions::FileUriPlugin => { |
| 216 | + plugins.push(rspack_plugin_schemes::FileUriPlugin::default().boxed()); |
| 217 | + } |
| 218 | + |
| 219 | + // Optimization plugins |
| 220 | + BuiltinPluginOptions::EnsureChunkConditionsPlugin => { |
| 221 | + plugins.push( |
| 222 | + rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin::default().boxed(), |
| 223 | + ); |
| 224 | + } |
| 225 | + BuiltinPluginOptions::MergeDuplicateChunksPlugin => { |
| 226 | + plugins.push( |
| 227 | + rspack_plugin_merge_duplicate_chunks::MergeDuplicateChunksPlugin::default().boxed(), |
| 228 | + ); |
| 229 | + } |
| 230 | + BuiltinPluginOptions::SideEffectsFlagPlugin => { |
| 231 | + plugins.push(rspack_plugin_javascript::SideEffectsFlagPlugin::default().boxed()); |
| 232 | + } |
| 233 | + BuiltinPluginOptions::FlagDependencyExportsPlugin => { |
| 234 | + plugins.push(rspack_plugin_javascript::FlagDependencyExportsPlugin::default().boxed()); |
| 235 | + } |
| 236 | + BuiltinPluginOptions::FlagDependencyUsagePlugin(value) => { |
| 237 | + plugins.push(rspack_plugin_javascript::FlagDependencyUsagePlugin::new(value).boxed()) |
| 238 | + } |
| 239 | + BuiltinPluginOptions::ModuleConcatenationPlugin => { |
| 240 | + plugins.push(rspack_plugin_javascript::ModuleConcatenationPlugin::default().boxed()); |
| 241 | + } |
| 242 | + BuiltinPluginOptions::MangleExportsPlugin(value) => { |
| 243 | + plugins.push(rspack_plugin_javascript::MangleExportsPlugin::new(value).boxed()) |
| 244 | + } |
| 245 | + |
| 246 | + // Experiments |
| 247 | + // TODO: support lazy compilation |
| 248 | + // BuiltinPluginOptions::LazyCompilationPlugin => { |
| 249 | + // plugins |
| 250 | + // .push(rspack_plugin_lazy_compilation::plugin::LazyCompilationPlugin::default().boxed()); |
| 251 | + // } |
| 252 | + |
| 253 | + // Output plugins |
| 254 | + BuiltinPluginOptions::EnableLibraryPlugin(library_type) => { |
| 255 | + rspack_plugin_library::enable_library_plugin(library_type, &mut plugins) |
| 256 | + } |
| 257 | + // BuiltinPluginOptions::SplitChunksPlugin => { |
| 258 | + // plugins.push(rspack_plugin_split_chunks::SplitChunksPlugin::default().boxed()) |
| 259 | + // } |
| 260 | + BuiltinPluginOptions::RemoveEmptyChunksPlugin => { |
| 261 | + plugins.push(rspack_plugin_remove_empty_chunks::RemoveEmptyChunksPlugin::default().boxed()) |
| 262 | + } |
| 263 | + BuiltinPluginOptions::RealContentHashPlugin => { |
| 264 | + plugins.push(rspack_plugin_real_content_hash::RealContentHashPlugin::default().boxed()) |
| 265 | + } |
| 266 | + |
| 267 | + // Module and chunk ID plugins |
| 268 | + BuiltinPluginOptions::NamedModuleIdsPlugin => { |
| 269 | + plugins.push(rspack_ids::NamedModuleIdsPlugin::default().boxed()) |
| 270 | + } |
| 271 | + BuiltinPluginOptions::NaturalModuleIdsPlugin => { |
| 272 | + plugins.push(rspack_ids::NaturalModuleIdsPlugin::default().boxed()) |
| 273 | + } |
| 274 | + BuiltinPluginOptions::DeterministicModuleIdsPlugin => { |
| 275 | + plugins.push(rspack_ids::DeterministicModuleIdsPlugin::default().boxed()) |
| 276 | + } |
| 277 | + BuiltinPluginOptions::NaturalChunkIdsPlugin => { |
| 278 | + plugins.push(rspack_ids::NaturalChunkIdsPlugin::default().boxed()) |
| 279 | + } |
| 280 | + BuiltinPluginOptions::NamedChunkIdsPlugin => { |
| 281 | + plugins.push(rspack_ids::NamedChunkIdsPlugin::new(None, None).boxed()) |
| 282 | + } |
| 283 | + BuiltinPluginOptions::DeterministicChunkIdsPlugin => { |
| 284 | + plugins.push(rspack_ids::DeterministicChunkIdsPlugin::default().boxed()) |
| 285 | + } |
| 286 | + BuiltinPluginOptions::OccurrenceChunkIdsPlugin(options) => { |
| 287 | + plugins.push(rspack_ids::OccurrenceChunkIdsPlugin::new(options).boxed()) |
| 288 | + } |
| 289 | + |
| 290 | + // Define and optimization plugins |
| 291 | + BuiltinPluginOptions::DefinePlugin(values) => { |
| 292 | + plugins.push(rspack_plugin_javascript::define_plugin::DefinePlugin::new(values).boxed()) |
| 293 | + } |
| 294 | + BuiltinPluginOptions::AnyMinimizerRspackPlugin(plugin) => plugins.push(plugin), |
| 295 | + |
| 296 | + // TODO: support performance |
| 297 | + // BuiltinPluginOptions::SizeLimitsPlugin => { |
| 298 | + // plugins.push(rspack_plugin_size_limits::SizeLimitsPlugin::default().boxed()) |
| 299 | + // } |
| 300 | + |
| 301 | + // Cache plugins |
| 302 | + // BuiltinPluginOptions::MemoryCachePlugin => MemoryCachePlugin::default().boxed(), |
| 303 | + |
| 304 | + // Worker plugins |
| 305 | + BuiltinPluginOptions::WorkerPlugin => { |
| 306 | + plugins.push(rspack_plugin_worker::WorkerPlugin::default().boxed()) |
| 307 | + } |
| 308 | + }); |
| 309 | + plugins |
| 310 | + } |
| 311 | +} |
0 commit comments