summaryrefslogtreecommitdiff
path: root/core/extensions.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-02-20 21:45:34 +0100
committerGitHub <noreply@github.com>2023-02-20 21:45:34 +0100
commit914b08fc19e5c7268e7b04a216337e765d6a06e8 (patch)
tree39f73f4e046346f358d74ff10f62cb2d5f7078a8 /core/extensions.rs
parent4d1a14ca7fa9496f36470a7771448a9b006b0204 (diff)
build: add "include_js_files_for_snapshotting" Cargo feature (#17826)
This allows to not include source code into the binary (because it will already be included in the V8 snapshot). Nothing changes for the embedders - everything should still build the same. This commit brings the binary size from 87Mb to 82Mb on M1. Alternative to https://github.com/denoland/deno/pull/17820 and https://github.com/denoland/deno/pull/17653 --------- Co-authored-by: Leo Kettmeir <crowlkats@toaxl.com>
Diffstat (limited to 'core/extensions.rs')
-rw-r--r--core/extensions.rs49
1 files changed, 47 insertions, 2 deletions
diff --git a/core/extensions.rs b/core/extensions.rs
index ab686d868..f84ab0a91 100644
--- a/core/extensions.rs
+++ b/core/extensions.rs
@@ -1,7 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::OpState;
+use anyhow::Context as _;
use anyhow::Error;
use std::cell::RefCell;
+use std::path::PathBuf;
use std::rc::Rc;
use std::task::Context;
use v8::fast_api::FastFunction;
@@ -13,8 +15,24 @@ pub enum ExtensionFileSourceCode {
/// will result in two copies of the source code being included - one in the
/// snapshot, the other the static string in the `Extension`.
IncludedInBinary(&'static str),
- // TODO(bartlomieju): add more variants that allow to read file from the disk,
- // and not include it in the binary.
+
+ // Source code is loaded from a file on disk. It's meant to be used if the
+ // embedder is creating snapshots. Files will be loaded from the filesystem
+ // during the build time and they will only be present in the V8 snapshot.
+ LoadedFromFsDuringSnapshot(PathBuf),
+}
+
+impl ExtensionFileSourceCode {
+ pub fn load(&self) -> Result<String, Error> {
+ match self {
+ ExtensionFileSourceCode::IncludedInBinary(code) => Ok(code.to_string()),
+ ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) => {
+ let msg = format!("Failed to read \"{}\"", path.display());
+ let code = std::fs::read_to_string(path).context(msg)?;
+ Ok(code)
+ }
+ }
+ }
}
#[derive(Clone, Debug)]
@@ -299,6 +317,7 @@ impl ExtensionBuilder {
/// - "internal:my_extension/js/01_hello.js"
/// - "internal:my_extension/js/02_goodbye.js"
/// ```
+#[cfg(not(feature = "include_js_files_for_snapshotting"))]
#[macro_export]
macro_rules! include_js_files {
(dir $dir:literal, $($file:literal,)+) => {
@@ -323,3 +342,29 @@ macro_rules! include_js_files {
]
};
}
+
+#[cfg(feature = "include_js_files_for_snapshotting")]
+#[macro_export]
+macro_rules! include_js_files {
+ (dir $dir:literal, $($file:literal,)+) => {
+ vec![
+ $($crate::ExtensionFileSource {
+ specifier: concat!($dir, "/", $file).to_string(),
+ code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
+ std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($dir).join($file)
+ ),
+ },)+
+ ]
+ };
+
+ ($($file:literal,)+) => {
+ vec![
+ $($crate::ExtensionFileSource {
+ specifier: $file.to_string(),
+ code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
+ std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($file)
+ ),
+ },)+
+ ]
+ };
+}