Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add primitives to support sub-compiler usage #1887

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

ssttevee
Copy link
Contributor

Description:

I'm building framework around farm that requires "sub-build"s. The main problem blocking me from achieving this is the inability to access the plugins from the current compiler context/plugin driver.

With these changes, I am able to create a sub-compiler to create a sub-build like this:

fn load(
  &self,
  param: &PluginLoadHookParam,
  context: &Arc<CompilationContext>,
  hook_context: &PluginHookContext,
) -> Result<Option<PluginLoadHookResult>> {
  let mut config = context.config.as_ref().clone();
  config.progress = false;
  config.input = HashMap::new();
  config.input.insert(String::from("client"), "anothermodule.ts");

  let compiler =
    Compiler::new_without_internal_plugins(config, context.plugin_driver.plugins.clone())?;

  // copy module graph to new compiler
  context
    .module_graph
    .read()
    .copy_to(&mut *compiler.context().module_graph.write(), true)?;

  compiler.compile()?;

  let resource_map = compiler.context().resource_map.lock();
  let js = String::from_utf8(resource_map.get("client.js").unwrap().bytes.clone()).unwrap();

  // ...
}

Note that there are a few issues with this approach:

  • a new thread pool is created for each sub-compiler,
  • modules may be cloned twice per sub-compiler, once when creating each sub-compiler and once when updating the main module graph with newly cached modules, and
  • my plugin binary becomes more bloated than necessary because the entire compiler crate including all other default plugins and dependencies are compiled and bundled with my plugin logic.

BREAKING CHANGE:

None.

Related issue (if exists):

Copy link

changeset-bot bot commented Oct 27, 2024

⚠️ No Changeset found

Latest commit: 2aa3bb7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@fu050409 fu050409 requested a review from wre232114 October 27, 2024 09:34
@fu050409 fu050409 changed the title Add primitives to support sub-compiler usage feat(plugins): add primitives to support sub-compiler usage Oct 27, 2024
@wre232114
Copy link
Member

Thanks for the PR. We'll support compiling dynamically added inputs in Farm v2.0, it can solve the issue you mentioned, sharing thread pool and module graph.

Copy link

codspeed-hq bot commented Oct 28, 2024

CodSpeed Performance Report

Merging #1887 will create unknown performance changes

Comparing ssttevee:subcompiler (2aa3bb7) with main (a785d6f)

Summary

⚠️ No benchmarks were detected in both the base of the PR and the PR.

@wre232114
Copy link
Member

wre232114 commented Oct 31, 2024

There are CI errors @ssttevee , we'll merge and publish it as soon as possible when all tests passed

@ssttevee
Copy link
Contributor Author

Oh sorry, I didn't catch those before. It should be fixed now.

@fu050409 fu050409 requested a review from wre232114 October 31, 2024 08:18
@wre232114 wre232114 enabled auto-merge (squash) October 31, 2024 08:32
@wre232114 wre232114 merged commit 110626d into farm-fe:main Oct 31, 2024
36 checks passed
@github-actions github-actions bot mentioned this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants