Skip to content

Commit

Permalink
fix(es): Allow input source map file to be omitted (#8951)
Browse files Browse the repository at this point in the history
**Description:**

Some libraries generate source maps but do not upload them to npm, which causes SWC to fail.

**Related issue:**

- #8789 (comment)
  • Loading branch information
kdy1 authored May 13, 2024
1 parent 6bd0d98 commit 606cb67
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ extern crate swc_common as common;

use std::{
fs::{read_to_string, File},
io::ErrorKind,
path::{Path, PathBuf},
sync::Arc,
};
Expand Down Expand Up @@ -154,6 +155,7 @@ use swc_ecma_visit::{FoldWith, VisitMutWith, VisitWith};
pub use swc_error_reporters::handler::{try_with_handler, HandlerOpts};
pub use swc_node_comments::SwcComments;
use swc_timer::timer;
use tracing::warn;
use url::Url;

pub use crate::builder::PassBuilder;
Expand Down Expand Up @@ -339,6 +341,23 @@ impl Compiler {
let path = map_path.display().to_string();
let file = File::open(&path);

// If file is not found, we should return None.
// Some libraries generates source map but omit them from the
// npm package.
//
// See https://github.com/swc-project/swc/issues/8789#issuecomment-2105055772
if file
.as_ref()
.is_err_and(|err| err.kind() == ErrorKind::NotFound)
{
warn!(
"source map is specified by sourceMappingURL but \
there's no source map at `{}`",
path
);
return Ok(None);
}

// Old behavior.
let file = if !is_default {
file?
Expand Down

0 comments on commit 606cb67

Please sign in to comment.