mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
parent
a6beab8248
commit
7e9028b532
4 changed files with 56 additions and 12 deletions
6
cli/dts/lib.deno.unstable.d.ts
vendored
6
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -498,8 +498,10 @@ declare namespace Deno {
|
||||||
|
|
||||||
interface EmitOptions {
|
interface EmitOptions {
|
||||||
/** Indicate that the source code should be emitted to a single file
|
/** Indicate that the source code should be emitted to a single file
|
||||||
* JavaScript bundle that is an ES module (`"esm"`). */
|
* JavaScript bundle that is a single ES module (`"esm"`) or a single file
|
||||||
bundle?: "esm";
|
* self contained script we executes in an immediately invoked function
|
||||||
|
* when loaded (`"iife"`). */
|
||||||
|
bundle?: "esm" | "iife";
|
||||||
/** If `true` then the sources will be typed checked, returning any
|
/** If `true` then the sources will be typed checked, returning any
|
||||||
* diagnostic errors in the result. If `false` type checking will be
|
* diagnostic errors in the result. If `false` type checking will be
|
||||||
* skipped. Defaults to `true`.
|
* skipped. Defaults to `true`.
|
||||||
|
|
|
@ -614,8 +614,10 @@ pub enum BundleType {
|
||||||
/// Return the emitted contents of the program as a single "flattened" ES
|
/// Return the emitted contents of the program as a single "flattened" ES
|
||||||
/// module.
|
/// module.
|
||||||
Esm,
|
Esm,
|
||||||
// TODO(@kitsonk) once available in swc
|
/// Return the emitted contents of the program as a single script that
|
||||||
// Iife,
|
/// executes the program using an immediately invoked function execution
|
||||||
|
/// (IIFE).
|
||||||
|
Iife,
|
||||||
/// Do not bundle the emit, instead returning each of the modules that are
|
/// Do not bundle the emit, instead returning each of the modules that are
|
||||||
/// part of the program as individual files.
|
/// part of the program as individual files.
|
||||||
None,
|
None,
|
||||||
|
@ -780,7 +782,8 @@ impl Graph {
|
||||||
let maybe_ignored_options =
|
let maybe_ignored_options =
|
||||||
ts_config.merge_tsconfig(options.maybe_config_path)?;
|
ts_config.merge_tsconfig(options.maybe_config_path)?;
|
||||||
|
|
||||||
let s = self.emit_bundle(&root_specifier, &ts_config.into())?;
|
let s =
|
||||||
|
self.emit_bundle(&root_specifier, &ts_config.into(), &BundleType::Esm)?;
|
||||||
let stats = Stats(vec![
|
let stats = Stats(vec![
|
||||||
("Files".to_string(), self.modules.len() as u32),
|
("Files".to_string(), self.modules.len() as u32),
|
||||||
("Total time".to_string(), start.elapsed().as_millis() as u32),
|
("Total time".to_string(), start.elapsed().as_millis() as u32),
|
||||||
|
@ -951,7 +954,7 @@ impl Graph {
|
||||||
"target": "esnext",
|
"target": "esnext",
|
||||||
}));
|
}));
|
||||||
let opts = match options.bundle_type {
|
let opts = match options.bundle_type {
|
||||||
BundleType::Esm => json!({
|
BundleType::Esm | BundleType::Iife => json!({
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
}),
|
}),
|
||||||
BundleType::None => json!({
|
BundleType::None => json!({
|
||||||
|
@ -992,7 +995,7 @@ impl Graph {
|
||||||
|
|
||||||
let graph = graph.lock().unwrap();
|
let graph = graph.lock().unwrap();
|
||||||
match options.bundle_type {
|
match options.bundle_type {
|
||||||
BundleType::Esm => {
|
BundleType::Esm | BundleType::Iife => {
|
||||||
assert!(
|
assert!(
|
||||||
response.emitted_files.is_empty(),
|
response.emitted_files.is_empty(),
|
||||||
"No files should have been emitted from tsc."
|
"No files should have been emitted from tsc."
|
||||||
|
@ -1003,7 +1006,11 @@ impl Graph {
|
||||||
"Only a single root module supported."
|
"Only a single root module supported."
|
||||||
);
|
);
|
||||||
let specifier = &graph.roots[0];
|
let specifier = &graph.roots[0];
|
||||||
let s = graph.emit_bundle(specifier, &config.into())?;
|
let s = graph.emit_bundle(
|
||||||
|
specifier,
|
||||||
|
&config.into(),
|
||||||
|
&options.bundle_type,
|
||||||
|
)?;
|
||||||
emitted_files.insert("deno:///bundle.js".to_string(), s);
|
emitted_files.insert("deno:///bundle.js".to_string(), s);
|
||||||
}
|
}
|
||||||
BundleType::None => {
|
BundleType::None => {
|
||||||
|
@ -1044,14 +1051,18 @@ impl Graph {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let mut emit_count = 0_u32;
|
let mut emit_count = 0_u32;
|
||||||
match options.bundle_type {
|
match options.bundle_type {
|
||||||
BundleType::Esm => {
|
BundleType::Esm | BundleType::Iife => {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
self.roots.len(),
|
self.roots.len(),
|
||||||
1,
|
1,
|
||||||
"Only a single root module supported."
|
"Only a single root module supported."
|
||||||
);
|
);
|
||||||
let specifier = &self.roots[0];
|
let specifier = &self.roots[0];
|
||||||
let s = self.emit_bundle(specifier, &config.into())?;
|
let s = self.emit_bundle(
|
||||||
|
specifier,
|
||||||
|
&config.into(),
|
||||||
|
&options.bundle_type,
|
||||||
|
)?;
|
||||||
emit_count += 1;
|
emit_count += 1;
|
||||||
emitted_files.insert("deno:///bundle.js".to_string(), s);
|
emitted_files.insert("deno:///bundle.js".to_string(), s);
|
||||||
}
|
}
|
||||||
|
@ -1104,6 +1115,7 @@ impl Graph {
|
||||||
&self,
|
&self,
|
||||||
specifier: &ModuleSpecifier,
|
specifier: &ModuleSpecifier,
|
||||||
emit_options: &ast::EmitOptions,
|
emit_options: &ast::EmitOptions,
|
||||||
|
bundle_type: &BundleType,
|
||||||
) -> Result<String, AnyError> {
|
) -> Result<String, AnyError> {
|
||||||
let cm = Rc::new(swc_common::SourceMap::new(
|
let cm = Rc::new(swc_common::SourceMap::new(
|
||||||
swc_common::FilePathMapping::empty(),
|
swc_common::FilePathMapping::empty(),
|
||||||
|
@ -1111,12 +1123,20 @@ impl Graph {
|
||||||
let globals = swc_common::Globals::new();
|
let globals = swc_common::Globals::new();
|
||||||
let loader = BundleLoader::new(self, emit_options, &globals, cm.clone());
|
let loader = BundleLoader::new(self, emit_options, &globals, cm.clone());
|
||||||
let hook = Box::new(BundleHook);
|
let hook = Box::new(BundleHook);
|
||||||
|
let module = match bundle_type {
|
||||||
|
BundleType::Esm => swc_bundler::ModuleType::Es,
|
||||||
|
BundleType::Iife => swc_bundler::ModuleType::Iife,
|
||||||
|
_ => unreachable!("invalid bundle type"),
|
||||||
|
};
|
||||||
let bundler = swc_bundler::Bundler::new(
|
let bundler = swc_bundler::Bundler::new(
|
||||||
&globals,
|
&globals,
|
||||||
cm.clone(),
|
cm.clone(),
|
||||||
loader,
|
loader,
|
||||||
self,
|
self,
|
||||||
swc_bundler::Config::default(),
|
swc_bundler::Config {
|
||||||
|
module,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
hook,
|
hook,
|
||||||
);
|
);
|
||||||
let mut entries = HashMap::new();
|
let mut entries = HashMap::new();
|
||||||
|
|
|
@ -35,6 +35,8 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||||
enum RuntimeBundleType {
|
enum RuntimeBundleType {
|
||||||
#[serde(rename = "esm")]
|
#[serde(rename = "esm")]
|
||||||
Esm,
|
Esm,
|
||||||
|
#[serde(rename = "iife")]
|
||||||
|
Iife,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
|
@ -106,7 +108,8 @@ async fn op_emit(
|
||||||
})?;
|
})?;
|
||||||
let bundle_type = match args.bundle {
|
let bundle_type = match args.bundle {
|
||||||
Some(RuntimeBundleType::Esm) => BundleType::Esm,
|
Some(RuntimeBundleType::Esm) => BundleType::Esm,
|
||||||
_ => BundleType::None,
|
Some(RuntimeBundleType::Iife) => BundleType::Iife,
|
||||||
|
None => BundleType::None,
|
||||||
};
|
};
|
||||||
let graph = builder.get_graph();
|
let graph = builder.get_graph();
|
||||||
let debug = program_state.flags.log_level == Some(log::Level::Debug);
|
let debug = program_state.flags.log_level == Some(log::Level::Debug);
|
||||||
|
|
|
@ -319,3 +319,22 @@ Deno.test({
|
||||||
assert(typeof files[`${specifier}.js.map`] === "string");
|
assert(typeof files[`${specifier}.js.map`] === "string");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: `Deno.emit() - bundle supports iife`,
|
||||||
|
async fn() {
|
||||||
|
const { diagnostics, files } = await Deno.emit("/a.ts", {
|
||||||
|
bundle: "iife",
|
||||||
|
sources: {
|
||||||
|
"/a.ts": `import { b } from "./b.ts";
|
||||||
|
console.log(b);`,
|
||||||
|
"/b.ts": `export const b = "b";`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
assert(diagnostics);
|
||||||
|
assertEquals(diagnostics.length, 0);
|
||||||
|
assertEquals(Object.keys(files).length, 1);
|
||||||
|
assert(files["deno:///bundle.js"].startsWith("(function() {\n"));
|
||||||
|
assert(files["deno:///bundle.js"].endsWith("})();\n"));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue