Skip to main content

cli/lib/compiler/
webpack_compiler.rs

1//! Upstream source: `../nest-cli/lib/compiler/webpack-compiler.ts`.
2
3use std::path::PathBuf;
4
5use crate::configuration::DEFAULT_TSCONFIG_FILENAME;
6
7use super::defaults::webpack_defaults::{WebpackDefaults, webpack_defaults_factory};
8pub use super::get_webpack_config_path;
9use super::plugins::plugins_loader::MultiNestCompilerPlugins;
10
11#[derive(Clone, Debug, Default, PartialEq, Eq)]
12pub struct WebpackCompiler;
13
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct WebpackCompilerRunPlan {
16    pub default_options: WebpackDefaults,
17    pub user_config_path: Option<PathBuf>,
18    pub watch: bool,
19    pub mode: String,
20    pub stats_errors: usize,
21    pub stats_warnings: usize,
22}
23
24impl WebpackCompiler {
25    pub fn plan_run(
26        source_root: &str,
27        relative_source_root: &str,
28        entry_filename: &str,
29        is_debug_enabled: bool,
30        ts_config_file: Option<&str>,
31        plugins: &MultiNestCompilerPlugins,
32        user_config_path: Option<PathBuf>,
33        watch: bool,
34    ) -> WebpackCompilerRunPlan {
35        let mut default_options = webpack_defaults_factory(
36            source_root,
37            relative_source_root,
38            entry_filename,
39            is_debug_enabled,
40            Some(ts_config_file.unwrap_or(DEFAULT_TSCONFIG_FILENAME)),
41            plugins,
42        );
43        if watch {
44            default_options.mode = "development".to_string();
45        }
46        WebpackCompilerRunPlan {
47            mode: default_options.mode.clone(),
48            default_options,
49            user_config_path,
50            watch,
51            stats_errors: 0,
52            stats_warnings: 0,
53        }
54    }
55
56    pub fn format_building_message() -> &'static str {
57        "Webpack is building your sources..."
58    }
59
60    pub fn should_fail(errors: usize) -> bool {
61        errors > 0
62    }
63}