diff --git a/changelog.md b/changelog.md index 5c2756ea610a6..32ea7f12473a1 100644 --- a/changelog.md +++ b/changelog.md @@ -228,6 +228,9 @@ - Added `ZZZ` and `ZZZZ` patterns to `times.nim` `DateTime` parsing, to match time zone offsets without colons, e.g. `UTC+7 -> +0700`. +- Added `jscore.debugger` to [call any available debugging functionality, such as breakpoint](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger). + +- Added `jscore.jsExport` to [generate JavaScript `export` statements, including aliased exports and default export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export). diff --git a/lib/js/jscore.nim b/lib/js/jscore.nim index 693e5079918f9..5f753fa2c358c 100644 --- a/lib/js/jscore.nim +++ b/lib/js/jscore.nim @@ -110,3 +110,23 @@ proc parse*(l: JsonLib, s: cstring): JsRoot {.importcpp.} since (1, 5): func debugger*() {.importjs: "debugger@".} ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger + + template jsExport*(symbol: typed{sym}; name = "") = + ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export + ## + ## .. Note:: Using `name = "default"` is `special in JavaScript `_. + ## jsExport `can be used in browsers `_ + ## + ## .. Warning:: jsExport must be called at top level only. jsExport can not be called from a template. + var _ {.codegenDecl: "const $2", exportc: astToStr(symbol).} = symbol + {.emit: ["export { ", astToStr(symbol), (if name.len > 0: " as " & name else: ""), " };"] .} + + # pending bug #16993, move inside template + # pending https://github.com/timotheecour/Nim/issues/645, use simpler @ syntax + runnableExamples(r"-r:off"): # needs mjs extension for `export` + proc example = echo "This is exported as 'default' in JavaScript" + let example2 = "This is exported as 'another' in JavaScript" + const example3 = "This is exported as 'example3' in JavaScript" + jsExport(example, name = "default") ## Alias for `export { symbol as default };` + jsExport(example2, name = "another") ## Alias for `export { symbol as another };` + jsExport(example3) ## Alias for `export { symbol };`