Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript #6

Closed
garrett-hopper opened this issue Sep 29, 2022 · 3 comments
Closed

TypeScript #6

garrett-hopper opened this issue Sep 29, 2022 · 3 comments

Comments

@garrett-hopper
Copy link

I'd like to be able to evaluate TS files without manually removing type annotations.

@jamesdiacono
Copy link
Owner

jamesdiacono commented Sep 29, 2022

Support for transpiled languages can be achieved by providing appropriate command, read and headers functions to run.

import run from "https://deno.land/x/replete/run.js";
import typescript from "https://esm.sh/typescript";

function compile_typescript(source) {
    return typescript.transpileModule(
        source,
        {
            compilerOptions: {
                module: typescript.ModuleKind.ES2015,
                newLine: "lf"
            }
        }
    ).outputText;
}

run({
    command(message) {
        if (message.locator !== undefined && message.locator.endsWith(".ts")) {
            message.source = compile_typescript(message.source);
        }
        return Promise.resolve(message);
    },
    read(locator) {
        return Deno.readFile(new URL(locator)).then(function (buffer) {
            return (
                locator.endsWith(".ts")
                ? compile_typescript(new TextDecoder().decode(buffer))
                : buffer
            );
        });
    },
    headers(locator) {
        if (locator.endsWith(".js") || locator.endsWith(".ts")) {
            return {"Content-Type": "text/javascript"};
        }
    }
});

Or, equivalently, use this replete.json at the root of your project:

{
    "command": [
        "deno",
        "run",
        "--allow-all",
        "--importmap",
        "https://deno.land/x/replete/import_map.json",
        "https://gist.githubusercontent.com/jamesdiacono/b260251fdb8e4d00dacc40d78ae16c31/raw/b1c52a46c332cce579da40b79ab2e660a7f13be3/replete_typescript.js"
    ]
}

@adamzerner
Copy link

I don't see a typescript.js file and thus am getting this error when I start Replete:

node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/azerner/indeed/Replete/typescript.js' imported from /Users/azerner/indeed/Replete/replete.js
    at new NodeError (node:internal/errors:399:5)
    at finalizeResolution (node:internal/modules/esm/resolve:231:11)
    at moduleResolve (node:internal/modules/esm/resolve:850:10)
    at defaultResolve (node:internal/modules/esm/resolve:1058:11)
    at nextResolve (node:internal/modules/esm/hooks:654:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:309:30)
    at ESMLoader.resolve (node:internal/modules/esm/loader:312:26)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:172:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v19.8.0

@jamesdiacono
Copy link
Owner

Replete does not include TypeScript, or any other transpilers. You are expected to bring your own.

Weirdly, TypeScript is not available as an ES module (see microsoft/TypeScript#32949), so you will have to get hold of the CommonJS build and add the line export default ts; at the end of the typescript.js file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants