Skip to content

Instantly share code, notes, and snippets.

@pbrisbin
Last active March 30, 2026 13:37
Show Gist options
  • Select an option

  • Save pbrisbin/35f35d361092b3d67affed008dd71e2d to your computer and use it in GitHub Desktop.

Select an option

Save pbrisbin/35f35d361092b3d67affed008dd71e2d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Convert a typescript action to an ESM module. This expects how Freckle's
# actions are organized; it is not general-purpose.
#
###
set -euo pipefail
# Ensure clean slate
git checkout .
git clean -f .
git checkout main
git pull
# Update local imports to have a .js extension, now that they will be transpiled
find src -name '*.ts' -exec sed -i 's/from\( "\..*\)";/from\1.js"/' {} +
# Add "type: module" after "version"
sed -i 's/^\( *\)\("version":.*\)$/\1\2\n\1"type": "module",/' package.json
# Update test script, with or without trailing comma
sed -i 's/"test": "jest"\(,\)\?$/"test": "NODE_OPTIONS='\''--experimental-vm-modules'\'' jest"\1/' package.json
# Remove any ts jest config...
rm jest.config.ts || true
# and add a json one
cat >jest.config.js <<'EOM'
import { createDefaultPreset } from "ts-jest";
const presetConfig = createDefaultPreset();
const jestConfig = {
...presetConfig,
resetMocks: true,
testEnvironment: "node",
extensionsToTreatAsEsm: [".ts"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.ts$": [
"ts-jest",
{
useESM: true,
},
],
},
};
export default jestConfig;
EOM
# Ensure module is NodeNext (some are already, some not)
sed -i 's/"module":.*$/"module": "NodeNext",/' tsconfig.json
# Add moduleResolution: after module: (why are both needed?)
sed -i 's/^\( *\)\("module": .*\)$/\1\2\n\1"moduleResolution": "NodeNext",/' tsconfig.json
# Ensure isolatedModules after strict, this only matches if there's no
# trailing comma (meaning we haven't done this fix already)
sed -i 's/^\( *\)\("strict": true\)$/\1\2,\n\1"isolatedModules": true/' tsconfig.json
yarn test
git checkout -b pb/esm
git commit -a -m 'fix: convert to esm module'
yarn build
git add dist
git commit -m 'chore(dist): build'
create-pr --draft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment