Skip to content

Commit

Permalink
Add support for transpiling ESM code (and convert this repo itself to…
Browse files Browse the repository at this point in the history
… ESM)
  • Loading branch information
airhorns committed Sep 28, 2024
1 parent 39abfa8 commit afdf5bd
Show file tree
Hide file tree
Showing 87 changed files with 1,926 additions and 2,750 deletions.
48 changes: 48 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module.exports = {
extends: "@gadgetinc/eslint-config",
parserOptions: {
tsconfigRootDir: __dirname,
project: ["./tsconfig.test.json"],
},
ignorePatterns: [".eslintrc.cjs", "spec/fixtures/**/*"],
rules: {
"lodash/import-scope": "off",
// Disable all Jest-related ESLint rules
"jest/no-alias-methods": "off",
"jest/no-commented-out-tests": "off",
"jest/no-conditional-expect": "off",
"jest/no-deprecated-functions": "off",
"jest/no-disabled-tests": "off",
"jest/no-done-callback": "off",
"jest/no-duplicate-hooks": "off",
"jest/no-export": "off",
"jest/no-focused-tests": "off",
"jest/no-hooks": "off",
"jest/no-identical-title": "off",
"jest/no-if": "off",
"jest/no-interpolation-in-snapshots": "off",
"jest/no-jasmine-globals": "off",
"jest/no-jest-import": "off",
"jest/no-large-snapshots": "off",
"jest/no-mocks-import": "off",
"jest/no-restricted-matchers": "off",
"jest/no-standalone-expect": "off",
"jest/no-test-prefixes": "off",
"jest/no-test-return-statement": "off",
"jest/prefer-called-with": "off",
"jest/prefer-expect-assertions": "off",
"jest/prefer-hooks-on-top": "off",
"jest/prefer-spy-on": "off",
"jest/prefer-strict-equal": "off",
"jest/prefer-to-be": "off",
"jest/prefer-to-contain": "off",
"jest/prefer-to-have-length": "off",
"jest/prefer-todo": "off",
"jest/require-to-throw-message": "off",
"jest/require-top-level-describe": "off",
"jest/valid-describe-callback": "off",
"jest/valid-expect": "off",
"jest/valid-expect-in-promise": "off",
"jest/valid-title": "off"
},
};
15 changes: 0 additions & 15 deletions .eslintrc.js

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
- uses: actions/checkout@v2
- uses: ./.github/actions/setup-test-env
- run: pnpm build
- run: integration-test/test.sh
- run: pnpm jest
- run: pnpm zx integration-test/test.js
- run: pnpm test

lint:
runs-on: ubuntu-latest
Expand Down
3 changes: 1 addition & 2 deletions .swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"target": "esnext"
},
"module": {
"type": "commonjs",
"lazy": true
"type": "es6"
}
}
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
packages =
rec {
bash = pkgs.bash;
nodejs = pkgs.nodejs_20;
nodejs = pkgs.nodejs_22;
corepack = pkgs.corepack;
};

Expand Down
4 changes: 2 additions & 2 deletions gitpkg.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { execSync } = require('child_process')
import { execSync } from "child_process";

