Skip to content

Commit d2aec21

Browse files
committed
refactor: migrate to esm, use pkg-utils for build, switch tests to Vitest
1 parent dc8ad40 commit d2aec21

24 files changed

+2916
-1550
lines changed

build/compat-runtime-stubs.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Generates backwards compatible runtime no-ops after build completes
2+
// - lib/empty.js → empty ESM module (runtime no-op)
3+
// - lib/empty.cjs → empty CJS module (runtime no-op)
4+
5+
import {mkdirSync, writeFileSync} from 'node:fs'
6+
import {fileURLToPath} from 'node:url'
7+
import path from 'node:path'
8+
9+
// Logging helpers
10+
const GREY = '\x1b[0m'
11+
const GREEN = '\x1b[32m'
12+
const RED = '\x1b[31m'
13+
const TAG = `[compat-shims]${GREY}`
14+
const info = (msg) => console.log(`${GREEN}${TAG} ${msg}`)
15+
const fail = (msg, err) => {
16+
console.error(`${RED}${TAG}${GREY} ${msg}`)
17+
if (err) console.error(err)
18+
process.exitCode = 1
19+
}
20+
21+
const __filename = fileURLToPath(import.meta.url)
22+
const __dirname = path.dirname(__filename)
23+
const LIB_DIR = path.resolve(__dirname, '..', 'lib', 'compat')
24+
const EMPTY_ESM = path.join(LIB_DIR, 'empty.js')
25+
const EMPTY_CJS = path.join(LIB_DIR, 'empty.cjs')
26+
const compatComment = '// Generated for backwards compatibility'
27+
28+
try {
29+
mkdirSync(LIB_DIR, {recursive: true})
30+
31+
const esmStub = `${compatComment}
32+
export {}
33+
`
34+
const cjsStub = `${compatComment}
35+
module.exports = {}
36+
`
37+
38+
writeFileSync(EMPTY_ESM, esmStub, 'utf8')
39+
writeFileSync(EMPTY_CJS, cjsStub, 'utf8')
40+
41+
info('Wrote runtime compat no-op files')
42+
} catch (err) {
43+
fail('Failed to write runtime compat no-op files', err)
44+
}

index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {defineConfig} from '@sanity/pkg-utils'
2+
3+
export default defineConfig({
4+
extract: {
5+
rules: {
6+
'ae-internal-missing-underscore': 'off',
7+
},
8+
},
9+
tsconfig: 'tsconfig.json',
10+
})

package.json

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,50 @@
22
"name": "@sanity/image-url",
33
"version": "1.2.0",
44
"description": "Tools to generate image urls from Sanity content",
5-
"source": "src/builder.ts",
6-
"main": "lib/node/index.js",
7-
"browser": "lib/browser/image-url.umd.js",
8-
"umd:main": "lib/browser/image-url.umd.js",
9-
"typings": "lib/types/index.d.ts",
5+
"type": "module",
6+
"exports": {
7+
".": {
8+
"source": "./src/index.ts",
9+
"import": "./lib/index.js",
10+
"require": "./lib/index.cjs",
11+
"default": "./lib/index.js"
12+
},
13+
"./lib/types/*": {
14+
"import": "./lib/compat/empty.js",
15+
"require": "./lib/compat/empty.cjs",
16+
"default": "./lib/compat/empty.js"
17+
},
18+
"./package.json": "./package.json"
19+
},
20+
"main": "./lib/index.cjs",
21+
"module": "./lib/index.js",
22+
"types": "./lib/index.d.ts",
1023
"files": [
1124
"lib",
12-
"index.js",
13-
"urlForImage.js"
25+
"src",
26+
"build/compat-shims.mjs"
1427
],
1528
"sideEffects": false,
1629
"amdName": "SanityImageUrlBuilder",
1730
"scripts": {
1831
"prepublishOnly": "npm run build",
1932
"prebuild": "rimraf lib coverage .rts2*",
20-
"build": "npm run build:node && npm run build:browser",
21-
"build:node": "tsc -m commonjs",
22-
"build:browser": "microbundle build -i src/browser.ts -o lib/browser -f umd,es --no-compress",
33+
"build": "pkg build --strict",
34+
"build:compat": "tsc -p tsconfig.compat.json && node build/compat-runtime-stubs.mjs",
35+
"postbuild": "npm run build:compat",
36+
"prepack": "npm run build:compat",
2337
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
24-
"test": "jest --coverage",
25-
"test:watch": "jest --coverage --watch",
38+
"test": "vitest run --coverage",
39+
"test:watch": "vitest coverage",
2640
"test:prod": "npm run lint && npm run test -- --no-cache",
2741
"posttest": "npm run lint",
2842
"format": "prettier . --write",
2943
"release": "changeset publish"
3044
},
45+
"browserslist": "extends @sanity/browserslist-config",
3146
"engines": {
3247
"node": ">=10.0.0"
3348
},
34-
"jest": {
35-
"transform": {
36-
".(ts|tsx)": "ts-jest"
37-
},
38-
"testEnvironment": "node",
39-
"testURL": "http://localhost",
40-
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
41-
"moduleFileExtensions": [
42-
"ts",
43-
"tsx",
44-
"js"
45-
],
46-
"coveragePathIgnorePatterns": [
47-
"/node_modules/",
48-
"/test/"
49-
],
50-
"collectCoverageFrom": [
51-
"src/*.{js,ts}"
52-
]
53-
},
5449
"prettier": {
5550
"semi": false,
5651
"singleQuote": true,
@@ -66,16 +61,16 @@
6661
"@changesets/changelog-github": "^0.5.1",
6762
"@changesets/cli": "^2.29.6",
6863
"@sanity/client": "^6.22.2",
69-
"@types/jest": "^27.4.0",
70-
"@types/node": "^17.0.18",
71-
"jest": "^27.5.1",
64+
"@sanity/pkg-utils": "^8.0.3",
65+
"@types/node": "^24.3.0",
66+
"@vitest/coverage-v8": "^3.2.4",
7267
"microbundle": "^0.15.1",
7368
"prettier": "^2.5.1",
7469
"rimraf": "^3.0.2",
75-
"ts-jest": "^27.1.3",
7670
"tslint": "^6.1.3",
7771
"tslint-config-prettier": "^1.15.0",
78-
"typescript": "^4.5.5"
72+
"typescript": "^5.9.2",
73+
"vitest": "^3.2.4"
7974
},
8075
"repository": {
8176
"type": "git",

0 commit comments

Comments
 (0)