diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2021-09-08 14:05:34 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 14:05:34 +1000 |
commit | bf6dbf9855cb21d27838c56932c7536c0cd80a55 (patch) | |
tree | 2a175ada4300064d0b6f54b20f475f6bc436a8c3 /cli | |
parent | 48331030111e9d346f054156fe24bc600a57e100 (diff) |
fix(cli): better handling of source maps (#11954)
Ref: #11874
Diffstat (limited to 'cli')
-rw-r--r-- | cli/ast/bundle_hook.rs | 8 | ||||
-rw-r--r-- | cli/ast/mod.rs | 16 | ||||
-rw-r--r-- | cli/ast/transforms.rs | 3 | ||||
-rw-r--r-- | cli/config_file.rs | 1 | ||||
-rw-r--r-- | cli/module_graph.rs | 35 | ||||
-rw-r--r-- | cli/tools/repl.rs | 1 |
6 files changed, 34 insertions, 30 deletions
diff --git a/cli/ast/bundle_hook.rs b/cli/ast/bundle_hook.rs index 8e5b56c32..6b29bd8dd 100644 --- a/cli/ast/bundle_hook.rs +++ b/cli/ast/bundle_hook.rs @@ -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, }))), diff --git a/cli/ast/mod.rs b/cli/ast/mod.rs index 57117bf7b..7a05b9caf 100644 --- a/cli/ast/mod.rs +++ b/cli/ast/mod.rs @@ -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); diff --git a/cli/ast/transforms.rs b/cli/ast/transforms.rs index 142f27093..5e5e20802 100644 --- a/cli/ast/transforms.rs +++ b/cli/ast/transforms.rs @@ -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); diff --git a/cli/config_file.rs b/cli/config_file.rs index f2f119a0b..94373334a 100644 --- a/cli/config_file.rs +++ b/cli/config_file.rs @@ -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, diff --git a/cli/module_graph.rs b/cli/module_graph.rs index fb8b4762e..c88f2af57 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -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)) } } diff --git a/cli/tools/repl.rs b/cli/tools/repl.rs index 4b3080c63..774cece24 100644 --- a/cli/tools/repl.rs +++ b/cli/tools/repl.rs @@ -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, |