Skip to content

Commit fdcc785

Browse files
authored
refactor(cli): migrate runtime compile/bundle to new infrastructure (#8192)
Fixes #8060
1 parent 3558769 commit fdcc785

23 files changed

+852
-2770
lines changed

cli/diagnostics.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
use crate::colors;
44

5+
use deno_core::serde::Deserialize;
6+
use deno_core::serde::Deserializer;
7+
use deno_core::serde::Serialize;
8+
use deno_core::serde::Serializer;
59
use regex::Regex;
6-
use serde::Deserialize;
7-
use serde::Deserializer;
810
use std::error::Error;
911
use std::fmt;
1012

@@ -157,6 +159,21 @@ impl<'de> Deserialize<'de> for DiagnosticCategory {
157159
}
158160
}
159161

162+
impl Serialize for DiagnosticCategory {
163+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
164+
where
165+
S: Serializer,
166+
{
167+
let value = match self {
168+
DiagnosticCategory::Warning => 0 as i32,
169+
DiagnosticCategory::Error => 1 as i32,
170+
DiagnosticCategory::Suggestion => 2 as i32,
171+
DiagnosticCategory::Message => 3 as i32,
172+
};
173+
Serialize::serialize(&value, serializer)
174+
}
175+
}
176+
160177
impl From<i64> for DiagnosticCategory {
161178
fn from(value: i64) -> Self {
162179
match value {
@@ -169,7 +186,7 @@ impl From<i64> for DiagnosticCategory {
169186
}
170187
}
171188

172-
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
189+
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
173190
#[serde(rename_all = "camelCase")]
174191
pub struct DiagnosticMessageChain {
175192
message_text: String,
@@ -196,14 +213,14 @@ impl DiagnosticMessageChain {
196213
}
197214
}
198215

199-
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
216+
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
200217
#[serde(rename_all = "camelCase")]
201218
pub struct Position {
202219
pub line: u64,
203220
pub character: u64,
204221
}
205222

206-
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
223+
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
207224
#[serde(rename_all = "camelCase")]
208225
pub struct Diagnostic {
209226
pub category: DiagnosticCategory,
@@ -367,6 +384,15 @@ impl<'de> Deserialize<'de> for Diagnostics {
367384
}
368385
}
369386

387+
impl Serialize for Diagnostics {
388+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
389+
where
390+
S: Serializer,
391+
{
392+
Serialize::serialize(&self.0, serializer)
393+
}
394+
}
395+
370396
impl fmt::Display for Diagnostics {
371397
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
372398
let mut i = 0;

cli/dts/lib.deno.unstable.d.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,6 @@ declare namespace Deno {
303303
/** Provide full support for iterables in `for..of`, spread and
304304
* destructuring when targeting ES5 or ES3. Defaults to `false`. */
305305
downlevelIteration?: boolean;
306-
/** Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files.
307-
* Defaults to `false`. */
308-
emitBOM?: boolean;
309306
/** Only emit `.d.ts` declaration files. Defaults to `false`. */
310307
emitDeclarationOnly?: boolean;
311308
/** Emit design-type metadata for decorated declarations in source. See issue
@@ -316,29 +313,32 @@ declare namespace Deno {
316313
* ecosystem compatibility and enable `allowSyntheticDefaultImports` for type
317314
* system compatibility. Defaults to `true`. */
318315
esModuleInterop?: boolean;
319-
/** Enables experimental support for ES decorators. Defaults to `false`. */
316+
/** Enables experimental support for ES decorators. Defaults to `true`. */
320317
experimentalDecorators?: boolean;
318+
/** Import emit helpers (e.g. `__extends`, `__rest`, etc..) from
319+
* [tslib](https://www.npmjs.com/package/tslib). */
320+
importHelpers?: boolean;
321321
/** Emit a single file with source maps instead of having a separate file.
322322
* Defaults to `false`. */
323323
inlineSourceMap?: boolean;
324324
/** Emit the source alongside the source maps within a single file; requires
325325
* `inlineSourceMap` or `sourceMap` to be set. Defaults to `false`. */
326326
inlineSources?: boolean;
327327
/** Perform additional checks to ensure that transpile only would be safe.
328-
* Defaults to `false`. */
328+
* Defaults to `true`. */
329329
isolatedModules?: boolean;
330330
/** Support JSX in `.tsx` files: `"react"`, `"preserve"`, `"react-native"`.
331331
* Defaults to `"react"`. */
332332
jsx?: "react" | "preserve" | "react-native";
333333
/** Specify the JSX factory function to use when targeting react JSX emit,
334334
* e.g. `React.createElement` or `h`. Defaults to `React.createElement`. */
335335
jsxFactory?: string;
336+
/** Specify the JSX fragment factory function to use when targeting react
337+
* JSX emit, e.g. `Fragment`. Defaults to `React.Fragment`. */
338+
jsxFragmentFactory?: string;
336339
/** Resolve keyof to string valued property names only (no numbers or
337340
* symbols). Defaults to `false`. */
338341
keyofStringsOnly?: string;
339-
/** Emit class fields with ECMAScript-standard semantics. Defaults to `false`.
340-
*/
341-
useDefineForClassFields?: boolean;
342342
/** List of library files to be included in the compilation. If omitted,
343343
* then the Deno main runtime libs are used. */
344344
lib?: string[];
@@ -389,10 +389,6 @@ declare namespace Deno {
389389
noUnusedLocals?: boolean;
390390
/** Report errors on unused parameters. Defaults to `false`. */
391391
noUnusedParameters?: boolean;
392-
/** Redirect output structure to the directory. This only impacts
393-
* `Deno.compile` and only changes the emitted file names. Defaults to
394-
* `undefined`. */
395-
outDir?: string;
396392
/** List of path mapping entries for module names to locations relative to the
397393
* `baseUrl`. Defaults to `undefined`. */
398394
paths?: Record<string, string[]>;
@@ -402,8 +398,6 @@ declare namespace Deno {
402398
/** Remove all comments except copy-right header comments beginning with
403399
* `/*!`. Defaults to `true`. */
404400
removeComments?: boolean;
405-
/** Include modules imported with `.json` extension. Defaults to `true`. */
406-
resolveJsonModule?: boolean;
407401
/** Specifies the root directory of input files. Only use to control the
408402
* output directory structure with `outDir`. Defaults to `undefined`. */
409403
rootDir?: string;
@@ -418,6 +412,8 @@ declare namespace Deno {
418412
* specified will be embedded in the sourceMap to direct the debugger where
419413
* the source files will be located. Defaults to `undefined`. */
420414
sourceRoot?: string;
415+
/** Skip type checking of all declaration files (`*.d.ts`). */
416+
skipLibCheck?: boolean;
421417
/** Enable all strict type checking options. Enabling `strict` enables
422418
* `noImplicitAny`, `noImplicitThis`, `alwaysStrict`, `strictBindCallApply`,
423419
* `strictNullChecks`, `strictFunctionTypes` and
@@ -472,6 +468,9 @@ declare namespace Deno {
472468
* ```
473469
*/
474470
types?: string[];
471+
/** Emit class fields with ECMAScript-standard semantics. Defaults to
472+
* `false`. */
473+
useDefineForClassFields?: boolean;
475474
}
476475

477476
/** **UNSTABLE**: new API, yet to be vetted.

cli/main.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ mod lint;
3535
mod lockfile;
3636
mod media_type;
3737
mod metrics;
38-
mod module_graph;
3938
mod module_graph2;
4039
mod module_loader;
4140
mod op_fetch_asset;
@@ -50,7 +49,6 @@ mod specifier_handler;
5049
mod test_runner;
5150
mod text_encoding;
5251
mod tokio_util;
53-
mod tsc;
5452
mod tsc2;
5553
mod tsc_config;
5654
mod upgrade;
@@ -242,14 +240,19 @@ async fn cache_command(
242240
flags: Flags,
243241
files: Vec<String>,
244242
) -> Result<(), AnyError> {
243+
let lib = if flags.unstable {
244+
module_graph2::TypeLib::UnstableDenoWindow
245+
} else {
246+
module_graph2::TypeLib::DenoWindow
247+
};
245248
let program_state = ProgramState::new(flags)?;
246249

247250
for file in files {
248251
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
249252
program_state
250253
.prepare_module_load(
251254
specifier,
252-
tsc::TargetLib::Main,
255+
lib.clone(),
253256
Permissions::allow_all(),
254257
false,
255258
program_state.maybe_import_map.clone(),
@@ -343,21 +346,20 @@ async fn bundle_command(
343346
module_graph2::TypeLib::DenoWindow
344347
};
345348
let graph = graph.clone();
346-
let (stats, diagnostics, maybe_ignored_options) =
347-
graph.check(module_graph2::CheckOptions {
348-
debug,
349-
emit: false,
350-
lib,
351-
maybe_config_path: flags.config_path.clone(),
352-
reload: flags.reload,
353-
})?;
354-
355-
debug!("{}", stats);
356-
if let Some(ignored_options) = maybe_ignored_options {
349+
let result_info = graph.check(module_graph2::CheckOptions {
350+
debug,
351+
emit: false,
352+
lib,
353+
maybe_config_path: flags.config_path.clone(),
354+
reload: flags.reload,
355+
})?;
356+
357+
debug!("{}", result_info.stats);
358+
if let Some(ignored_options) = result_info.maybe_ignored_options {
357359
eprintln!("{}", ignored_options);
358360
}
359-
if !diagnostics.is_empty() {
360-
return Err(generic_error(diagnostics.to_string()));
361+
if !result_info.diagnostics.is_empty() {
362+
return Err(generic_error(result_info.diagnostics.to_string()));
361363
}
362364
}
363365

cli/media_type.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
22

3+
use deno_core::ModuleSpecifier;
34
use serde::Serialize;
45
use serde::Serializer;
56
use std::fmt;
@@ -60,6 +61,22 @@ impl<'a> From<&'a String> for MediaType {
6061
}
6162
}
6263

64+
impl<'a> From<&'a ModuleSpecifier> for MediaType {
65+
fn from(specifier: &'a ModuleSpecifier) -> Self {
66+
let url = specifier.as_url();
67+
let path = if url.scheme() == "file" {
68+
if let Ok(path) = url.to_file_path() {
69+
path
70+
} else {
71+
PathBuf::from(url.path())
72+
}
73+
} else {
74+
PathBuf::from(url.path())
75+
};
76+
MediaType::from_path(&path)
77+
}
78+
}
79+
6380
impl Default for MediaType {
6481
fn default() -> Self {
6582
MediaType::Unknown

0 commit comments

Comments
 (0)