diff --git a/README.md b/README.md index 2d66480..375a57f 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+ built-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/npm/NpmModuleVersion.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/npm/NpmModuleVersion.kt new file mode 100644 index 0000000..0164004 --- /dev/null +++ b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/npm/NpmModuleVersion.kt @@ -0,0 +1,27 @@ +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 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"] + ?.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: NpmModuleVersion, 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/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/WebPackBundleTask.kt b/kotlin-frontend/src/main/kotlin/org/jetbrains/kotlin/gradle/frontend/webpack/WebPackBundleTask.kt index 5a43c80..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 @@ -1,8 +1,8 @@ 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.npm.NpmModuleVersion import org.jetbrains.kotlin.gradle.frontend.util.* /** @@ -29,23 +29,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 (NpmModuleVersion(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..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.* @@ -77,6 +78,9 @@ open class WebPackRunTask : AbstractStartStopTask() { .replace("require('\$RunConfig\$')", JsonBuilder(GenerateWebpackHelperTask.config(project, config, webPackConfigFile)).toPrettyString() ) + .replace("\$SetMode\$\n", + if (NpmModuleVersion(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\"") } }