Skip to content

Commit

Permalink
feat: support ast merge sourcemap generation
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 committed Nov 4, 2024
1 parent ad42e40 commit d15f6d3
Show file tree
Hide file tree
Showing 22 changed files with 960 additions and 637 deletions.
39 changes: 20 additions & 19 deletions configs/farm-js-plugin.base.config.mjs
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
import { builtinModules } from 'module';
import { builtinModules } from "module";

const format = process.env.FARM_FORMAT || 'cjs';
const ext = format === 'esm' ? 'mjs' : 'cjs';
const format = process.env.FARM_FORMAT || "cjs";
const ext = format === "esm" ? "mjs" : "cjs";

export function createFarmJsPluginBuildConfig(plugins, options = {}) {
return {
compilation: {
input: {
index: './src/index.ts'
index: "./src/index.ts",
},
output: {
path: `build/${format}`,
entryFilename: `[entryName].${ext}`,
targetEnv: 'node',
format
targetEnv: "node",
format,
},
external: [
'@farmfe/core',
"@farmfe/core",
...builtinModules.map((m) => `^${m}$`),
...builtinModules.map((m) => `^node:${m}$`),
...(options.external || [])
...(options.external || []),
],
partialBundling: {
enforceResources: [
{
name: 'index.js',
test: ['.+']
}
]
name: "index.js",
test: [".+"],
},
],
},
minify: false,
sourcemap: false,
presetEnv: false,
persistentCache: {
envs: {
FARM_FORMAT: format
}
}
persistentCache: false,
// persistentCache: {
// envs: {
// FARM_FORMAT: format
// }
// }
},
server: {
hmr: false
hmr: false,
},
plugins
plugins,
};
}
6 changes: 3 additions & 3 deletions crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ impl Compiler {
}

// triggering build stage
let res = {
{
#[cfg(feature = "profile")]
farmfe_core::puffin::profile_scope!("Build Stage");
self.build()
self.build()?
};

self.context.record_manager.set_build_end_time();
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Compiler {

self.context.record_manager.set_end_time();

res
Ok(())
}

pub fn context(&self) -> &Arc<CompilationContext> {
Expand Down
116 changes: 58 additions & 58 deletions crates/compiler/src/update/regenerate_resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,64 +80,64 @@ pub fn render_and_generate_update_resource(
|resource_pot: &mut ResourcePot| -> farmfe_core::error::Result<String> {
let async_modules = context.custom.get(ASYNC_MODULES).unwrap();
let async_modules = async_modules.downcast_ref::<HashSet<ModuleId>>().unwrap();
if !resource_pot.modules().is_empty() {
let RenderedJsResourcePot {
mut bundle,
rendered_modules,
..
} = resource_pot_to_runtime_object(resource_pot, &module_graph, async_modules, context)?;
bundle.prepend("(");
bundle.append(")", None);

let mut rendered_map_chain = vec![];

if context.config.sourcemap.enabled(resource_pot.immutable) {
let root = context.config.root.clone();
let map = bundle
.generate_map(SourceMapOptions {
include_content: Some(true),
remap_source: Some(Box::new(move |src| format!("/{}", relative(&root, src)))),
..Default::default()
})
.map_err(|_| CompilationError::GenerateSourceMapError {
id: resource_pot.id.clone(),
})?;

let mut buf = vec![];
map.to_writer(&mut buf).expect("failed to write sourcemap");
rendered_map_chain.push(Arc::new(String::from_utf8(buf).unwrap()));
}
// The hmr result should alway be a js resource
resource_pot.meta = ResourcePotMetaData {
rendered_modules,
rendered_content: Arc::new(bundle.to_string()),
rendered_map_chain,
..Default::default()
};

let (mut update_resources, _) = render_resource_pot_generate_resources(
resource_pot,
context,
&Default::default(),
true,
&mut None,
)?;

if let Some(map) = update_resources.source_map {
// inline source map
update_resources.resource.bytes.append(
&mut format!(
"\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,{}",
base64_encode(&map.bytes)
)
.into_bytes(),
);
}

let code = String::from_utf8(update_resources.resource.bytes).unwrap();

return Ok(code);
}
// if !resource_pot.modules().is_empty() {
// let RenderedJsResourcePot {
// mut bundle,
// rendered_modules,
// ..
// } = resource_pot_to_runtime_object(resource_pot, &module_graph, async_modules, context)?;
// bundle.prepend("(");
// bundle.append(")", None);

// let mut rendered_map_chain = vec![];

// if context.config.sourcemap.enabled(resource_pot.immutable) {
// let root = context.config.root.clone();
// let map = bundle
// .generate_map(SourceMapOptions {
// include_content: Some(true),
// remap_source: Some(Box::new(move |src| format!("/{}", relative(&root, src)))),
// ..Default::default()
// })
// .map_err(|_| CompilationError::GenerateSourceMapError {
// id: resource_pot.id.clone(),
// })?;

// let mut buf = vec![];
// map.to_writer(&mut buf).expect("failed to write sourcemap");
// rendered_map_chain.push(Arc::new(String::from_utf8(buf).unwrap()));
// }
// // The hmr result should alway be a js resource
// resource_pot.meta = ResourcePotMetaData {
// rendered_modules,
// rendered_content: Arc::new(bundle.to_string()),
// rendered_map_chain,
// ..Default::default()
// };

// let (mut update_resources, _) = render_resource_pot_generate_resources(
// resource_pot,
// context,
// &Default::default(),
// true,
// &mut None,
// )?;

// if let Some(map) = update_resources.source_map {
// // inline source map
// update_resources.resource.bytes.append(
// &mut format!(
// "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,{}",
// base64_encode(&map.bytes)
// )
// .into_bytes(),
// );
// }

// let code = String::from_utf8(update_resources.resource.bytes).unwrap();

// return Ok(code);
// }

Ok("{}".to_string())
};
Expand Down
35 changes: 33 additions & 2 deletions crates/core/src/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::{any::Any, path::Path, sync::Arc};
use std::{
any::Any,
path::{Path, PathBuf},
sync::Arc,
};

use dashmap::DashMap;
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use swc_common::Globals;
use swc_common::{FileName, Globals, SourceFile, SourceMap};

use crate::{
cache::CacheManager,
Expand Down Expand Up @@ -207,13 +211,40 @@ impl Default for ContextMetaData {
/// Shared script meta data used for [swc]
pub struct ScriptContextMetaData {
pub globals: Globals,
pub module_source_maps: DashMap<ModuleId, (Arc<SourceMap>, Arc<SourceFile>)>,
}

impl ScriptContextMetaData {
pub fn new() -> Self {
Self {
globals: Globals::new(),
module_source_maps: DashMap::new(),
}
}

/// create a swc source map from a source
pub fn create_swc_source_map(
&self,
module_id: &ModuleId,
content: Arc<String>,
) -> (Arc<SourceMap>, Arc<SourceFile>) {
// if the source map already exists, return it
if let Some(value) = self.module_source_maps.get(module_id) {
return (value.0.clone(), value.1.clone());
}

let cm = Arc::new(SourceMap::default());
let sf = cm.new_source_file_from(
Arc::new(FileName::Real(PathBuf::from(module_id.to_string()))),
content,
);

// store the source map and source file
self
.module_source_maps
.insert(module_id.clone(), (cm.clone(), sf.clone()));

(cm, sf)
}
}

Expand Down
Loading

0 comments on commit d15f6d3

Please sign in to comment.