Skip to content

Commit

Permalink
Merge branch 'main' into feat-create-react-ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost authored Apr 21, 2024
2 parents a250e63 + 3581ee5 commit 6b5da76
Show file tree
Hide file tree
Showing 43 changed files with 681 additions and 206 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-goats-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': minor
---

feat: disable polyfill when entry is not html
5 changes: 5 additions & 0 deletions .changeset/nice-carpets-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': patch
---

Support lazy compilation when targeting node
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/compiler/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use farmfe_core::{
serde_json::json,
};

use farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_PREFIX;
use farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_SUFFIX;
use farmfe_toolkit::resolve::load_package_json;
use farmfe_utils::stringify_query;

Expand Down Expand Up @@ -634,7 +634,7 @@ fn resolve_module(
resolve_module_id_result.module_id.clone(),
resolve_module_id_result.resolve_result.external,
// treat all lazy virtual modules as mutable
!module_id_str.starts_with(DYNAMIC_VIRTUAL_PREFIX)
!module_id_str.ends_with(DYNAMIC_VIRTUAL_SUFFIX)
&& context
.config
.partial_bundling
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use farmfe_core::{
};

pub use farmfe_plugin_css::FARM_CSS_MODULES_SUFFIX;
pub use farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_PREFIX;
pub use farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_SUFFIX;
pub use farmfe_plugin_runtime::RUNTIME_SUFFIX;

pub mod build;
Expand Down
2 changes: 2 additions & 0 deletions crates/compiler/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Compiler {
where
F: FnOnce() + Send + Sync + 'static,
{
// mark the compilation as update
self.context.set_update();
let (err_sender, err_receiver) = Self::create_thread_channel();
let update_context = Arc::new(UpdateContext::new());

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/update/regenerate_resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn render_and_generate_update_resource(

let is_lazy = updated_module_ids.iter().any(|id| {
id.to_string()
.starts_with(farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_PREFIX)
.ends_with(farmfe_plugin_lazy_compilation::DYNAMIC_VIRTUAL_SUFFIX)
});
let cached_enabled = context.config.persistent_cache.enabled() && is_lazy;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
({"dep.ts":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
var _default = "dep";
}
,
"dep.ts.farm_dynamic_import_virtual_module":function (module, exports, farmRequire, farmDynamicRequire) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return _default;
}
});
var _export_star = farmRequire("@swc/helpers/_/_export_star");
var _interop_require_default = farmRequire("@swc/helpers/_/_interop_require_default");
var _depts = _interop_require_default._(_export_star._(farmRequire("dep.ts"), exports));
var _default = _depts.default;
}
,})
{}
56 changes: 51 additions & 5 deletions crates/compiler/tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use std::io::{Read, Write};
use std::path::PathBuf;

