From 8597db7945900a2d330f3bdfee7dc3b3b3dec881 Mon Sep 17 00:00:00 2001 From: sinclairzx81 Date: Sun, 31 Dec 2023 08:33:48 +0900 Subject: [PATCH] ESM Support (#120) --- index.ts => index.mts | 0 package.json | 33 +++++++++++++++---- post-build.js | 17 ++++++++++ tests/index.js | 2 +- tsconfig.json | 2 +- types/{index.test-d.ts => index.test-d.mts} | 2 +- types/{plugin.test-d.ts => plugin.test-d.mts} | 2 +- 7 files changed, 48 insertions(+), 10 deletions(-) rename index.ts => index.mts (100%) create mode 100644 post-build.js rename types/{index.test-d.ts => index.test-d.mts} (93%) rename types/{plugin.test-d.ts => plugin.test-d.mts} (98%) diff --git a/index.ts b/index.mts similarity index 100% rename from index.ts rename to index.mts diff --git a/package.json b/package.json index e4d3adf..708406c 100644 --- a/package.json +++ b/package.json @@ -2,17 +2,36 @@ "name": "@fastify/type-provider-typebox", "version": "3.6.0", "description": "A Type Provider for Typebox over Fastify", - "main": "dist/index.js", - "types": "dist/index.d.ts", + "module": "dist/esm/index.mjs", + "main": "dist/cjs/index.js", + "types": "dist/cjs/index.d.ts", + "exports": { + ".": { + "require": { + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.js" + }, + "import": { + "types": "./dist/esm/index.d.mts", + "default": "./dist/esm/index.mjs" + } + } + }, "peerDependencies": { "@sinclair/typebox": ">=0.26 <=0.32" }, "scripts": { - "build": "rimraf ./dist && mkdir dist && tsc --outDir dist", - "test": "npm run build && npm run typescript && tap tests/index.js --no-check-coverage", - "lint": "eslint . --ext .ts --ext .js", + "build:clean": "rimraf ./dist", + "build:cjs": "tsc --outDir dist/cjs --module CommonJS --moduleResolution Node10 --sourcemap false", + "build:esm": "tsc --outDir dist/esm --module NodeNext --moduleResolution NodeNext --sourcemap false", + "build:post": "node post-build.js", + "build:test": "attw --pack .", + "build": "npm-run-all build:clean build:cjs build:esm build:post build:test", + "test:tap": "tap tests/index.js --no-check-coverage", + "test:types": "tsd", + "test": "npm-run-all build test:tap test:types", "lint:fix": "npm run lint -- --fix", - "typescript": "tsd", + "lint": "eslint . --ext .ts --ext .js --ext .mts --ext .mjs", "prepublishOnly": "npm run build" }, "repository": { @@ -30,6 +49,7 @@ }, "homepage": "https://github.com/fastify/fastify-type-provider-typebox#readme", "devDependencies": { + "@arethetypeswrong/cli": "^0.13.5", "@types/node": "^20.1.0", "@typescript-eslint/eslint-plugin": "^5.32.0", "@typescript-eslint/parser": "^5.32.0", @@ -41,6 +61,7 @@ "fastify": "^4.17.0", "fastify-plugin": "^4.5.0", "fastify-tsconfig": "^2.0.0", + "npm-run-all": "^4.1.5", "rimraf": "^5.0.0", "tap": "^16.3.0", "tsd": "^0.29.0", diff --git a/post-build.js b/post-build.js new file mode 100644 index 0000000..16a5334 --- /dev/null +++ b/post-build.js @@ -0,0 +1,17 @@ +// ------------------------------------------------------------------ +// Post Build +// +// TypeScript emits .mjs files when compiling from .mts. This +// is true even if the module target is set to CommonJS. This +// code renames the compiler emitted files to use the correct +// file extension. +// +// Run via: +// +// $ npm run build:post +// +// ------------------------------------------------------------------ + +const fs = require('fs') +fs.renameSync('dist/cjs/index.d.mts', 'dist/cjs/index.d.ts') +fs.renameSync('dist/cjs/index.mjs', 'dist/cjs/index.js') diff --git a/tests/index.js b/tests/index.js index fbc679c..945f03d 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2,7 +2,7 @@ const tap = require('tap') const Fastify = require('fastify') -const { Type, TypeBoxValidatorCompiler } = require('../dist/index') +const { Type, TypeBoxValidatorCompiler } = require('../dist/cjs/index') // This test ensures AJV ignores the TypeBox [Kind] symbol property in strict tap.test('should compile typebox schema without configuration', async t => { diff --git a/tsconfig.json b/tsconfig.json index a643857..43e00fa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,5 +5,5 @@ "sourceMap": true, "declaration": true }, - "exclude": ["./node_modules", "types/**/*.test-d.ts"] + "exclude": ["./node_modules", "types/**/*.test-d.mts"] } diff --git a/types/index.test-d.ts b/types/index.test-d.mts similarity index 93% rename from types/index.test-d.ts rename to types/index.test-d.mts index 4a76570..c5cab16 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.mts @@ -1,4 +1,4 @@ -import { Type, TypeBoxTypeProvider } from '../index' +import { Type, TypeBoxTypeProvider } from '../index.mjs' import { expectAssignable, expectType } from 'tsd' import Fastify, { FastifyInstance, FastifyBaseLogger, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault } from 'fastify' diff --git a/types/plugin.test-d.ts b/types/plugin.test-d.mts similarity index 98% rename from types/plugin.test-d.ts rename to types/plugin.test-d.mts index c775434..fcc68e2 100644 --- a/types/plugin.test-d.ts +++ b/types/plugin.test-d.mts @@ -1,4 +1,4 @@ -import { Type, FastifyPluginAsyncTypebox, FastifyPluginCallbackTypebox } from '../index' +import { Type, FastifyPluginAsyncTypebox, FastifyPluginCallbackTypebox } from '../index.mjs' import { expectType } from 'tsd' import Fastify, { FastifyPluginAsync, FastifyPluginCallback } from 'fastify' import fp from 'fastify-plugin'