Skip to content

Commit

Permalink
Add ew files for typescript project embeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Apr 25, 2023
1 parent 171ed8a commit e5d1169
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 41 deletions.
28 changes: 28 additions & 0 deletions build/eslint_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@npm//:eslint/package_json.bzl", eslint_bin = "bin")

def eslint_test(name, srcs, config, tsconfig, tags = []):
"""Generates a test, which runs eslint.
Args:
name: target name
srcs: source files to lint
config: eslint config file
tsconfig: tsconfig file
"""
eslint_bin.eslint_test(
name = name,
data = [
config,
tsconfig,
"//ts:eslintrc.base.json",
"//ts:tsconfig.base.json",
"//:node_modules/@typescript-eslint/eslint-plugin",
] + srcs,
args = [
"--config $(location " + config + ")",
] + [
"$(location " + src + ")"
for src in srcs
],
tags = ["lint"] + tags,
)
65 changes: 65 additions & 0 deletions build/wd_ts_project_embed.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
load("@workerd//:build/eslint_test.bzl", "eslint_test")
load("@workerd//:build/wd_js_bundle.bzl", "wd_js_bundle")

def _js_modules(ts_modules):
"""convert dict with ts modules to dict with js modules."""
return dict([
(f.removesuffix(".ts") + ".js", m)
for f, m in ts_modules.items()
# .d.ts do not generate .js
if not f.endswith(".d.ts")])


def wd_ts_project_embed(
name,
builtin_modules,
internal_modules,
tsconfig,
eslintrc,
tags = []):
"""Define typescript project embedding compiled code into c++ library.
Args:
name: project name
srcs: .ts source files
tsconfig: tsconfig.json file for the project
"""

srcs = list(builtin_modules) + list(internal_modules)

ts_config(
name = name + "@tsconfig",
src = tsconfig,
deps = ["//ts:tsconfig.base.json"],
tags = tags,
)

ts_project(
name = name,
srcs = srcs,
tsconfig = ":{}@tsconfig".format(name),
deps = ["//:node_modules/@cloudflare/workers-types"],
tags = tags,
)

