Skip to content

Commit

Permalink
feat(Compilers): make official gleam packages available by default
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Apr 24, 2024
1 parent 52a269a commit 5aed5ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
8 changes: 7 additions & 1 deletion docs/docs/languages/gleam.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
33 changes: 19 additions & 14 deletions src/livecodes/languages/gleam/lang-gleam-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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',
Expand All @@ -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() {
Expand Down Expand Up @@ -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 '';
Expand All @@ -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}"`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/livecodes/vendors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/');

Expand Down

0 comments on commit 5aed5ea

Please sign in to comment.