Building my project with Gradle, I can use `js { m...
# javascript
h
Building my project with Gradle, I can use
js { moduleName = "myModuleName" }
to adjust my module name, but according to the docs, this «_does not affect the webpacked output_». In the top of the produced .js file in my project, I find this:
root["jsapi"] = factory()
. Now how do I go about having Gradle change that string from the project name (jsapi) into something else?
s
Copy code
kotlin {
  js {
    browser {
      webpackTask {
        outputFileName = "my-output.js"
      }
      runTask {
        outputFileName = "my-output.js"
      }
    }
  }
}
h
So changing the filename will also change that string inside the file? OK, I'll give it a try!
s
Nope. This only changes the output file name. Changing the name of the module inside the file is done with
moduleName
.
h
No, not according to the docs, and not to my experience.
s
The webpack output does not contain the module name. What String are you talking about ?
h
In my example above, the "jsapi" string. This ends up as the name of the JS object in my js file. "jsapi" is the name of my project, but I wish to name the javascript object otherwise. My solution for now is to change the contents of the output file with a
doFirst { }
clause in the jar task, but I was hoping the compile/build process could take care of this by itself, somehow.
s
Ah. Sorry, thought you were talking about the webpack output ><.
h
That is in the webpack output, as far as I can tell.
s
I fail to understand. The webpack output does not contain any String with the project name.
Can you give a sample project ?
h
Well, actually, it's not even a webpack. I am building a udm module and packing it in a webjar. But
gradle build
results in a js file that starts like this:
Copy code
(function webpackUniversalModuleDefinition(root, factory) {
        if(typeof exports === 'object' && typeof module === 'object')
                module.exports = factory();
        else if(typeof define === 'function' && define.amd)
                define([], factory);
        else if(typeof exports === 'object')
                exports["jsapi"] = factory();
        else
                root["jsapi"] = factory();
})(window, function() { ...
... and I thought setting the moduleName in the
js { }
block would do the trick for changing "jsapi" into my module name.
I think I'll just stick to my workaround for now, if there is nothing obvious that I have missed.
👍 1