JoakimForslund
11/09/2019, 10:40 PMmodule {__esModule: true, Symbol(Symbol.toStringTag): "Module"}
The code that is supposted to be exported is from multiplatform common, but I also tried to export directly from the javascript target. What am I missing?Ilya Goncharov [JB]
11/11/2019, 9:44 AMJoakimForslund
11/11/2019, 10:48 AMcontents = contents.replace("(function (_, Kotlin) {", "export default (function (_, Kotlin) {")
contents = contents.replace("}(module.exports, require('kotlin')));", "}({}, require('kotlin')));")
and then you need to make sure as you said, that webpack uses the library mode. Which for whatever reason works the best when using commonjs2 as a targetJoakimForslund
11/11/2019, 10:50 AMtypeof
JoakimForslund
11/11/2019, 10:53 AMIlya Goncharov [JB]
11/11/2019, 10:54 AMbuild/js/packages/<package-name>/kotlin
folderJoakimForslund
11/11/2019, 10:56 AMJoakimForslund
11/11/2019, 10:56 AMIlya Goncharov [JB]
11/11/2019, 10:58 AMwebpack.config.d
folder in your project root
In this folder you can create js
file with configuration of webpack config object, that you can call with config
For example
webpack.config.d/additional.js
config.output = config.output || {};
config.output.library = 'lib-commin'
config.output.libraryTarget = 'comminjs2'
config.output.libraryExport = 'default'
JoakimForslund
11/11/2019, 11:01 AM"@babel/runtime": "^7.6.0",
Is needed, I'm not sure if this is because of the project using the library or not, but I figure it should be the sameJoakimForslund
11/11/2019, 11:02 AMnpm ../build/js/packages/<package-name>/
And try to import that, I get issues with babel and typeof
JoakimForslund
11/11/2019, 11:02 AMJoakimForslund
11/11/2019, 11:04 AMJoakimForslund
11/11/2019, 11:10 AMkotlinx-serialization-kotlinx-serialization-runtime
, since there is no npm dependency in existance for that right now) In the test of using the above folder as install sourceIlya Goncharov [JB]
11/11/2019, 11:12 AMbuild.gradle
file?JoakimForslund
11/11/2019, 11:13 AMdependencies { implementation..}
in the javascript targetIlya Goncharov [JB]
11/11/2019, 11:17 AMExperimental support for NPM and Webpack
JoakimForslund
11/11/2019, 11:18 AMJoakimForslund
11/11/2019, 11:18 AMJoakimForslund
11/11/2019, 11:21 AMnpm ../build/js/packages/<package-name>/
and then later in the webapp code using the exported library (which is transpiled with babel)
i run import * as libObj from 'lib-common';
When trying to print the libObj
object to console
I get Cannot read property 'MyPackageName' of undefined
Which then points me towards this section:
var package$MyPackageName = _.MyPackageName || (_.MyPackageName = {});
package$MyPackageName.CoreRepository = CoreRepository;
package$MyPackageName.Device = Device;
package$MyPackageName.Game = Game;
JoakimForslund
11/11/2019, 11:24 AM(function (_, Kotlin, $module$kotlinx_coroutines_core) {
never sends in the module.exportsJoakimForslund
11/11/2019, 11:25 AM_
is in this case undefinedIlya Goncharov [JB]
11/11/2019, 11:25 AMIlya Goncharov [JB]
11/11/2019, 11:25 AMJoakimForslund
11/11/2019, 11:25 AM/build/js/packages/<package-name>/
not pure?JoakimForslund
11/11/2019, 11:30 AMIlya Goncharov [JB]
11/11/2019, 11:35 AMJoakimForslund
11/11/2019, 11:36 AMJoakimForslund
11/11/2019, 11:36 AMdefault
JoakimForslund
11/11/2019, 11:37 AMkotlinOptions {
moduleKind = "commonjs"
metaInfo = true
sourceMap = false
sourceMapEmbedSources = null
//outputFile = jsOutputFile
}
JoakimForslund
11/11/2019, 12:11 PMnpm install ../build/js/packages/companyxr-common
while inside the webapp
-folder
and then try npm run start
This will give you an error with @babel and that it needs runtime
, once you get passed that
you will run into the error i posted aboveJoakimForslund
11/11/2019, 12:12 PMIlya Goncharov [JB]
11/11/2019, 12:48 PMnode_modules
, then tries to find in parent’s node_modules
etc
When you run webpack in your webapp
folder, you have babel installed only in ./webapp
node_modules
, but Kotlin/JS module is parallel hierarchy level in ./build/js
When webpack build bundle, it finds babel’s dependencies for Kotlin/JS module in build/js/node_modules
instead of webapp/node_modules
, where babel is installedJoakimForslund
11/11/2019, 12:54 PMpackage.json
and webpack.config.js
in order to solve that problemJoakimForslund
11/11/2019, 12:55 PMexports default
or module.exports should behaveJoakimForslund
11/11/2019, 12:56 PMIlya Goncharov [JB]
11/11/2019, 12:56 PMresolve.modules
for your webpack in webapp
https://webpack.js.org/configuration/resolve/#resolvemodules
You should define it in absolute style to point to node_modules of your webapp or in relative style based on build/js/packages/<package-name>/kotlin
For example:
resolve.modules = ['node_modules', '../../../../../webapp/node_modules]
2. You can change your layout
You need to declare your own package.json
in root of project
In this case, npm install modules in root node_modules
and node should find babelJoakimForslund
11/11/2019, 12:58 PMresolve.modules = ['node_modules', '../../../../../webapp/node_modules]
inside the folder webpack.config.d
as a new file?Ilya Goncharov [JB]
11/11/2019, 1:00 PMwebpack.config.js
in webapp
I added to it:
resolve: {
modules: ['node_modules', '../../../../../webapp/node_modules']
}
Ilya Goncharov [JB]
11/11/2019, 1:00 PMIlya Goncharov [JB]
11/11/2019, 1:03 PMUncaught TypeError: Cannot read property 'company' of undefined
Without babel-loader, your sample works well
It can be babel-related problemJoakimForslund
11/11/2019, 1:04 PMJoakimForslund
11/11/2019, 1:05 PMcontents = contents.replace("(function (_, Kotlin) {", "export default (function (_, Kotlin) {")
contents = contents.replace("}(module.exports, require('kotlin')));", "}({}, require('kotlin')));")
in the custom task that I created (Which is currently commented out)JoakimForslund
11/11/2019, 1:05 PMexport default
JoakimForslund
11/11/2019, 1:06 PM_
is indeed undefinedJoakimForslund
11/11/2019, 1:13 PMIlya Goncharov [JB]
11/11/2019, 1:14 PMmodule.exports
valueJoakimForslund
11/11/2019, 1:17 PM"@babel/plugin-transform-modules-commonjs": "^7.6.0",
to devDependencies in the webappJoakimForslund
11/11/2019, 1:17 PMJoakimForslund
11/11/2019, 1:17 PMJoakimForslund
11/11/2019, 1:18 PMplugins: [
'@babel/plugin-transform-regenerator',
'@babel/plugin-transform-runtime',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-transform-modules-commonjs'
],
Ilya Goncharov [JB]
11/11/2019, 1:19 PMJoakimForslund
11/11/2019, 1:21 PMJoakimForslund
11/11/2019, 1:23 PMIlya Goncharov [JB]
11/11/2019, 1:34 PMJoakimForslund
11/11/2019, 1:36 PMbuild/js/packages/companyxr-common/package.json
as a dependency that does not existJoakimForslund
11/11/2019, 1:36 PMnpm ERR! 404 Not Found - GET <https://registry.npmjs.org/kotlinx-serialization-kotlinx-serialization-runtime> - Not found
Ilya Goncharov [JB]
11/11/2019, 1:53 PMcompanyxr-common
as dependencies in package.json, but as alias for webpackJoakimForslund
11/11/2019, 1:57 PMJoakimForslund
11/11/2019, 2:00 PMentry: {
"kotlin":"./src/kotlin.js",
"glootxr-common" :'./src/glootxr-common.js',
"kotlinx-serialization-kotlinx-serialization-runtime": "./src/kotlinx-serialization-kotlinx-serialization-runtime.js",
"kotlinx-coroutines-core":"./src/kotlinx-coroutines-core.js"
},
resolve: {
alias: {
"kotlin": path.resolve(__dirname, './src/kotlin.js'),
"kotlinx-serialization-kotlinx-serialization-runtime": path.resolve(__dirname, './src/kotlinx-serialization-kotlinx-serialization-runtime.js'),
"kotlinx-coroutines-core": path.resolve(__dirname, './src/kotlinx-coroutines-core.js')
},
extensions: [".js"]
},
inside of webpackJoakimForslund
11/11/2019, 2:01 PMIlya Goncharov [JB]
11/11/2019, 2:08 PMresolve
section of your webpack.config.js
alias: {
"companyxr-common": path.resolve(__dirname, "../build/js/packages/companyxr-common")
}
And not install companyxr-common via npmJoakimForslund
11/11/2019, 2:14 PMJoakimForslund
11/11/2019, 2:14 PMJoakimForslund
11/11/2019, 2:15 PMIlya Goncharov [JB]
11/11/2019, 4:25 PMJoakimForslund
11/11/2019, 7:10 PM