Skip to content
Snippets Groups Projects
Commit a6bb3e56 authored by Maxim Shafirov's avatar Maxim Shafirov
Browse files

Refactor loader to a full webpack plugin. Declares aliases for kotlin module...

Refactor loader to a full webpack plugin. Declares aliases for kotlin module names and watches sources to re-run gradle build.
parent aa39eb07
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions" suppressed-tasks="Babel" />
</project>
\ No newline at end of file
# kwp
webpack loader for javascript artifacts produced by Kotlin.JS Gradle builds
webpack plugin for resolving modules produced by Kotlin.JS Gradle builds
 
This comes as Gradle plugin, which adds webpack_loader task to a project, which scans project jar artifacts for generated javascript files
and generates a module to require them in proper order.
Also this provides a webpack loader, that handles build.gradle files and accepts gradle project name as query parameter to require request.
as well as Webpack plugin, which declares an alias for each Kotlin module to it resulting javascript output. Webpack plugin is also watching
Kotlin sourcefiles and launches Webpack build appropriately.
 
For example of usage please look at https://github.com/shafirov/kwp-sample
 
Loading
Loading
@@ -19,7 +18,7 @@ buildscript {
}
 
dependencies {
classpath "org.jetbrains.kwp:kwp:0.1.5"
classpath "org.jetbrains.kwp:kwp:0.1.7"
}
}
```
Loading
Loading
@@ -33,33 +32,20 @@ This is it for gradle part of setup. Now you need to hook up a loader that comes
define ```Kotlin``` global via ProvidePlugin.
 
```javascript
var path = require('path');
var KotlinWebpackPlugin = require('./build/kwp/kwp')
 
var webpackConfig = {
...
module: {
loaders: [
{
test: /\.gradle\?.*$/,
loaders: [
'./build/kwp/kwp'
]
}
]
}
...
plugins: [
new webpack.ProvidePlugin({
Kotlin: path.join(__dirname, 'build/kwp/kotlin-js-library-1.0.2-1.js')
new KotlinWebpackPlugin({
buildFile: './build.gradle',
project: 'showcase'
})
]
```
 
As of Kotlin 1.0.3 Kotlin.js modules do not export anything so you won't be able to use Kotlin code from javascript modules easily.
So you just require some Kotlin modules to be included in webpack build target for now:
Then you can just require some Kotlin modules:
 
```javascript
require('./build.gradle?projectname')
require('showcase')
```
Loading
Loading
@@ -26,7 +26,7 @@ class KWP implements Plugin<Project> {
installKwp(targetDir, true)
}
 
target.tasks['jar'] << {
rootProject.tasks['clean'] << {
installKwp(targetDir, false)
}
 
Loading
Loading
@@ -37,14 +37,18 @@ class KWP implements Plugin<Project> {
 
targetDir.mkdirs()
 
writeSafely(new File(targetDir, "__modules.js")) { buf ->
writeSafely(new File(targetDir, "__modules.txt")) { buf ->
dependencies.each {jar ->
def m = unzipSafely(jar, targetDir)
buf.append("require('${normalizedAbsolutePath(m)}');\n")
def moduleName = m.name.substring(0, m.name.lastIndexOf('.'))
if (moduleName.startsWith("kotlin-js-library")) {
moduleName = "kotlin"
}
buf.append("${ moduleName}:${normalizedAbsolutePath(m)}\n")
}
}
 
writeSafely(new File(targetDir, "__deps.txt")) { buf ->
writeSafely(new File(targetDir, "__sources.txt")) { buf ->
sources.each {
buf.append("$it\n")
}
Loading
Loading
var path = require("path");
var fs = require('fs')
var exec = require('child_process').exec
var execSync = require('child_process').execSync
var isWin = /^win/.test(process.platform);
 
module.exports = function(content) {
var loader = this
loader.cacheable()
var callback = loader.async()
var ModuleAliasPlugin = require("webpack/node_modules/enhanced-resolve/lib/ModuleAliasPlugin");
function KotlinWebpackPlugin(options) {
this.options = options
}
 
var fail = function(message) {
callback(message)
KotlinWebpackPlugin.prototype.apply = function (compiler) {
var buildFile = path.resolve(compiler.context, this.options.buildFile)
if (!buildFile) {
console.error("Cannot resolve build file: ", this.options.buildFile)
return
}
 
var success = function (res, map) {
callback(null, res, map)
var project = this.options.project
if (!project) {
console.error("kwp needs 'buildFile' and 'project' properties specified")
return
}
 
var buildFile = path.resolve(loader.resourcePath);
var root = path.join(buildFile, "../")
var project = loader.resourceQuery.substr(1)
var gradle = path.join(root, "gradlew")
if (isWin) {
gradle += ".bat"
Loading
Loading
@@ -28,38 +30,51 @@ module.exports = function(content) {
 
gradle = path.resolve(gradle)
if (!gradle) {
fail("Cannot find ./gradlew")
console.error("Cannot find ./gradlew")
return
}
 
if (!buildFile) {
fail("Cannot resolve build file: " + buildArg)
return
var cmdline = gradle + " " + "--build-file " + buildFile + " " + project + ":webpack_loader";
var running = false
function execGradle() {
if (running) return
running = true
console.log("\nRunning Gradle: " + cmdline)
console.time("Done Gradle")
var stdout = execSync(cmdline)
// console.log(stdout)
console.timeEnd("Done Gradle")
running = false
}
 
console.time("Done Gradle")
var cmdline = gradle + " " + "--build-file " + buildFile + " " + project + ":webpack_loader";
console.log("Running Gradle: " + cmdline)
execGradle()
 
exec(cmdline, function(err, stdout, stderr) {
console.log(stderr)
if (err) {
console.log(stdout)
fail(err)
var deps = fs.readFileSync(path.join(root, 'build/kwp/__sources.txt'), "UTF-8").split("\n")
for (var i = 0; i < deps.length; i++) {
var t = deps[i].trim()
if (t.length > 0) {
fs.watch(t, {
recursive: true
}, execGradle)
}
else {
// console.log(stdout)
var deps = fs.readFileSync(path.join(root, 'build/kwp/__deps.txt'), "UTF-8").split("\n")
}
 
for (var i = 0; i < deps.length; i++) {
var t = deps[i].trim()
if (t.length > 0) {
loader.addContextDependency(t)
}
}
var aliasMap = {}
var modules = fs.readFileSync(path.join(root, 'build/kwp/__modules.txt'), "UTF-8").split("\n")
for (var i = 0; i < modules.length; i++) {
var line = modules[i].trim()
if (line.length > 0) {
var sep = line.indexOf(':')
var m_name = line.substring(0, sep).trim()
var m_path = line.substring(sep + 1).trim()
 
success(fs.readFileSync(path.join(root, 'build/kwp/__modules.js'), "UTF-8"))
console.timeEnd("Done Gradle")
aliasMap[m_name] = m_path
}
})
}
compiler.resolvers.normal.apply(new ModuleAliasPlugin(aliasMap))
}
module.exports = KotlinWebpackPlugin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment