what is the best way to define a custom entrypoint...
# javascript
t
what is the best way to define a custom entrypoint for webpack (a js file) using the kotlin frontend plugin? I have some dependencies that need to be required before the main (kostlinjs) module
a
cc @cy
f
As a workaround, you may actually just
require
them in your
main
function
a
@timm When you write something like this:
Copy code
kotlinFrontend {
    npm {
        dependency "style-loader" // production dependency
        devDependency "karma"     // development dependency
    }
}
Does it not load those dependencies before the actual kotlin code?
f
it really shouln't
dependencies should only be loaded on demand
I mean, included in bundle and executed
a
I am bit hazy on this. Why shouldn't it just load the JS files in order? Aren't you supposed to use module system of your choice if you want lazy loading?
f
Just adding a dependency means that it's loaded into your
node_modules
directory. Requiring it from your code means that it's added in bundle
a
Ok, I see.
f
Plus, most of the packages don't do anything but declare exported values
There are only a few that actually execute something when you
require
them
In kotlin, the former packages should be consumed via
@JsModule
, and the latter via
js("require(...)")
a
Is the latter one for manually controlling when the side effect is going to occur?
t
I tried using require in the main method. The library i am trying to use is phaser js which is not optimized for a module system (same for its dependencies). They attach themselfs to
window
on load. So i added an
index.js
in
src/main/web
Copy code
require('pixi');
require('p2');
require('phaser');
require('../../../build/kotlin-js-min/main/browser-game-js');
And then added the following customization to the webpack task:
Copy code
config.entry.main = path.join(__dirname, '../src/main/web/index.js');
This seems to work for now (in combination with
expose-loader
). Feels like I have lost sourcemaps when clicking on stacktraces though, will have to check that more.
so pixi, phaser and p2 have to be added to the global `window`before the actual kotlin compiled code loads. This is only an issue with "legacy" libraries though and the workaround here should work for most of them.
Thanks @anton.bannykh @Filipp Riabchun btw