Skip to content

Commit 14006b9

Browse files
committed
Generate char-utils.ts and known-tlds.ts. Remove punycode support for known tlds
1 parent b0d138d commit 14006b9

15 files changed

+137
-250
lines changed

BUILD.bazel

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@npm//:defs.bzl", "npm_link_all_packages")
2-
load("@aspect_rules_js//js:defs.bzl", "js_library")
2+
load("@aspect_rules_js//js:defs.bzl", "js_library", "js_run_binary")
33
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
44
load("//build-tools:ts-bundle.bzl", "ts_bundle")
55

@@ -9,7 +9,14 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file")
99

1010
npm_link_all_packages(name = "node_modules")
1111

12-
src_files = glob(["src/**/*.ts"])
12+
src_files = glob(["src/**/*.ts"]) + [
13+
"src/char-utils.ts", # Generated file
14+
"src/parser/known-tlds.ts", # Generated file
15+
]
16+
17+
test_files = glob(["tests/**/*.ts"]) + [
18+
"tests/char-utils.spec.ts", # Generated file
19+
]
1320

1421
npm_package(
1522
name = "package",
@@ -21,6 +28,7 @@ npm_package(
2128

2229
js_library(
2330
name = "library",
31+
visibility = ["//visiblity:public"],
2432
deps = [
2533
":src_esm",
2634
":src_cjs",
@@ -30,6 +38,7 @@ js_library(
3038
# Create ESM bundle
3139
ts_bundle(
3240
name = "src_esm",
41+
visibility = ["//visibility:private"],
3342
entry_point = "src/index.ts",
3443
root_dir = "src",
3544
srcs = src_files,
@@ -46,6 +55,7 @@ ts_bundle(
4655
# Create CommonJS bundle
4756
ts_bundle(
4857
name = "src_cjs",
58+
visibility = ["//visibility:private"],
4959
entry_point = "src/index.ts",
5060
root_dir = "src",
5161
srcs = src_files,
@@ -61,6 +71,25 @@ ts_bundle(
6171

6272
ts_config(
6373
name = "tsconfig",
64-
src = "tsconfig.json",
6574
visibility = [":__subpackages__"],
75+
src = "tsconfig.json",
76+
)
77+
78+
# Generate the 'src/char-utils.ts' and 'tests/char-utils.spec.ts' files
79+
js_run_binary(
80+
name = "generate_char_utils",
81+
tool = "//scripts:generate_char_utils",
82+
outs = [
83+
"src/char-utils.ts",
84+
"tests/char-utils.spec.ts"
85+
],
86+
)
87+
88+
# Generate the 'parser/known-tlds.ts' file
89+
js_run_binary(
90+
name = "generate_known_tlds",
91+
tool = "//scripts:generate_known_tlds",
92+
outs = [
93+
"src/parser/known-tlds.ts",
94+
],
6695
)

MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build-tools/ts-bundle.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def ts_bundle(
1717
deps = [],
1818
target = "esnext",
1919
platform = "node",
20+
visibility = None,
2021
):
2122
"""
2223
Compiles TypeScript and bundles the output into a single .js / .d.ts files.
@@ -43,6 +44,7 @@ def ts_bundle(
4344
See https://esbuild.github.io/api/#target for reference.
4445
platform: The platform ('browser', 'node', or 'neutral') to build for.
4546
See: https://esbuild.github.io/api/#platform
47+
visibility: The visibility for the generated target.
4648
"""
4749
js_bundle_target = name + "_bundle"
4850
declarations_bundle_target = name + "_declarations_bundle"
@@ -51,6 +53,7 @@ def ts_bundle(
5153

5254
js_library(
5355
name = name,
56+
visibility = visibility,
5457
srcs = [
5558
":" + js_bundle_target,
5659
":" + declarations_bundle_target

build-tools/ts-declarations.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def ts_declarations(
6767
"sourceMap": true,
6868
"declarationMap": true,
6969
"emitDeclarationOnly": true,
70-
"noEmit": false
7170
}
7271
}
7372
""" % (tsconfig_file_path, target, module)]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@
9494
"npm-run-all": "^4.1.5",
9595
"nyc": "^17.1.0",
9696
"prettier": "3.4.2",
97-
"punycode": "^2.3.1",
9897
"puppeteer": "^24.7.2",
9998
"rimraf": "^6.0.1",
10099
"rollup": "^4.29.1",
101100
"rollup-plugin-dts": "^6.2.1",
102101
"sinon": "^20.0.0",
103102
"style-loader": "^4.0.0",
104103
"terser": "^5.37.0",
104+
"tlds": "^1.258.0",
105105
"ts-loader": "^9.5.1",
106106
"ts-node": "^10.9.2",
107107
"typescript": "5.8.3",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/BUILD.bazel

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_binary")
2+
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
3+
4+
# Script to generate the 'src/char-utils.ts' and 'tests/char-utils.spec.ts' files
5+
js_binary(
6+
name = "generate_char_utils",
7+
visibility = ["//visibility:public"],
8+
entry_point = "generate-char-utils.mjs",
9+
data = [
10+
":tsc",
11+
],
12+
)
13+
14+
# Script to generate the 'parser/known-tlds.ts' file
15+
js_binary(
16+
name = "generate_known_tlds",
17+
visibility = ["//visibility:public"],
18+
entry_point = "generate-known-tlds.mjs",
19+
data = [
20+
":tsc",
21+
],
22+
)
23+
24+
# Compile the scripts/ folder's TypeScript into JavaScript for execution
25+
ts_project(
26+
name = "tsc",
27+
tsconfig = ":tsconfig",
28+
srcs = glob(["**/*.ts", "**/*.mts"]),
29+
deps = [
30+
"//:node_modules/@types/fs-extra",
31+
"//:node_modules/@types/node",
32+
"//:node_modules/dedent",
33+
"//:node_modules/fast-glob",
34+
"//:node_modules/fs-extra",
35+
"//:node_modules/html-webpack-plugin",
36+
"//:node_modules/tlds",
37+
"//:node_modules/webpack",
38+
],
39+
source_map = True,
40+
declaration = True,
41+
declaration_map = True,
42+
)
43+
44+
ts_config(
45+
name = "tsconfig",
46+
src = "tsconfig.json",
47+
deps = ["//:tsconfig"]
48+
)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dedent from 'dedent';
2-
import fs from 'fs';
2+
import fse from 'fs-extra';
33
import path from 'path';
4-
import { alphaNumericAndMarksRe } from './alpha-numeric-and-marks-re';
4+
import { alphaNumericAndMarksRe } from './alpha-numeric-and-marks-re.mjs';
55

66
/*
77
* This script generates functions which check that a single character matches
@@ -15,7 +15,7 @@ import { alphaNumericAndMarksRe } from './alpha-numeric-and-marks-re';
1515
* as fast as possible.
1616
*/
1717

18-
const rootPath = path.normalize(`${__dirname}/..`);
18+
const rootPath = path.normalize(`${import.meta.dirname}/..`);
1919
const generateScriptName = 'generate-char-utils';
2020

2121
const { srcFileContents, specFileContents } = generateCharUtils([
@@ -32,15 +32,15 @@ const { srcFileContents, specFileContents } = generateCharUtils([
3232
['isCloseBraceChar', /[)}\]]/],
3333
]);
3434

35-
// console.log(srcFileContents);
36-
// console.log(specFileContents);
35+
console.log(srcFileContents);
36+
console.log(specFileContents);
3737

3838
const srcFilePath = `src/char-utils.ts`;
39-
fs.writeFileSync(`${rootPath}/${srcFilePath}`, srcFileContents, 'utf-8');
39+
fse.outputFileSync(`${rootPath}/${srcFilePath}`, srcFileContents, 'utf-8');
4040
console.log(`Wrote ${srcFilePath}`);
4141

4242
const specFilePath = `tests/char-utils.spec.ts`;
43-
fs.writeFileSync(`${rootPath}/${specFilePath}`, specFileContents, 'utf-8');
43+
fse.outputFileSync(`${rootPath}/${specFilePath}`, specFileContents, 'utf-8');
4444
console.log(`Wrote ${specFilePath}`);
4545

4646
// -------------------------------------------------------------

scripts/generate-known-tlds.mts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import dedent from 'dedent';
3+
import fse from 'fs-extra';
4+
import tlds from 'tlds' with { type: "json" };
5+
6+
generateKnownTlds().catch(error => {
7+
console.error(error);
8+
process.exit(1);
9+
});
10+
11+
/**
12+
* Updates the set of known top-level domains (TLDs). Ex: '.com', '.net', etc.
13+
*/
14+
export async function generateKnownTlds() {
15+
const tldRegexStr = tlds.toSorted(compareLengthLongestFirst).join('|');
16+
17+
const outputFile =
18+
dedent`
19+
// NOTE: THIS IS A GENERATED FILE\n// To update with the latest TLD list, update the 'tlds' dependency version and then run a build
20+
21+
export const tldRegex = /^(?:${tldRegexStr})$/;
22+
` + '\n';
23+
await fse.outputFile('./src/parser/known-tlds.ts', outputFile);
24+
}
25+
26+
function compareLengthLongestFirst(a: string, b: string): number {
27+
let result = b.length - a.length;
28+
if (result === 0) {
29+
result = a.localeCompare(b);
30+
}
31+
return result;
32+
}

0 commit comments

Comments
 (0)