module.exports = () => ({
export default () => ({
getTagName: (pkg) =>
`${pkg.name}-v${pkg.version}-gitpkg-${execSync(
'git rev-parse --short HEAD',
Expand Down
14 changes: 14 additions & 0 deletions integration-test/cjs-js-from-ts/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true,
"dynamicImport": true
},
"target": "esnext"
},
"module": {
"type": "commonjs"
}
}
11 changes: 11 additions & 0 deletions integration-test/cjs-js-from-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "cjs-js-from-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions integration-test/cjs-js-from-ts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.utility = (str) => str.toUpperCase();
14 changes: 14 additions & 0 deletions integration-test/cjs-sourcemap/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true,
"dynamicImport": true
},
"target": "esnext"
},
"module": {
"type": "commonjs"
}
}
8 changes: 8 additions & 0 deletions integration-test/cjs-sourcemap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "cjs-sourcemap",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"license": "ISC"
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $DIR/run.ts 2>&1 | grep "sourcemap/utils.ts:7"
$DIR/../../pkg/wds.bin.js $DIR/run.ts 2>&1 | tee /dev/stderr | grep "sourcemap/utils.ts:7"
echo "Found correct source location"
File renamed without changes.
14 changes: 14 additions & 0 deletions integration-test/cjs-ts-from-js/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true,
"dynamicImport": true
},
"target": "esnext"
},
"module": {
"type": "commonjs"
}
}
11 changes: 11 additions & 0 deletions integration-test/cjs-ts-from-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "esm-ts-from-js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
3 changes: 3 additions & 0 deletions integration-test/cjs-ts-from-js/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { utility } = require("./utils");

console.log(utility("It worked!"));
5 changes: 5 additions & 0 deletions integration-test/cjs-ts-from-js/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $@ $DIR/run.js | grep "IT WORKED"
File renamed without changes.
14 changes: 14 additions & 0 deletions integration-test/cjs-ts-from-ts/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"decorators": true,
"dynamicImport": true
},
"target": "esnext"
},
"module": {
"type": "commonjs"
}
}
11 changes: 11 additions & 0 deletions integration-test/cjs-ts-from-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "simple-esm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
3 changes: 3 additions & 0 deletions integration-test/cjs-ts-from-ts/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { utility } from "./utils";

console.log(utility("It worked!"));
5 changes: 5 additions & 0 deletions integration-test/cjs-ts-from-ts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $@ $DIR/run.ts | grep "IT WORKED"
1 change: 1 addition & 0 deletions integration-test/cjs-ts-from-ts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utility = (str: string) => str.toUpperCase();
11 changes: 11 additions & 0 deletions integration-test/esm-js-from-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "esm-js-from-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
3 changes: 3 additions & 0 deletions integration-test/esm-js-from-ts/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { utility } from "./utils.js";

console.log(utility("It worked!"));
5 changes: 5 additions & 0 deletions integration-test/esm-js-from-ts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $@ $DIR/run.ts | grep "IT WORKED"
1 change: 1 addition & 0 deletions integration-test/esm-js-from-ts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utility = (str) => str.toUpperCase();
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "sourcemap",
"name": "esm-sourcemap",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"license": "ISC"
}
3 changes: 3 additions & 0 deletions integration-test/esm-sourcemap/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { utility } from "./utils.js";

console.log(utility("It worked!"));
6 changes: 6 additions & 0 deletions integration-test/esm-sourcemap/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $DIR/run.ts 2>&1 | tee /dev/stderr | grep "sourcemap/utils.ts:7"
echo "Found correct source location"
8 changes: 8 additions & 0 deletions integration-test/esm-sourcemap/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// add some lines that move the source lines around but not the output lines
export type Whatever = any;

/** A nice util */
export const utility = (str: string) => {
// this is on line 7 which we look for in the tests
throw new Error("error in utils");
};
11 changes: 11 additions & 0 deletions integration-test/esm-ts-from-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "esm-ts-from-js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
3 changes: 3 additions & 0 deletions integration-test/esm-ts-from-js/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { utility } from "./utils.js";

console.log(utility("It worked!"));
5 changes: 5 additions & 0 deletions integration-test/esm-ts-from-js/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $@ $DIR/run.js | grep "IT WORKED"
1 change: 1 addition & 0 deletions integration-test/esm-ts-from-js/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utility = (str: string) => str.toUpperCase();
11 changes: 11 additions & 0 deletions integration-test/esm-ts-from-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "simple-esm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
3 changes: 3 additions & 0 deletions integration-test/esm-ts-from-ts/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { utility } from "./utils.js";

console.log(utility("It worked!"));
5 changes: 5 additions & 0 deletions integration-test/esm-ts-from-ts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $@ $DIR/run.ts | grep "IT WORKED"
1 change: 1 addition & 0 deletions integration-test/esm-ts-from-ts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const utility = (str: string) => str.toUpperCase();
2 changes: 1 addition & 1 deletion integration-test/oom/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const leak = [];
while (true) {
leak.push("consuming memory");
leak.push("consuming memory".repeat(10000));
}
2 changes: 1 addition & 1 deletion integration-test/oom/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -ex

$DIR/../../pkg/wds.bin.js $@ --max-old-space-size=50 $DIR/run.ts 2>&1 | grep "ReportOOMFailure"
$DIR/../../pkg/wds.bin.js $@ --max-old-space-size=50 $DIR/run.ts 2>&1 | grep -E "ReportOOMFailure|JavaScript heap out of memory"

echo "found OOM failure"
2 changes: 1 addition & 1 deletion integration-test/reload/run-scratch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const http = require("http");
import http from "http";

const requestListener = function (req, res) {
res.writeHead(200);
Expand Down
Loading

0 comments on commit afdf5bd

Please sign in to comment.