Is there an easy way to output a valid npm module ...
# javascript
j
Is there an easy way to output a valid npm module from the JS IR compiler?
Copy code
js(IR) {
    moduleName = project.name
    nodejs()
    binaries.executable()
}
The only relevant documentation I can find areย hereย andย here. There also appears to be a bug when the module name includes a group resulting in an invalid package main path:
r
j
@Robert Jaros good find, thank you!
Looking through that repo, there's an option
binaries.library()
which produces a folder called
productionLibrary
. This might be the ticket.
The transitive dependencies are not included. I get the sense that outputting an NPM library for usage in an existing project is not yet possible. ๐Ÿ˜ž
r
@Big Chungus
j
For reference here is the personal project that I am bootstrapping. The idea is to allow the server authors to maintain the JS/TS client as an npm package that's plug-and-play for app developers.
b
@Joel with my plugin above, you should get your library properly assembled after running gradle assemble You can find the assemblies in build/publications/npm
You can also run gradle pack to get installable npm tarballs
j
Thanks @Big Chungus. Your plugin is creating the library but there's an issue with yours and the built-in plugin when using transitive dependencies.
ktor-client-websockets
depends on a few npm packages such as
ws
. The IR compiler correctly compiles
ktor-client-websockets
into the output, but those transitive npm dependencies are not enumerated in
package.json
using either plugin.
b
They're not, because they're bundled into js output file via webpack
The plugin only includes dependencies declared as dependency(npm("xxx","1")) into package.json. All kotlin.js dependencies are bundled into js output instead (since they're not on npmjs)
j
ktor-client-websockets
relies on
ktor-client-core
which does have npm dependencies. See here.
b
And as such, transitive dependencies of kotlin.js dependencies also get bundled. But you should be able to install packed tarball into your node package regardless
j
I'm using the
nodejs
target, let me see if it works using
browser
which enable webpack
b
Try installing packed tarball first and see if it works
j
ok
@Big Chungus no dice ๐Ÿ˜ž
Error: Cannot find module 'ws'
Shall I try browser targer?
b
Ah, damn it. Give it a go with browser then.
If it still doesn't work, I'd appreciate you raising an issue on that plugin repo for me to look into. Add as much details as you can from your trials.
A small reproduce would also be a massive boost
j
Will do. I'll link to my project.
Note that this is also broken in the plugin provided by JetBrains. If you solve this you should probably notify them and put it on your resume. ๐Ÿ™‚
b
I've offered them a while a go to move my plugin features to kotlin plugins. Unfortunately they're stretched too thin for now to maintain that. I'll sort it out on npm publish over the weekend
Anyways, thanks for pointing this out!
j
That'd be great. I feel like being able to cleanly publish npm packages will greatly improve the chances of kotlin.js and MMP being adopted, since you can drop these into existing projects.
b
That's the idea behind the plugin. I'm trying to keep the experience on par with maven-publish.
๐Ÿ™ 1
Btw, the assembly should work fine with legacy mode for you ;)
j
Need that sweet TypeScript definiton ๐Ÿ˜ž
b
Hah, yeah, can't argue with that ๐Ÿ˜€
j
It feels like there are a bunch of bugs with packaging and IR compiler. The main and types paths are also wrong.
b
Well type declarations are still experimental. Also JB is now focusing at ir compiler stability over kotlin.js usability as a way of writing js libraries
j
Makes a lot of sense
b
@Joel, there you go npm-publish@v1.1.3 Let me know if you still face any issues (you shouldn't)!
j
@Big Chungus amazing!!! Iโ€™ll check this out today and report back. Canโ€™t wait and thank you for fixing so quickly.
@Big Chungus integrated here and working flawlessly. Is there a good way to DRY up the configuration for multi-module projects?
b
You can do that from root project via allprojects {}
Alternatively use gradle conventional plugins
j
Very cool, makes sense. I try to limit plugin application to the applicable modules, so my root is unaware of the
npmPublishing
extension. I figured there may be an incantation like
extensionWithType<NpmPublishingExtension>
but no cigar.
b
In that case use conventional plugins
j
That's a heck of a config
b
Yeah, multiplatform libs targeting ALL kotlin targets are not easy ๐Ÿ˜€
j
Think I'll stay off of native for now ๐Ÿ˜„
Lots of other good nuggets in here
b
And you should. I only linked to that to show what you can do with allprojecrs builder
K 1
๐Ÿ™ 1
j
I know a couple libraries that could really benefit from explicit API mode
I must've missed that one in the release notes
b
It came with 1.4.0 there's also explicitApiWarning() for easier migration
j
@Big Chungus another one for you ๐Ÿ˜ž
b
Clean your gradle global and local caches, I have encountered simmilar errors with my own convention plugins before
๐Ÿ‘ 1
Also what gradle version are you on?
j
6.8.3
OK well that problem is gone, now replaced by
Unresolved reference: npmPublishing
b
Can you reopen that issue and drop new console output?
Also post/link to your buildSrc/build.gradle.kts
j
Done. The code is linked in the original issue.
b
Did bumping kotlin version fix your issue?
j
Trying now
It's not happy about something
@Big Chungus I backed out your plugin entirely and added this single line to
buildSrc/build.gradle.kts
:
Copy code
implementation("lt.petuska:npm-publish:1.1.3")
Copy code
FAILURE: Build failed with an exception.

* Where:
Build file '/Users/joel/Code/bank/build.gradle.kts' line: 4

* What went wrong:
Error resolving plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.4.32', apply: false]
> Plugin request for plugin already on the classpath must not include a version
b
Ah, that's an error with your own setup. Remove kotlin(jvm) and kotlin(multiplatform) plugins from your root build.gradle.kts (they're applied for you somewhere else already
j
Right, but it builds fine when I remove npm-publish plugin, meaning you're defining my kotlin version and I'm not sure I like that ๐Ÿ™‚
Not that I don't trust you, but I want to control my kotlin version
b
it shouldn't, really.
Just double-checking in code
j
sec I'll push this branch
Here's the branch, if you back out this line it builds fine
b
I know what's up
๐Ÿฆœ 1
Since you're adding a plugin via convention plugin, it
hard
chooses transitive dependency versions (in this case kotlin). You can still override it by adding
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:_")
to your
buildSrc/build.gradle.kts
I guess it's just the way convention plugin dependencies are resolved
While it doesn't implicitly apply those transitive plugins, it adds them to the classpath via buildSrc and as such locks the version from further modifications outside of buildSrc
j
oh boy
b
which is kinda nice when you think about it, it allows you to manage ALL versions in buildSrc
As opposed to splitting between Versions object and buildSrc. Add refreshVersions plugin and it'll be even easier
j
After all that it's still unable to find
npmPublishing
b
push your latest and greatest. I'll have a look
j
I've never see the cache get corrupted so quickly than with this convention mechanism
Nothing is working right
b
can you temporary add me to that repo so i could push back?
j
Done
Copy code
joel@computer bank % ./gradlew clean
Watching the file system is not supported on this operating system.
> Task :buildSrc:extractPrecompiledScriptPluginPlugins UP-TO-DATE
> Task :buildSrc:generateExternalPluginSpecBuilders UP-TO-DATE
> Task :buildSrc:compilePluginsBlocks UP-TO-DATE
> Task :buildSrc:generatePrecompiledScriptPluginAccessors UP-TO-DATE
> Task :buildSrc:configurePrecompiledScriptDependenciesResolver
> Task :buildSrc:generateScriptPluginAdapters UP-TO-DATE

> Task :buildSrc:compileKotlin FAILED
The `kotlin-dsl` plugin applied to project ':buildSrc' enables experimental Kotlin compiler features. For more information see <https://docs.gradle.org/6.8.3/userguide/kotlin_dsl.html#sec:kotlin-dsl_plugin>
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (5, 1): Unresolved reference: npmPublishing
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (6, 5): Unresolved reference: organization
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (8, 5): Unresolved reference: publications
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (9, 9): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.get(key: TypeVariable(K)): TypeVariable(V)? defined in kotlin.collections
public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text
public operator fun <T : Any> NamedDomainObjectCollection<TypeVariable(T)>.get(name: String): TypeVariable(T) defined in org.gradle.kotlin.dsl
public operator fun ExtensionContainer.get(name: String): Any defined in org.gradle.kotlin.dsl
public inline fun <S : Any, T : SoftwareComponent> BinaryCollection<TypeVariable(T)>.get(type: KClass<TypeVariable(S)>, spec: Spec<in TypeVariable(S)>): BinaryProvider<TypeVariable(S)> defined in org.gradle.kotlin.dsl
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (9, 13): No get method providing array access
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (10, 13): Unresolved reference: repository
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (11, 17): Unresolved reference: url
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (17, 9): Unresolved reference: repository
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (18, 13): Unresolved reference: registry
e: /Users/joel/Code/bank/buildSrc/src/main/kotlin/springbank.npm-conventions.gradle.kts: (19, 13): Unresolved reference: authToken

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:compileKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at <https://help.gradle.org>

BUILD FAILED in 729ms
b
Apparently there's no
project.properties
during conventional plugins resolution... Had to refactor into using it the ugly way ๐Ÿ˜„
j
Ah so there was an issue in the plug-in?
Did the linter change the spacing on your PR? I thought it used four spaces.
b
Might've been my ide, apologies. Rerun it on that branch to fix.
And the issue was actually gradle bug, will report it tomorrow. My plugin simply surfaced that bug ๐Ÿ˜€
๐Ÿ‘Ž 1
j
Less than ideal. I think weโ€™re certainly at the bleeding edge right now.
Thanks again for handling this. Itโ€™s a very cool way of doing business.
b
The bug is irrelevant to the npm-publish plugin now as I've implemented a more verbose workaround. Also it only relates to convention plugins.
In any case, happy to fix the issues with the plugin. Thanks for such detailed reports!