Skip to content

Commit

Permalink
Merge branch 'opt/binary_performance' of https://github.com/ErKeLost/…
Browse files Browse the repository at this point in the history
…farm into opt/binary_performance
  • Loading branch information
ErKeLost committed Jul 29, 2024
2 parents 11b60e8 + 2a75366 commit 19c7b52
Show file tree
Hide file tree
Showing 71 changed files with 1,451 additions and 98 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-pandas-listen.md
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
84 changes: 84 additions & 0 deletions crates/compiler/tests/bundle.rs
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}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output": {
"targetEnv": "library-node",
"format": "esm"
}
}
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);
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();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('runtime');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output": {
"targetEnv": "library-node",
"format": "esm"
}
}
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';
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('runtime');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output": {
"targetEnv": "library-node",
"format": "esm"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './foo.ts';
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 };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('runtime');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output": {
"targetEnv": "library-node",
"format": "esm"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { foo, bar } from './foo.ts';
Loading

0 comments on commit 19c7b52

Please sign in to comment.