1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -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> { ) -> Result<Vec<deno_ast::swc::ast::KeyValueProp>, AnyError> {
use deno_ast::swc::ast; 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![ Ok(vec![
ast::KeyValueProp { ast::KeyValueProp {
key: ast::PropName::Ident(ast::Ident::new("url".into(), span)), key: ast::PropName::Ident(ast::Ident::new("url".into(), span)),
value: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str { value: Box::new(ast::Expr::Lit(ast::Lit::Str(ast::Str {
span, span,
value: value.into(), value: module_record.file_name.to_string().into(),
kind: ast::StrKind::Synthesized, kind: ast::StrKind::Synthesized,
has_escape: false, 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 { let filename = match &swc_loc.file.name {
Real(path_buf) => path_buf.to_string_lossy().to_string(), Real(path_buf) => path_buf.to_string_lossy().to_string(),
Custom(str_) => str_.to_string(), Custom(str_) => str_.to_string(),
Url(url) => url.to_string(),
_ => panic!("invalid filename"), _ => panic!("invalid filename"),
}; };
@ -117,6 +118,8 @@ pub struct EmitOptions {
/// Should the source map be inlined in the emitted code file, or provided /// Should the source map be inlined in the emitted code file, or provided
/// as a separate file. Defaults to `true`. /// as a separate file. Defaults to `true`.
pub inline_source_map: bool, 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 // Should a corresponding .map file be created for the output. This should be
// false if inline_source_map is true. Defaults to `false`. // false if inline_source_map is true. Defaults to `false`.
pub source_map: bool, pub source_map: bool,
@ -139,6 +142,7 @@ impl Default for EmitOptions {
emit_metadata: false, emit_metadata: false,
imports_not_used_as_values: ImportsNotUsedAsValues::Remove, imports_not_used_as_values: ImportsNotUsedAsValues::Remove,
inline_source_map: true, inline_source_map: true,
inline_sources: true,
source_map: false, source_map: false,
jsx_factory: "React.createElement".into(), jsx_factory: "React.createElement".into(),
jsx_fragment_factory: "React.Fragment".into(), jsx_fragment_factory: "React.Fragment".into(),
@ -162,6 +166,7 @@ impl From<config_file::TsConfig> for EmitOptions {
emit_metadata: options.emit_decorator_metadata, emit_metadata: options.emit_decorator_metadata,
imports_not_used_as_values, imports_not_used_as_values,
inline_source_map: options.inline_source_map, inline_source_map: options.inline_source_map,
inline_sources: options.inline_sources,
source_map: options.source_map, source_map: options.source_map,
jsx_factory: options.jsx_factory, jsx_factory: options.jsx_factory,
jsx_fragment_factory: options.jsx_fragment_factory, jsx_fragment_factory: options.jsx_fragment_factory,
@ -204,7 +209,8 @@ pub fn transpile(
) -> Result<(String, Option<String>), AnyError> { ) -> Result<(String, Option<String>), AnyError> {
let program: Program = (*parsed_source.program()).clone(); let program: Program = (*parsed_source.program()).clone();
let source_map = Rc::new(SourceMap::default()); 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 source_map
.new_source_file(file_name, parsed_source.source().text().to_string()); .new_source_file(file_name, parsed_source.source().text().to_string());
let comments = parsed_source.comments().as_single_threaded(); // needs to be mutable 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 /// A low level function which transpiles a source module into an swc
/// SourceFile. /// SourceFile.
pub fn transpile_module( pub fn transpile_module(
specifier: &str, specifier: &ModuleSpecifier,
source: &str, source: &str,
media_type: MediaType, media_type: MediaType,
emit_options: &EmitOptions, emit_options: &EmitOptions,
@ -292,10 +298,8 @@ pub fn transpile_module(
cm: Rc<SourceMap>, cm: Rc<SourceMap>,
) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> { ) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> {
let source = strip_bom(source); let source = strip_bom(source);
let source_file = cm.new_source_file( let source_file =
FileName::Custom(specifier.to_string()), cm.new_source_file(FileName::Url(specifier.clone()), source.to_string());
source.to_string(),
);
let input = StringInput::from(&*source_file); let input = StringInput::from(&*source_file);
let comments = SingleThreadedComments::default(); let comments = SingleThreadedComments::default();
let syntax = get_syntax(media_type); let syntax = get_syntax(media_type);

View file

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

View file

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

View file

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