I have a devtools Chrome extensions written in Kot...
# javascript
a
I have a devtools Chrome extensions written in Kotlin, the UI is implemented with Compose for Web. The project is distributed as
browser()
+
binaries.library()
and everything works fine. Now I'm trying to replace Compose with React (
org.jetbrains.kotlin-wrappers:kotlin-react-dom
), and I'm getting the following runtime error:
Copy code
Uncaught Error: Error loading module 'xyz'. Its dependency 'react-dom/client' was not found. Please, check whether 'react-dom/client' is loaded prior to 'xyz'.
If I use
binaries.executable()
then there is another runtime error:
Copy code
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self'"
Is there a way to distribute a Chrome extension with React dependencies?
b
I'm guessing react wrappers are adding react-don as optional or peer dependency. Try adding it as npm dependency directly.
Binaries.library does not bundle other npm libs and instead declares them as node-externals. You could also have a look at generated webpack config and then try to remove all declared externals via webpack.config.d
Is your project open source? I could have a look if you still get stuck after trying these out
a
I believe I tried adding them as npm, but I will try once again. And yes, the project is open source. I will push to a branch a bit later, so you could check if you have spare time, I would appreciate that.
πŸ‘ 1
b
Try the webpack bit then
Happy to have a peek when you're ready
a
You could also have a look at generated webpack config and then try to remove all declared externals via webpack.config.d
@Big Chungus Do you have an example how it can be done? For me, everything I put inside
webpack.config.d
directory looks ignored. Also is it applicable for
binaries.library()
?
b
Odd, I've used it with binaries.library just fine https://github.com/mpetuska/kmdc/tree/master/gradle/webpack.config.d
t
Looks like compile output used instead of distribution output. If you use Kotlin Wrappers required NPM dependencies (
react
and
react-dom
in your example) will be available by default
b
Not quite. His problem is that chrome extensions do not resolve package.json and binaries.library does not bundle npm dependencies
@Ilya Goncharov [JB] Do you know where generated webpack.config.js for binaries.library() goes? Clearly it uses it for test task at least, but I can't find it in regular places (
^/build/js/packages/...
). Or does it not even use webpack for libraries anymore? I kinda assumed it was using webpack externals feature before, but now I feel that it might have been a wrong assumption. EDIT: for test task I found webpack config embedded into
karma.conf.js
, so that solves part of the mystery. But how exactly is the binary built?
FYI:
binaries.executable()
will not work here due to (I assume) missing main method since without it everything gets treeshaken
@Arkadii Ivanov you could hack it with
binaries.library()
by running webpack, rollup or any other bundler on the distribution task output manually.
i
Hi, with
binaries.library()
webpack is not used. It is just compilation output (and with IR, all kotlin related modules are included into script) Moreover, it is the case to not use webpack and leave it as is (and integrate dependencies with own JS project with npm/yarn and bundle it with preferable way)
b
Ah, makes sense now, thanks. @Arkadii Ivanov looks like you'll need to post-process your distribution output with some bundler to convert it into "fat" js module manually
a
I have uploaded a simple reproducer, just in case - https://github.com/arkivanov/ChromeExtensionReproducer @Big Chungus Do you know any examples of the "fat" js?
b
Just building you one. Gimmie a sec
πŸ™ 1
Ok, this might still need some tweaking, but try this MVP 1.
./gradlew jsBrowserProductionLibraryDistribution
2.
cd build/productionLibrary
3.
npx rollup <your_module_output_file_name>.js --file output.js --format iife
I'm using rollup here instead of webpack as it has most sensible defaults
You could also try parcel
Ok, some tweaks are needed afterall. Replace your build.gradle.kts with this
Copy code
plugins {
  kotlin("js")
}

kotlin {
  js(IR) {
    browser()
    binaries.library()
  }
}

