Skip to content

Commit 2978fce

Browse files
committed
Modulize the compiled js
1 parent bf89746 commit 2978fce

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

js/jscompiler.vim

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,19 @@ function! s:test()
852852
let c = s:JavascriptCompiler.new()
853853
let lines = c.compile(p.parse(r))
854854
unlet lines[0 : index(lines, 'var NIL = [];') - 1]
855-
call writefile(head + lines + ['', 'main()'], pyfile)
855+
let tail = [
856+
\ 'if (require.main === module) {',
857+
\ ' main();',
858+
\ '}',
859+
\ 'else {',
860+
\ ' module.exports = {',
861+
\ ' VimLParser: VimLParser,',
862+
\ ' StringReader: StringReader,',
863+
\ ' Compiler: Compiler',
864+
\ ' };',
865+
\ '}',
866+
\ ]
867+
call writefile(head + lines + tail, pyfile)
856868
catch
857869
echoerr substitute(v:throwpoint, '\.\.\zs\d\+', '\=s:numtoname(submatch(0))', 'g') . "\n" . v:exception
858870
endtry

0 commit comments

Comments
 (0)