Skip to content

Commit

Permalink
ESM Dual Publish
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 30, 2023
1 parent 023208a commit 4daeadf
Show file tree
Hide file tree
Showing 25 changed files with 172 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"files.exclude": {
"node_modules": true,
"node_modules": false,
"package-lock.json": true
},
"editor.suggest.showStatusBar": false,
Expand Down
5 changes: 0 additions & 5 deletions benchmark/measurement/index.ts

This file was deleted.

41 changes: 40 additions & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
import { String } from 'typebox'
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})

type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------

const C = TypeCompiler.Compile(T)

console.log(C.Code())

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------

console.log(C.Check(V))
18 changes: 14 additions & 4 deletions hammer.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { compression, measurement } from './benchmark'
import { compression, measurement } from './task/benchmark'
import { convertToCommonJs } from './task/build'
import { readFileSync } from 'fs'

// -------------------------------------------------------------------------------
Expand All @@ -12,7 +13,7 @@ export async function clean() {
// Format
// -------------------------------------------------------------------------------
export async function format() {
await shell('prettier --no-semi --single-quote --print-width 240 --trailing-comma all --write src test examples/index.ts benchmark')
await shell('prettier --no-semi --single-quote --print-width 240 --trailing-comma all --write src test task examples/index.ts')
}
// -------------------------------------------------------------------------------
// Start
Expand Down Expand Up @@ -57,10 +58,18 @@ export async function test(filter = '') {
// -------------------------------------------------------------------------------
// Build
// -------------------------------------------------------------------------------
export async function build_esm(target = 'target/build/esm') {
await shell(`tsc -p ./src/tsconfig.json --outDir ${target} --target ESNext --module ESNext --declaration`)
}
export async function build_cjs(target = 'target/build/cjs') {
await shell(`tsc -p ./src/tsconfig.json --outDir ${target} -target ES2020 --module CommonJS`)
convertToCommonJs(target)
}
export async function build(target = 'target/build') {
await test()
await folder(target).delete()
await shell(`tsc -p ./src/tsconfig.json --outDir ${target}`)
// await test()
await build_cjs(`${target}/cjs`)
await build_esm(`${target}/esm`)
await folder(target).add('package.json')
await folder(target).add('readme.md')
await folder(target).add('license')
Expand All @@ -70,6 +79,7 @@ export async function build(target = 'target/build') {
// Install
// -------------------------------------------------------------------------------
export async function install_local(target = 'target/typebox') {
await clean()
await build(target)
await folder('node_modules').add(target)
}
Expand Down
4 changes: 0 additions & 4 deletions index.mjs

This file was deleted.

36 changes: 27 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 32 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.32.0-dev-1",
"version": "0.32.0-dev-2",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand All @@ -10,15 +10,34 @@
],
"author": "sinclairzx81",
"license": "MIT",
"module": "./index.mjs",
"types": "./index.d.mts",
"main": "./cjs/index.js",
"module": "./esm/index.mjs",
"types": "./esm/index.d.mts",
"exports": {
"./compiler": "./compiler/index.mjs",
"./errors": "./errors/index.mjs",
"./system": "./system/index.mjs",
"./type": "./type/index.mjs",
"./value": "./value/index.mjs",
".": "./index.mjs"
"./compiler": {
"require": "./cjs/compiler/index.js",
"import": "./esm/compiler/index.mjs"
},
"./errors": {
"require": "./cjs/errors/index.js",
"import": "./esm/errors/index.mjs"
},
"./system": {
"require": "./cjs/system/index.js",
"import": "./esm/system/index.mjs"
},
"./type": {
"require": "./cjs/type/index.js",
"import": "./esm/type/index.mjs"
},
"./value": {
"require": "./cjs/value/index.js",
"import": "./esm/value/index.mjs"
},
".": {
"require": "./cjs/index.js",
"import": "./esm/index.mjs"
}
},
"repository": {
"type": "git",
Expand All @@ -32,18 +51,20 @@
"test:typescript": "hammer task test_typescript",
"test:static": "hammer task test_static",
"test:runtime": "hammer task test_runtime",
"build:esm": "hammer task build_esm",
"build:cjs": "hammer task build_cjs",
"build": "hammer task build",
"test": "hammer task test",
"clean": "hammer task clean",
"format": "hammer task format",
"start": "hammer task start",
"build": "hammer task build",
"publish": "hammer task publish",
"publish:dev": "hammer task publish_dev"
},
"devDependencies": {
"@sinclair/hammer": "^0.18.0",
"@types/mocha": "^9.1.1",
"@types/node": "^18.11.9",
"@types/node": "^20.10.1",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"mocha": "^9.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { statSync, readdirSync } from 'fs'
import { basename, extname } from 'path'

export async function measure(test: string) {
await shell(`hammer build benchmark/compression/module/${test}.ts --dist target/benchmark/compression`)
await shell(`hammer build task/benchmark/compression/module/${test}.ts --dist target/benchmark/compression`)
const compiled = statSync(`target/benchmark/compression/${test}.js`)
await shell(`hammer build benchmark/compression/module/${test}.ts --dist target/benchmark/compression --minify`)
await shell(`hammer build task/benchmark/compression/module/${test}.ts --dist target/benchmark/compression --minify`)
const minified = statSync(`target/benchmark/compression/${test}.js`)
return {
test: test.padEnd(20),
Expand All @@ -16,7 +16,7 @@ export async function measure(test: string) {
}

export async function compression() {
const tests = readdirSync('benchmark/compression/module').map((name) => basename(name, extname(name)))
const tests = readdirSync('task/benchmark/compression/module').map((name) => basename(name, extname(name)))
const results = await Promise.all(tests.map((test) => measure(test)))
const present = results.reduce((acc, c) => {
return { ...acc, [c.test.replace(/-/g, '/')]: { Compiled: c.compiled, Minified: c.minified, Compression: `${c.ratio.toFixed(2)} x` } }
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions task/benchmark/measurement/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { shell } from '@sinclair/hammer'

export async function measurement() {
await shell(`hammer run task/benchmark/measurement/module/index.ts --dist target/benchmark/measurement`)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
49 changes: 49 additions & 0 deletions task/build/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// ------------------------------------------------------------------
// CJS-Transform
// ------------------------------------------------------------------
import * as Path from 'node:path'
import * as Fs from 'node:fs'

// prettier-ignore
function shouldProcess(sourcePath: string) {
const extname = Path.extname(sourcePath)
return ['.mjs'].includes(extname)
}
// prettier-ignore
function rewriteSpecifiers(contents: string): string {
return contents.replaceAll('.mjs', '')
}
// prettier-ignore
function newExtension(extname: string) {
return extname === '.mjs' ? '.js' : extname
}
// prettier-ignore
function processFile(sourcePath: string) {
if(!shouldProcess(sourcePath)) return
const extname = Path.extname(sourcePath)
const dirname = Path.dirname(sourcePath)
const basename = Path.basename(sourcePath, extname)
const new_extname = newExtension(extname)
const sourceContent = Fs.readFileSync(sourcePath, 'utf-8')
const targetContent = rewriteSpecifiers(sourceContent)
const targetPath = `${Path.join(dirname, basename)}${new_extname}`
Fs.writeFileSync(sourcePath, targetContent)
Fs.renameSync(sourcePath, targetPath)
}
// prettier-ignore
function processSourcePath(sourcePath: string) {
const stat = Fs.statSync(sourcePath)
if(stat.isDirectory()) return readDirectory(sourcePath)
if(stat.isFile()) return processFile(sourcePath)
}
// prettier-ignore
function readDirectory(sourceDirectory: string) {
for(const entry of Fs.readdirSync(sourceDirectory)) {
const sourcePath = Path.join(sourceDirectory, entry)
processSourcePath(sourcePath)
}
}
// prettier-ignore
export function convertToCommonJs(sourceDirectory: string) {
readDirectory(sourceDirectory)
}
1 change: 1 addition & 0 deletions task/build/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build'
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"declaration": true,
"baseUrl": ".",
"paths": {
"@sinclair/typebox/compiler": ["src/compiler/index.mts"],
Expand Down

0 comments on commit 4daeadf

Please sign in to comment.