File size: 3,715 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
use anyhow::Result;
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::parse::Request,
};
use turbopack_resolve::{
ecmascript::apply_cjs_specific_options, resolve_options_context::ResolveOptionsContext,
};
#[turbo_tasks::function]
fn react_refresh_request() -> Vc<Request> {
Request::parse_string(rcstr!("@next/react-refresh-utils/dist/runtime"))
}
#[turbo_tasks::function]
fn react_refresh_request_in_next() -> Vc<Request> {
Request::parse_string(rcstr!(
"next/dist/compiled/@next/react-refresh-utils/dist/runtime"
))
}
#[turbo_tasks::value]
pub enum ResolveReactRefreshResult {
NotFound,
Found(ResolvedVc<Request>),
}
impl ResolveReactRefreshResult {
pub fn as_request(&self) -> Option<Vc<Request>> {
match self {
ResolveReactRefreshResult::NotFound => None,
ResolveReactRefreshResult::Found(r) => Some(**r),
}
}
pub fn is_found(&self) -> bool {
match self {
ResolveReactRefreshResult::NotFound => false,
ResolveReactRefreshResult::Found(_) => true,
}
}
}
/// Checks whether we can resolve the React Refresh runtime module from the
/// given path. Emits an issue if we can't.
#[turbo_tasks::function]
pub async fn assert_can_resolve_react_refresh(
path: FileSystemPath,
resolve_options_context: Vc<ResolveOptionsContext>,
) -> Result<Vc<ResolveReactRefreshResult>> {
let resolve_options = apply_cjs_specific_options(turbopack_resolve::resolve::resolve_options(
path.clone(),
resolve_options_context,
));
for request in [react_refresh_request_in_next(), react_refresh_request()] {
let result = turbopack_core::resolve::resolve(
path.clone(),
ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined),
request,
resolve_options,
)
.first_source();
if result.await?.is_some() {
return Ok(ResolveReactRefreshResult::Found(request.to_resolved().await?).cell());
}
}
ReactRefreshResolvingIssue { path }.resolved_cell().emit();
Ok(ResolveReactRefreshResult::NotFound.cell())
}
/// An issue that occurred while resolving the React Refresh runtime module.
#[turbo_tasks::value(shared)]
pub struct ReactRefreshResolvingIssue {
path: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl Issue for ReactRefreshResolvingIssue {
fn severity(&self) -> IssueSeverity {
IssueSeverity::Warning
}
#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text(rcstr!("Could not resolve React Refresh runtime")).cell()
}
#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Resolve.cell()
}
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.path.clone().cell()
}
#[turbo_tasks::function]
fn description(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(
StyledString::Line(vec![
StyledString::Text(rcstr!(
"React Refresh will be disabled.\nTo enable React Refresh, install the "
)),
StyledString::Code(rcstr!("react-refresh")),
StyledString::Text(rcstr!(" and ")),
StyledString::Code(rcstr!("@next/react-refresh-utils")),
StyledString::Text(rcstr!(" modules.")),
])
.resolved_cell(),
))
}
}
|