-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand plugin traits and plugin config (#9744)
* Expand plugin traits and plugin config * Use trait objects * Use Rc
- Loading branch information
1 parent
0b58e51
commit 1180254
Showing
18 changed files
with
992 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub struct BundleGraph {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
use super::PluginConfig; | ||
use crate::bundle_graph::BundleGraph; | ||
|
||
/// Converts an asset graph into a BundleGraph | ||
/// | ||
/// Bundlers accept the entire asset graph and modify it to add bundle nodes that group the assets | ||
/// into output bundles. | ||
/// | ||
/// Bundle and optimize run in series and are functionally identitical. | ||
/// | ||
pub trait BundlerPlugin {} | ||
pub trait BundlerPlugin { | ||
/// A hook designed to load config necessary for the bundler to operate | ||
/// | ||
/// This function will run once, shortly after the plugin is initialised. | ||
/// | ||
fn load_config(&mut self, config: &PluginConfig) -> Result<(), anyhow::Error>; | ||
|
||
// TODO: Should BundleGraph be AssetGraph or something that contains AssetGraph in the name? | ||
fn bundle(&self, bundle_graph: &mut BundleGraph) -> Result<(), anyhow::Error>; | ||
|
||
fn optimize(&self, bundle_graph: &mut BundleGraph) -> Result<(), anyhow::Error>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[derive(Debug)] | ||
struct TestBundlerPlugin {} | ||
|
||
impl BundlerPlugin for TestBundlerPlugin { | ||
fn load_config(&mut self, _config: &PluginConfig) -> Result<(), anyhow::Error> { | ||
todo!() | ||
} | ||
|
||
fn bundle(&self, _bundle_graph: &mut BundleGraph) -> Result<(), anyhow::Error> { | ||
todo!() | ||
} | ||
|
||
fn optimize(&self, _bundle_graph: &mut BundleGraph) -> Result<(), anyhow::Error> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[test] | ||
fn can_be_dyn() { | ||
let _bundler: Box<dyn BundlerPlugin> = Box::new(TestBundlerPlugin {}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,44 @@ | ||
use std::fs::File; | ||
|
||
pub struct CompressedFile { | ||
/// An optional file extension appended to the output file | ||
/// | ||
/// When no extension is returned, then the returned stream replaces the original file. | ||
/// | ||
pub extension: Option<String>, | ||
|
||
/// The compressed file | ||
pub file: File, | ||
} | ||
|
||
/// Compresses the input file stream | ||
pub trait CompressorPlugin {} | ||
pub trait CompressorPlugin { | ||
/// Compress the given file | ||
/// | ||
/// The file contains the final contents of bundles and sourcemaps as they are being written. | ||
/// A new stream can be returned, or None to forward compression onto the next plugin. | ||
/// | ||
fn compress(&self, file: &File) -> Result<Option<CompressedFile>, String>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
struct TestCompressorPlugin {} | ||
|
||
impl CompressorPlugin for TestCompressorPlugin { | ||
fn compress(&self, _file: &File) -> Result<Option<CompressedFile>, String> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[test] | ||
fn can_be_defined_in_dyn_vec() { | ||
let mut compressors = Vec::<Box<dyn CompressorPlugin>>::new(); | ||
|
||
compressors.push(Box::new(TestCompressorPlugin {})); | ||
|
||
assert_eq!(compressors.len(), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,60 @@ | ||
use std::path::PathBuf; | ||
|
||
use super::PluginConfig; | ||
use crate::bundle_graph::BundleGraph; | ||
use crate::types::Bundle; | ||
|
||
/// Determines the output filename for a bundle | ||
/// | ||
/// Namers run in a pipeline until one returns a result. | ||
/// | ||
pub trait NamerPlugin {} | ||
pub trait NamerPlugin { | ||
/// A hook designed to setup config needed for naming bundles | ||
/// | ||
/// This function will run once, shortly after the plugin is initialised. | ||
/// | ||
fn load_config(&mut self, config: &PluginConfig) -> Result<(), anyhow::Error>; | ||
|
||
/// Names the given bundle | ||
/// | ||
/// The returned file path should be relative to the target dist directory, and will be used to | ||
/// name the bundle. Naming can be forwarded onto the next plugin by returning None. | ||
/// | ||
fn name( | ||
&mut self, | ||
config: &PluginConfig, | ||
bundle: &Bundle, | ||
bundle_graph: &BundleGraph, | ||
) -> Result<Option<PathBuf>, anyhow::Error>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
struct TestNamerPlugin {} | ||
|
||
impl NamerPlugin for TestNamerPlugin { | ||
fn load_config(&mut self, _config: &PluginConfig) -> Result<(), anyhow::Error> { | ||
todo!() | ||
} | ||
|
||
fn name( | ||
&mut self, | ||
_config: &PluginConfig, | ||
_bundle: &Bundle, | ||
_bundle_graph: &BundleGraph, | ||
) -> Result<Option<PathBuf>, anyhow::Error> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[test] | ||
fn can_be_defined_in_dyn_vec() { | ||
let mut namers = Vec::<Box<dyn NamerPlugin>>::new(); | ||
|
||
namers.push(Box::new(TestNamerPlugin {})); | ||
|
||
assert_eq!(namers.len(), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,65 @@ | ||
use std::fs::File; | ||
|
||
use super::PluginConfig; | ||
use crate::bundle_graph::BundleGraph; | ||
use crate::types::Bundle; | ||
use crate::types::SourceMap; | ||
|
||
pub struct PackageContext<'a> { | ||
pub bundle: &'a Bundle, | ||
pub bundle_graph: &'a BundleGraph, | ||
pub contents: &'a File, // TODO We may want this to be a String or File later | ||
pub map: Option<&'a SourceMap>, | ||
// TODO getSourceMapReference? | ||
} | ||
|
||
pub struct PackagedBundle { | ||
pub contents: File, | ||
// TODO ast, map, type | ||
} | ||
|
||
/// Combines all the assets in a bundle together into an output file | ||
/// | ||
/// Packagers are also responsible for resolving URL references, bundle inlining, and generating | ||
/// source maps. | ||
/// | ||
pub trait PackagerPlugin: Send + Sync {} | ||
pub trait PackagerPlugin: Send + Sync { | ||
/// A hook designed to setup config needed for packaging | ||
/// | ||
/// This function will run once, shortly after the plugin is initialised. | ||
/// | ||
fn load_config(&mut self, config: &PluginConfig) -> Result<(), anyhow::Error>; | ||
|
||
/// Combines assets in a bundle | ||
fn package( | ||
&mut self, | ||
config: &PluginConfig, | ||
ctx: PackageContext, | ||
) -> Result<PackagedBundle, anyhow::Error>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
struct TestPackagerPlugin {} | ||
|
||
impl PackagerPlugin for TestPackagerPlugin { | ||
fn load_config(&mut self, _config: &PluginConfig) -> Result<(), anyhow::Error> { | ||
todo!() | ||
} | ||
|
||
fn package( | ||
&mut self, | ||
_config: &PluginConfig, | ||
_ctx: PackageContext, | ||
) -> Result<PackagedBundle, anyhow::Error> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[test] | ||
fn can_be_dyn() { | ||
let _packager: Box<dyn PackagerPlugin> = Box::new(TestPackagerPlugin {}); | ||
} | ||
} |
Oops, something went wrong.