From 1a5236e53d4bb1366c58172a125049c708f6a63a Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Mon, 20 Jul 2020 18:57:13 +0200 Subject: [PATCH] jsgen: export module through commonjs (#5895) --- vlib/v/gen/js/js.v | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 2eab7af24e..391d81c6f5 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -87,8 +87,9 @@ pub fn gen(files []ast.File, table &table.Table, pref &pref.Preferences) string } // resolve imports deps_resolved := graph.resolve() + nodes := deps_resolved.nodes mut out := g.hashes() + g.definitions.str() - for node in deps_resolved.nodes { + for node in nodes { name := g.js_name(node.name).replace('.', '_') if g.enable_doc { out += '/** @namespace $name */\n' @@ -129,6 +130,11 @@ pub fn gen(files []ast.File, table &table.Table, pref &pref.Preferences) string } out += ');\n\n' } + if pref.is_shared { + // Export, through CommonJS, the module of the entry file if `-shared` was passed + export := nodes[nodes.len - 1].name + out += 'if (typeof module === "object" && module.exports) module.exports = $export;' + } return out }