Why are imports from ES modules mangled in the res...
# javascript
h
Why are imports from ES modules mangled in the resulting mjs, like
node_process_process_dcpv5o
?
Copy code
// kotlin external library
package node.process

@JsModule("node:process")
external val process: Process

// Kotlin my code
val Context.token: String get() = process.env["GITHUB_TOKEN"]!!

// mjs:
import { ensureNotNull1e947j3ixpazm as ensureNotNull } from './kotlin-kotlin-stdlib.mjs';
import node_process_process_dcpv5o from 'node:process';
//region block: imports
//endregion
//region block: pre-declaration
//endregion
function get_token(_this__u8e3s4) {
  return ensureNotNull(node_process_process_dcpv5o.env['GITHUB_TOKEN']);
}
//region block: exports
export {
  get_token as get_token5mn2ctm2tja1,
};
//endregion
Instead of
node_process_process_dcpv5o
, shouldn't it be
process
?
t
AFAIK there is a compilation flag, which can disable mangling.
Instead of
node_process_process_dcpv5o
, shouldn't it be
process
? (edited)
If mangling enabled (default) compiler can use any safe name 🙂
h
But should it also mangle external js imports?
How does the import work when it is mangled?
t
When you import whole library/module (your case) you can use any name
h
Oh okay, but I want to import the process const 🤔 not the package
t
Node.js full of old-school const-packages 🙂
process
,
path
...
h
Hm, but how do I use the
process
from kotlin-wrappers then? 🤔
t
Global
process
and
node:process
are equal
Single difference
node:process
is safer
Copy code
import nodeProcess from 'node:process'

nodeProcess === process // true
h
Honestly, I don't get it: I want to use https://github.com/JetBrains/kotlin-wrappers/blob/ff98abbb082d2b3887ebd719aa8b942e869610be/kotlin-node/src/jsMain/kotlin/node/process/process.export.kt#L6, and as a workaround I use this without any import `js("'node:process'.process")`: https://github.com/hfhbd/kotlin-actions/blob/fdcd12317cc36171af7b4b093df34a6c13590c8d/runtime/src/jsMain/kotlin/actions-github.kt#L32 This code also did not work:
Copy code
package node.process

@JsModule("process")
external val process: Process
So I guessed, it is due the (wrong) imports. Because webpack creates a runtime error wrongly using
import.meta.url
when enabling es modules. And now I don't know if this is a bug in kotlin-node, Kotlin compiler or web pack (my config or compiler).
Wait, you also import the whole package, not
import { process } from 'node:process'}
🤔
t
You can do this check in scratches:
Copy code
import nodeProcess from "node:process"

console.log(nodeProcess === process)
process
declaration and import are fine
Screenshot 2024-05-05 at 20.29.05.png
and as a workaround I use this without any import
js("'node:process'.process")
Magic 🙂 Looks like you call
process
property from string
For diagnostic you can use following declaration:
Copy code
// Global
external val process: Process
AFAIK DefinePlugin from Webpack works fine with global
process
only
I'm personally use tokens, which are platform independent (without
process.env.
) call
You can use
GITHUB_TOKEN
val for start
Copy code
@JsName("process.env.GITHUB_TOKEN") // process.env.GITHUB_TOKEN - token
external val GITHUB_TOKEN: String
But for fine solution custom tokens - my recommendation
Copy code
@JsName("___GITHUB_TOKEN___") // ___GITHUB_TOKEN___ - token
external val GITHUB_TOKEN: String
external val
- guarantees, that you will have token in required place in JS
@hfhbd Does it work for you? Or you need sample?
h
Thanks for all your input, but I don't want to add a custom behavior for the GitHub token, all es modules should just work. And I managed to get it working, by using
es2022
target instead of
node
but now I do have problems with some commonjs imports caused by the GitHub toolkit dependency. This is my branch if you are interested: https://github.com/hfhbd/kotlin-actions/pull/55
t
es2022
target for Webpack?
h
Yes