1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor(cli): rename Deno.emit() bundle options to "module" and "classic" (#10332)

This commit is contained in:
Nayeem Rahman 2021-04-25 21:54:57 +01:00 committed by GitHub
parent 83bece56b0
commit fb1ccc3d88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 43 deletions

View file

@ -441,10 +441,10 @@ declare namespace Deno {
*/
export interface EmitOptions {
/** Indicate that the source code should be emitted to a single file
* JavaScript bundle that is a single ES module (`"esm"`) or a single file
* self contained script we executes in an immediately invoked function
* when loaded (`"iife"`). */
bundle?: "esm" | "iife";
* JavaScript bundle that is a single ES module (`"module"`) or a single
* file self contained script we executes in an immediately invoked function
* when loaded (`"classic"`). */
bundle?: "module" | "classic";
/** If `true` then the sources will be typed checked, returning any
* diagnostic errors in the result. If `false` type checking will be
* skipped. Defaults to `true`.

View file

@ -606,11 +606,11 @@ pub struct CheckOptions {
pub enum BundleType {
/// Return the emitted contents of the program as a single "flattened" ES
/// module.
Esm,
Module,
/// Return the emitted contents of the program as a single script that
/// executes the program using an immediately invoked function execution
/// (IIFE).
Iife,
Classic,
/// Do not bundle the emit, instead returning each of the modules that are
/// part of the program as individual files.
None,
@ -776,8 +776,11 @@ impl Graph {
let maybe_ignored_options =
ts_config.merge_tsconfig(options.maybe_config_path)?;
let s =
self.emit_bundle(&root_specifier, &ts_config.into(), &BundleType::Esm)?;
let s = self.emit_bundle(
&root_specifier,
&ts_config.into(),
&BundleType::Module,
)?;
let stats = Stats(vec![
("Files".to_string(), self.modules.len() as u32),
("Total time".to_string(), start.elapsed().as_millis() as u32),
@ -952,7 +955,7 @@ impl Graph {
"useDefineForClassFields": true,
}));
let opts = match options.bundle_type {
BundleType::Esm | BundleType::Iife => json!({
BundleType::Module | BundleType::Classic => json!({
"noEmit": true,
}),
BundleType::None => json!({
@ -993,7 +996,7 @@ impl Graph {
let graph = graph.lock().unwrap();
match options.bundle_type {
BundleType::Esm | BundleType::Iife => {
BundleType::Module | BundleType::Classic => {
assert!(
response.emitted_files.is_empty(),
"No files should have been emitted from tsc."
@ -1049,7 +1052,7 @@ impl Graph {
let start = Instant::now();
let mut emit_count = 0_u32;
match options.bundle_type {
BundleType::Esm | BundleType::Iife => {
BundleType::Module | BundleType::Classic => {
assert_eq!(
self.roots.len(),
1,
@ -1122,8 +1125,8 @@ impl Graph {
let loader = BundleLoader::new(self, emit_options, &globals, cm.clone());
let hook = Box::new(BundleHook);
let module = match bundle_type {
BundleType::Esm => swc_bundler::ModuleType::Es,
BundleType::Iife => swc_bundler::ModuleType::Iife,
BundleType::Module => swc_bundler::ModuleType::Es,
BundleType::Classic => swc_bundler::ModuleType::Iife,
_ => unreachable!("invalid bundle type"),
};
let bundler = swc_bundler::Bundler::new(
@ -2381,7 +2384,7 @@ pub mod tests {
let (emitted_files, result_info) = graph
.emit(EmitOptions {
check: true,
bundle_type: BundleType::Esm,
bundle_type: BundleType::Module,
debug: false,
maybe_user_config: None,
})

View file

@ -33,10 +33,10 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
#[derive(Debug, Deserialize)]
enum RuntimeBundleType {
#[serde(rename = "esm")]
Esm,
#[serde(rename = "iife")]
Iife,
#[serde(rename = "module")]
Module,
#[serde(rename = "classic")]
Classic,
}
#[derive(Debug, Deserialize)]
@ -108,8 +108,8 @@ async fn op_emit(
))
})?;
let bundle_type = match args.bundle {
Some(RuntimeBundleType::Esm) => BundleType::Esm,
Some(RuntimeBundleType::Iife) => BundleType::Iife,
Some(RuntimeBundleType::Module) => BundleType::Module,
Some(RuntimeBundleType::Classic) => BundleType::Classic,
None => BundleType::None,
};
let graph = builder.get_graph();

View file

@ -173,12 +173,12 @@ Deno.test({
});
Deno.test({
name: "Deno.emit() - bundle esm - with sources",
name: "Deno.emit() - bundle as module script - with sources",
async fn() {
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
"/foo.ts",
{
bundle: "esm",
bundle: "module",
sources: {
"/foo.ts": `export * from "./bar.ts";\n`,
"/bar.ts": `export const bar = "bar";\n`,
@ -194,12 +194,12 @@ Deno.test({
});
Deno.test({
name: "Deno.emit() - bundle esm - no sources",
name: "Deno.emit() - bundle as module script - no sources",
async fn() {
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
"./subdir/mod1.ts",
{
bundle: "esm",
bundle: "module",
},
);
assertEquals(diagnostics.length, 0);
@ -211,12 +211,12 @@ Deno.test({
});
Deno.test({
name: "Deno.emit() - bundle esm - include js modules",
name: "Deno.emit() - bundle as module script - include js modules",
async fn() {
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
"/foo.js",
{
bundle: "esm",
bundle: "module",
sources: {
"/foo.js": `export * from "./bar.js";\n`,
"/bar.js": `export const bar = "bar";\n`,
@ -321,10 +321,10 @@ Deno.test({
});
Deno.test({
name: `Deno.emit() - bundle supports iife`,
name: `Deno.emit() - bundle as classic script iife`,
async fn() {
const { diagnostics, files } = await Deno.emit("/a.ts", {
bundle: "iife",
bundle: "classic",
sources: {
"/a.ts": `import { b } from "./b.ts";
console.log(b);`,

View file

@ -23,10 +23,10 @@ The emit options are defined in the `Deno` namespace as:
```ts
interface EmitOptions {
/** Indicate that the source code should be emitted to a single file
* JavaScript bundle that is a single ES module (`"esm"`) or a single file
* self contained script we executes in an immediately invoked function
* when loaded (`"iife"`). */
bundle?: "esm" | "iife";
* JavaScript bundle that is a single ES module (`"module"`) or a single
* file self contained script we executes in an immediately invoked function
* when loaded (`"classic"`). */
bundle?: "module" | "classic";
/** If `true` then the sources will be typed checked, returning any
* diagnostic errors in the result. If `false` type checking will be
* skipped. Defaults to `true`.
@ -181,13 +181,13 @@ if (diagnostics.length) {
### Bundling
`Deno.emit()` is also capable of providing output similar to `deno bundle` on
the command line. This is enabled by setting the _bundle_ option to `"esm"` or
`"iife"`. Currently Deno supports bundling as a single file ES module (`"esm"`)
or a single file self contained legacy script (`"iife"`).
the command line. This is enabled by setting the _bundle_ option to `"module"`
or `"classic"`. Currently Deno supports bundling as a single file ES module
(`"module"`) or a single file self contained legacy script (`"classic"`).
```ts
const { files, diagnostics } = await Deno.emit("./mod.ts", {
bundle: "esm",
bundle: "module",
});
```
@ -215,7 +215,7 @@ version of _lodash_ I am using with my project. I could do the following:
```ts
const { files } = await Deno.emit("mod.ts", {
bundle: "esm",
bundle: "module",
importMap: {
imports: {
"lodash": "https://deno.land/x/lodash",

View file

@ -17,7 +17,7 @@
/**
* @typedef {object} OpEmitRequest
* @property {"esm"=} bundle
* @property {"module" | "classic"=} bundle
* @property {boolean=} check
* @property {Record<string, any>=} compilerOptions
* @property {ImportMap=} importMap
@ -35,7 +35,7 @@
*/
/**
* @param {OpEmitRequest} request
* @param {OpEmitRequest} request
* @returns {Promise<OpEmitResponse>}
*/
function opEmit(request) {
@ -43,7 +43,7 @@
}
/**
* @param {string} specifier
* @param {string} specifier
* @returns {string}
*/
function checkRelative(specifier) {
@ -54,7 +54,7 @@
/**
* @typedef {object} EmitOptions
* @property {"esm"=} bundle
* @property {"module" | "classic"=} bundle
* @property {boolean=} check
* @property {Record<string, any>=} compilerOptions
* @property {ImportMap=} importMap
@ -63,9 +63,9 @@
*/
/**
* @param {string | URL} rootSpecifier
* @param {string | URL} rootSpecifier
* @param {EmitOptions=} options
* @returns {Promise<OpEmitResponse>}
* @returns {Promise<OpEmitResponse>}
*/
function emit(rootSpecifier, options = {}) {
util.log(`Deno.emit`, { rootSpecifier });