Skip to content

Commit

Permalink
The big ESM migration commit.
Browse files Browse the repository at this point in the history
Refs #33.

This updates `@rules_prerender` to use ESM modules everywhere. This makes a few changes throughout the workspace:
1. Renames `*.{ts,js}` -> `*.{mts,mjs}`.
2. Updates imports to include `.mjs` in the file path.
3. Updates any hard-coded CommonJS `require()` or `module.exports` to use ESM syntax.
4. Updates `tsconfig.json` files to use `module: "ES2020"` and `moduleResolution: "node"`.
5. Updates `package.json` files to use `type: "module"`.

Most of the changes are fairly mechanical syntax modifications, though there are a couple random edits thrown in as necessary to switch over to ESM.

Most of this was generated by running:

```
$ echo {common,examples,packages,tools}/**/*.ts | tr " " "\n" | xargs -I {} bash -c "git mv {} \$(echo {} | sed 's,\\.ts$,.mts,g')"
$ sed -i -E "s,^import (.*?) from '(\..*?)';,import \1 from '\2.mjs';,g" {common,examples,packages,tools}/**/*.mts
$ sed -i -E "s,^export (.*?) from '(\..*?)';,export \1 from '\2.mjs';,g" {common,examples,packages,tools}/**/*.mts
$ sed -i "s,\\.ts\",.mts\",g" {.,common,examples,packages,tools}/**/BUILD.bazel
$ sed -i "s,\\.js\",.mjs\",g" {.,common,examples,packages,tools}/**/BUILD.bazel
```
  • Loading branch information
dgp1130 committed Mar 4, 2023
1 parent a181c30 commit 71ebac2
Show file tree
Hide file tree
Showing 240 changed files with 455 additions and 447 deletions.
28 changes: 14 additions & 14 deletions common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ load(":paths_test.bzl", "paths_test_suite")

ts_project(
name = "binary",
srcs = ["binary.ts"],
srcs = ["binary.mts"],
visibility = ["//visibility:public"],
deps = ["//:node_modules/@types/node"],
)