use common::generate_runtime;
use farmfe_compiler::Compiler;
use farmfe_compiler::{Compiler, DYNAMIC_VIRTUAL_SUFFIX};
use farmfe_core::config::bool_or_obj::BoolOrObj;
use farmfe_core::config::config_regex::ConfigRegex;
use farmfe_core::config::persistent_cache::PersistentCacheConfig;
use farmfe_core::config::TargetEnv;
use farmfe_core::config::{preset_env::PresetEnvConfig, Config, Mode, SourcemapConfig};
use farmfe_core::plugin::UpdateType;
use farmfe_testing_helpers::fixture;
Expand All @@ -20,6 +21,7 @@ fn create_compiler_internal(
crate_path: PathBuf,
minify: bool,
lazy_compilation: bool,
target_env: TargetEnv,
) -> Compiler {
let compiler = Compiler::new(
Config {
Expand All @@ -28,6 +30,7 @@ fn create_compiler_internal(
runtime: generate_runtime(crate_path),
output: farmfe_core::config::OutputConfig {
filename: "[resourceName].[ext]".to_string(),
target_env,
..Default::default()
},
mode: Mode::Development,
Expand Down Expand Up @@ -56,16 +59,17 @@ fn create_update_compiler(
crate_path: PathBuf,
minify: bool,
) -> Compiler {
create_compiler_internal(input, cwd, crate_path, minify, false)
create_compiler_internal(input, cwd, crate_path, minify, false, TargetEnv::Browser)
}

fn create_lazy_update_compiler(
input: HashMap<String, String>,
cwd: PathBuf,
crate_path: PathBuf,
minify: bool,
target_env: TargetEnv,
) -> Compiler {
create_compiler_internal(input, cwd, crate_path, minify, true)
create_compiler_internal(input, cwd, crate_path, minify, true, target_env)
}

fn asset_update_result_code(
Expand Down Expand Up @@ -343,12 +347,13 @@ fn update_lazy_compilation() {
cwd.clone(),
crate_path,
false,
TargetEnv::Browser,
);

compiler.compile().unwrap();

let update_file = cwd.join("dep.ts").to_string_lossy().to_string();
let update_module_id = format!("virtual:FARMFE_DYNAMIC_IMPORT:{}", update_file);
let update_module_id = format!("{}{}", update_file, DYNAMIC_VIRTUAL_SUFFIX);
let result = compiler
.update(
vec![(update_module_id.clone(), UpdateType::Updated)],
Expand All @@ -358,8 +363,49 @@ fn update_lazy_compilation() {
.unwrap();

assert_eq!(result.added_module_ids, vec!["dep.ts".into()]);
assert_eq!(result.updated_module_ids, vec![update_module_id.into()]);
assert_eq!(
result.updated_module_ids,
vec![format!("dep.ts{}", DYNAMIC_VIRTUAL_SUFFIX).into()]
);
assert_eq!(result.removed_module_ids.len(), 0);
}
);
}

#[test]
fn update_lazy_compilation_node() {
fixture!(
"tests/fixtures/update/lazy-compilation/index.ts",
|file, crate_path| {
let cwd = file.parent().unwrap().to_path_buf();
let compiler = create_lazy_update_compiler(
HashMap::from([("index".to_string(), "./index.ts".to_string())]),
cwd.clone(),
crate_path,
false,
TargetEnv::Node,
);

compiler.compile().unwrap();

let update_file = cwd.join("dep.ts").to_string_lossy().to_string();
let update_module_id = format!("{}{}", update_file, DYNAMIC_VIRTUAL_SUFFIX);
let result = compiler
.update(
vec![(update_module_id.clone(), UpdateType::Updated)],
|| {},
true,
)
.unwrap();

assert_eq!(result.added_module_ids, vec!["dep.ts".into()]);
assert_eq!(
result.updated_module_ids,
vec![format!("dep.ts{}", DYNAMIC_VIRTUAL_SUFFIX).into()]
);
assert_eq!(result.removed_module_ids.len(), 0);

asset_update_result_code(cwd, &result, Some("update0"));
}
);
}
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ puffin = { version = "0.18.0", features = [
"serialization",
], optional = true }
regex = "1.7.3"
enhanced-magic-string = { version = "0.0.12" }
enhanced-magic-string = { version = "0.0.13" }

[features]
profile = ["dep:puffin"]
2 changes: 1 addition & 1 deletion crates/core/src/cache/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use crate::config::Mode;

const FARM_CACHE_VERSION: &str = "0.3.3";
const FARM_CACHE_VERSION: &str = "0.4.0";
const FARM_CACHE_MANIFEST_FILE: &str = "farm-cache.json";

// TODO make CacheStore a trait and implement DiskCacheStore or RemoteCacheStore or more.
Expand Down
9 changes: 9 additions & 0 deletions crates/core/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use self::log_store::LogStore;

pub mod log_store;
pub(crate) const EMPTY_STR: &str = "";
pub const IS_UPDATE: &str = "";

/// Shared context through the whole compilation.
pub struct CompilationContext {
Expand Down Expand Up @@ -65,6 +66,14 @@ impl CompilationContext {
})
}

pub fn set_update(&self) {
self.custom.insert(IS_UPDATE.to_string(), Box::new(true));
}

pub fn is_update(&self) -> bool {
self.custom.contains_key(IS_UPDATE)
}

pub fn create_plugin_driver(plugins: Vec<Arc<dyn Plugin>>, record: bool) -> PluginDriver {
PluginDriver::new(plugins, record)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/node/src/plugin_adapters/js_plugin_adapter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::sync::Arc;

use farmfe_compiler::{DYNAMIC_VIRTUAL_PREFIX, FARM_CSS_MODULES_SUFFIX, RUNTIME_SUFFIX};
use farmfe_compiler::{DYNAMIC_VIRTUAL_SUFFIX, FARM_CSS_MODULES_SUFFIX, RUNTIME_SUFFIX};
use farmfe_core::{
context::CompilationContext,
error::{CompilationError, Result},
Expand Down Expand Up @@ -118,7 +118,7 @@ impl JsPluginAdapter {
}

pub fn is_internal_virtual_module(&self, path: &str) -> bool {
path.starts_with(DYNAMIC_VIRTUAL_PREFIX)
path.ends_with(DYNAMIC_VIRTUAL_SUFFIX)
|| FARM_CSS_MODULES_SUFFIX.is_match(path)
|| path.ends_with(RUNTIME_SUFFIX)
}
Expand Down
61 changes: 49 additions & 12 deletions crates/plugin_lazy_compilation/src/dynamic_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ interface RawLazyCompileResult {
dynamicResourcesMap: Record<string, any>;
}


const FarmModuleSystem: any = 'FARM_MODULE_SYSTEM';
const moduleId = `MODULE_ID`;
const modulePath = `MODULE_PATH`;
const serverUrl = 'FARM_NODE_LAZY_COMPILE_SERVER_URL';

/**
* If the serverUrl is 'FARM_NODE_LAZY_COMPILE_SERVER_URL', it means the serverUrl is not set and it's not node lazy compile, and we should think it's a browser lazy compile.
* FARM_NODE_LAZY_COMPILE_SERVER_URL will be replaced by the real server url during the build process when node lazy compile is enabled.
*/
const isNodeLazyCompile =
serverUrl !== 'FARM_NODE_LAZY' + '_COMPILE_SERVER_URL';

const debouncePageReload = function(ms: number) {
const debouncePageReload = function (ms: number) {
let timer;

return function () {
Expand All @@ -23,6 +30,27 @@ const debouncePageReload = function(ms: number) {
};
const pageReload = debouncePageReload(500);

async function fetch(path: string) {
const url = `${serverUrl}${path}`;
const http = 'http';
return import(http).then((http) => {
return new Promise((resolve, reject) => {
http.get(url, (res: any) => {
let data = '';
res.on('data', (chunk: any) => {
data += chunk;
});
res.on('end', () => {
resolve(JSON.parse(data));
});
res.on('error', (err: any) => {
reject(err);
});
});
});
});
}

const lazyCompilationRuntimePlugin = {
name: 'farm-lazy-compilation',
moduleNotFound: () => {
Expand Down Expand Up @@ -68,7 +96,7 @@ if (compilingModules.has(modulePath)) {
modulePath,
moduleId,
resolve,
promise,
promise
]);
} else {
promise = queueItem[2];
Expand All @@ -79,18 +107,25 @@ if (compilingModules.has(modulePath)) {
const queue = [...FarmModuleSystem.lazyCompilingQueue];
FarmModuleSystem.lazyCompilingQueue = [];

const url = '/__lazy_compile?paths=' + paths.join(',') + `&t=${Date.now()}`;
const url = `/__lazy_compile?paths=${paths.join(',')}&t=${Date.now()}${
isNodeLazyCompile ? '&node=true' : ''
}`;

const fetchLazyCompileResult = !isNodeLazyCompile
? import(url)
: fetch(url);

promise = fetchLazyCompileResult.then((module: any) => {
const result: RawLazyCompileResult = module.default || module;

promise = import(url).then((module: any) => {
const result: RawLazyCompileResult = module.default;

if (result.dynamicResourcesMap) {
FarmModuleSystem.dynamicModuleResourcesMap = result.dynamicResourcesMap;
FarmModuleSystem.dynamicModuleResourcesMap =
result.dynamicResourcesMap;
}

const mutableModules = eval(result.mutableModules);
const immutableModules = eval(result.immutableModules);

const modules = { ...mutableModules, ...immutableModules };

for (const moduleId in modules) {
Expand All @@ -114,10 +149,12 @@ if (compilingModules.has(modulePath)) {
}

// fix #878
FarmModuleSystem.addPlugin(lazyCompilationRuntimePlugin);
!isNodeLazyCompile &&
FarmModuleSystem.addPlugin(lazyCompilationRuntimePlugin);
// The lazy compiled module should not contains side effects, as it may be executed twice
const exports = FarmModuleSystem.require(moduleId);
FarmModuleSystem.removePlugin(lazyCompilationRuntimePlugin.name);
!isNodeLazyCompile &&
FarmModuleSystem.removePlugin(lazyCompilationRuntimePlugin.name);

return exports;
});
Expand Down
Loading

0 comments on commit 6b5da76

Please sign in to comment.