diff --git a/docs/docs/languages/gleam.md b/docs/docs/languages/gleam.md index 59f2082ca..03e33b98e 100644 --- a/docs/docs/languages/gleam.md +++ b/docs/docs/languages/gleam.md @@ -15,7 +15,13 @@ The compiled JavaScript code can be inspected in the [Compiled Code Viewer](../f ### Standard Library -[Gleam's standard library](https://hexdocs.pm/gleam_stdlib/), [gleam/javascript](https://hexdocs.pm/gleam_javascript/) and [gleam/json](https://hexdocs.pm/gleam_json/) packages are available for use and can be imported as usual. +[Gleam's standard library](https://hexdocs.pm/gleam_stdlib/) in addition to the following packages are available for use and can be imported as usual with no additional configuration: + +- [gleam/crypto](https://hexdocs.pm/gleam_crypto/) +- [gleam/fetch](https://hexdocs.pm/gleam_fetch/) +- [gleam/http](https://hexdocs.pm/gleam_http/) +- [gleam/javascript](https://hexdocs.pm/gleam_javascript/) +- [gleam/json](https://hexdocs.pm/gleam_json/) Demo: diff --git a/src/livecodes/languages/gleam/lang-gleam-compiler.ts b/src/livecodes/languages/gleam/lang-gleam-compiler.ts index 2d2b11a85..ab3c68ae6 100644 --- a/src/livecodes/languages/gleam/lang-gleam-compiler.ts +++ b/src/livecodes/languages/gleam/lang-gleam-compiler.ts @@ -22,6 +22,7 @@ import { getLanguageCustomSettings } from '../utils'; } const modules: Modules = { + // gleam_stdlib 'gleam/bit_array': { srcUrl: srcBaseUrl + 'gleam_stdlib/src/gleam/bit_array.gleam' }, 'gleam/bool': { srcUrl: srcBaseUrl + 'gleam_stdlib/src/gleam/bool.gleam' }, 'gleam/bytes_builder': { srcUrl: srcBaseUrl + 'gleam_stdlib/src/gleam/bytes_builder.gleam' }, @@ -43,6 +44,7 @@ import { getLanguageCustomSettings } from '../utils'; 'gleam/string': { srcUrl: srcBaseUrl + 'gleam_stdlib/src/gleam/string.gleam' }, 'gleam/string_builder': { srcUrl: srcBaseUrl + 'gleam_stdlib/src/gleam/string_builder.gleam' }, 'gleam/uri': { srcUrl: srcBaseUrl + 'gleam_stdlib/src/gleam/uri.gleam' }, + // extras 'gleam/javascript': { srcUrl: srcBaseUrl + 'gleam_javascript/src/gleam/javascript.gleam' }, 'gleam/javascript/array': { srcUrl: srcBaseUrl + 'gleam_javascript/src/gleam/javascript/array.gleam', @@ -53,9 +55,14 @@ import { getLanguageCustomSettings } from '../utils'; 'gleam/javascript/promise': { srcUrl: srcBaseUrl + 'gleam_javascript/src/gleam/javascript/promise.gleam', }, - 'gleam/json': { - srcUrl: srcBaseUrl + 'gleam_json/src/gleam/json.gleam', - }, + 'gleam/json': { srcUrl: srcBaseUrl + 'gleam_json/src/gleam/json.gleam' }, + 'gleam/crypto': { srcUrl: srcBaseUrl + 'gleam_crypto/src/gleam/crypto.gleam' }, + 'gleam/fetch': { srcUrl: srcBaseUrl + 'gleam_fetch/src/gleam/fetch.gleam' }, + 'gleam/http': { srcUrl: srcBaseUrl + 'gleam_http/src/gleam/http.gleam' }, + 'gleam/http/cookie': { srcUrl: srcBaseUrl + 'gleam_http/src/gleam/http/cookie.gleam' }, + 'gleam/http/request': { srcUrl: srcBaseUrl + 'gleam_http/src/gleam/http/request.gleam' }, + 'gleam/http/response': { srcUrl: srcBaseUrl + 'gleam_http/src/gleam/http/response.gleam' }, + 'gleam/http/service': { srcUrl: srcBaseUrl + 'gleam_http/src/gleam/http/service.gleam' }, }; async function initGleamCompiler() { @@ -187,20 +194,20 @@ import { getLanguageCustomSettings } from '../utils'; const compilerLoaded = Promise.all([initGleamCompiler(), loadModules(modules)]); - // workaround for the compiler not allowing `@` in external URLs // e.g.: @external(javascript, "npm:uuid@9.0.1", "v4") - const externalPattern = /(@external\s{0,20}\(\s{0,20}javascript\s{0,20},\s{0,20}".{0,200}?)(@)(.{0,200}?"\s{0,20},\s{0,20}".{0,200}?"\))/g; + const externalPattern = + /(@external\s{0,20}\(\s{0,20}javascript\s{0,20},\s{0,20}".{0,200}?)(@)(.{0,200}?"\s{0,20},\s{0,20}".{0,200}?"\))/g; const placeholder = '______at______'; const removeAt = (str: string) => { if (!str.includes('@')) return str; const pattern = new RegExp(externalPattern); return str.replace(pattern, `$1${placeholder}$3`); - } + }; const restoreAt = (str: string) => { if (!str.includes(placeholder)) return str; return str.split(placeholder).join('@'); - } + }; const compile: CompilerFunction = async (code, { config }) => { if (!code) return ''; @@ -221,18 +228,16 @@ import { getLanguageCustomSettings } from '../utils'; project.compilePackage('javascript'); const js = project.readCompiledJavaScript('main'); return restoreAt(js).replace(/from\s+"\.\/(.+)"/g, (_: string, mod: string) => { - if (mod === 'gleam.mjs') { + if (mod === 'gleam.mjs' || mod === 'prelude.mjs') { return `from "${compiledBaseUrl}prelude.mjs"`; } + const modName = mod.replace('.mjs', ''); if (mod.startsWith('gleam/')) { - const dir = mod.startsWith('gleam/javascript') - ? 'gleam_javascript/' - : mod.startsWith('gleam/json') - ? 'gleam_json/' - : 'gleam_stdlib/'; + const [_root, path] = modName.split('/'); + const extras = ['javascript', 'json', 'crypto', 'fetch', 'http']; + const dir = extras.includes(path) ? `gleam_${path}/` : 'gleam_stdlib/'; return `from "${compiledBaseUrl + dir + mod}"`; } - const modName = mod.replace('.mjs', ''); if (modules[modName]?.compiledUrl) { return `from "${modules[modName].compiledUrl}"`; } diff --git a/src/livecodes/vendors.ts b/src/livecodes/vendors.ts index b8d2befed..30ea98619 100644 --- a/src/livecodes/vendors.ts +++ b/src/livecodes/vendors.ts @@ -216,7 +216,7 @@ export const githubMarkdownCss = /* @__PURE__ */ getUrl( 'github-markdown-css@5.1.0/github-markdown.css', ); -export const gleamBaseUrl = /* @__PURE__ */ getUrl('gh:live-codes/gleam-precompiled@v0.1.0/'); +export const gleamBaseUrl = /* @__PURE__ */ getUrl('gh:live-codes/gleam-precompiled@v0.2.0/'); export const go2jsBaseUrl = /* @__PURE__ */ getUrl('@live-codes/go2js@0.4.0/build/');