Hello,
recently I tried to use ES Modules with Parenscript so I modified the
code. I just wanted to share the patch - maybe you want to include it in
the official repository.
Kind regards,
Moritz Hedtke
From 4862600435c3dfbf4b329b5d4578e5ae77f9eaa2 Mon Sep 17 00:00:00 2001
From: mohe2015 <mohe2015(a)users.noreply.github.com>
Date: Sun, 24 Mar 2019 13:00:31 +0100
Subject: [PATCH] Implement importing and exporting ES Modules
---
src/js-ir-package.lisp | 4 ++++
src/package.lisp | 6 ++++++
src/printer.lisp | 15 +++++++++++++++
src/special-operators.lisp | 12 ++++++++++++
4 files changed, 37 insertions(+)
diff --git a/src/js-ir-package.lisp b/src/js-ir-package.lisp
index dd5fa80..46aefc7 100644
--- a/src/js-ir-package.lisp
+++ b/src/js-ir-package.lisp
@@ -144,4 +144,8 @@
#:funcall
#:escape
#:regex
+ #:export
+ #:export-default
+ #:import
+ #:import-default
))
diff --git a/src/package.lisp b/src/package.lisp
index 2a39628..3262cc8 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -150,6 +150,12 @@
;; compile-time stuff
#:eval-when
+ ;; modules
+ #:export
+ #:export-default
+ #:import
+ #:import-default
+
;; body forms
#:progn
diff --git a/src/printer.lisp b/src/printer.lisp
index fc52e71..df94dfa 100644
--- a/src/printer.lisp
+++ b/src/printer.lisp
@@ -415,3 +415,18 @@ vice-versa.")
;; literal-js should be a form that evaluates to a string containing
;; valid JavaScript
(psw literal-js))
+
+(defprinter ps-js:export (object)
+ "export " (ps-print object))
+
+(defprinter ps-js:export-default (object)
+ "export default " (ps-print object))
+
+(defprinter ps-js:import (file objects)
+ "import { "
+ (loop for (object . remaining) on objects do
+ (psw object) (when remaining (psw ", ")))
+ " } from " (ps-print file))
+
+(defprinter ps-js:import-default (file name)
+ "import " (psw name) " from " (ps-print file))
diff --git a/src/special-operators.lisp b/src/special-operators.lisp
index a3abc90..4a90683 100644
--- a/src/special-operators.lisp
+++ b/src/special-operators.lisp
@@ -702,3 +702,15 @@ code in :execute."
(if (member :execute situation-list)
(ps-compile `(progn ,@body))
(ps-compile `(progn))))
+
+(define-expression-operator export (object)
+ `(ps-js:export ,(ps-compile object)))
+
+(define-expression-operator export-default (object)
+ `(ps-js:export-default ,(ps-compile object)))
+
+(define-expression-operator import (file &rest objects)
+ `(ps-js:import ,file ,objects))
+
+(define-expression-operator import-default (file name)
+ `(ps-js:import-default ,file ,name))