ts_project(
name = "binary_test_lib",
srcs = ["binary_test.ts"],
srcs = ["binary_test.mts"],
testonly = True,
deps = [
":binary",
Expand All @@ -28,13 +28,13 @@ jasmine_node_test(

ts_project(
name = "collections",
srcs = ["collections.ts"],
srcs = ["collections.mts"],
visibility = ["//visibility:public"],
)

ts_project(
name = "collections_test_lib",
srcs = ["collections_test.ts"],
srcs = ["collections_test.mts"],
testonly = True,
deps = [
":collections",
Expand All @@ -49,13 +49,13 @@ jasmine_node_test(

ts_project(
name = "formatters",
srcs = ["formatters.ts"],
srcs = ["formatters.mts"],
visibility = ["//visibility:public"],
)

ts_project(
name = "formatters_test_lib",
srcs = ["formatters_test.ts"],
srcs = ["formatters_test.mts"],
testonly = True,
deps = [
":formatters",
Expand All @@ -70,14 +70,14 @@ jasmine_node_test(

ts_project(
name = "file_system",
srcs = ["file_system.ts"],
srcs = ["file_system.mts"],
visibility = ["//visibility:public"],
deps = ["//:node_modules/@types/node"],
)

ts_project(
name = "file_system_fake",
srcs = ["file_system_fake.ts"],
srcs = ["file_system_fake.mts"],
visibility = ["//visibility:public"],
testonly = True,
deps = [
Expand All @@ -88,14 +88,14 @@ ts_project(

ts_project(
name = "http",
srcs = ["http.ts"],
srcs = ["http.mts"],
visibility = ["//visibility:public"],
deps = ["//:node_modules/@types/node"],
)

ts_project(
name = "http_test_lib",
srcs = ["http_test.ts"],
srcs = ["http_test.mts"],
testonly = True,
deps = [
":http",
Expand Down Expand Up @@ -139,7 +139,7 @@ paths_test_suite(name = "paths_test")

ts_project(
name = "prerender_annotation_walker",
srcs = ["prerender_annotation_walker.ts"],
srcs = ["prerender_annotation_walker.mts"],
visibility = ["//visibility:public"],
deps = [
"//common/models:prerender_annotation",
Expand All @@ -149,7 +149,7 @@ ts_project(

ts_project(
name = "prerender_annotation_walker_test_lib",
srcs = ["prerender_annotation_walker_test.ts"],
srcs = ["prerender_annotation_walker_test.mts"],
testonly = True,
data = ["//:node_modules/node-html-parser"],
deps = [
Expand All @@ -167,13 +167,13 @@ jasmine_node_test(

ts_project(
name = "probably",
srcs = ["probably.ts"],
srcs = ["probably.mts"],
visibility = ["//visibility:public"],
)

ts_project(
name = "probably_test_lib",
srcs = ["probably_test.ts"],
srcs = ["probably_test.mts"],
testonly = True,
deps = [
":probably",
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/binary_test.ts → common/binary_test.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { main as mainReal } from './binary';
import { main as mainReal } from './binary.mjs';

// Wrap `main`'s type to return its `Promise` to easily `await` it.
function main(...args: Parameters<typeof mainReal>): Promise<void> {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/collections_test.ts → common/collections_test.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { unique } from './collections';
import { unique } from './collections.mjs';

describe('collections', () => {
describe('unique()', () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/file_system_fake.ts → common/file_system_fake.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';
import { FileSystem } from './file_system';
import { FileSystem } from './file_system.mjs';

/**
* A fake implementation of the `FileSystem` interface which stores everything
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/formatters_test.ts → common/formatters_test.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mdSpacing } from './formatters';
import { mdSpacing } from './formatters.mjs';

describe('formatters', () => {
describe('spaceMarkdown()', () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/http_test.ts → common/http_test.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nodeHttp from 'http';
import { URL } from 'url';
import * as http from './http';
import * as http from './http.mjs';

describe('http', () => {
describe('get', () => {
Expand Down
20 changes: 10 additions & 10 deletions common/models/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ load("//tools/typescript:defs.bzl", "ts_project")

ts_project(
name = "prerender_annotation",
srcs = ["prerender_annotation.ts"],
srcs = ["prerender_annotation.mts"],
visibility = ["//visibility:public"],
)

ts_project(
name = "prerender_annotation_test_lib",
srcs = ["prerender_annotation_test.ts"],
srcs = ["prerender_annotation_test.mts"],
testonly = True,
deps = [
":prerender_annotation",
Expand All @@ -24,21 +24,21 @@ jasmine_node_test(

ts_project(
name = "prerender_annotation_mock",
srcs = ["prerender_annotation_mock.ts"],
srcs = ["prerender_annotation_mock.mts"],
testonly = True,
visibility = ["//visibility:public"],
deps = [":prerender_annotation"],
)

ts_project(
name = "prerender_metadata",
srcs = ["prerender_metadata.ts"],
srcs = ["prerender_metadata.mts"],
visibility = ["//visibility:public"],
)

ts_project(
name = "prerender_metadata_test_lib",
srcs = ["prerender_metadata_test.ts"],
srcs = ["prerender_metadata_test.mts"],
testonly = True,
deps = [
":prerender_metadata",
Expand All @@ -53,22 +53,22 @@ jasmine_node_test(

ts_project(
name = "prerender_metadata_mock",
srcs = ["prerender_metadata_mock.ts"],
srcs = ["prerender_metadata_mock.mts"],
testonly = True,
visibility = ["//visibility:public"],
deps = [":prerender_metadata"],
)

ts_project(
name = "prerender_resource",
srcs = ["prerender_resource.ts"],
srcs = ["prerender_resource.mts"],
visibility = ["//visibility:public"],
deps = [":url_path"],
)

ts_project(
name = "prerender_resource_test_lib",
srcs = ["prerender_resource_test.ts"],
srcs = ["prerender_resource_test.mts"],
testonly = True,
deps = [
":prerender_resource",
Expand All @@ -83,12 +83,12 @@ jasmine_node_test(

ts_project(
name = "url_path",
srcs = ["url_path.ts"],
srcs = ["url_path.mts"],
)

ts_project(
name = "url_path_test_lib",
srcs = ["url_path_test.ts"],
srcs = ["url_path_test.mts"],
testonly = True,
deps = [
":url_path",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @see /README.md#Mocking
*/

import { PrerenderAnnotation, ScriptAnnotation, StyleAnnotation } from './prerender_annotation';
import { PrerenderAnnotation, ScriptAnnotation, StyleAnnotation } from './prerender_annotation.mjs';

/**
* Mocks a {@link PrerenderAnnotation} object. Since this type is a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrerenderAnnotation, createAnnotation, parseAnnotation, ScriptAnnotation, annotationsEqual, StyleAnnotation } from './prerender_annotation';
import { PrerenderAnnotation, createAnnotation, parseAnnotation, ScriptAnnotation, annotationsEqual, StyleAnnotation } from './prerender_annotation.mjs';

describe('prerender_annotation', () => {
describe('PrerenderAnnotation', () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @see /README.md#Mocking
*/

import { PrerenderMetadata, ScriptMetadata } from './prerender_metadata';
import { PrerenderMetadata, ScriptMetadata } from './prerender_metadata.mjs';

/** Mocks the {@link PrerenderMetadata} object with the given overrides. */
export function mockPrerenderMetadata(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrerenderMetadata, ScriptMetadata } from './prerender_metadata';
import { PrerenderMetadata, ScriptMetadata } from './prerender_metadata.mjs';

describe('prerender_metadata', () => {
describe('PrerenderMetadata', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UrlPath } from './url_path';
import { UrlPath } from './url_path.mjs';

/** Represents a resource to be rendered / generated at a particular path. */
export class PrerenderResource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrerenderResource } from './prerender_resource';
import { PrerenderResource } from './prerender_resource.mjs';

describe('PrerenderResource', () => {
describe('of()', () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UrlPath } from './url_path';
import { UrlPath } from './url_path.mjs';

describe('UrlPath', () => {
describe('of()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommentNode, HTMLElement, Node } from 'node-html-parser';
import { parseAnnotation, PrerenderAnnotation } from './models/prerender_annotation';
import { parseAnnotation, PrerenderAnnotation } from './models/prerender_annotation.mjs';

/**
* A reference to a `node-html-parser` `Node` which contains a
Expand Down Expand Up @@ -138,4 +138,4 @@ function* walk(node: Node, parent?: HTMLElement):
yield* walk(child, node);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HTMLElement, parse } from 'node-html-parser';
import { createAnnotation } from './models/prerender_annotation';
import { walkAllAnnotations } from './prerender_annotation_walker';
import { createAnnotation } from './models/prerender_annotation.mjs';
import { walkAllAnnotations } from './prerender_annotation_walker.mjs';

describe('prerender_annotation_walker', () => {
describe('walkAllAnnotations()', () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion common/probably_test.ts → common/probably_test.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { definitely, Definitely, Probably } from './probably';
import { definitely, Definitely, Probably } from './probably.mjs';

interface User {
name: string;
Expand Down
Loading

0 comments on commit 71ebac2

Please sign in to comment.