TypeScript + bundler & declareattribute
Migrating a project to Max 9 and [v8], I stumbled upon an issue with declareattribute using esbuild as a bundler to transpile TypeScript into 1 single JavaScript file.
But I also found a solution and wanted to share it - as other TypeScript related threads did not touch this.
The solution is
1) use cjs
as the format parameter for esbuild
esbuild src/testifyAttributes-bundle.ts --bundle --format=cjs --platform=browser --external:max-api --target=es2022 --outfile=build/testifyAttributes-bundle.js
Normally one would tend to use iife
to protect polluting the global namespace, but for declareattribute to work, the additional lexical scope creates a problem in that the values can be accessed by [attrui] for example, but they are not updated.
2) assign every variable or function that otherwise would be "optimized away" by the bundler to globalThis
(globalThis as any).fluffy = fluffy;
A working example can be seen in the attached patcher, which makes it also obvious that each [v8] instance has its own global context, which Joshua pointed out in the Discord Office Hours JavaScript Session - so globalThis has no influence on other [v8] instances.