wd_js_bundle(
name = name + "_bundle",
schema_id = "0xb7723ddc413262ab",
const_name = name + "Bundle",
builtin_modules = _js_modules(builtin_modules),
internal_modules = _js_modules(internal_modules),
tags = tags,
target_compatible_with = select({
"@//build/config:no_build": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

eslint_test(
name = name + "@eslint",
srcs = srcs,
config = eslintrc,
tsconfig = tsconfig,
tags = tags,
)
2 changes: 2 additions & 0 deletions samples/tcp/gopher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// - curl localhost:8080/ # Retrieve the main segment in gopher.floodgap.com.
// - curl localhost:8080/gstats # Retrieve the "gstats" segment.
// - curl localhost:8080/gstats?use_proxy=1 # Retrieve the "gstats" segment using the configured proxy.
import { connect } from 'cloudflare:sockets';

export default {
async fetch(req, env) {
const gopherAddr = "gopher.floodgap.com:70";
Expand Down
25 changes: 0 additions & 25 deletions src/cloudflare/.eslintrc.json

This file was deleted.

1 change: 0 additions & 1 deletion src/cloudflare/README.md

This file was deleted.

1 change: 1 addition & 0 deletions src/workerd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ wd_cc_library(
"@capnp-cpp//src/capnp:capnpc",
"//src/workerd/io",
"//src/workerd/jsg",
"//ts:cloudflareEdgeworker_bundle",
],
)

Expand Down
3 changes: 3 additions & 0 deletions src/workerd/server/workerd-api.c++
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <openssl/hmac.h>
#include <openssl/rand.h>

#include "ts/cloudflareEdgeworker_bundle.capnp.h"

namespace workerd::server {

JSG_DECLARE_ISOLATE_TYPE(JsgWorkerdIsolate,
Expand Down Expand Up @@ -350,6 +352,7 @@ kj::Own<jsg::ModuleRegistry> WorkerdApiIsolate::compileModules(
}

api::registerSocketsModule(*modules, getFeatureFlags());
modules->addBuiltinBundle(CLOUDFLARE_EDGEWORKER_BUNDLE);

jsg::setModulesForResolveCallback<JsgWorkerdIsolate_TypeWrapper>(lock, modules);

Expand Down
1 change: 1 addition & 0 deletions ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
44 changes: 44 additions & 0 deletions ts/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
load("@workerd//:build/wd_ts_project_embed.bzl", "wd_ts_project_embed")

# these will be accessible under "node:<name_without_extension>"
node_modules = glob(["node/*.ts"])

# these will be accessible under "node-internal:<name_without_extension>"
node_internal_modules = glob(["node/internal/*.ts"])

# these will be accessible under "cloudflare:<name_without_extension>"
cloudflare_modules = glob(["cloudflare/*.ts"])

# these will be accessible under "cloudflare-internal:<name_without_extension>"
cloudflare_internal_modules = glob(["cloudflare/internal/*.ts"])

wd_ts_project_embed(
# give bundle an "internal" name to be clear which one comes from workerd/edgeworker.
name = "nodeEdgeworker",
builtin_modules = dict([(
m,
"node:" + m.removeprefix("node/").removesuffix(".ts"),
) for m in node_modules]),
eslintrc = "node/.eslintrc.json",
internal_modules = dict([(
m,
"node-internal:" + m.removeprefix("node/internal/").removesuffix(".ts"),
) for m in node_internal_modules]),
tags = ["no-arm64"],
tsconfig = "node/tsconfig.json",
)

wd_ts_project_embed(
name = "cloudflareEdgeworker",
builtin_modules = dict([(
m,
"cloudflare:" + m.removeprefix("cloudflare/").removesuffix(".ts"),
) for m in cloudflare_modules]),
eslintrc = "cloudflare/.eslintrc.json",
internal_modules = dict([(
m,
"cloudflare-internal:" + m.removeprefix("cloudflare/internal/").removesuffix(".ts"),
) for m in cloudflare_internal_modules]),
tags = ["no-arm64"],
tsconfig = "cloudflare/tsconfig.json",
)
6 changes: 6 additions & 0 deletions ts/cloudflare/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../eslintrc.base.json",
"parserOptions": {
"project": "ts/cloudflare/tsconfig.json"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions ts/cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types"],
"paths": {
"cloudflare:*": ["./*"],
"cloudflare-internal:*": ["./internal/*"],
}
},
"include": [
"*.ts",
"internal/*.ts"
],
}
24 changes: 24 additions & 0 deletions ts/eslintrc.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"env": {
"es2022": true,
"worker": true
},
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:@typescript-eslint/strict"
],
"overrides": [],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/prefer-enum-initializers": "error",
"@typescript-eslint/type-annotation-spacing": "error"
}
}
6 changes: 6 additions & 0 deletions ts/node/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../eslintrc.base.json",
"parserOptions": {
"project": "ts/node/tsconfig.json"
}
}
8 changes: 8 additions & 0 deletions ts/node/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function basename(path: string, ext?: string): string {
if (ext) {
throw new Error(`basename with ext not implemented: ${path} ${ext}`);
}

const idx = path.lastIndexOf("/");
return idx >= 0 ? path.substring(idx + 1) : path;
}
14 changes: 14 additions & 0 deletions ts/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types"],
"paths": {
"node:*": ["./*"],
"node-internal:*": ["./internal/*"],
}
},
"include": [
"*.ts",
"internal/*.ts"
],
}
17 changes: 2 additions & 15 deletions src/cloudflare/tsconfig.json → ts/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": [ "ESNext", "DOM" ],
"lib": ["ESNext"],
"alwaysStrict": true,
"strict": true,
"allowJs": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"exactOptionalPropertyTypes": true,
Expand All @@ -18,17 +17,5 @@
"noUnusedParameters": true,
"strictNullChecks": true,
"esModuleInterop": true,
"types": [
// todo: consume generated workerd types
// "@cloudflare/workers-types"
],
"paths": {
"cloudflare:*": ["./*"],
"cloudflare-internal:*": ["./internal/*"]
}
},
"include": [
"*.ts",
"internal/*.ts"
],
}
}

0 comments on commit e5d1169

Please # to comment.