Skip to content

Commit

Permalink
feat: ESM
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The package is now exported as ESM module.
  • Loading branch information
typeofweb committed Jun 9, 2021
1 parent 650a8e7 commit d88685b
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
jest-*.ts
docs
jest.config.js
rollup.config.js
dist
coverage
__tests__/bench.js
8 changes: 6 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": {
"es6": true
},
"parserOptions": {
"sourceType": "module",
"project": "tsconfig.eslint.json",
"ecmaVersion": 2017
"ecmaVersion": 2021
},
"plugins": ["functional", "@typescript-eslint"],
"plugins": ["functional", "@typescript-eslint", "unicorn"],
"extends": [
"prettier",
"plugin:import/errors",
Expand All @@ -25,6 +28,7 @@
"import/order": ["error", { "newlines-between": "always", "alphabetize": { "order": "asc" } }],
"import/no-duplicates": "error",
"import/no-cycle": "error",
"unicorn/prefer-module": "error",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/consistent-type-imports": "error",
Expand Down
25 changes: 14 additions & 11 deletions __tests__/exports.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
/* eslint-disable import/dynamic-import-chunkname */
import Fs from 'fs';
import { resolve, extname } from 'path';
import Path, { resolve, extname } from 'path';
import Url from 'url';

describe('check exports', () => {
describe('validators', () => {
const validatorsDir = Fs.readdirSync(resolve(__dirname, '..', 'src', 'validators'));
const validatorsDir = Fs.readdirSync(
resolve(Path.dirname(Url.fileURLToPath(import.meta.url)), '..', 'src', 'validators'),
);
const validators = validatorsDir
.filter((filename) => !filename.startsWith('__'))
.map((name) => (extname(name).length ? name.slice(0, -extname(name).length) : name));

validators.forEach((v) => {
it(`${v} should be exported from index.ts`, async () =>
expect(Object.keys(await import('../src/index'))).toContain(v));
});
it.each(validators)(`%s should be exported from index.ts`, async (v) =>
expect(Object.keys(await import('../src/index'))).toContain(v),
);
});

describe('modifiers', () => {
const modifiersDir = Fs.readdirSync(resolve(__dirname, '..', 'src', 'modifiers'));
const modifiersDir = Fs.readdirSync(
resolve(Path.dirname(Url.fileURLToPath(import.meta.url)), '..', 'src', 'modifiers'),
);
const modifiers = modifiersDir
.filter((filename) => !filename.startsWith('__'))
.map((name) => (extname(name).length ? name.slice(0, -extname(name).length) : name));

modifiers.forEach((v) => {
it(`${v} should be exported from index.ts`, async () =>
expect(Object.keys(await import('../src/index'))).toContain(v));
});
it.each(modifiers)(`%s should be exported from index.ts`, async (v) =>
expect(Object.keys(await import('../src/index'))).toContain(v),
);
});
});
10 changes: 7 additions & 3 deletions __tests__/types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { join } from 'path';
import Path, { join } from 'path';
import Url from 'url';

import { identity } from 'ramda';
import tsd from 'tsd';
import TsdModule from 'tsd';

// @ts-expect-error @todo
const tsd = (TsdModule as { readonly default: typeof TsdModule }).default;

describe('@typeofweb/schema', () => {
it('tsd', async () => {
const diagnostics = await tsd({
cwd: join(__dirname, '..'),
cwd: join(Path.dirname(Url.fileURLToPath(import.meta.url)), '..'),
typingsFile: './dist/index.d.ts',
testFiles: ['./__tests__/*.test-d.ts'],
});
Expand Down
1 change: 1 addition & 0 deletions __tests__/unit-tests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jest } from '@jest/globals';
import Qs from 'qs';

import type { SomeSchema } from '../src';
Expand Down
12 changes: 9 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
module.exports = {
export default {
roots: ['<rootDir>'],
setupFiles: ['<rootDir>/jest.setup.js'],
moduleFileExtensions: ['js', 'ts', 'json'],
testPathIgnorePatterns: ['<rootDir>[/\\\\](node_modules)[/\\\\]'],
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
// '^.+\\.tsx?$': 'ts-jest',
},
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
setupFiles: ['./jest-setup.ts'],
setupFilesAfterEnv: ['./jest-setup-after-env.ts'],
extensionsToTreatAsEsm: ['.ts'],
preset: 'ts-jest/presets/default-esm',
globals: {
'ts-jest': {
useESM: true,
},
},
};
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"name": "@typeofweb/schema",
"version": "0.8.0-2",
"main": "dist/index.common.js",
"module": "dist/index.esm.js",
"type": "module",
"exports": {
"import": "./dist/index.mjs",
"default": "./dist/index.mjs",
"require": "./dist/index.cjs",
"browser": "./dist/index.umd.js"
},
"main": "./dist/index.cjs",
"browser": "dist/index.umd.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
Expand All @@ -15,7 +21,7 @@
"author": "Michał Miszczyszyn - Type of Web <[email protected]> (https://typeofweb.com/)",
"license": "MIT",
"engines": {
"node": ">12.0.0"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"publishConfig": {
"access": "public"
Expand All @@ -41,6 +47,7 @@
"@tsconfig/node12": "1.0.7",
"@tsconfig/node14": "1.0.0",
"@types/jest": "26.0.23",
"@types/node": "12",
"@types/qs": "6.9.6",
"@types/ramda": "0.27.40",
"@typescript-eslint/eslint-plugin": "4.26.1",
Expand All @@ -50,6 +57,7 @@
"eslint-config-prettier": "8.3.0",
"eslint-plugin-functional": "3.2.1",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-unicorn": "33.0.1",
"fast-check": "2.16.0",
"husky": "6.0.0",
"jest": "27.0.4",
Expand All @@ -71,8 +79,8 @@
},
"scripts": {
"prejest": "yarn build",
"jest": "jest",
"test": "yarn jest --detectOpenHandles --forceExit --passWithNoTests --coverage",
"jest": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "NODE_OPTIONS=--experimental-vm-modules yarn jest --detectOpenHandles --forceExit --passWithNoTests --coverage",
"test:dev": "yarn jest --watch",
"build": "rimraf dist && rollup --config",
"build:watch": "rollup --config --watch",
Expand Down
8 changes: 3 additions & 5 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const rollupConfig = [
name: '@typeofweb/schema',
format: 'es',
dir: './',
entryFileNames: pkg.module,
entryFileNames: pkg.exports.import.replace(/^\.\//, ''),
sourcemap: true,
plugins: [
shouldCompress
Expand All @@ -37,7 +37,7 @@ const rollupConfig = [
name: '@typeofweb/schema',
format: 'cjs',
dir: './',
entryFileNames: pkg.main,
entryFileNames: pkg.exports.require.replace(/^\.\//, ''),
sourcemap: true,
plugins: [
shouldCompress
Expand All @@ -53,7 +53,7 @@ const rollupConfig = [
},
{
name: '@typeofweb/schema',
entryFileNames: pkg.browser,
entryFileNames: pkg.exports.browser.replace(/^\.\//, ''),
sourcemap: true,
format: 'umd',
dir: './',
Expand All @@ -76,8 +76,6 @@ const rollupConfig = [
declaration: true,
declarationDir: 'dist/',
rootDir: 'src/',
module: 'ES2020',
resolveJsonModule: false,
include: ['src/**/*.ts'],
}),
filesize({}),
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "@tsconfig/node12/tsconfig.json",
"compilerOptions": {
"module": "ES2020",
"moduleResolution": "node",
"isolatedModules": true,
"strict": true,
"alwaysStrict": true,
Expand Down
Loading

1 comment on commit d88685b

@vercel
Copy link

@vercel vercel bot commented on d88685b Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.