Comments (11)
HI @swuecho , I'm trying to do the same thing.
I have questions, did you install google-closure-library as a node dependency?
Did you have any repo with an example?
Thank you:)
no node dependecy is required, but you have to install bazel and bazel will fetch... the whole internet. (a lot of depenency :) )
check:
https://github.com/swuecho/j2cl_export_symbol_demo
this will output the optimized js, without any dependency.
https://github.com/swuecho/j2cl_webpack_demo
demonstrate how to use it in a webpack project. the example use vue-cli, but not vue specific
from j2cl.
@Mr-struct unfortunately, I did not use j2cl any more. Follow the instruction in this issues and copy the helloworld.js to your src folder and do the import, should works. I could make a demo in near future if you still have problem to set it up.
from j2cl.
I use grpc-web in production. I believe grpc-web generated goog.module. but I can import grpc-web directly.
https://github.com/grpc/grpc-web/tree/master/javascript/net/grpc/web
Not sure how grpc-web achieve this. perhaps simiar technology can be adapted?
If so, we can use the generated js module transparently.
Hope someone from grpc-web can give some guidance?
from j2cl.
That's what the HelloWorld sample shows (helloworldlib used with js):
https://github.com/google/j2cl/tree/master/samples/helloworld/src/main/java/com/google/j2cl/samples/helloworld
Are you asking about using it in a way where the helloworldlib loaded separately in the page with a script tag?
from j2cl.
Thanks! The helloworld example is great and amazing when use ibazel to see the 'realtime' change.
You are right. if I can load helloworldlib loaded separately, I can mix the lib in other js project seamlessly.
in my case, I want to use the helloworldlib in vue js project with webpack. (I checked the helloworldlib.js.zip. seems the the generated code is goog.module format. I believe closure compiler could compile it to normal js. so the gap is another build step or a parameter to tell j2cl_library the target js module format?)
from j2cl.
Yes, in that particular case you would want to change app.js in the sample to expose the things that you want to be available for the host page. So it would look like:
goog.module('j2cl.samples.app');
var {sayHello} = goog.require('j2cl.samples.hello');
goog.exportSymbol("helloworld.sayHello", sayHello);
Then in the other script you can call helloworld.sayHello()
if you include the output .js from j2cl_application target.
It would be great if somebody contributes a webpack example for this use case...
from j2cl.
goog.exportSymbol("helloworld.sayHello", sayHello);
will make all this work. thanks!
from j2cl.
direct import in webpack does not work, because the generated app is not in supported module syntax(?)
obviously, with the exportSymbol, include the 'helloworld.js' in script tag will work. but in order to make it work with webpack directly. use import
is the solution I can find so far. not the best solution, but works.
// copy helloworld.js from bazel-bin
import('./helloworld.js').then(module => {
let helloworld = module.helloworld
console.log(helloworld.sayHello())
});
from j2cl.
HI @swuecho , I'm trying to do the same thing.
I have questions, did you install google-closure-library as a node dependency?
Did you have any repo with an example?
Thank you:)
from j2cl.
Thanks a lot for your answers 😄
from j2cl.
I'm just getting started here, but the hello world example doesn't look like it would actually be runnable in a browser or in node. Wouldn't it need require("google-closure-library")
somewhere?
A complete source-to-browser example, maybe also with a little jsinterop and element2 would be super helpful for newcomers.
from j2cl.
Related Issues (20)
- Wasm sample broken HOT 2
- package listing?
- io_bazel_rules_kotlin_configured HOT 4
- How to fix the error: unused native file on Windows 11? HOT 1
- Ready to work on download file
- Wasm sample broken v2 HOT 2
- JUnit test example for wasm HOT 5
- I receive the following issue during the build. Have anyone an idea what the reason is: HOT 3
- [WASM] HashMap.computeIfAbsent doesn't work as expected HOT 1
- Can't run j2cl on Bazel 6 HOT 6
- Bazel: accidental impossible target HOT 2
- goog.reflect.sinkValue and deadcode elimination
- How to use it in vite +vue3
- Automate release/tag of the repo on green builds
- predicate is not usable by but exposed to JavaScript. HOT 3
- Support `rules_jvm_external` or `j2cl_maven_import_external` private registry HOT 1
- JRE emulation misses java.io.Externalizable
- Add dependabot to keep GitHub Actions up-to-date
- public methods are too eagerly optimized away; need ability to declare "@export" annotation HOT 9
- "Native JsType method '...' should be native, abstract or JsOverlay." error prevents default implementation for vanilla Java HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from j2cl.