dependencies {
  implementation("org.jetbrains.kotlin-wrappers:kotlin-react")
  implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom")
  implementation("org.jetbrains.kotlin-wrappers:kotlin-styled")
  implementation("org.jetbrains.kotlin-wrappers:kotlin-extensions")
  implementation("org.jetbrains.kotlin-wrappers:kotlin-emotion")
  implementation("org.jetbrains.kotlin-wrappers:kotlin-mui")
  implementation("org.jetbrains.kotlin-wrappers:kotlin-mui-icons")
  implementation(enforcedPlatform("org.jetbrains.kotlin-wrappers:kotlin-wrappers-bom:1.0.0-pre.348"))

  // New stuff
  implementation(npm("react-dom", "*"))
  implementation(npm("@mui/material", "*"))
  implementation(npm("react", "*"))
  implementation(npm("@emotion/react", "*"))
  implementation(npm("@emotion/styled", "*"))
  implementation(devNpm("rollup", "*"))
  implementation(devNpm("@rollup/plugin-commonjs", "*"))
  implementation(devNpm("@rollup/plugin-node-resolve", "*"))
}
Run
./gradlew jsBrowserProductionLibraryDistribution
Then
cd build/productionLibrary
Then
yarn install
And finally
yarn rollup ChromeExtensionReproducer-chrome-extension.js --file _out.js --format iife --plugin @rollup/plugin-commonjs --plugin @rollup/plugin-node-resolve
After that your "fat-lib" will be at
_out.js
Ping me if you want to discuss what's happening in more details
a
Thanks! I did everything as described and got the fat jar. The extension now crashes with another error:
Copy code
Uncaught ReferenceError: process is not defined
This looks like the produced code is for server (node), not browser. Overall this looks quite complicated. Maybe I should stick with Compose and without any npm dependencies for now? I won't be able to use
mui
either, I guess. The whole reason why I am replacing Compose with React is because I wanted Kotlin 1.7.0, and converting Compose -> React is trivial in my case.
By the way, the LEGACY mode works out of the box without any workarounds. I will consider switching to legacy for now.
b
Yes, because LEGACY ignores your binaries and just embeds everything all the time
Simpler times... πŸ˜„
βž• 1
As for the error, it's indeed strange. You could probably get rid of process references with some more rollup config, but at this point I'd either wait for CfW 1.7.0 or just switch back to legacy backend
πŸ™ 1
I suspect react references process for some SSR parts and webpack normally removes that
Surely rollup can do that too or you could try bundling it with webpack instead
a
Thanks @Big Chungus for your efforts to help me!
b
But this is now entirely js ecosystem and has nothing else to do with kjs
No worries. I found it quite interesting myself.
Wait, one quick hack. Append
window.process = window
to the very top of your fat-bundle. See if that hacks it πŸ˜„
Otherwise, I'm 99% sure this extra plugin would fix it too https://www.npmjs.com/package/rollup-plugin-inject-process-env
React is known to rely on process.NODE_ENV a lot
Probably not the most scalable solution at this point, but now I'm curious to see if it could. Don't even care if it should πŸ˜„
a
window.process = window
results in
uncaught TypeError: window is not a function
πŸ™ƒ
b
What's the global object for extensions?
For regular webapp it's window
For nodejs app it's process
Try with this. You build now with
yarn rollup -c
a
I see that
window
is defined, I'm able to show an alert
Try with this.
It works now without errors! Thanks again!
b
Wow, I did not expect to get it working to be honest πŸ˜„
πŸ˜‚ 1
I should probably documment all this in a blogpost eventually... Would you interested to collaborate? I have no clue how web extensions are developed...
a
@Big Chungus I would love to! But I have little experience in web dev overall.
b
I'll cover web dev part, you can just append extension dev bit
πŸ‘ 1
i.e. what files are needed and why
I'll ping you with the draft of what I do know
a
Sounds good! Overall, it feels that there is a use case for bundling npm deps in a library project. Would be good to have a configuration or a Gradle task for it.
b
Absolutely, wanna raise an issue?
Although looks like it would be a pretty big change for KGP since binaries.library() does not use webpack at all right now
t
bundling npm deps in a library project
It looks like
executable
distribution
b
There are subtle differences. Executables must have an entrypoint, libraries usually don't
Also he did try binaries.executable() and that didn't quite work due to unsafe eval usage for whatever reason
But I do agree that this particular use-case should be covered by the executable and not a library
a
Something like
binaries.library { bundle = true }
would be nice to have. Maybe worth filing a ticket.
I have also tried webpack and I find it easier to use in this particular case. My steps: 1. Run
./gradlew :chrome-extension:browserProductionLibraryDistribution
2.
cd chrome-extension/build/productionLibrary
3.
npm install
4.
npx webpack --config ../../webpack.config.js
The config file:
Copy code
const path = require('path');

module.exports = {
    entry: './ChromeExtensionReproducer-chrome-extension.js',
    target: 'web',
    mode: 'production',
    output: {
        path: path.resolve(__dirname, 'build/dist'),
        filename: 'ChromeExtensionReproducer-chrome-extension.js',
    },
};
πŸ‘ 1
b
Yeah i started with rollup hoping to avoid config file but in the end webpack turned out to be simpler as it did not need plugins
a
I have also noticed the following change after upgrading Kotlin from 1.6.10 to 1.7.0. When assembling
binaries.library()
via
jsBrowserDevelopmentLibraryDistribution
on Kotlin 1.6.10, all Kotlin dependencies (other Kotlin modules from my project, coroutines, atomicfu, etc.) are bundled inside the resulting
.js
file. So if I don't have any NPM direct or transitive dependencies, then there is only one single
.js
file created with everything bundled. But on Kotlin 1.7.0 there is separate
.js
file per each dependency:
kotlinx.coroutines-kotlinx-coroutines-core-js-ir.js
,
88b0986a7186d029-atomicfu-js-ir.js
, and also files for each of my project's modules. Is this change intentional?
b
It is, there used yo be a compiler flag for that
Note that it only applies to kjs dependencies, not npm
a
Do you remember the name of the flag?
b
-Xir-per-module i think
That would enable 1.7.0 behaviour
a
So now this flag is always enabled, and this is the new behavior, and there is no switch off?
b
I'd assume so
Maybe try setting to false explicitly
πŸ‘ 1
a
So, this mode is enabled by default since
1.6.20
. To switch it off:
Copy code
# gradle.properties
kotlin.js.ir.output.granularity=whole-program // `per-module` is the default
b
Awesome to know!
a
Well, with this parameter in gradle.properties, the resulting js file has all modules bundled indeed. But there are still separate js files produced for each module (that perhaps unused?).
b
Did you clean before build?
a
Yep
Oh no, I cleaned only the app's build folder. After
./gradlew clean
everything is good! Thanks!
πŸ˜„ 1
Just in case someone wants Gradle tasks to webpack
binaries.library()
. I'm using the following snippet by running
./gradlew browserLibraryBundle
, the output is in
build/dist
folder.
b
You could also hook into KGP nodejs executable and setup tasks to avoid needing a separate global nodejs installation on the machine
πŸ”₯ 1
a
Thanks!