diff --git a/proto/raw-js-openapi/webpack.config.js b/proto/raw-js-openapi/webpack.config.js index 793653ffae7..05627b1d856 100644 --- a/proto/raw-js-openapi/webpack.config.js +++ b/proto/raw-js-openapi/webpack.config.js @@ -8,7 +8,11 @@ module.exports = { output: { path: __dirname+'/build/js-out/', filename: 'dh-internal.js', - libraryTarget: "umd", + libraryTarget: 'module', + module:true, + }, + experiments: { + outputModule:true }, resolve : { modules: ['node_modules', __dirname + '/build/js-src'], diff --git a/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinker.java b/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinker.java index 501ffc28ab4..2f1667112d4 100644 --- a/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinker.java +++ b/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinker.java @@ -9,7 +9,9 @@ import com.google.gwt.core.ext.linker.*; import com.google.gwt.dev.About; import com.google.gwt.dev.util.DefaultTextOutput; +import com.google.gwt.util.tools.Utility; +import java.io.IOException; import java.util.Set; /** @@ -35,8 +37,6 @@ public ArtifactSet link(TreeLogger logger, LinkerContext context, ArtifactSet ar ArtifactSet toReturn = new ArtifactSet(artifacts); DefaultTextOutput out = new DefaultTextOutput(true); - out.print("(function(){"); - out.newline(); // get compilation result Set results = artifacts.find(CompilationResult.class); @@ -54,23 +54,41 @@ public ArtifactSet link(TreeLogger logger, LinkerContext context, ArtifactSet ar // get the generated javascript String[] javaScript = result.getJavaScript(); - out.print("self.dh = {}"); - out.newline(); - out.print("var $wnd = self, $doc, $entry, $moduleName, $moduleBase;"); - out.newline(); - out.print("var $gwt_version = \"" + About.getGwtVersionNum() + "\";"); - out.newlineOpt(); - out.print(javaScript[0]); - out.newline(); - - out.print("gwtOnLoad(null,'" + context.getModuleName() + "',null);"); - out.newline(); - out.print("})();"); - out.newline(); + + StringBuffer buffer = readFileToStringBuffer(getSelectionScriptTemplate(), logger); + replaceAll(buffer, "__GWT_VERSION__", About.getGwtVersionNum()); + replaceAll(buffer, "__JAVASCRIPT_RESULT__", javaScript[0]); + replaceAll(buffer, "__MODULE_NAME__", context.getModuleName()); + + out.print(buffer.toString()); toReturn.add(emitString(logger, out.toString(), "dh-core.js")); return toReturn; } + protected StringBuffer readFileToStringBuffer(String filename, + TreeLogger logger) throws UnableToCompleteException { + StringBuffer buffer; + try { + buffer = new StringBuffer(Utility.getFileFromClassPath(filename)); + } catch (IOException e) { + logger.log(TreeLogger.ERROR, "Unable to read file: " + filename, e); + throw new UnableToCompleteException(); + } + return buffer; + } + + protected String getSelectionScriptTemplate() { + return "io/deephaven/web/DeephavenJsApiLinkerTemplate.js"; + } + + protected static void replaceAll(StringBuffer buf, String search, + String replace) { + int len = search.length(); + for (int pos = buf.indexOf(search); pos >= 0; pos = buf.indexOf(search, + pos + replace.length())) { + buf.replace(pos, pos + len, replace); + } + } } diff --git a/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinkerTemplate.js b/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinkerTemplate.js new file mode 100644 index 00000000000..6a7ed749806 --- /dev/null +++ b/web/client-api/src/main/java/io/deephaven/web/DeephavenJsApiLinkerTemplate.js @@ -0,0 +1,29 @@ +function bindTo(target, source) { + var descriptors = Object.getOwnPropertyDescriptors(source); + for (var key in descriptors) { + if (!(key[0].toUpperCase() === key[0])) { + var descriptor = descriptors[key]; + if (typeof (descriptor.value) === 'function') { + descriptor.value = descriptor.value.bind(source) + } else if (typeof (descriptor.get) === 'function') { + descriptor.get = descriptor.get.bind(source); + } + } + } + Object.defineProperties(target, descriptors); +} + +var Scope = function () { +}; +Scope.prototype = self; +var $doc, $entry, $moduleName, $moduleBase; +var $wnd = new Scope(); +bindTo($wnd, self); +var dh = {} +$wnd.dh = dh; +import {dhinternal} from './dh-internal.js'; +$wnd.dhinternal = dhinternal; +var $gwt_version = "__GWT_VERSION__"; +__JAVASCRIPT_RESULT__ +gwtOnLoad(null, '__MODULE_NAME__', null); +export default dh; diff --git a/web/client-api/src/main/java/io/deephaven/web/public/console.html b/web/client-api/src/main/java/io/deephaven/web/public/console.html index 597a221129a..96428fab658 100644 --- a/web/client-api/src/main/java/io/deephaven/web/public/console.html +++ b/web/client-api/src/main/java/io/deephaven/web/public/console.html @@ -4,15 +4,16 @@ Deephaven JS API Console Interaction - -
- - -