dart-lang/build

Preamble for Customized Web Builders

pattobrien opened this issue · 2 comments

Context

i've been experimenting with the latest js_interop features, by building a node.js interop package (see: https://github.com/pattobrien/dart3_node_interop).

Since node.js is designed to run outside of the browser, there have been a number of unique aspects to compiling the js code from Dart source - notably, there's some code that must be initialized to add globals like require and process to the global scope. A node.js specific preamble can be found here.

Issue Description

It should be possible for devs to define some js code to be inserted along side the generated Dart code. This would make it a lot easier for developers to stay within the dart ecosystem, even when interop-ing with various configurations of js code.

Not being able to do so has caused interop package authors to fork the entire build_web_compilers package in order to work around the limitations and apply a few basic customizations. Such packages are:

I'm curious if such functionality already exists, or if it's something that would need to be added to the builders. As a side note, the build_web_compilers readme has not had content updated in about 6 years, and the Manual Usage section is especially out of date.

Additional Info

  • Dart SDK version: 3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "macos_arm64"
  • build_web_compilers v4.0.9

This package does not support compiling to node. Ultimately, nobody on the team has any expertise in node or even uses it at all. So we can't reasonably support it.

Previously, there was a community build_node_compilers package but it looks to be quite out of date now.

If we could resolve this through some general mechanism to include a pre-amble as you suggest, that would be interesting though. I don't know exactly what it would look like, but if you wanted to work on a contribution we could review that.

if you wanted to work on a contribution we could review that.

Absolutely! I'd love to take a swing at it.

I have a working Dart VSCode extension prototype, which runs in a Node runtime - so I can confirm its not only possible, but was generally surprised by how interoperable it is considering its a completely different environment. The implementation for preamble insertion is somewhat dependent on module-scoped exports, so I'm going to gather some feedback there and will open a PR once I have a good direction to propose.