Skip to content

Commit

Permalink
feat: support obj external & dts support resolvedPaths (#1282)
Browse files Browse the repository at this point in the history
* feat: support obj external & dts support resolvedPaths

* fix: test case

* fix: dts file ext

* fix: window path match

* chore: remove print

* fix: avoid break change

* feat: clean code

---------

Co-authored-by: brightwu <[email protected]>
  • Loading branch information
shulandmimi and wre232114 committed May 9, 2024
1 parent 9703b9d commit df7ac2a
Show file tree
Hide file tree
Showing 57 changed files with 1,338 additions and 354 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-turtles-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/js-plugin-dts': patch
---

support custom resolvedPaths & reduce product size
5 changes: 5 additions & 0 deletions .changeset/silly-chairs-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': patch
---

support record external
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"editor.tabSize": 2,
"editor.inlineSuggest.showToolbar": "always"
"editor.inlineSuggest.showToolbar": "always",
"biome.enabled": true,
"prettier.enable": false,
"editor.defaultFormatter": "biomejs.biome",
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome",
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome",
},
}
20 changes: 20 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ farmfe_plugin_polyfill = { path = "../plugin_polyfill", version = "0.0.6" }
farmfe_plugin_progress = { path = "../plugin_progress", version = "0.0.6" }
farmfe_plugin_define = { path = "../plugin_define", version = "0.0.6" }
num_cpus = "1.16.0"
farmfe_testing = { path = "../macro_testing" }

