Skip to content

Commit

Permalink
fix: bundle import namespace name uniq (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
shulandmimi committed Aug 8, 2024
1 parent a5554b9 commit ed5c527
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-rice-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe/core": patch
---

fix bundle import namespace name uniq
126 changes: 83 additions & 43 deletions crates/compiler/tests/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::common::{
#[allow(dead_code)]
#[cfg(test)]
fn test(file: String, crate_path: String) {
use std::fs;

use common::get_config_field;
use farmfe_core::config::partial_bundling::PartialBundlingEnforceResourceConfig;

Expand All @@ -21,62 +23,100 @@ fn test(file: String, crate_path: String) {

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 files = fs::read_dir(cwd)
.unwrap()
.filter_map(|item| item.map(|item| Some(item)).unwrap_or(None))
.filter_map(|item| {
let filename = item
.path()
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();

if filename.starts_with("config") || filename.ends_with(".json") {
Some((
item.path(),
filename
.trim_start_matches("config")
.trim_start_matches(".")
.trim_end_matches("json")
.trim_end_matches(".")
.to_string(),
))
} else {
None
}
})
.collect::<Vec<_>>();

let config_from_file = try_read_config_from_json(config_entry);
for (config_entry, config_named) in files {
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;
let compiler = create_compiler_with_args(
cwd.to_path_buf(),
create_path_buf.clone(),
|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;
}
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.input = HashMap::from_iter(vec![(entry_name.clone(), file.clone())]);

config.minify = Box::new(BoolOrObj::Bool(false));
config.tree_shaking = Box::new(BoolOrObj::Bool(false));
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;
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(),
}];
// 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(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(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;
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()
},
);
(config, plugins)
},
);

compiler.compile().unwrap();

assert_compiler_result_with_config(
&compiler,
AssertCompilerResultConfig {
entry_name: Some(entry_name.clone()),
ignore_emitted_field: false,
output_file: Some(format!(
"output.{}js",
if config_named.is_empty() {
"".into()
} else {
format!("{}.", config_named)
}
)),
..Default::default()
},
);
}
}

farmfe_testing::testing! {"tests/fixtures/bundle/library/**/index.ts", test}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = 'a.ts';

console.log(fs);

export default 'a.ts';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as fs from 'node:fs';

console.log('b.ts', fs);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output": {
"targetEnv": "library-node",
"format": "cjs"
}
}
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,5 @@
import fs from 'node:fs';
import './a';
import './b';

console.log('index.ts', fs);
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//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 node_fs_ns = _interop_require_wildcard(require("node:fs"));
var fs$1 = _interop_require_default(node_fs_ns).default;
const fs = 'a.ts';
console.log(fs);
var a_default = 'a.ts';

console.log('b.ts', node_fs_ns);

console.log('index.ts', fs$1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//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);
}import * as node_fs_ns from "node:fs";
import fs$1 from "node:fs";
const fs = 'a.ts';
console.log(fs);
var a_default = 'a.ts';

console.log('b.ts', node_fs_ns);

console.log('index.ts', fs$1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//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);
}import * as node_fs_ns from "node:fs";
import fs$1 from "node:fs";
const fs = 'a.ts';
console.log(fs);
var a_default = 'a.ts';

console.log('b.ts', node_fs_ns);

console.log('index.ts', fs$1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const lodash = 'a.ts';

console.log(lodash);

export default 'a.ts';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as lodash from './lodash';

console.log('b.ts', lodash);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output": {
"targetEnv": "library-node",
"format": "cjs"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import lodash from './lodash';
import './a';
import './b';

console.log('index.ts', lodash);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports.name = 'lodash';
module.exports.default = 'foo';
Loading

0 comments on commit ed5c527

Please sign in to comment.