summaryrefslogtreecommitdiff
path: root/cli/ops/compiler.rs
blob: f6f1dc34e2fc163e93ba4eaeacfdda0b00a8b523 (plain)
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::Deserialize;
use super::dispatch_json::JsonOp;
use super::dispatch_json::Value;
use crate::futures::future::try_join_all;
use crate::msg;
use crate::op_error::OpError;
use crate::state::State;
use deno_core::ModuleLoader;
use deno_core::*;
use futures::future::FutureExt;

pub fn init(i: &mut Isolate, s: &State) {
  i.register_op("op_cache", s.stateful_json_op(op_cache));
  i.register_op("op_resolve_modules", s.stateful_json_op(op_resolve_modules));
  i.register_op(
    "op_fetch_source_files",
    s.stateful_json_op(op_fetch_source_files),
  );
  let custom_assets = std::collections::HashMap::new(); // TODO(ry) use None.
  i.register_op(
    "op_fetch_asset",
    deno_typescript::op_fetch_asset(custom_assets),
  );
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheArgs {
  module_id: String,
  contents: String,
  extension: String,
}

fn op_cache(
  state: &State,
  args: Value,
  _zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
  let args: CacheArgs = serde_json::from_value(args)?;

  let module_specifier = ModuleSpecifier::resolve_url(&args.module_id)
    .expect("Should be valid module specifier");

  let state_ = &state.borrow().global_state;
  let ts_compiler = state_.ts_compiler.clone();
  ts_compiler.cache_compiler_output(
    &module_specifier,
    &args.extension,
    &args.contents,
  )?;

  Ok(JsonOp::Sync(json!({})))
}

#[derive(Deserialize, Debug)]
struct SpecifiersReferrerArgs {
  specifiers: Vec<String>,
  referrer: Option<String>,
}

fn op_resolve_modules(
  state: &State,
  args: Value,
  _data: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
  let args: SpecifiersReferrerArgs = serde_json::from_value(args)?;
  let (referrer, is_main) = if let Some(referrer) = args.referrer {
    (referrer, false)
  } else {
    ("<unknown>".to_owned(), true)
  };

  let mut specifiers = vec![];

  for specifier in &args.specifiers {
    let specifier = state
      .resolve(specifier, &referrer, is_main)
      .map_err(OpError::from)?;
    specifiers.push(specifier.as_str().to_owned());
  }

  Ok(JsonOp::Sync(json!(specifiers)))
}

fn op_fetch_source_files(
  state: &State,
  args: Value,
  _data: Option<ZeroCopyBuf>,
) -> Result<JsonOp, OpError> {
  let args: SpecifiersReferrerArgs = serde_json::from_value(args)?;

  let ref_specifier = if let Some(referrer) = args.referrer {
    let specifier = ModuleSpecifier::resolve_url(&referrer)
      .expect("Referrer is not a valid specifier");
    Some(specifier)
  } else {
    None
  };

  let global_state = state.borrow().global_state.clone();
  let file_fetcher = global_state.file_fetcher.clone();
  let specifiers = args.specifiers.clone();
  let future = async move {
    let file_futures: Vec<_> = specifiers
      .into_iter()
      .map(|specifier| {
        let file_fetcher_ = file_fetcher.clone();
        let ref_specifier_ = ref_specifier.clone();
        async move {
          let resolved_specifier = ModuleSpecifier::resolve_url(&specifier)
            .expect("Invalid specifier");
          file_fetcher_
            .fetch_source_file(&resolved_specifier, ref_specifier_)
            .await
        }
        .boxed_local()
      })
      .collect();

    let files = try_join_all(file_futures).await.map_err(OpError::from)?;
    // We want to get an array of futures that resolves to
    let v = files.into_iter().map(|f| {
      async {
        // if the source file contains a `types_url` we need to replace
        // the module with the type definition when requested by the compiler
        let file = match f.types_url {
          Some(types_url) => {
            let types_specifier = ModuleSpecifier::from(types_url);
            global_state
              .file_fetcher
              .fetch_source_file(&types_specifier, ref_specifier.clone())
              .await
              .map_err(OpError::from)?
          }
          _ => f,
        };
        // Special handling of WASM and JSON files:
        // compile them into JS first!
        // This allows TS to do correct export types as well as bundles.
        let source_code = match file.media_type {
          msg::MediaType::Wasm => {
            global_state
              .wasm_compiler
              .compile(global_state.clone(), &file)
              .await
              .map_err(|e| OpError::other(e.to_string()))?
              .code
          }
          msg::MediaType::Json => {
            global_state
              .json_compiler
              .compile(&file)
              .await
              .map_err(|e| OpError::other(e.to_string()))?
              .code
          }
          _ => String::from_utf8(file.source_code)
            .map_err(|_| OpError::invalid_utf8())?,
        };
        Ok::<_, OpError>(json!({
          "url": file.url.to_string(),
          "filename": file.filename.to_str().unwrap(),
          "mediaType": file.media_type as i32,
          "sourceCode": source_code,
        }))
      }
    });

    let v = try_join_all(v).await?;
    Ok(v.into())
  }
  .boxed_local();

  Ok(JsonOp::Async(future))
}