[features]
profile = [
Expand Down
34 changes: 27 additions & 7 deletions crates/compiler/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use std::{collections::HashMap, path::PathBuf, sync::Arc};
use farmfe_compiler::Compiler;
use farmfe_core::{
config::{
bool_or_obj::BoolOrObj, config_regex::ConfigRegex, persistent_cache::PersistentCacheConfig,
preset_env::PresetEnvConfig, Config, CssConfig, Mode, RuntimeConfig, SourcemapConfig,
bool_or_obj::BoolOrObj,
config_regex::ConfigRegex,
external::{ExternalConfig, ExternalConfigItem},
persistent_cache::PersistentCacheConfig,
preset_env::PresetEnvConfig,
Config, CssConfig, Mode, RuntimeConfig, SourcemapConfig,
},
plugin::Plugin,
resource::ResourceType,
};

pub fn generate_runtime(crate_path: PathBuf) -> RuntimeConfig {
Expand Down Expand Up @@ -35,6 +38,7 @@ pub fn generate_runtime(crate_path: PathBuf) -> RuntimeConfig {
}
}

#[allow(dead_code)]
pub fn create_css_compiler(
input: HashMap<String, String>,
cwd: PathBuf,
Expand Down Expand Up @@ -79,7 +83,7 @@ pub fn create_config(cwd: PathBuf, crate_path: PathBuf) -> Config {
runtime: generate_runtime(crate_path),
output: farmfe_core::config::OutputConfig::default(),
mode: Mode::Production,
external: vec![],
external: Default::default(),
sourcemap: SourcemapConfig::Bool(false),
lazy_compilation: false,
progress: false,
Expand All @@ -90,10 +94,24 @@ pub fn create_config(cwd: PathBuf, crate_path: PathBuf) -> Config {
}
}

#[allow(dead_code)]
pub fn create_compiler_with_args<F>(cwd: PathBuf, crate_path: PathBuf, handle: F) -> Compiler
where
F: FnOnce(Config, Vec<Arc<dyn Plugin>>) -> (Config, Vec<Arc<dyn Plugin>>),
{
let config = create_config(cwd, crate_path);

let plguins = vec![];

let (config, plugins) = handle(config, plguins);
Compiler::new(config, plugins).expect("faile to create compiler")
}

#[allow(dead_code)]
pub fn create_with_compiler(config: Config, plugin_adapters: Vec<Arc<dyn Plugin>>) -> Compiler {
Compiler::new(config, plugin_adapters).expect("faile to create compiler")
}

#[allow(dead_code)]
pub fn create_compiler(
input: HashMap<String, String>,
cwd: PathBuf,
Expand Down Expand Up @@ -128,7 +146,7 @@ pub fn create_compiler(

compiler
}

#[allow(dead_code)]
pub fn create_compiler_with_plugins(
input: HashMap<String, String>,
cwd: PathBuf,
Expand Down Expand Up @@ -180,7 +198,7 @@ pub fn create_compiler_with_plugins(

compiler
}

#[allow(dead_code)]
pub fn get_compiler_result(compiler: &Compiler, entry_name: Option<&String>) -> String {
let resources_map = compiler.context().resources_map.lock();
let mut result = vec![];
Expand Down Expand Up @@ -215,10 +233,12 @@ pub fn get_compiler_result(compiler: &Compiler, entry_name: Option<&String>) ->
result_file_str
}

#[allow(dead_code)]
pub fn load_expected_result(cwd: PathBuf) -> String {
std::fs::read_to_string(cwd.join("output.js")).unwrap_or("".to_string())
}

#[allow(dead_code)]
pub fn assert_compiler_result(compiler: &Compiler, entry_name: Option<&String>) {
let expected_result = load_expected_result(PathBuf::from(compiler.context().config.root.clone()));
let result = get_compiler_result(compiler, entry_name);
Expand Down
63 changes: 63 additions & 0 deletions crates/compiler/tests/external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
mod common;
use crate::common::{assert_compiler_result, create_compiler_with_args};
use std::{collections::HashMap, path::PathBuf};

use farmfe_core::config::{
config_regex::ConfigRegex, custom::CUSTOM_CONFIG_EXTERNAL_RECORD, ModuleFormat, TargetEnv,
};

fn test(file: String, crate_path: String) {
let file_path_buf = PathBuf::from(file.clone());
let create_path_buf = PathBuf::from(crate_path);
let cwd = file_path_buf.parent().unwrap();
println!("testing test case: {:?}", cwd);

let entry_name = "index".to_string();
let normolized_file = file.replace('\\', "/");
let compiler =
create_compiler_with_args(cwd.to_path_buf(), create_path_buf, |mut config, plugins| {
config.input = HashMap::from_iter(vec![(entry_name.clone(), file.clone())]);

if normolized_file.contains("/browser/") || normolized_file.contains("/node/") {
config.output.target_env = if normolized_file.contains("browser") {
TargetEnv::Browser
} else {
TargetEnv::Node
};
}

if normolized_file.contains("/normal/") || normolized_file.contains("/object/") || true {
if normolized_file.contains("/object") {
config
.custom
.entry(CUSTOM_CONFIG_EXTERNAL_RECORD.to_string())
.or_insert(
r#"
{
"jquery": "$"
}
"#
.to_string(),
);
} else {
config.external = vec![ConfigRegex::new("^jquery$")];
}
}

if normolized_file.contains("/cjs/") || normolized_file.contains("/esm/") {
config.output.format = if normolized_file.contains("cjs") {
ModuleFormat::CommonJs
} else {
ModuleFormat::EsModule
};
}

(config, plugins)
});

compiler.compile().unwrap();

assert_compiler_result(&compiler, Some(&entry_name));
}

farmfe_testing::testing! {"tests/fixtures/external/**/*.ts", test}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import $ from 'jquery';

console.log($.find);

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import $ from 'jquery';

console.log($.find);

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import $ from 'jquery';

console.log($.find);

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import $ from 'jquery';

console.log($.find);

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import $ from 'jquery';

console.log($.find);
14 changes: 14 additions & 0 deletions crates/compiler/tests/fixtures/external/node/normala/cjs/output.js

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import $ from 'jquery';

console.log($.find);
14 changes: 14 additions & 0 deletions crates/compiler/tests/fixtures/external/node/normala/esm/output.js

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

36 changes: 35 additions & 1 deletion crates/core/src/config/custom.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use crate::context::CompilationContext;

use super::{
config_regex::ConfigRegex,
external::{ExternalConfig, ExternalObject},
Config,
};

const CUSTOM_CONFIG_RUNTIME_ISOLATE: &str = "runtime.isolate";
pub const CUSTOM_CONFIG_EXTERNAL_RECORD: &str = "external.record";

pub fn get_config_runtime_isolate(context: &Arc<CompilationContext>) -> bool {
if let Some(val) = context.config.custom.get(CUSTOM_CONFIG_RUNTIME_ISOLATE) {
Expand All @@ -11,3 +18,30 @@ pub fn get_config_runtime_isolate(context: &Arc<CompilationContext>) -> bool {
false
}
}

pub fn get_config_external_record(config: &Config) -> ExternalConfig {
if let Some(val) = config.custom.get(CUSTOM_CONFIG_EXTERNAL_RECORD) {
if val.is_empty() {
return ExternalConfig::new();
}

let external: HashMap<String, String> = serde_json::from_str(val)
.unwrap_or_else(|_| panic!("failed parse record external {:?}", val));

let mut external_config = ExternalConfig::new();

for (regex, name) in external {
external_config
.0
.push(super::external::ExternalConfigItem::Object(
ExternalObject {
pattern: ConfigRegex::new(&regex),
global_name: name,
},
));
}
external_config
} else {
ExternalConfig::new()
}
}
Loading

0 comments on commit df7ac2a

Please sign in to comment.