Skip to content

Commit bd8c65b

Browse files
Add resolver trait and placeholders (#9725)
1 parent 25d010a commit bd8c65b

14 files changed

+157
-0
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct BundleGraph {}

crates/parcel_core/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Core re-implementation in Rust
22
3+
pub mod bundle_graph;
34
pub mod hash;
5+
pub mod plugin;
46
pub mod types;
57

68
/// New-type for paths relative to a project-root

crates/parcel_core/src/plugin.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
mod bundler;
2+
pub use bundler::*;
3+
4+
mod compressor;
5+
pub use compressor::*;
6+
7+
mod namer;
8+
pub use namer::*;
9+
10+
mod optimizer;
11+
pub use optimizer::*;
12+
13+
mod packager;
14+
pub use packager::*;
15+
16+
mod reporter;
17+
pub use reporter::*;
18+
19+
mod resolver;
20+
pub use resolver::*;
21+
22+
mod runtime;
23+
pub use runtime::*;
24+
25+
mod transformer;
26+
pub use transformer::*;
27+
28+
mod validator;
29+
pub use validator::*;
30+
31+
pub struct PluginContext {
32+
pub options: PluginOptions,
33+
pub logger: PluginLogger,
34+
}
35+
36+
pub struct PluginLogger {}
37+
38+
pub struct PluginOptions {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Converts an asset graph into a BundleGraph
2+
///
3+
/// Bundlers accept the entire asset graph and modify it to add bundle nodes that group the assets
4+
/// into output bundles.
5+
///
6+
/// Bundle and optimize run in series and are functionally identitical.
7+
///
8+
pub trait BundlerPlugin {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Compresses the input file stream
2+
pub trait CompressorPlugin {}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Determines the output filename for a bundle
2+
///
3+
/// Namers run in a pipeline until one returns a result.
4+
///
5+
pub trait NamerPlugin {}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// Optimises a bundle
2+
///
3+
/// Optimizers are commonly used to implement minification, tree shaking, dead code elimination,
4+
/// and other size reduction techniques that need a full bundle to be effective. However,
5+
/// optimizers can also be used for any type of bundle transformation, such as prepending license
6+
/// headers, converting inline bundles to base 64, etc.
7+
///
8+
/// Multiple optimizer plugins may run in series, and the result of each optimizer is passed to
9+
/// the next.
10+
///
11+
pub trait OptimizerPlugin: Send + Sync {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// Combines all the assets in a bundle together into an output file
2+
///
3+
/// Packagers are also responsible for resolving URL references, bundle inlining, and generating
4+
/// source maps.
5+
///
6+
pub trait PackagerPlugin: Send + Sync {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// Receives events from Parcel as they occur throughout the build process
2+
///
3+
/// For example, reporters may write status information to stdout, run a dev server, or generate a
4+
/// bundle analysis report at the end of a build.
5+
///
6+
pub trait ReporterPlugin {}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::path::PathBuf;
2+
3+
use super::PluginContext;
4+
use crate::types::Dependency;
5+
use crate::types::JSONObject;
6+
use crate::types::Priority;
7+
8+
// TODO Diagnostics and invalidations
9+
10+
pub struct Resolution {
11+
/// Whether this dependency can be deferred by Parcel itself
12+
pub can_defer: bool,
13+
14+
/// The code of the resolved asset
15+
///
16+
/// If provided, this is used rather than reading the file from disk.
17+
///
18+
pub code: Option<String>,
19+
20+
/// An absolute path to the resolved file
21+
pub file_path: PathBuf,
22+
23+
/// Whether the resolved file should be excluded from the build
24+
pub is_excluded: bool,
25+
26+
/// Is spread (shallowly merged) onto the request's dependency.meta
27+
pub meta: JSONObject,
28+
29+
/// An optional named pipeline to use to compile the resolved file
30+
pub pipeline: Option<String>,
31+
32+
/// Overrides the priority set on the dependency
33+
pub priority: Option<Priority>,
34+
35+
/// Corresponds to the asset side effects
36+
pub side_effects: bool,
37+
38+
/// Query parameters to be used by transformers when compiling the resolved file
39+
pub query: Option<String>,
40+
}
41+
42+
/// Converts a dependency specifier into a file path that will be processed by transformers
43+
///
44+
/// Resolvers run in a pipeline until one of them return a result.
45+
///
46+
pub trait ResolverPlugin: Send + Sync {
47+
/// Determines what the dependency specifier resolves to
48+
fn resolve(
49+
&self,
50+
specifier: &str,
51+
dependency: &Dependency,
52+
pipeline: Option<&str>,
53+
context: &PluginContext,
54+
) -> Result<Resolution, anyhow::Error>;
55+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Programmatically insert assets into bundles
2+
pub trait RuntimePlugin {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// Compile a single asset, discover dependencies, or convert the asset to a different format
2+
///
3+
/// Many transformers are wrappers around other tools such as compilers and preprocessors, and are
4+
/// designed to integrate with Parcel.
5+
///
6+
pub trait TransformerPlugin: Send + Sync {}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// Analyzes assets to ensure they are in a valid state
2+
///
3+
/// Validators may throw errors or log warnings to indicate an asset is invalid. They can be used
4+
/// to verify linting, type safety, etc and are run after a build has completed. This enables more
5+
/// important compilation errors to occur first.
6+
///
7+
/// When Parcel runs in watch mode, the built bundles are served even if a validator throws an
8+
/// error. But when running a build, Parcel exits with a failure and status code to ensure code is
9+
/// not deployed for assets that do not meet the validation criteria. This ensures developers
10+
/// remain productive, and do not have to worry about every small typing or linting issue while
11+
/// trying to solve a problem.
12+
///
13+
pub trait ValidatorPlugin {}

crates/parcel_core/src/types/source.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ pub struct SourceLocation {
2525
/// The final location in the source code
2626
pub end: Location,
2727
}
28+
29+
pub struct SourceMap {}

0 commit comments

Comments
 (0)