This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
GenerateWebPackConfigTask.kt
140 lines (113 loc) · 4.88 KB
/
GenerateWebPackConfigTask.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.jetbrains.kotlin.gradle.frontend.webpack
import groovy.json.*
import groovy.lang.*
import org.gradle.api.*
import org.gradle.api.artifacts.*
import org.gradle.api.tasks.*
import org.jetbrains.kotlin.gradle.frontend.util.*
import org.jetbrains.kotlin.gradle.tasks.*
import java.io.*
/**
* @author Sergey Mashkov
*/
open class GenerateWebPackConfigTask : DefaultTask() {
private val configsDir: File
get() = project.projectDir.resolve("webpack.config.d")
@Input
val projectDirectory = project.projectDir.absolutePath
@get:Input
val contextDir by lazy { kotlinOutput(project).parentFile.absolutePath!! }
@get:Internal
val bundles by lazy { project.frontendExtension.bundles().filterIsInstance<WebPackExtension>() }
@get:Input
private val bundleNameInput: Any by lazy { bundles.singleOrNull()?.bundleName ?: "" }
@get:Input
private val publicPathInput: Any by lazy { bundles.singleOrNull()?.publicPath ?: "" }
@get:Input
private val outputFileName by lazy { kotlinOutput(project).name }
@get:Input
val bundleDirectory by lazy { handleFile(project, project.frontendExtension.bundlesDirectory) }
@OutputFile
val webPackConfigFile: File = project.buildDir.resolve("webpack.config.js")
@Input
val exts = project.frontendExtension.defined
init {
(inputs as TaskInputs).dir(configsDir).optional()
onlyIf {
bundles.size == 1 && bundles.single().webpackConfigFile == null
}
}
@TaskAction
fun generateConfig() {
val bundle = bundles.singleOrNull() ?: throw GradleException("Only single webpack bundle supported")
val resolveRoots = mutableListOf(
File(contextDir).toRelativeString(project.buildDir),
project.buildDir.resolve("node_modules").toRelativeString(project.buildDir),
project.buildDir.resolve("node_modules").absolutePath
)
project.configurations.findByName("compile")?.allDependencies
?.filterIsInstance<ProjectDependency>().orEmpty()
.mapNotNull { it.dependencyProject }
.flatMap { it.tasks.filterIsInstance<Kotlin2JsCompile>() }
.filter { !it.name.contains("test") }
.mapNotNull { it.outputFile }
.map { project.file(it) }
.map { resolveRoots.add(it.parentFile.toRelativeString(project.buildDir)) }
val json = linkedMapOf(
"context" to contextDir,
"entry" to mapOf(
bundle.bundleName to kotlinOutput(project).nameWithoutExtension.let { "./$it" }
),
"output" to mapOf(
"path" to bundleDirectory.absolutePath,
"filename" to "[name].bundle.js",
"chunkFilename" to "[id].bundle.js",
"publicPath" to bundle.publicPath
),
"module" to mapOf(
"rules" to emptyList<Any>()
),
"resolve" to mapOf(
"modules" to resolveRoots
),
"plugins" to listOf<Any>()
)
webPackConfigFile.bufferedWriter().use { out ->
out.appendln("'use strict';")
out.appendln()
out.appendln("var webpack = require('webpack');")
out.appendln()
out.append("var config = ")
out.append(JsonBuilder(json).toPrettyString())
out.appendln(";")
if (exts.isNotEmpty()) {
out.append("var defined = ")
out.append(JsonBuilder(exts).toPrettyString())
out.appendln(";")
out.appendln("config.plugins.push(new webpack.DefinePlugin(defined));")
}
out.appendln()
out.appendln("module.exports = config;")
out.appendln()
val p = "\\d+$".toRegex()
configsDir.listFiles()?.sortedBy { p.find(it.nameWithoutExtension)?.value?.toInt() ?: 0 }?.forEach {
out.appendln("// from file ${it.path}")
it.reader().use {
it.copyTo(out)
}
out.appendln()
}
}
}
companion object {
fun handleFile(project: Project, dir: Any): File {
return when (dir) {
is String -> File(dir).let { if (it.isAbsolute) it else project.buildDir.resolve(it) }
is File -> dir
is Function0<*> -> handleFile(project, dir() ?: throw IllegalArgumentException("function for webPackConfig.bundleDirectory shoudln't return null"))
is Closure<*> -> handleFile(project, dir.call() ?: throw IllegalArgumentException("closure for webPackConfig.bundleDirectory shoudln't return null"))
else -> project.file(dir)
}
}
}
}