1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

jsgen: export module through commonjs (#5895)

This commit is contained in:
spaceface777 2020-07-20 18:57:13 +02:00 committed by GitHub
parent 88c8e194d0
commit 1a5236e53d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}