From 4e10994e973f6a6a238e2d6f4984301d5fa1530e Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Fri, 9 Feb 2024 14:24:14 -0700 Subject: [PATCH 1/3] Fix ESM/CJS compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Superstruct cannot be imported in a TypeScript project that uses a `moduleResolution` of `node16` or `nodenext`. Any attempt to do so will result in type errors such as the following: ``` node_modules/superstruct/dist/index.d.ts:1:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path. 1 export * from './error'; ~~~~~~~~~ node_modules/superstruct/dist/index.d.ts:2:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path. 2 export * from './struct'; ~~~~~~~~~~ node_modules/superstruct/dist/index.d.ts:3:15 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path. 3 export * from './structs/coercions'; ~~~~~~~~~~~~~~~~~~~~~ ... ``` This is happening because although Superstruct is defined as an ESM library — the `module` field in `package.json` is set to `true` — its published TypeScript declaration files aren't ESM-compatible. This is due to the fact that imports are missing extensions, which is a requirement for ESM. At the same time, although Rollup is supposedly configured to emit both ESM- and CJS-compatible files, TypeScript does not know how to use the CJS variants. The `exports` field is the way to instruct TypeScript on how to do this, but `package.json` is missing this field. This makes it impossible to use Superstruct in a non-ESM library. This commit fixes both of these issues by making the following changes: - All imports in TypeScript files now use an extension of `.js`. This may seem like an odd choice, as there are no JavaScript files in the `src/` directory, but TypeScript doesn't resolve a module by trying to find a file with the given extension; it tries to find a declaration file that maps to the module path, based on a set of rules. Therefore, the file extension is really just a formality. In the end, Rollup is consolidating implementation files into one, so all of the imports will go away. - Type declaration files have been split into ESM and CJS variants. Since Rollup generates ESM variants with an `.mjs` extension and CJS variants with a `.cjs` extension, imports in declaration files also use either `.mjs` or `.cjs` depending on the variant. - `package.json` now has an `exports` field which instructs TypeScript how to resolve modules from either an ESM or CJS perspective. In addition, the tests have been updated to use Vitest instead of Babel so that they can read directly from the TypeScript configuration instead of having to use an explicit transform step. In conjunction with this change, the minimum Node version has been bumped to 16.x and the GitHub workflows have been updated to stop testing on Node 14.x. 14.x has reached EOL, the version of Vitest we're using isn't compatible with this version, and a `target` of `esnext` is being used in the TypeScript config, which require a JavaScript runtime that supports newer ES features such as `??=`, so the minimum Node version is effectively 16.x anyway. This commit is derived from the following PR on the Superstruct repo: For more information on how TypeScript resolves modules, the "Modules Reference" section of the TypeScript handbook is quite helpful, and in particular these sections: - - --- .babelrc | 15 --------- .eslintrc | 8 ++++- .github/workflows/ci.yml | 2 +- package.json | 39 +++++++++++++--------- scripts/split-tsd-files.sh | 22 ++++++++++++ src/index.ts | 12 +++---- src/struct.ts | 4 +-- src/structs/coercions.ts | 6 ++-- src/structs/refinements.ts | 4 +-- src/structs/types.ts | 6 ++-- src/structs/utilities.ts | 11 ++++-- src/utils.ts | 4 +-- test/api/{assert.ts => assert.test.ts} | 1 + test/api/{create.ts => create.test.ts} | 1 + test/api/{is.ts => is.test.ts} | 1 + test/api/{mask.ts => mask.test.ts} | 1 + test/api/{validate.ts => validate.test.ts} | 1 + test/{index.ts => index.test.ts} | 16 +++------ test/register.cjs | 3 -- test/tsconfig.json | 6 +++- test/typings/any.ts | 2 +- test/typings/array.ts | 2 +- test/typings/assign.ts | 2 +- test/typings/bigint.ts | 2 +- test/typings/boolean.ts | 2 +- test/typings/coerce.ts | 2 +- test/typings/date.ts | 2 +- test/typings/defaulted.ts | 2 +- test/typings/deprecated.ts | 2 +- test/typings/describe.ts | 2 +- test/typings/dynamic.ts | 2 +- test/typings/empty.ts | 2 +- test/typings/enums.ts | 2 +- test/typings/func.ts | 2 +- test/typings/infer.ts | 2 +- test/typings/instance.ts | 2 +- test/typings/integer.ts | 2 +- test/typings/intersection.ts | 2 +- test/typings/lazy.ts | 2 +- test/typings/literal.ts | 2 +- test/typings/map.ts | 2 +- test/typings/max.ts | 2 +- test/typings/min.ts | 2 +- test/typings/never.ts | 2 +- test/typings/nonempty.ts | 2 +- test/typings/nullable.ts | 2 +- test/typings/number.ts | 2 +- test/typings/object.ts | 2 +- test/typings/omit.ts | 2 +- test/typings/optional.ts | 2 +- test/typings/partial.ts | 2 +- test/typings/pattern.ts | 2 +- test/typings/pick.ts | 2 +- test/typings/record.ts | 2 +- test/typings/refine.ts | 2 +- test/typings/regexp.ts | 2 +- test/typings/set.ts | 2 +- test/typings/size.ts | 2 +- test/typings/string.ts | 2 +- test/typings/struct.ts | 2 +- test/typings/trimmed.ts | 2 +- test/typings/tuple.ts | 2 +- test/typings/type.ts | 2 +- test/typings/union.ts | 2 +- test/typings/unknown.ts | 2 +- 65 files changed, 140 insertions(+), 113 deletions(-) delete mode 100644 .babelrc create mode 100755 scripts/split-tsd-files.sh rename test/api/{assert.ts => assert.test.ts} (95%) rename test/api/{create.ts => create.test.ts} (97%) rename test/api/{is.ts => is.test.ts} (91%) rename test/api/{mask.ts => mask.test.ts} (98%) rename test/api/{validate.ts => validate.test.ts} (98%) rename test/{index.ts => index.test.ts} (92%) delete mode 100644 test/register.cjs diff --git a/.babelrc b/.babelrc deleted file mode 100644 index fd985359..00000000 --- a/.babelrc +++ /dev/null @@ -1,15 +0,0 @@ -{ - "presets": [ - "@babel/typescript", - [ - "@babel/preset-env", - { - "targets": { "browsers": "defaults, not ie 11", "node": true }, - "modules": false, - "useBuiltIns": false, - "loose": true - } - ] - ], - "plugins": ["@babel/plugin-transform-modules-commonjs"] -} diff --git a/.eslintrc b/.eslintrc index 85e957da..78f1a4e2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -20,8 +20,14 @@ "node": true }, "settings": { - "import/extensions": [".js", ".ts"] + "import/extensions": [".js", ".ts"], + "import/resolver": { + "typescript": { + "project": ["./tsconfig.json"] + } + } }, + "ignorePatterns": ["/dist/**"], "rules": { "@typescript-eslint/no-unused-vars": [ "error", diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 727c0e35..7b8c6d54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [14.x, 16.x, 18.x] + node-version: [16.x, 18.x] steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v2 diff --git a/package.json b/package.json index c1008fcc..999bdc4d 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,21 @@ "license": "MIT", "repository": "git://github.com/MetaMask/superstruct.git", "type": "module", + "exports": { + ".": { + "import": { + "default": "./dist/index.mjs", + "types": "./dist/index.d.mts" + }, + "require": { + "default": "./dist/index.cjs", + "types": "./dist/index.d.cts" + } + } + }, "main": "./dist/index.cjs", "module": "./dist/index.mjs", - "types": "./dist/index.d.ts", + "types": "./dist/index.d.cts", "sideEffects": false, "files": [ "dist" @@ -16,39 +28,36 @@ "registry": "https://registry.npmjs.org" }, "engines": { - "node": ">=14.0.0" + "node": ">=16.0.0" }, "devDependencies": { - "@babel/cli": "^7.6.3", - "@babel/core": "^7.6.3", - "@babel/plugin-transform-modules-commonjs": "^7.12.1", - "@babel/preset-env": "^7.20.2", - "@babel/preset-typescript": "^7.6.0", - "@babel/register": "^7.6.2", "@rollup/plugin-typescript": "^9.0.2", "@types/expect": "^24.3.0", "@types/lodash": "^4.14.144", + "@types/lodash-es": "^4.17.12", "@types/mocha": "^10.0.0", "@types/node": "^18.7.14", "@typescript-eslint/eslint-plugin": "^5.43.0", "@typescript-eslint/parser": "^5.43.0", - "babel-eslint": "^10.0.3", "eslint": "^7.14.0", "eslint-config-prettier": "^7.2.0", - "eslint-plugin-import": "^2.22.1", + "eslint-import-resolver-typescript": "^3.6.1", + "eslint-plugin-import": "^2.29.1", "eslint-plugin-prettier": "^4.0.0", "is-email": "^1.0.0", "is-url": "^1.2.4", "is-uuid": "^1.0.2", - "lodash": "^4.17.15", + "jest": "^29.7.0", + "lodash-es": "^4.17.21", "mocha": "^10.0.0", "np": "^7.6.2", "prettier": "^2.0.5", "rollup": "^3.3.0", - "typescript": "^4.8.3" + "typescript": "^4.8.3", + "vitest": "^1.2.2" }, "scripts": { - "build": "rm -rf ./{dist} && rollup --config ./rollup.config.js", + "build": "rm -rf ./dist && rollup --config ./rollup.config.js && scripts/split-tsd-files.sh", "clean": "rm -rf ./{dist,node_modules}", "fix": "npm run fix:eslint && npm run fix:prettier", "fix:eslint": "npm run lint:eslint --fix", @@ -57,8 +66,8 @@ "lint:eslint": "eslint '{src,test}/*.{js,ts}'", "lint:prettier": "prettier --list-different '**/*.{js,json,ts}'", "release": "npm run build && npm run lint && np", - "test": "npm run build && npm run test:types && npm run test:mocha", - "test:mocha": "mocha --require ./test/register.cjs --require source-map-support/register ./test/index.ts", + "test": "npm run build && npm run test:types && npm run test:vitest", + "test:vitest": "vitest run", "test:types": "tsc --noEmit && tsc --project ./test/tsconfig.json --noEmit", "watch": "npm run build -- --watch" }, diff --git a/scripts/split-tsd-files.sh b/scripts/split-tsd-files.sh new file mode 100755 index 00000000..336a7b0d --- /dev/null +++ b/scripts/split-tsd-files.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +set -euo pipefail + +for file in dist/*.d.ts dist/structs/*.d.ts; do + mv $file "${file%.d.ts}.d.cts" + cp "${file%.d.ts}.d.cts" "${file%.d.ts}.d.mts" +done + +for file in dist/*.d.cts dist/structs/*.d.cts; do + echo "File: $file" + sed -e 's/\.d\.ts/.d.cts/' -i.bak "$file" + sed -e 's/\.js/.cjs/' -i.bak "$file" +done + +for file in dist/*.d.mts dist/structs/*.d.mts; do + echo "File: $file" + sed -e 's/\.d\.ts/\.d\.mts/' -i.bak "$file" + sed -e 's/\.js/.mjs/' -i.bak "$file" +done + +find dist -name '*.bak' | xargs rm diff --git a/src/index.ts b/src/index.ts index de84ece0..53baf221 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -export * from './error' -export * from './struct' -export * from './structs/coercions' -export * from './structs/refinements' -export * from './structs/types' -export * from './structs/utilities' +export * from './error.js' +export * from './struct.js' +export * from './structs/coercions.js' +export * from './structs/refinements.js' +export * from './structs/types.js' +export * from './structs/utilities.js' diff --git a/src/struct.ts b/src/struct.ts index b482fd3b..9a5c37ec 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -1,5 +1,5 @@ -import { toFailures, shiftIterator, StructSchema, run } from './utils' -import { StructError, Failure } from './error' +import { toFailures, shiftIterator, StructSchema, run } from './utils.js' +import { StructError, Failure } from './error.js' /** * `Struct` objects encapsulate the validation logic for a specific type of diff --git a/src/structs/coercions.ts b/src/structs/coercions.ts index 6dca4ece..62566c50 100644 --- a/src/structs/coercions.ts +++ b/src/structs/coercions.ts @@ -1,6 +1,6 @@ -import { Struct, is, Coercer } from '../struct' -import { isPlainObject } from '../utils' -import { string, unknown } from './types' +import { Struct, is, Coercer } from '../struct.js' +import { isPlainObject } from '../utils.js' +import { string, unknown } from './types.js' /** * Augment a `Struct` to add an additional coercion step to its input. diff --git a/src/structs/refinements.ts b/src/structs/refinements.ts index e3f7736d..276bc925 100644 --- a/src/structs/refinements.ts +++ b/src/structs/refinements.ts @@ -1,5 +1,5 @@ -import { Struct, Refiner } from '../struct' -import { toFailures } from '../utils' +import { Struct, Refiner } from '../struct.js' +import { toFailures } from '../utils.js' /** * Ensure that a string, array, map, or set is empty. diff --git a/src/structs/types.ts b/src/structs/types.ts index 8bcc0ef3..19493104 100644 --- a/src/structs/types.ts +++ b/src/structs/types.ts @@ -1,5 +1,5 @@ -import { Infer, Struct } from '../struct' -import { define } from './utilities' +import { Infer, Struct } from '../struct.js' +import { define } from './utilities.js' import { ObjectSchema, ObjectType, @@ -9,7 +9,7 @@ import { AnyStruct, InferStructTuple, UnionToIntersection, -} from '../utils' +} from '../utils.js' /** * Ensure that any value passes validation. diff --git a/src/structs/utilities.ts b/src/structs/utilities.ts index d2162a3d..af87c1c8 100644 --- a/src/structs/utilities.ts +++ b/src/structs/utilities.ts @@ -1,6 +1,11 @@ -import { Context, Struct, Validator } from '../struct' -import { Assign, ObjectSchema, ObjectType, PartialObjectSchema } from '../utils' -import { object, optional, type } from './types' +import { Context, Struct, Validator } from '../struct.js' +import { + Assign, + ObjectSchema, + ObjectType, + PartialObjectSchema, +} from '../utils.js' +import { object, optional, type } from './types.js' /** * Create a new struct that combines the properties properties from multiple diff --git a/src/utils.ts b/src/utils.ts index 92df9755..0928871a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ -import { Struct, Infer, Result, Context, Describe } from './struct' -import { Failure } from './error' +import { Struct, Infer, Result, Context, Describe } from './struct.js' +import { Failure } from './error.js' /** * Check if a value is an iterator. diff --git a/test/api/assert.ts b/test/api/assert.test.ts similarity index 95% rename from test/api/assert.ts rename to test/api/assert.test.ts index 65d55a56..60306388 100644 --- a/test/api/assert.ts +++ b/test/api/assert.test.ts @@ -1,5 +1,6 @@ import { throws, doesNotThrow } from 'assert' import { assert, string, StructError } from '../../src' +import { describe, it } from 'vitest' describe('assert', () => { it('valid as helper', () => { diff --git a/test/api/create.ts b/test/api/create.test.ts similarity index 97% rename from test/api/create.ts rename to test/api/create.test.ts index 873d30ec..64fdd87b 100644 --- a/test/api/create.ts +++ b/test/api/create.test.ts @@ -1,3 +1,4 @@ +import { describe, it } from 'vitest' import { strictEqual, deepEqual, deepStrictEqual, throws } from 'assert' import { type, diff --git a/test/api/is.ts b/test/api/is.test.ts similarity index 91% rename from test/api/is.ts rename to test/api/is.test.ts index 46789f95..f60910ba 100644 --- a/test/api/is.ts +++ b/test/api/is.test.ts @@ -1,3 +1,4 @@ +import { describe, it } from 'vitest' import { strictEqual } from 'assert' import { is, string } from '../../src' diff --git a/test/api/mask.ts b/test/api/mask.test.ts similarity index 98% rename from test/api/mask.ts rename to test/api/mask.test.ts index c054d978..72fecd35 100644 --- a/test/api/mask.ts +++ b/test/api/mask.test.ts @@ -1,3 +1,4 @@ +import { describe, it } from 'vitest' import { deepStrictEqual, throws } from 'assert' import { mask, diff --git a/test/api/validate.ts b/test/api/validate.test.ts similarity index 98% rename from test/api/validate.ts rename to test/api/validate.test.ts index cb6d878f..52bcc5b1 100644 --- a/test/api/validate.ts +++ b/test/api/validate.test.ts @@ -1,3 +1,4 @@ +import { describe, it } from 'vitest' import { deepStrictEqual, strictEqual } from 'assert' import { validate, diff --git a/test/index.ts b/test/index.test.ts similarity index 92% rename from test/index.ts rename to test/index.test.ts index c63f45a0..eb7daad8 100644 --- a/test/index.ts +++ b/test/index.test.ts @@ -1,6 +1,6 @@ import assert, { CallTracker } from 'assert' import fs from 'fs' -import { pick } from 'lodash' +import { pick } from 'lodash-es' import { basename, extname, resolve } from 'path' import { any, @@ -12,15 +12,9 @@ import { StructError, } from '../src' -describe('superstruct', () => { - describe('api', () => { - require('./api/assert') - require('./api/create') - require('./api/is') - require('./api/mask') - require('./api/validate') - }) +import { describe, it } from 'vitest' +describe('superstruct', () => { describe('validation', () => { const kindsDir = resolve(__dirname, 'validation') const kinds = fs @@ -29,7 +23,7 @@ describe('superstruct', () => { .map((t) => basename(t, extname(t))) for (const kind of kinds) { - describe(kind, () => { + describe(kind, async () => { const testsDir = resolve(kindsDir, kind) const tests = fs .readdirSync(testsDir) @@ -37,7 +31,7 @@ describe('superstruct', () => { .map((t) => basename(t, extname(t))) for (const name of tests) { - const module = require(resolve(testsDir, name)) + const module = await import(resolve(testsDir, name)) const { Struct, data, create, only, skip, output, failures } = module const run = only ? it.only : skip ? it.skip : it run(name, () => { diff --git a/test/register.cjs b/test/register.cjs deleted file mode 100644 index 976606be..00000000 --- a/test/register.cjs +++ /dev/null @@ -1,3 +0,0 @@ -require('@babel/register')({ - extensions: ['.js', '.jsx', '.ts', '.tsx'], -}) diff --git a/test/tsconfig.json b/test/tsconfig.json index 5197ce27..50836b87 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,4 +1,8 @@ { "extends": "../tsconfig.json", - "include": ["./**/*.ts"] + "include": ["./**/*.ts"], + "compilerOptions": { + "lib": ["esnext"], + "skipLibCheck": true + } } diff --git a/test/typings/any.ts b/test/typings/any.ts index cbca7d74..30f36867 100644 --- a/test/typings/any.ts +++ b/test/typings/any.ts @@ -1,5 +1,5 @@ import { assert, any } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, any()) diff --git a/test/typings/array.ts b/test/typings/array.ts index e79e3218..0b1f4c59 100644 --- a/test/typings/array.ts +++ b/test/typings/array.ts @@ -1,5 +1,5 @@ import { assert, array, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test>((x) => { assert(x, array()) diff --git a/test/typings/assign.ts b/test/typings/assign.ts index 223e0944..adcddb1d 100644 --- a/test/typings/assign.ts +++ b/test/typings/assign.ts @@ -1,5 +1,5 @@ import { assert, assign, object, number, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ a: number diff --git a/test/typings/bigint.ts b/test/typings/bigint.ts index 3138bdcc..5e36581b 100644 --- a/test/typings/bigint.ts +++ b/test/typings/bigint.ts @@ -1,5 +1,5 @@ import { assert, bigint } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, bigint()) diff --git a/test/typings/boolean.ts b/test/typings/boolean.ts index 4a78f245..beca4702 100644 --- a/test/typings/boolean.ts +++ b/test/typings/boolean.ts @@ -1,5 +1,5 @@ import { assert, boolean } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, boolean()) diff --git a/test/typings/coerce.ts b/test/typings/coerce.ts index 6c75fc16..1cb7b2dc 100644 --- a/test/typings/coerce.ts +++ b/test/typings/coerce.ts @@ -1,5 +1,5 @@ import { assert, coerce, string, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert( diff --git a/test/typings/date.ts b/test/typings/date.ts index 9e145bc3..00fc0ca3 100644 --- a/test/typings/date.ts +++ b/test/typings/date.ts @@ -1,5 +1,5 @@ import { assert, date } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, date()) diff --git a/test/typings/defaulted.ts b/test/typings/defaulted.ts index 55cb30a3..f75a0685 100644 --- a/test/typings/defaulted.ts +++ b/test/typings/defaulted.ts @@ -1,5 +1,5 @@ import { assert, defaulted, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, defaulted(string(), 'Untitled')) diff --git a/test/typings/deprecated.ts b/test/typings/deprecated.ts index dbd6b745..906c20e1 100644 --- a/test/typings/deprecated.ts +++ b/test/typings/deprecated.ts @@ -1,5 +1,5 @@ import { assert, object, deprecated, any, Context } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { const log = (value: unknown, ctx: Context) => {} diff --git a/test/typings/describe.ts b/test/typings/describe.ts index 643b3774..5c22599d 100644 --- a/test/typings/describe.ts +++ b/test/typings/describe.ts @@ -28,7 +28,7 @@ import { min, pattern, } from '../../src' -import { test } from '..' +import { test } from '../index.test' test>((x) => { return any() diff --git a/test/typings/dynamic.ts b/test/typings/dynamic.ts index bfdd1948..ec8a4820 100644 --- a/test/typings/dynamic.ts +++ b/test/typings/dynamic.ts @@ -1,5 +1,5 @@ import { assert, dynamic, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert( diff --git a/test/typings/empty.ts b/test/typings/empty.ts index 67b25be1..559bddc0 100644 --- a/test/typings/empty.ts +++ b/test/typings/empty.ts @@ -1,5 +1,5 @@ import { assert, empty, string, array, map, set } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, empty(string())) diff --git a/test/typings/enums.ts b/test/typings/enums.ts index 87505ec8..719c520e 100644 --- a/test/typings/enums.ts +++ b/test/typings/enums.ts @@ -1,5 +1,5 @@ import { assert, enums } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<'a' | 'b' | 'c'>((x) => { assert(x, enums(['a', 'b', 'c'])) diff --git a/test/typings/func.ts b/test/typings/func.ts index 95348aca..82769831 100644 --- a/test/typings/func.ts +++ b/test/typings/func.ts @@ -1,5 +1,5 @@ import { assert, func } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, func()) diff --git a/test/typings/infer.ts b/test/typings/infer.ts index 20f8426f..0dcfd85a 100644 --- a/test/typings/infer.ts +++ b/test/typings/infer.ts @@ -1,5 +1,5 @@ import { Infer, object, number, string, assert } from '../../src' -import { test } from '..' +import { test } from '../index.test' const Struct = object() type T = Infer diff --git a/test/typings/instance.ts b/test/typings/instance.ts index 440ae863..41b5f63d 100644 --- a/test/typings/instance.ts +++ b/test/typings/instance.ts @@ -1,5 +1,5 @@ import { assert, instance } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, instance(Date)) diff --git a/test/typings/integer.ts b/test/typings/integer.ts index b6f95141..d872ffff 100644 --- a/test/typings/integer.ts +++ b/test/typings/integer.ts @@ -1,5 +1,5 @@ import { assert, integer } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, integer()) diff --git a/test/typings/intersection.ts b/test/typings/intersection.ts index e179be0c..d1051ac1 100644 --- a/test/typings/intersection.ts +++ b/test/typings/intersection.ts @@ -1,5 +1,5 @@ import { assert, intersection, object, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ a: string; b: string }>((x) => { assert(x, intersection([object({ a: string() }), object({ b: string() })])) diff --git a/test/typings/lazy.ts b/test/typings/lazy.ts index 69c3d2cf..8e99666a 100644 --- a/test/typings/lazy.ts +++ b/test/typings/lazy.ts @@ -1,5 +1,5 @@ import { assert, lazy, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert( diff --git a/test/typings/literal.ts b/test/typings/literal.ts index 08b8a345..9cfae598 100644 --- a/test/typings/literal.ts +++ b/test/typings/literal.ts @@ -1,5 +1,5 @@ import { assert, literal } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, literal(true)) diff --git a/test/typings/map.ts b/test/typings/map.ts index 8771e7e5..bb69a8da 100644 --- a/test/typings/map.ts +++ b/test/typings/map.ts @@ -1,5 +1,5 @@ import { assert, map, string, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test>((x) => { assert(x, map(string(), number())) diff --git a/test/typings/max.ts b/test/typings/max.ts index d30541f8..52061027 100644 --- a/test/typings/max.ts +++ b/test/typings/max.ts @@ -1,5 +1,5 @@ import { assert, number, max } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, max(number(), 0)) diff --git a/test/typings/min.ts b/test/typings/min.ts index d8072988..d1401c93 100644 --- a/test/typings/min.ts +++ b/test/typings/min.ts @@ -1,5 +1,5 @@ import { assert, number, min } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, min(number(), 0)) diff --git a/test/typings/never.ts b/test/typings/never.ts index 18ebd35b..20a4cfde 100644 --- a/test/typings/never.ts +++ b/test/typings/never.ts @@ -1,5 +1,5 @@ import { assert, never } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, never()) diff --git a/test/typings/nonempty.ts b/test/typings/nonempty.ts index 56a89bd5..f6b20ff9 100644 --- a/test/typings/nonempty.ts +++ b/test/typings/nonempty.ts @@ -1,5 +1,5 @@ import { assert, nonempty, string, array, map, set } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, nonempty(string())) diff --git a/test/typings/nullable.ts b/test/typings/nullable.ts index abb666e1..20ec100c 100644 --- a/test/typings/nullable.ts +++ b/test/typings/nullable.ts @@ -1,5 +1,5 @@ import { assert, nullable, string, object, enums } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, nullable(string())) diff --git a/test/typings/number.ts b/test/typings/number.ts index 594c4ce5..7c7ba2ad 100644 --- a/test/typings/number.ts +++ b/test/typings/number.ts @@ -1,5 +1,5 @@ import { assert, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, number()) diff --git a/test/typings/object.ts b/test/typings/object.ts index 553d786e..b54101de 100644 --- a/test/typings/object.ts +++ b/test/typings/object.ts @@ -1,5 +1,5 @@ import { assert, object, number, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test>((x) => { assert(x, object()) diff --git a/test/typings/omit.ts b/test/typings/omit.ts index da1ec000..e85108c6 100644 --- a/test/typings/omit.ts +++ b/test/typings/omit.ts @@ -1,5 +1,5 @@ import { assert, omit, object, number, string, type } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ b: string diff --git a/test/typings/optional.ts b/test/typings/optional.ts index 39ad575d..25a34567 100644 --- a/test/typings/optional.ts +++ b/test/typings/optional.ts @@ -1,5 +1,5 @@ import { assert, optional, string, number, object, enums } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, optional(string())) diff --git a/test/typings/partial.ts b/test/typings/partial.ts index c304da7a..b7af3aeb 100644 --- a/test/typings/partial.ts +++ b/test/typings/partial.ts @@ -1,5 +1,5 @@ import { assert, object, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ a?: number }>((x) => { assert(x, object({ a: number() })) diff --git a/test/typings/pattern.ts b/test/typings/pattern.ts index fdfce3f2..b6b0034b 100644 --- a/test/typings/pattern.ts +++ b/test/typings/pattern.ts @@ -1,5 +1,5 @@ import { assert, pattern, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, pattern(string(), /.*/)) diff --git a/test/typings/pick.ts b/test/typings/pick.ts index a3ab2c12..06c6c0ba 100644 --- a/test/typings/pick.ts +++ b/test/typings/pick.ts @@ -1,5 +1,5 @@ import { assert, pick, object, number, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ b: string diff --git a/test/typings/record.ts b/test/typings/record.ts index 74180ca2..cbaf66f0 100644 --- a/test/typings/record.ts +++ b/test/typings/record.ts @@ -1,5 +1,5 @@ import { assert, record, string, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test>((x) => { assert(x, record(string(), number())) diff --git a/test/typings/refine.ts b/test/typings/refine.ts index e384687d..15128cb8 100644 --- a/test/typings/refine.ts +++ b/test/typings/refine.ts @@ -1,5 +1,5 @@ import { assert, refine, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert( diff --git a/test/typings/regexp.ts b/test/typings/regexp.ts index c4fa510a..941fc5d1 100644 --- a/test/typings/regexp.ts +++ b/test/typings/regexp.ts @@ -1,5 +1,5 @@ import { assert, regexp } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, regexp()) diff --git a/test/typings/set.ts b/test/typings/set.ts index 911e52b7..a8557070 100644 --- a/test/typings/set.ts +++ b/test/typings/set.ts @@ -1,5 +1,5 @@ import { assert, set, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test>((x) => { assert(x, set(string())) diff --git a/test/typings/size.ts b/test/typings/size.ts index bae09977..8e258ca7 100644 --- a/test/typings/size.ts +++ b/test/typings/size.ts @@ -1,5 +1,5 @@ import { assert, size, string, array, number, map, set } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, size(number(), 1, 5)) diff --git a/test/typings/string.ts b/test/typings/string.ts index 3d6cf159..d7f70a81 100644 --- a/test/typings/string.ts +++ b/test/typings/string.ts @@ -1,5 +1,5 @@ import { assert, string } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, string()) diff --git a/test/typings/struct.ts b/test/typings/struct.ts index ceef1261..81ea5874 100644 --- a/test/typings/struct.ts +++ b/test/typings/struct.ts @@ -1,5 +1,5 @@ import { assert, define } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert( diff --git a/test/typings/trimmed.ts b/test/typings/trimmed.ts index 5dc035b0..fb7b3135 100644 --- a/test/typings/trimmed.ts +++ b/test/typings/trimmed.ts @@ -1,5 +1,5 @@ import { assert, string, trimmed } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, trimmed(string())) diff --git a/test/typings/tuple.ts b/test/typings/tuple.ts index 4889083b..2d6bd235 100644 --- a/test/typings/tuple.ts +++ b/test/typings/tuple.ts @@ -1,5 +1,5 @@ import { assert, tuple, string, number, literal } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<[string, number]>((x) => { assert(x, tuple([string(), number()])) diff --git a/test/typings/type.ts b/test/typings/type.ts index b8c0f2cd..8e5abf24 100644 --- a/test/typings/type.ts +++ b/test/typings/type.ts @@ -1,5 +1,5 @@ import { assert, type, number } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ a: number }>((x) => { assert(x, type({ a: number() })) diff --git a/test/typings/union.ts b/test/typings/union.ts index f07bd39a..d282f264 100644 --- a/test/typings/union.ts +++ b/test/typings/union.ts @@ -1,5 +1,5 @@ import { assert, union, object, string, literal } from '../../src' -import { test } from '..' +import { test } from '../index.test' test<{ a: string } | { b: string }>((x) => { assert(x, union([object({ a: string() }), object({ b: string() })])) diff --git a/test/typings/unknown.ts b/test/typings/unknown.ts index e564795d..38e28b82 100644 --- a/test/typings/unknown.ts +++ b/test/typings/unknown.ts @@ -1,5 +1,5 @@ import { assert, unknown } from '../../src' -import { test } from '..' +import { test } from '../index.test' test((x) => { assert(x, unknown()) From 9ee70b4935aba13a98740101cb56aae446c5687e Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Fri, 23 Feb 2024 13:44:01 -0700 Subject: [PATCH 2/3] Add logging to split-tsd-files.sh --- scripts/split-tsd-files.sh | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/scripts/split-tsd-files.sh b/scripts/split-tsd-files.sh index 336a7b0d..f7c9d4cb 100755 --- a/scripts/split-tsd-files.sh +++ b/scripts/split-tsd-files.sh @@ -2,21 +2,28 @@ set -euo pipefail -for file in dist/*.d.ts dist/structs/*.d.ts; do - mv $file "${file%.d.ts}.d.cts" - cp "${file%.d.ts}.d.cts" "${file%.d.ts}.d.mts" +echo "Splitting *.d.ts files into *.d.cts and *.d.mts" +find dist -name '*.d.ts' -print0 | while IFS= read -r -d $'\0' file; do + echo " $file -> ${file%.d.ts}.d.cts" + cp "$file" "${file%.d.ts}.d.cts" + echo " $file -> ${file%.d.ts}.d.mts" + cp "$file" "${file%.d.ts}.d.mts" + rm "$file" done -for file in dist/*.d.cts dist/structs/*.d.cts; do - echo "File: $file" +echo "Processing *.d.cts files to replace instances of .d.ts with .d.cts and .js with .cjs" +find dist -name '*.d.cts' -print0 | while IFS= read -r -d $'\0' file; do + echo " $file" sed -e 's/\.d\.ts/.d.cts/' -i.bak "$file" sed -e 's/\.js/.cjs/' -i.bak "$file" done -for file in dist/*.d.mts dist/structs/*.d.mts; do - echo "File: $file" - sed -e 's/\.d\.ts/\.d\.mts/' -i.bak "$file" +echo "Processing *.d.mts files to replace instances of .d.ts with .d.mts and .js with .mjs" +find dist -name '*.d.mts' -print0 | while IFS= read -r -d $'\0' file; do + echo " $file" + sed -e 's/\.d\.ts/.d.mts/' -i.bak "$file" sed -e 's/\.js/.mjs/' -i.bak "$file" done -find dist -name '*.bak' | xargs rm +echo "Removing backup files" +find dist -name '*.bak' -print0 | xargs -0 rm From cbc26fb6e38529bfbb64fcd5435df697decfa790 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Fri, 23 Feb 2024 13:44:09 -0700 Subject: [PATCH 3/3] Remove logging --- scripts/split-tsd-files.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/split-tsd-files.sh b/scripts/split-tsd-files.sh index f7c9d4cb..a5306f8b 100755 --- a/scripts/split-tsd-files.sh +++ b/scripts/split-tsd-files.sh @@ -2,7 +2,6 @@ set -euo pipefail -echo "Splitting *.d.ts files into *.d.cts and *.d.mts" find dist -name '*.d.ts' -print0 | while IFS= read -r -d $'\0' file; do echo " $file -> ${file%.d.ts}.d.cts" cp "$file" "${file%.d.ts}.d.cts" @@ -11,19 +10,16 @@ find dist -name '*.d.ts' -print0 | while IFS= read -r -d $'\0' file; do rm "$file" done -echo "Processing *.d.cts files to replace instances of .d.ts with .d.cts and .js with .cjs" find dist -name '*.d.cts' -print0 | while IFS= read -r -d $'\0' file; do echo " $file" sed -e 's/\.d\.ts/.d.cts/' -i.bak "$file" sed -e 's/\.js/.cjs/' -i.bak "$file" done -echo "Processing *.d.mts files to replace instances of .d.ts with .d.mts and .js with .mjs" find dist -name '*.d.mts' -print0 | while IFS= read -r -d $'\0' file; do echo " $file" sed -e 's/\.d\.ts/.d.mts/' -i.bak "$file" sed -e 's/\.js/.mjs/' -i.bak "$file" done -echo "Removing backup files" find dist -name '*.bak' -print0 | xargs -0 rm