Experiments with node.js
interoperability using the latest Dart 3 features.
- Run
webdev build --output bin:build/gen
to generate the js. Files should be generated in thebuild/gen
directory. - Modify the import in
build/main_with_preamble.js
with the name of the new entrypoint generated in step 1. - Run
node build/main_with_preamble.js
to test the compiled js.
- Alternatively, you can also run it in the browser by using
webdev serve build:8080
.
NOTE: For some reason, webdev serve
isnt regenerating the js files on change, so this implementation currently requires use of webdev build
first.
webdev build --output bin:build/gen
webdev serve build:8080
- basic node.js interop script using 0 dependencies
- create a JS interop type that represents the (built-in) console log
- verify platform agnostic type (e.g.
console
) can be run from browser. - create a Node.JS interop type that only exists via an import (e.g.
fs
) - create a Browser JS interop type, and verify it doesn't work using node
- can the
node.js
preamble be programatically added to existingbuild_web_compilers
? (see: https://github.com/dart-lang/build/tree/82889aea57247a32b327f4e51e5c42f4b70fb230/build_web_compilers#manual-usage) - can we get intellisense to work on generated code (possibly using
jsconfig.json
)? this would help debug issues at compile time, rather than run time - build Dart extension type definitions from ts declaration files
- couldn't access
import.meta.url
(didn't try everything) - is there a reason that this would be needed innode.js
Some use cases to validate full functionality with.
- VSCode extension interop
- ReactJS interop
Dart currently doesn't have a way to declare modules that can automatically be imported into the global scope. There are two options (but possibly more) for dealing with module imports:
- only use static imports (e.g.
<script>
inindex.html
) - explicitly import a module using
js_interop.importModule('fs')
note: unsure why static imports cant be used, e.g:
import { foo } from "foo";
Relevant:
- dart-lang/sdk#53783
- dart-lang/sdk#52852
- deferred loading of a library - https://dart.dev/language/libraries#lazily-loading-a-library
The generated dart code requires some preamble in order to interop with node.js
(e.g. adding require
to the global scope for future import statements).
An issue has been opened against buid_web_compilers
to allow custom premable scripts to be added to the compiler output.
Note: digging around build_modules may give some insight on how to customize the build pipeline.
Dart hot reload cannot work together to hot reload NodeJS. Unlocking this feature would likely require interop with a JS reloader, e.g. Webpack HMR.
TODO: description
see:
- https://pub.dev/packages/chartjs - old js interop example
- https://github.com/dart-archive/js_facade_gen - used to generate
chart.js
interop example