-
Notifications
You must be signed in to change notification settings - Fork 147
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
Import with require does not work anymore for transpiled library #261
Comments
To double check; you are using the compiled output, not the dev bundle from j2cl_application target, right? |
@gkdn. If you mean the transpiled library (above called |
More context: With the newest updates, the transpiled goog.global = globalThis;
goog.exportPath_ = function (a, b, c, d) {
a = a.split(".");
d = d || globalThis;
...
}; When I replace |
Ah I see. It seems like the code is loaded in different context than globalThis in commonjs setup. I'm talking Closure folks it will be ok with reverting this behavior back. Do you know if there a good way to reproduce this problem in rules_closure without introducing new deps so we can add some tests? |
@gkdn Thank you for looking into it. Unfortunately, I am not very familiar with rules_closure. I have tried, but unfortunately - at least in the short time available - could not provide a meaningful test. I'm already looking forward to hearing how things are going. |
Can you try bazelbuild/rules_closure#654 to double check if it fixes the problem? (Haven't tried but pointing to commit c32bb7c37d532155ba4bbfe3d56383e4d519bc53 in rules_closure dep might do the job.) |
Hmm I think it will be difficult to try it out since rules_closure head version switched to Bazel 8 and we haven't yet (pretty soon) though it seems like should fix the issue. I think I will proceed with the patch. In the meantime, note that Closure Compiler team suggested a different setup for common.js: explicitly declare an extern for the commonjs 'module' object, and then explicitly export things onto the module.exports object, sort of like Something to consider in case this might break again in the future. |
@gkdn Thank you again! As mentioned, I am not too familar with closure, which is why I maybe do not fully understand your comment on the extern. What I tried is to the following: create an externs.js /** @externs */
var module = {};
module.exports = {}; include it in the BUILD ... // as above
j2cl_application(
.... // as above
extra_production_args = [
'--compilation_level=SIMPLE',
'--externs=externs.js'
]
) and then add to the following line the lib.js: ... // as above
module["exports"]["MyUtils"] = MyUtils; when running |
We have a Java library that is transpiled with j2cl to JavaScript, resulting in a "mylib.js". Until now, we were able to use the transpiled Java-classes and libraries as follows:
However, since a change in recent days (it seems to be this commit here: bazelbuild/rules_closure@b159f54), this kind of import does not work anymore. What now works is
We use browserify for bundling. I see now the following options:
globalThis
like above.Is
globalThis
the way to go now? Or is there a reasonable and future-proof way to keep ourrequire
imports (e.g. some transpiler argument?) with the given setting (browserify)? Maybe, the way we userequire
was never intended like this for j2cl-transpiled libraries?bazel version 7.4.0
bazelrc (like in the samples)
BUILD
lib.js
The text was updated successfully, but these errors were encountered: