Repository: parcel-bundler/parcel
File: packages/transformers/js/core/src/lib.rs (Rust)

Code before:
              );

              module.fold_with(&mut passes)
            };

            let mut has_node_replacements = false;

            let module = {
              let mut passes = chain!(
                // Insert placeholders for context variables like __dirname and __filename
                Optional::new(
                  NodeReplacer {
                    source_map: &source_map,
                    items: &mut global_deps,
                    globals: HashMap::new(),
                    project_root: Path::new(&config.project_root),
                    filename: Path::new(&config.filename),
                    decls: &mut decls,
                    global_mark,
                    scope_hoist: config.scope_hoist,
                    has_node_replacements: &mut has_node_replacements,
                  },
                  config.node_replacer
                ),
                // Inject SWC helpers if needed.
                helpers::inject_helpers(),
              );

              module.fold_with(&mut passes)
            };

            result.has_node_replacements = has_node_replacements;

            let module = module.fold_with(
              // Collect dependencies
              &mut dependency_collector(
                &source_map,
                &mut result.dependencies,
                &decls,
                ignore_mark,
                &config,
                &mut diagnostics,
              ),
            );

            if diagnostics
              .iter()
              .any(|d| d.severity == DiagnosticSeverity::Error)
            {
              result.diagnostics = Some(diagnostics);
              return Ok(result);

Review comment (refactor):
1. We don't need to run `inject_helpers` another time
2. ~I'd just move the NodeReplacer directly between `GlobalReplacer` and `preset_env` directly above this~ I see, you did this because of the borrow checker

Code after:
              );

              module.fold_with(&mut passes)
            };

            let mut has_node_replacements = false;

            let module = module.fold_with(
              // Replace __dirname and __filename with placeholders in Node env
              &mut Optional::new(
                NodeReplacer {
                  source_map: &source_map,
                  items: &mut global_deps,
                  globals: HashMap::new(),
                  project_root: Path::new(&config.project_root),
                  filename: Path::new(&config.filename),
                  decls: &mut decls,
                  global_mark,
                  scope_hoist: config.scope_hoist,
                  has_node_replacements: &mut has_node_replacements,
                },
                config.node_replacer,
              ),
            );

            result.has_node_replacements = has_node_replacements;

            let module = module.fold_with(
              // Collect dependencies
              &mut dependency_collector(
                &source_map,
                &mut result.dependencies,
                &decls,
                ignore_mark,
                &config,
                &mut diagnostics,
              ),
            );

            if diagnostics
              .iter()
              .any(|d| d.severity == DiagnosticSeverity::Error)
            {
              result.diagnostics = Some(diagnostics);
              return Ok(result);
            }

            let mut collect = Collect::new(
              source_map.clone(),
              decls,
              ignore_mark,