diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index 0133e01b52..797c701d42 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -12,6 +12,7 @@ pub mod plugin_toolkit; #[cfg(feature = "profile")] pub mod profile_gui; +use farmfe_core::rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use farmfe_core::{ config::{Config, Mode}, module::ModuleId, @@ -211,7 +212,7 @@ impl JsCompiler { } /// async compile, return promise - #[napi] + #[napi(ts_return_type = "Promise")] pub fn compile(&self, e: Env) -> napi::Result { let (promise, result) = e.create_deferred:: napi::Result>>()?; @@ -438,6 +439,21 @@ impl JsCompiler { resources_map } + #[napi] + pub fn write_resources_to_disk(&self, output_path: String) -> () { + let context = self.compiler.context(); + let resources = context.resources_map.lock(); + + resources.par_iter().for_each(|(name, resource)| { + let path = Path::new(&output_path).join(name.split(|c| c == '?' || c == '#').next().unwrap()); + let dir = path.parent().unwrap(); + if !dir.exists() { + std::fs::create_dir_all(dir).unwrap(); + }; + std::fs::write(path, resource.bytes.clone()).unwrap(); + }); + } + #[napi] pub fn watch_modules(&self) -> Vec { let context = self.compiler.context(); diff --git a/packages/core/binding/binding.d.ts b/packages/core/binding/binding.d.ts index fe777018a3..b0b78ed59d 100644 --- a/packages/core/binding/binding.d.ts +++ b/packages/core/binding/binding.d.ts @@ -64,7 +64,7 @@ export declare class Compiler { traceDependencies(): object traceModuleGraph(): object /** async compile, return promise */ - compile(): object + compile(): Promise /** sync compile */ compileSync(): void /** TODO: usage example */ @@ -74,6 +74,7 @@ export declare class Compiler { getParentFiles(resolvedPath: string): Array resources(): Record resourcesMap(): Record + writeResourcesToDisk(outputPath: string): void watchModules(): Array relativeModulePaths(): Array resource(name: string): Buffer | null diff --git a/packages/core/src/compiler/index.ts b/packages/core/src/compiler/index.ts index d1ed3bae64..18d99527c6 100644 --- a/packages/core/src/compiler/index.ts +++ b/packages/core/src/compiler/index.ts @@ -1,4 +1,4 @@ -import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { existsSync, rmSync } from 'node:fs'; import path from 'node:path'; import { Compiler as BindingCompiler } from '../../binding/index.js'; @@ -46,7 +46,6 @@ export class Compiler { constructor(public config: ResolvedUserConfig) { this._bindingCompiler = new BindingCompiler({ - // @ts-ignore config: config.compilation, jsPlugins: config.jsPlugins, rustPlugins: config.rustPlugins @@ -169,16 +168,14 @@ export class Compiler { return this._bindingCompiler.resourcesMap() as Record; } + /** + * Writes the compiled resources to disk and calls the write resources hook. + */ writeResourcesToDisk(): void { - const resources = this.resources(); const outputPath = this.getOutputPath(); - - Object.entries(resources).forEach(([name, resource]) => { - const filePath = path.join(outputPath, name.split(/[?#]/)[0]); - mkdirSync(path.dirname(filePath), { recursive: true }); - writeFileSync(filePath, new Uint8Array(resource)); - }); - + // Write the resources to disk using the binding compiler + this._bindingCompiler.writeResourcesToDisk(outputPath); + // Call the write resources hook to allow plugins to perform additional actions this.callWriteResourcesHook(); }