1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fix(cli): better handling of source maps (#11954)

Ref: #11874
This commit is contained in:
Kitson Kelly 2021-09-08 14:05:34 +10:00 committed by GitHub
parent 4833103011
commit bf6dbf9855
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 30 deletions

View file

@ -14,18 +14,12 @@ impl Hook for BundleHook {
) -> Result<Vec<deno_ast::swc::ast::KeyValueProp>, AnyError> {
use deno_ast::swc::ast;
// we use custom file names, and swc "wraps" these in `<` and `>` so, we
// want to strip those back out.
let mut value = module_record.file_name.to_string();
value.pop();
value.remove(0);
Ok(vec![
ast::KeyValueProp {
key: ast::PropName::Ident(ast::Ident::new("url".into(), span)),
value: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str {
span,
value: value.into(),
value: module_record.file_name.to_string().into(),
kind: ast::StrKind::Synthesized,
has_escape: false,
}))),

View file

@ -74,6 +74,7 @@ impl From<deno_ast::swc::common::Loc> for Location {
let filename = match &swc_loc.file.name {
Real(path_buf) => path_buf.to_string_lossy().to_string(),
Custom(str_) => str_.to_string(),
Url(url) => url.to_string(),
_ => panic!("invalid filename"),
};
@ -117,6 +118,8 @@ pub struct EmitOptions {
/// Should the source map be inlined in the emitted code file, or provided
/// as a separate file. Defaults to `true`.
pub inline_source_map: bool,
/// Should the sources be inlined in the source map. Defaults to `true`.
pub inline_sources: bool,
// Should a corresponding .map file be created for the output. This should be
// false if inline_source_map is true. Defaults to `false`.
pub source_map: bool,
@ -139,6 +142,7 @@ impl Default for EmitOptions {
emit_metadata: false,
imports_not_used_as_values: ImportsNotUsedAsValues::Remove,
inline_source_map: true,
inline_sources: true,
source_map: false,
jsx_factory: "React.createElement".into(),
jsx_fragment_factory: "React.Fragment".into(),
@ -162,6 +166,7 @@ impl From<config_file::TsConfig> for EmitOptions {
emit_metadata: options.emit_decorator_metadata,
imports_not_used_as_values,
inline_source_map: options.inline_source_map,
inline_sources: options.inline_sources,
source_map: options.source_map,
jsx_factory: options.jsx_factory,
jsx_fragment_factory: options.jsx_fragment_factory,
@ -204,7 +209,8 @@ pub fn transpile(
) -> Result<(String, Option<String>), AnyError> {
let program: Program = (*parsed_source.program()).clone();
let source_map = Rc::new(SourceMap::default());
let file_name = FileName::Custom(parsed_source.specifier().to_string());
let specifier = resolve_url_or_path(parsed_source.specifier())?;
let file_name = FileName::Url(specifier);
source_map
.new_source_file(file_name, parsed_source.source().text().to_string());
let comments = parsed_source.comments().as_single_threaded(); // needs to be mutable
@ -284,7 +290,7 @@ pub fn transpile(
/// A low level function which transpiles a source module into an swc
/// SourceFile.
pub fn transpile_module(
specifier: &str,
specifier: &ModuleSpecifier,
source: &str,
media_type: MediaType,
emit_options: &EmitOptions,
@ -292,10 +298,8 @@ pub fn transpile_module(
cm: Rc<SourceMap>,
) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> {
let source = strip_bom(source);
let source_file = cm.new_source_file(
FileName::Custom(specifier.to_string()),
source.to_string(),
);
let source_file =
cm.new_source_file(FileName::Url(specifier.clone()), source.to_string());
let input = StringInput::from(&*source_file);
let comments = SingleThreadedComments::default();
let syntax = get_syntax(media_type);

View file

@ -260,6 +260,7 @@ mod test {
use deno_ast::swc::parser::TsConfig;
use deno_ast::swc::visit::Fold;
use deno_ast::swc::visit::FoldWith;
use deno_ast::ModuleSpecifier;
use std::rc::Rc;
use super::*;
@ -434,7 +435,7 @@ mod test {
fn parse(src: &str) -> (Rc<SourceMap>, Module) {
let source_map = Rc::new(SourceMap::default());
let source_file = source_map.new_source_file(
FileName::Custom("file.ts".to_string()),
FileName::Url(ModuleSpecifier::parse("file:///test.ts").unwrap()),
src.to_string(),
);
let input = StringInput::from(&*source_file);

View file

@ -25,6 +25,7 @@ pub struct EmitConfigOptions {
pub emit_decorator_metadata: bool,
pub imports_not_used_as_values: String,
pub inline_source_map: bool,
pub inline_sources: bool,
pub source_map: bool,
pub jsx: String,
pub jsx_factory: String,

View file

@ -158,16 +158,14 @@ impl deno_ast::swc::bundler::Load for BundleLoader<'_> {
file: &deno_ast::swc::common::FileName,
) -> Result<deno_ast::swc::bundler::ModuleData, AnyError> {
match file {
deno_ast::swc::common::FileName::Custom(filename) => {
let specifier = resolve_url_or_path(filename)
.context("Failed to convert swc FileName to ModuleSpecifier.")?;
if let Some(src) = self.graph.get_source(&specifier) {
deno_ast::swc::common::FileName::Url(specifier) => {
if let Some(src) = self.graph.get_source(specifier) {
let media_type = self
.graph
.get_media_type(&specifier)
.get_media_type(specifier)
.context("Looking up media type during bundling.")?;
let (source_file, module) = transpile_module(
filename,
specifier,
&src,
media_type,
self.emit_options,
@ -181,8 +179,11 @@ impl deno_ast::swc::bundler::Load for BundleLoader<'_> {
})
} else {
Err(
GraphError::MissingDependency(specifier, "<bundle>".to_string())
.into(),
GraphError::MissingDependency(
specifier.clone(),
"<bundle>".to_string(),
)
.into(),
)
}
}
@ -803,6 +804,7 @@ impl Graph {
"emitDecoratorMetadata": false,
"importsNotUsedAsValues": "remove",
"inlineSourceMap": false,
"inlineSources": false,
"sourceMap": false,
"jsx": "react",
"jsxFactory": "React.createElement",
@ -852,6 +854,7 @@ impl Graph {
"emitDecoratorMetadata": false,
"importsNotUsedAsValues": "remove",
"inlineSourceMap": true,
"inlineSources": true,
"outDir": "deno://",
"removeComments": true,
}));
@ -994,6 +997,7 @@ impl Graph {
"experimentalDecorators": true,
"importsNotUsedAsValues": "remove",
"inlineSourceMap": false,
"inlineSources": false,
"sourceMap": false,
"isolatedModules": true,
"jsx": "react",
@ -1208,7 +1212,7 @@ impl Graph {
let mut entries = HashMap::new();
entries.insert(
"bundle".to_string(),
deno_ast::swc::common::FileName::Custom(specifier.to_string()),
deno_ast::swc::common::FileName::Url(specifier.clone()),
);
let output = bundler
.bundle(entries)
@ -1716,6 +1720,8 @@ impl Graph {
"emitDecoratorMetadata": false,
"importsNotUsedAsValues": "remove",
"inlineSourceMap": true,
// TODO(@kitsonk) make this actually work when https://github.com/swc-project/swc/issues/2218 addressed.
"inlineSources": true,
"sourceMap": false,
"jsx": "react",
"jsxFactory": "React.createElement",
@ -1834,20 +1840,17 @@ impl deno_ast::swc::bundler::Resolve for Graph {
specifier: &str,
) -> Result<deno_ast::swc::common::FileName, AnyError> {
let referrer =
if let deno_ast::swc::common::FileName::Custom(referrer) = referrer {
resolve_url_or_path(referrer)
.context("Cannot resolve swc FileName to a module specifier")?
if let deno_ast::swc::common::FileName::Url(referrer) = referrer {
referrer
} else {
unreachable!(
"An unexpected referrer was passed when bundling: {:?}",
referrer
)
};
let specifier = self.resolve(specifier, &referrer, false)?;
let specifier = self.resolve(specifier, referrer, false)?;
Ok(deno_ast::swc::common::FileName::Custom(
specifier.to_string(),
))
Ok(deno_ast::swc::common::FileName::Url(specifier))
}
}

View file

@ -663,6 +663,7 @@ impl ReplSession {
emit_metadata: false,
source_map: false,
inline_source_map: false,
inline_sources: false,
imports_not_used_as_values: ImportsNotUsedAsValues::Preserve,
// JSX is not supported in the REPL
transform_jsx: false,