Skip to content

Commit

Permalink
resolve encore.gen without alias import
Browse files Browse the repository at this point in the history
  • Loading branch information
eandre committed Dec 22, 2024
1 parent d1597fd commit 571bfac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
25 changes: 23 additions & 2 deletions tsparser/src/parser/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cell::{OnceCell, RefCell};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::io;
use std::path::Path;
use std::path::{Path, PathBuf};

use swc_common::comments::{Comments, NoopComments, SingleThreadedComments};
use swc_common::errors::Handler;
Expand Down Expand Up @@ -31,6 +31,7 @@ pub struct ModuleLoader {
errs: Lrc<Handler>,
file_set: Lrc<FileSet>,
resolver: Box<dyn Resolve>,
encore_gen_root: PathBuf,
by_path: RefCell<HashMap<FilePath, Lrc<Module>>>,

// The universe module, if it's been loaded.
Expand Down Expand Up @@ -83,11 +84,18 @@ impl Error {
}

impl ModuleLoader {
pub fn new(errs: Lrc<Handler>, file_set: Lrc<FileSet>, resolver: Box<dyn Resolve>) -> Self {
pub fn new(
errs: Lrc<Handler>,
file_set: Lrc<FileSet>,
resolver: Box<dyn Resolve>,
app_root: PathBuf,
) -> Self {
let encore_gen_root = app_root.join("encore.gen");
Self {
errs,
file_set,
resolver,
encore_gen_root,
by_path: RefCell::new(HashMap::new()),
universe: OnceCell::new(),
encore_app_clients: OnceCell::new(),
Expand Down Expand Up @@ -140,6 +148,19 @@ impl ModuleLoader {
return Ok(None);
}
}

// Check for the generated clients again, using the resolved path,
// in case the "~encore/*" alias is not set up.
if let Ok(suffix) = buf.strip_prefix(&self.encore_gen_root) {
// Need to check for trailing slash since the resolved path
// will be something like "clients/index.js".
if suffix.starts_with("clients/") {
return Ok(Some(self.encore_app_clients()));
} else if suffix.starts_with("auth/") {
return Ok(Some(self.encore_auth()));
}
}

FilePath::Real(buf.clone())
}
FileName::Custom(ref str) => FilePath::Custom(str.clone()),
Expand Down
1 change: 1 addition & 0 deletions tsparser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl ParseContext {
errs.clone(),
file_set.clone(),
Box::new(resolver),
app_root.clone(),
));
let type_checker = Lrc::new(TypeChecker::new(loader.clone()));

Expand Down

0 comments on commit 571bfac

Please sign in to comment.