diff --git a/examples.md b/examples.md index 39841bb0..c91695e0 100644 --- a/examples.md +++ b/examples.md @@ -115,6 +115,22 @@ fun main(args: Array) { +Use `data-dce` attribute with value `true` to get less code from JS IR target: + +
+ +```kotlin +fun mul(a: Int, b: Int): Int { + return a * b +} + +fun main(args: Array) { + println(mul(-1, 8)) +} +``` + +
+ Use `data-target-platform` attribute with value `junit` for creating examples with tests: diff --git a/src/config.js b/src/config.js index 003dfc3d..893ef624 100644 --- a/src/config.js +++ b/src/config.js @@ -8,10 +8,24 @@ export const RUNTIME_CONFIG = {...getConfigFromElement(currentScript)}; /** * API Paths * - * @type {{COMPILE: string, COMPLETE: string, VERSIONS: string, JQUERY: string, KOTLIN_JS: string}} + * @type {{COMPILE: string, COMPLETE: string, VERSIONS: string, JQUERY: string, KOTLIN_JS: string, WITH_DCE: this}} */ export const API_URLS = { server: RUNTIME_CONFIG.server || __WEBDEMO_URL__, + WITH_DCE(dce) { + const self = this; + return { + ...this, + COMPILE(...args) { + let url = self.COMPILE(...args); + if (url.includes("?")) { + url += "&" + } + url += "dce=" + Boolean(dce) + return url + } + }; + }, COMPILE(platform, version) { let url; diff --git a/src/executable-code/executable-fragment.js b/src/executable-code/executable-fragment.js index 01f2a973..2591d10b 100644 --- a/src/executable-code/executable-fragment.js +++ b/src/executable-code/executable-fragment.js @@ -278,7 +278,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate { execute() { const { - onOpenConsole, targetPlatform, waitingForOutput, compilerVersion, onRun, onError, + onOpenConsole, targetPlatform, waitingForOutput, compilerVersion, onRun, onError, dce, args, theme, hiddenDependencies, onTestPassed, onTestFailed, onCloseConsole, jsLibs, outputHeight, getJsCode } = this.state; if (waitingForOutput) { @@ -325,7 +325,7 @@ export default class ExecutableFragment extends ExecutableCodeTemplate { ) } else { this.jsExecutor.reloadIframeScripts(jsLibs, this.getNodeForMountIframe(), targetPlatform); - WebDemoApi.translateKotlinToJs(this.getCode(), compilerVersion, targetPlatform, args, hiddenDependencies).then( + WebDemoApi.translateKotlinToJs(this.getCode(), compilerVersion, targetPlatform, args, hiddenDependencies, { dce }).then( state => { state.waitingForOutput = false; const jsCode = state.jsCode; diff --git a/src/executable-code/index.js b/src/executable-code/index.js index f52ee8cf..0b3e8b30 100644 --- a/src/executable-code/index.js +++ b/src/executable-code/index.js @@ -52,6 +52,7 @@ const ATTRIBUTES = { COMPLETE: 'data-autocomplete', ON_FLY_HIGHLIGHT: 'highlight-on-fly', PLATFORM: 'data-target-platform', + DCE: 'data-dce', JS_LIBS: 'data-js-libs', FOLDED_BUTTON: 'folded-button', ARGUMENTS: 'args', @@ -95,6 +96,7 @@ export default class ExecutableCode { const hiddenDependencies = this.getHiddenDependencies(targetNode); const outputHeight = targetNode.getAttribute(ATTRIBUTES.OUTPUT_HEIGHT) || null; const targetPlatform = TargetPlatform.getById(targetNode.getAttribute(ATTRIBUTES.PLATFORM)); + const dce = targetNode.getAttribute(ATTRIBUTES.DCE) === "true"; const targetNodeStyle = targetNode.getAttribute(ATTRIBUTES.STYLE); const jsLibs = this.getJsLibraries(targetNode, targetPlatform); const isFoldedButton = targetNode.getAttribute(ATTRIBUTES.FOLDED_BUTTON) !== "false"; @@ -168,6 +170,7 @@ export default class ExecutableCode { onFlyHighLight: onFlyHighLight, autoIndent: autoIndent, highlightOnly: highlightOnly, + dce: dce, targetPlatform: targetPlatform, jsLibs: jsLibs, isFoldedButton: isFoldedButton, diff --git a/src/webdemo-api.js b/src/webdemo-api.js index c0f22832..f533d75c 100644 --- a/src/webdemo-api.js +++ b/src/webdemo-api.js @@ -47,9 +47,10 @@ export default class WebDemoApi { * @param platform - TargetPlatform * @param args - command line arguments * @param hiddenDependencies - read only additional files + * @param compilationOptions - options to handle different kind of compilation modes like DCE, PER MODULE, MINIFIED METHOD AND PROPERTY NAMES * @returns {*|PromiseLike|Promise} */ - static translateKotlinToJs(code, compilerVersion, platform, args, hiddenDependencies) { + static translateKotlinToJs(code, compilerVersion, platform, args, hiddenDependencies, compilationOptions = {}) { const MINIMAL_MINOR_VERSION_IR = 5 if (platform === TargetPlatform.JS_IR && parseInt(compilerVersion.split(".")[1]) < MINIMAL_MINOR_VERSION_IR) { return Promise.resolve({ @@ -61,7 +62,14 @@ export default class WebDemoApi { jsCode: "" }) } else { - return executeCode(API_URLS.COMPILE(platform, compilerVersion), code, compilerVersion, platform, args, hiddenDependencies).then(function (data) { + return executeCode( + API_URLS.WITH_DCE(compilationOptions.dce).COMPILE(platform, compilerVersion), + code, + compilerVersion, + platform, + args, + hiddenDependencies + ).then(function (data) { let output = ""; let errorsAndWarnings = flatten(Object.values(data.errors)); return {