-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'opt/binary_performance' of https://github.com/ErKeLost/…
…farm into opt/binary_performance
- Loading branch information
Showing
71 changed files
with
1,451 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@farmfe/core": patch | ||
--- | ||
|
||
import.meta.url and require compatible esm and cjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::{collections::HashMap, path::PathBuf}; | ||
|
||
use farmfe_core::config::{bool_or_obj::BoolOrObj, config_regex::ConfigRegex, Mode, TargetEnv}; | ||
mod common; | ||
use crate::common::{ | ||
assert_compiler_result_with_config, create_compiler_with_args, AssertCompilerResultConfig, | ||
}; | ||
|
||
#[allow(dead_code)] | ||
#[cfg(test)] | ||
fn test(file: String, crate_path: String) { | ||
use common::get_config_field; | ||
use farmfe_core::config::partial_bundling::PartialBundlingEnforceResourceConfig; | ||
|
||
use crate::common::try_read_config_from_json; | ||
|
||
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 config_entry = cwd.to_path_buf().join("config.json"); | ||
let runtime_entry = cwd.to_path_buf().join("runtime.ts"); | ||
|
||
let config_from_file = try_read_config_from_json(config_entry); | ||
|
||
let compiler = | ||
create_compiler_with_args(cwd.to_path_buf(), create_path_buf, |mut config, plugins| { | ||
config.mode = Mode::Production; | ||
|
||
if runtime_entry.is_file() { | ||
let runtime_entry = runtime_entry.to_string_lossy().to_string(); | ||
config.runtime.path = runtime_entry; | ||
} | ||
|
||
config.input = HashMap::from_iter(vec![(entry_name.clone(), file)]); | ||
|
||
config.minify = Box::new(BoolOrObj::Bool(false)); | ||
config.tree_shaking = Box::new(BoolOrObj::Bool(false)); | ||
|
||
config.external = vec![ConfigRegex::new("(^node:.*)"), ConfigRegex::new("^fs$")]; | ||
config.output.target_env = TargetEnv::Node; | ||
// config.output.format = ModuleFormat::CommonJs; | ||
|
||
// TODO: multiple bundle | ||
config.partial_bundling.enforce_resources = vec![PartialBundlingEnforceResourceConfig { | ||
test: vec![ConfigRegex::new(".+")], | ||
name: "index".to_string(), | ||
}]; | ||
|
||
if let Some(config_from_file) = config_from_file { | ||
if let Some(mode) = get_config_field(&config_from_file, &["mode"]) { | ||
config.mode = mode; | ||
} | ||
|
||
if let Some(format) = get_config_field(&config_from_file, &["output", "format"]) { | ||
config.output.format = format; | ||
} | ||
|
||
if let Some(target_env) = get_config_field(&config_from_file, &["output", "targetEnv"]) { | ||
config.output.target_env = target_env; | ||
} | ||
} | ||
|
||
(config, plugins) | ||
}); | ||
|
||
compiler.compile().unwrap(); | ||
|
||
assert_compiler_result_with_config( | ||
&compiler, | ||
AssertCompilerResultConfig { | ||
entry_name: Some(entry_name), | ||
ignore_emitted_field: false, | ||
..Default::default() | ||
}, | ||
); | ||
} | ||
|
||
farmfe_testing::testing! {"tests/fixtures/bundle/library/**/index.ts", test} | ||
// farmfe_testing::testing! {"tests/fixtures/runtime/bundle/cjs/export/entryExportStar/**/index.ts", test} | ||
// farmfe_testing::testing! {"tests/fixtures/runtime/bundle/external/import/namespace/**/index.ts", test} |
6 changes: 6 additions & 0 deletions
6
crates/compiler/tests/fixtures/bundle/library/hybrid/noexport/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"output": { | ||
"targetEnv": "library-node", | ||
"format": "esm" | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
crates/compiler/tests/fixtures/bundle/library/hybrid/noexport/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import fs from 'node:fs'; | ||
const os = require('node:os'); | ||
|
||
console.log(fs.read, os.cpus); |
69 changes: 69 additions & 0 deletions
69
crates/compiler/tests/fixtures/bundle/library/hybrid/noexport/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//index.js: | ||
import __farmNodeModule from 'module'; | ||
globalThis.nodeRequire = __farmNodeModule.createRequire(import.meta.url);function _interop_require_default(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
}function _export_star(from, to) { | ||
Object.keys(from).forEach(function(k) { | ||
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { | ||
Object.defineProperty(to, k, { | ||
enumerable: true, | ||
get: function() { | ||
return from[k]; | ||
} | ||
}); | ||
} | ||
}); | ||
return from; | ||
}function _interop_require_wildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) return obj; | ||
if (obj === null || typeof obj !== "object" && typeof obj !== "function") return { | ||
default: obj | ||
}; | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) return cache.get(obj); | ||
var newObj = { | ||
__proto__: null | ||
}; | ||
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for(var key in obj){ | ||
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; | ||
if (desc && (desc.get || desc.set)) Object.defineProperty(newObj, key, desc); | ||
else newObj[key] = obj[key]; | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) cache.set(obj, newObj); | ||
return newObj; | ||
}function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== "function") return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function(nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
}function __commonJs(mod) { | ||
var module; | ||
return () => { | ||
if (module) { | ||
return module.exports; | ||
} | ||
module = { | ||
exports: {}, | ||
}; | ||
if(typeof mod === "function") { | ||
mod(module, module.exports); | ||
}else { | ||
mod[Object.keys(mod)[0]](module, module.exports); | ||
} | ||
return module.exports; | ||
}; | ||
}import fs from "node:fs"; | ||
var index_cjs = __commonJs((module, exports)=>{ | ||
"use strict"; | ||
const os = globalThis.nodeRequire('node:os'); | ||
console.log(fs.read, os.cpus); | ||
}); | ||
index_cjs(); |
1 change: 1 addition & 0 deletions
1
crates/compiler/tests/fixtures/bundle/library/hybrid/noexport/runtime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('runtime'); |
6 changes: 6 additions & 0 deletions
6
crates/compiler/tests/fixtures/bundle/library/hybrid/normal/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"output": { | ||
"targetEnv": "library-node", | ||
"format": "esm" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
crates/compiler/tests/fixtures/bundle/library/hybrid/normal/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import fs from 'node:fs'; | ||
const os = require('node:os'); | ||
|
||
console.log(fs.read, os.cpus); | ||
|
||
|
||
export default { | ||
read: fs.read, | ||
c: 1, | ||
}; | ||
|
||
export const foo = 'foo'; | ||
export const bar = 'bar'; |
97 changes: 97 additions & 0 deletions
97
crates/compiler/tests/fixtures/bundle/library/hybrid/normal/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
//index.js: | ||
import __farmNodeModule from 'module'; | ||
globalThis.nodeRequire = __farmNodeModule.createRequire(import.meta.url);function _interop_require_default(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
}function _export_star(from, to) { | ||
Object.keys(from).forEach(function(k) { | ||
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { | ||
Object.defineProperty(to, k, { | ||
enumerable: true, | ||
get: function() { | ||
return from[k]; | ||
} | ||
}); | ||
} | ||
}); | ||
return from; | ||
}function _interop_require_wildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) return obj; | ||
if (obj === null || typeof obj !== "object" && typeof obj !== "function") return { | ||
default: obj | ||
}; | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) return cache.get(obj); | ||
var newObj = { | ||
__proto__: null | ||
}; | ||
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for(var key in obj){ | ||
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; | ||
if (desc && (desc.get || desc.set)) Object.defineProperty(newObj, key, desc); | ||
else newObj[key] = obj[key]; | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) cache.set(obj, newObj); | ||
return newObj; | ||
}function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== "function") return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function(nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
}function __commonJs(mod) { | ||
var module; | ||
return () => { | ||
if (module) { | ||
return module.exports; | ||
} | ||
module = { | ||
exports: {}, | ||
}; | ||
if(typeof mod === "function") { | ||
mod(module, module.exports); | ||
}else { | ||
mod[Object.keys(mod)[0]](module, module.exports); | ||
} | ||
return module.exports; | ||
}; | ||
}import fs from "node:fs"; | ||
var index_cjs = __commonJs((module, exports)=>{ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
function _export(target, all) { | ||
for(var name in all)Object.defineProperty(target, name, { | ||
enumerable: true, | ||
get: all[name] | ||
}); | ||
} | ||
_export(exports, { | ||
bar: function() { | ||
return bar; | ||
}, | ||
index_default: function() { | ||
return _default; | ||
}, | ||
foo: function() { | ||
return foo; | ||
} | ||
}); | ||
const os = globalThis.nodeRequire('node:os'); | ||
console.log(fs.read, os.cpus); | ||
var _default = { | ||
read: fs.read, | ||
c: 1 | ||
}; | ||
const foo = 'foo'; | ||
const bar = 'bar'; | ||
}); | ||
var index_default = _interop_require_default(index_cjs()).index_default, bar = index_cjs()["bar"], foo = index_cjs()["foo"]; | ||
export { bar, foo }; | ||
export default index_default; |
1 change: 1 addition & 0 deletions
1
crates/compiler/tests/fixtures/bundle/library/hybrid/normal/runtime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('runtime'); |
6 changes: 6 additions & 0 deletions
6
crates/compiler/tests/fixtures/bundle/library/reexport/esm/default/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"output": { | ||
"targetEnv": "library-node", | ||
"format": "esm" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
crates/compiler/tests/fixtures/bundle/library/reexport/esm/default/foo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'foo'; |
1 change: 1 addition & 0 deletions
1
crates/compiler/tests/fixtures/bundle/library/reexport/esm/default/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './foo.ts'; |
48 changes: 48 additions & 0 deletions
48
crates/compiler/tests/fixtures/bundle/library/reexport/esm/default/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//index.js: | ||
function _interop_require_default(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
}function _export_star(from, to) { | ||
Object.keys(from).forEach(function(k) { | ||
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { | ||
Object.defineProperty(to, k, { | ||
enumerable: true, | ||
get: function() { | ||
return from[k]; | ||
} | ||
}); | ||
} | ||
}); | ||
return from; | ||
}function _interop_require_wildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) return obj; | ||
if (obj === null || typeof obj !== "object" && typeof obj !== "function") return { | ||
default: obj | ||
}; | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) return cache.get(obj); | ||
var newObj = { | ||
__proto__: null | ||
}; | ||
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for(var key in obj){ | ||
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; | ||
if (desc && (desc.get || desc.set)) Object.defineProperty(newObj, key, desc); | ||
else newObj[key] = obj[key]; | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) cache.set(obj, newObj); | ||
return newObj; | ||
}function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== "function") return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function(nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
}var foo_default = 'foo'; | ||
|
||
export { foo_default as default }; |
1 change: 1 addition & 0 deletions
1
crates/compiler/tests/fixtures/bundle/library/reexport/esm/default/runtime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('runtime'); |
6 changes: 6 additions & 0 deletions
6
crates/compiler/tests/fixtures/bundle/library/reexport/esm/named/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"output": { | ||
"targetEnv": "library-node", | ||
"format": "esm" | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
crates/compiler/tests/fixtures/bundle/library/reexport/esm/named/foo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const foo = 'foo'; | ||
export const bar = 'bar'; |
1 change: 1 addition & 0 deletions
1
crates/compiler/tests/fixtures/bundle/library/reexport/esm/named/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { foo, bar } from './foo.ts'; |
Oops, something went wrong.