File size: 8,965 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
use anyhow::Result;
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, Vc};
use turbo_tasks_fs::{FileJsonContent, FileSystemPath, glob::Glob};
use turbopack_core::{
asset::Asset,
chunk::ChunkableModule,
error::PrettyPrintError,
file_source::FileSource,
issue::{
Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString,
},
module::Module,
resolve::{FindContextFileResult, find_context_file, package_json},
};
use crate::references::{
async_module::OptionAsyncModule,
esm::{EsmExport, EsmExports},
};
#[turbo_tasks::value_trait]
pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset {
#[turbo_tasks::function]
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports>;
#[turbo_tasks::function]
fn get_async_module(self: Vc<Self>) -> Vc<OptionAsyncModule> {
Vc::cell(None)
}
#[turbo_tasks::function]
async fn is_marked_as_side_effect_free(
self: Vc<Self>,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
Ok(is_marked_as_side_effect_free(
self.ident().path().owned().await?,
side_effect_free_packages,
))
}
}
#[turbo_tasks::value]
enum SideEffectsValue {
None,
Constant(bool),
Glob(ResolvedVc<Glob>),
}
#[turbo_tasks::function]
async fn side_effects_from_package_json(
package_json: FileSystemPath,
) -> Result<Vc<SideEffectsValue>> {
let package_json_file = FileSource::new(package_json).to_resolved().await?;
let package_json = &*package_json_file.content().parse_json().await?;
if let FileJsonContent::Content(content) = package_json
&& let Some(side_effects) = content.get("sideEffects")
{
if let Some(side_effects) = side_effects.as_bool() {
return Ok(SideEffectsValue::Constant(side_effects).cell());
} else if let Some(side_effects) = side_effects.as_array() {
let globs = side_effects
.iter()
.filter_map(|side_effect| {
if let Some(side_effect) = side_effect.as_str() {
if side_effect.contains('/') {
Some(Glob::new(
side_effect.strip_prefix("./").unwrap_or(side_effect).into(),
))
} else {
Some(Glob::new(format!("**/{side_effect}").into()))
}
} else {
SideEffectsInPackageJsonIssue {
// TODO(PACK-4879): This should point at the buggy element
source: IssueSource::from_source_only(ResolvedVc::upcast(
package_json_file,
)),
description: Some(
StyledString::Text(
format!(
"Each element in sideEffects must be a string, but found \
{side_effect:?}"
)
.into(),
)
.resolved_cell(),
),
}
.resolved_cell()
.emit();
None
}
})
.map(|glob| async move {
match glob.resolve().await {
Ok(glob) => Ok(Some(glob)),
Err(err) => {
SideEffectsInPackageJsonIssue {
// TODO(PACK-4879): This should point at the buggy glob
source: IssueSource::from_source_only(ResolvedVc::upcast(
package_json_file,
)),
description: Some(
StyledString::Text(
format!(
"Invalid glob in sideEffects: {}",
PrettyPrintError(&err)
)
.into(),
)
.resolved_cell(),
),
}
.resolved_cell()
.emit();
Ok(None)
}
}
})
.try_flat_join()
.await?;
return Ok(
SideEffectsValue::Glob(Glob::alternatives(globs).to_resolved().await?).cell(),
);
} else {
SideEffectsInPackageJsonIssue {
// TODO(PACK-4879): This should point at the buggy value
source: IssueSource::from_source_only(ResolvedVc::upcast(package_json_file)),
description: Some(
StyledString::Text(
format!(
"sideEffects must be a boolean or an array, but found {side_effects:?}"
)
.into(),
)
.resolved_cell(),
),
}
.resolved_cell()
.emit();
}
}
Ok(SideEffectsValue::None.cell())
}
#[turbo_tasks::value]
struct SideEffectsInPackageJsonIssue {
source: IssueSource,
description: Option<ResolvedVc<StyledString>>,
}
#[turbo_tasks::value_impl]
impl Issue for SideEffectsInPackageJsonIssue {
#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Parse.into()
}
fn severity(&self) -> IssueSeverity {
IssueSeverity::Warning
}
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.source.file_path()
}
#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text(rcstr!("Invalid value for sideEffects in package.json")).cell()
}
#[turbo_tasks::function]
fn description(&self) -> Vc<OptionStyledString> {
Vc::cell(self.description)
}
#[turbo_tasks::function]
fn source(&self) -> Vc<OptionIssueSource> {
Vc::cell(Some(self.source))
}
}
#[turbo_tasks::function]
pub async fn is_marked_as_side_effect_free(
path: FileSystemPath,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
if side_effect_free_packages.await?.matches(&path.path) {
return Ok(Vc::cell(true));
}
let find_package_json = find_context_file(path.parent(), package_json()).await?;
if let FindContextFileResult::Found(package_json, _) = &*find_package_json {
match *side_effects_from_package_json(package_json.clone()).await? {
SideEffectsValue::None => {}
SideEffectsValue::Constant(side_effects) => return Ok(Vc::cell(!side_effects)),
SideEffectsValue::Glob(glob) => {
if let Some(rel_path) = package_json.parent().get_relative_path_to(&path) {
let rel_path = rel_path.strip_prefix("./").unwrap_or(&rel_path);
return Ok(Vc::cell(!glob.await?.matches(rel_path)));
}
}
}
}
Ok(Vc::cell(false))
}
#[turbo_tasks::value(shared)]
pub enum EcmascriptExports {
/// A module using ESM exports.
EsmExports(ResolvedVc<EsmExports>),
/// A module using `__turbopack_export_namespace__`, used by custom module types.
DynamicNamespace,
/// A module using CommonJS exports.
CommonJs,
/// No exports at all, and falling back to CommonJS semantics.
EmptyCommonJs,
/// A value that is made available as both the CommonJS `exports` and the ESM default export.
Value,
/// Some error occurred while determining exports.
Unknown,
/// No exports, used by custom module types.
None,
}
#[turbo_tasks::value_impl]
impl EcmascriptExports {
#[turbo_tasks::function]
pub async fn needs_facade(&self) -> Result<Vc<bool>> {
Ok(match self {
EcmascriptExports::EsmExports(exports) => {
let exports = exports.await?;
let has_reexports = !exports.star_exports.is_empty()
|| exports.exports.iter().any(|(_, export)| {
matches!(
export,
EsmExport::ImportedBinding(..) | EsmExport::ImportedNamespace(_)
)
});
Vc::cell(has_reexports)
}
_ => Vc::cell(false),
})
}
}
|