From 1eb9b62b014239ce622e904f4444e6b767299fea Mon Sep 17 00:00:00 2001 From: ChainsDD Date: Fri, 17 Aug 2018 09:14:45 -0500 Subject: [PATCH 1/3] Automatically use appropriate webpack mode based on run/build testSimpleProjectWebPackBundleWithDce had to be modified to pass, since the output bundle was now being minified. This was causing the last assertion to fail, since "usedFunction2222" was being changed to something shorter. --- README.md | 3 +++ .../webpack/GenerateWebPackConfigTask.kt | 1 - .../frontend/webpack/NodeModuleVersion.kt | 27 +++++++++++++++++++ .../frontend/webpack/WebPackBundleTask.kt | 15 +++-------- .../frontend/webpack/WebPackExtension.kt | 3 ++- .../gradle/frontend/webpack/WebPackRunTask.kt | 3 +++ .../webpack/webpack-dev-server-launcher.js | 1 + .../frontend/SimpleFrontendProjectTest.kt | 1 + 8 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt diff --git a/README.md b/README.md index 2d66480..aafd199 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ kotlinFrontend { port = 8088 // dev server port proxyUrl = "" | "http://...." // URL to be proxied, useful to proxy backend webserver stats = "errors-only" // log level + mode = "production" | "development" | "none" // used for webpack 4+ build-in optimizations } } ``` @@ -116,6 +117,8 @@ dev server log is located at `build/logs/webpack-dev-server.log` config file is generated at `build/webpack.config.js` +If mode is not specified, the default behavior is to use development mode for running the dev server, and production mode for creating the final bundle. See [webpack documentation](https://webpack.js.org/concepts/mode/) for more information about modes. + ## webpack configuration customization To customize webpack configuration, you can apply additional scripts by placing them in the directory `webpack.config.d`. The scripts will be appended to the end of config script. Use number prefix to change order (it is very similar to UNIX rc.d config directories) diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/GenerateWebPackConfigTask.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/GenerateWebPackConfigTask.kt index 9efc485..4d0b540 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/GenerateWebPackConfigTask.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/GenerateWebPackConfigTask.kt @@ -130,7 +130,6 @@ open class GenerateWebPackConfigTask : DefaultTask() { val resolveRoots = getModuleResolveRoots(false) val json = linkedMapOf( - "mode" to bundle.mode, "context" to getContextDir(false).absolutePath, "entry" to mapOf( bundle.bundleName to kotlinOutput(project).nameWithoutExtension.let { "./$it" } diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt new file mode 100644 index 0000000..bc6d811 --- /dev/null +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt @@ -0,0 +1,27 @@ +package org.jetbrains.kotlin.gradle.frontend.webpack + +import groovy.json.JsonSlurper +import org.gradle.api.GradleException +import org.gradle.api.Project +import kotlin.reflect.KProperty + +class NodeModuleVersion(project: Project, module: String) { + @Suppress("UNCHECKED_CAST") + val version: String = project.buildDir.resolve("node_modules/$module/package.json") + .let { JsonSlurper().parse(it) as Map }["version"] + ?.let { it as String } ?: throw GradleException("Module \"$module\" not found") + + val major: Int by VersionComponent(0) + + val minor: Int by VersionComponent(1) + + val patch: Int by VersionComponent(2) + + private class VersionComponent(val component: Int) { + operator fun getValue(thisRef: NodeModuleVersion, property: KProperty<*>): Int { + return thisRef.version.split(".") + .getOrNull(component) + ?.toInt() ?: 0 + } + } +} \ No newline at end of file diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt index 5a43c80..4f04698 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt @@ -1,6 +1,5 @@ package org.jetbrains.kotlin.gradle.frontend.webpack -import groovy.json.JsonSlurper import org.gradle.api.* import org.gradle.api.tasks.* import org.jetbrains.kotlin.gradle.frontend.util.* @@ -29,23 +28,15 @@ open class WebPackBundleTask : DefaultTask() { @TaskAction fun buildBundle() { - @Suppress("UNCHECKED_CAST") - val webpackVersion = project.buildDir.resolve("node_modules/webpack/package.json") - .let { JsonSlurper().parse(it) as Map }["version"] - ?.let { it as String } - val processBuilderCommands = arrayListOf( nodePath(project, "node").first().absolutePath, project.buildDir.resolve("node_modules/webpack/bin/webpack.js").absolutePath, "--config", webPackConfigFile.absolutePath ) - val webpackMajorVersion = webpackVersion - ?.split('.') - ?.firstOrNull() - ?.toInt() - if (webpackMajorVersion != null && webpackMajorVersion >= 4) { + + if (NodeModuleVersion(project, "webpack").major >= 4) { processBuilderCommands.addAll(arrayOf( - "--mode", config.mode + "--mode", config.mode ?: "production" )) } ProcessBuilder(processBuilderCommands) diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackExtension.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackExtension.kt index 37349b8..4f95104 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackExtension.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackExtension.kt @@ -41,5 +41,6 @@ open class WebPackExtension(project: Project) : BundleConfig { var webpackConfigFile: Any? = null @Input - var mode: String = "development" + @Optional + var mode: String? = null } diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt index 15ec93a..2bfe85c 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt @@ -77,6 +77,9 @@ open class WebPackRunTask : AbstractStartStopTask() { .replace("require('\$RunConfig\$')", JsonBuilder(GenerateWebpackHelperTask.config(project, config, webPackConfigFile)).toPrettyString() ) + .replace("\$SetMode\$\n", + if (NodeModuleVersion(project, "webpack").major >= 4) "config.mode = \"${config.mode ?: "development"}\";\n" else "" + ) ) try { diff --git a/kotlin-frontend/src/main/resources/kotlin/webpack/webpack-dev-server-launcher.js b/kotlin-frontend/src/main/resources/kotlin/webpack/webpack-dev-server-launcher.js index 5f5a02b..2e2a506 100644 --- a/kotlin-frontend/src/main/resources/kotlin/webpack/webpack-dev-server-launcher.js +++ b/kotlin-frontend/src/main/resources/kotlin/webpack/webpack-dev-server-launcher.js @@ -11,6 +11,7 @@ var devServerVersion = require(__dirname + '/node_modules/webpack-dev-server/pac var RunConfig = require('$RunConfig$'); var config = require(RunConfig.webPackConfig); +$SetMode$ for (var name in config.entry) { if (config.entry.hasOwnProperty(name)) { diff --git a/kotlin-frontend/src/test/kotlin/org/jetbrains/kotlin/gradle/frontend/SimpleFrontendProjectTest.kt b/kotlin-frontend/src/test/kotlin/org/jetbrains/kotlin/gradle/frontend/SimpleFrontendProjectTest.kt index 6444d56..c63a3c0 100644 --- a/kotlin-frontend/src/test/kotlin/org/jetbrains/kotlin/gradle/frontend/SimpleFrontendProjectTest.kt +++ b/kotlin-frontend/src/test/kotlin/org/jetbrains/kotlin/gradle/frontend/SimpleFrontendProjectTest.kt @@ -97,6 +97,7 @@ class SimpleFrontendProjectTest(gradleVersion: String, kotlinVersion: String) : block("webpackBundle") { line("port = $port") line("bundleName = \"main\"") + line("mode = \"development\"") } } From 85333dc0811cc30e8a5773ba3218f4980da24232 Mon Sep 17 00:00:00 2001 From: ChainsDD Date: Fri, 17 Aug 2018 09:27:12 -0500 Subject: [PATCH 2/3] Rename and move NodeModuleVersion to a more appropriate place --- .../NodeModuleVersion.kt => npm/NpmModuleVersion.kt} | 6 +++--- .../kotlin/gradle/frontend/webpack/WebPackBundleTask.kt | 3 ++- .../kotlin/gradle/frontend/webpack/WebPackRunTask.kt | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) rename kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/{webpack/NodeModuleVersion.kt => npm/NpmModuleVersion.kt} (78%) diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/npm/NpmModuleVersion.kt similarity index 78% rename from kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt rename to kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/npm/NpmModuleVersion.kt index bc6d811..0164004 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/NodeModuleVersion.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/npm/NpmModuleVersion.kt @@ -1,11 +1,11 @@ -package org.jetbrains.kotlin.gradle.frontend.webpack +package org.jetbrains.kotlin.gradle.frontend.npm import groovy.json.JsonSlurper import org.gradle.api.GradleException import org.gradle.api.Project import kotlin.reflect.KProperty -class NodeModuleVersion(project: Project, module: String) { +class NpmModuleVersion(project: Project, module: String) { @Suppress("UNCHECKED_CAST") val version: String = project.buildDir.resolve("node_modules/$module/package.json") .let { JsonSlurper().parse(it) as Map }["version"] @@ -18,7 +18,7 @@ class NodeModuleVersion(project: Project, module: String) { val patch: Int by VersionComponent(2) private class VersionComponent(val component: Int) { - operator fun getValue(thisRef: NodeModuleVersion, property: KProperty<*>): Int { + operator fun getValue(thisRef: NpmModuleVersion, property: KProperty<*>): Int { return thisRef.version.split(".") .getOrNull(component) ?.toInt() ?: 0 diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt index 4f04698..f3bdc4f 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt @@ -2,6 +2,7 @@ package org.jetbrains.kotlin.gradle.frontend.webpack import org.gradle.api.* import org.gradle.api.tasks.* +import org.jetbrains.kotlin.gradle.frontend.npm.NpmModuleVersion import org.jetbrains.kotlin.gradle.frontend.util.* /** @@ -34,7 +35,7 @@ open class WebPackBundleTask : DefaultTask() { "--config", webPackConfigFile.absolutePath ) - if (NodeModuleVersion(project, "webpack").major >= 4) { + if (NpmModuleVersion(project, "webpack").major >= 4) { processBuilderCommands.addAll(arrayOf( "--mode", config.mode ?: "production" )) diff --git a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt index 2bfe85c..58435c8 100644 --- a/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackRunTask.kt @@ -4,6 +4,7 @@ import groovy.json.* import org.codehaus.groovy.runtime.* import org.gradle.api.* import org.gradle.api.tasks.* +import org.jetbrains.kotlin.gradle.frontend.npm.NpmModuleVersion import org.jetbrains.kotlin.gradle.frontend.servers.* import org.jetbrains.kotlin.gradle.frontend.util.* import java.io.* @@ -78,7 +79,7 @@ open class WebPackRunTask : AbstractStartStopTask() { JsonBuilder(GenerateWebpackHelperTask.config(project, config, webPackConfigFile)).toPrettyString() ) .replace("\$SetMode\$\n", - if (NodeModuleVersion(project, "webpack").major >= 4) "config.mode = \"${config.mode ?: "development"}\";\n" else "" + if (NpmModuleVersion(project, "webpack").major >= 4) "config.mode = \"${config.mode ?: "development"}\";\n" else "" ) ) From 010386e1b6eb5d1855dbefa8cbb2a3503e4199ad Mon Sep 17 00:00:00 2001 From: Adam Shanks Date: Thu, 6 Sep 2018 09:04:51 -0500 Subject: [PATCH 3/3] Fix typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aafd199..375a57f 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ kotlinFrontend { port = 8088 // dev server port proxyUrl = "" | "http://...." // URL to be proxied, useful to proxy backend webserver stats = "errors-only" // log level - mode = "production" | "development" | "none" // used for webpack 4+ build-in optimizations + mode = "production" | "development" | "none" // used for webpack 4+ built-in optimizations } } ```