Skip to content

Commit

Permalink
Build separate declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
makuko committed Jun 26, 2023
1 parent fc84088 commit 1467275
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ dist.new
.cache
.turbo
.rollup.cache
dist-dts
dist-dcts
dist-dmts
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"main": "./index.cjs",
"module": "./index.mjs",
"types": "./index.d.ts",
"types": "./index.d.cts",
"sideEffects": false,
"publishConfig": {
"provenance": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { entries, external } from './rollup.common';
export default defineConfig([
{
input: entries.reduce<Record<string, string>>((acc, entry) => {
const from = 'dist-dts/' + entry + '.d.ts';
const from = 'dist-dcts/' + entry + '.d.ts';
const to = entry;
acc[to] = from;
return acc;
}, {}),
output: {
dir: 'dist.new',
entryFileNames: '[name].d.cts',
},
external,
plugins: [
Expand Down
26 changes: 26 additions & 0 deletions drizzle-orm/rollup.dmts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json from '@rollup/plugin-json';
import { defineConfig } from 'rollup';
import dts from 'rollup-plugin-dts';
import { entries, external } from './rollup.common';

export default defineConfig([
{
input: entries.reduce<Record<string, string>>((acc, entry) => {
const from = 'dist-dmts/' + entry + '.d.ts';
const to = entry;
acc[to] = from;
return acc;
}, {}),
output: {
dir: 'dist.new',
entryFileNames: '[name].d.mts',
},
external,
plugins: [
json({
preferConst: true,
}),
dts(),
],
},
]);
38 changes: 27 additions & 11 deletions drizzle-orm/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ import 'zx/globals';
import concurrently from 'concurrently';
import { entries } from '../rollup.common';

interface Export {
types: string;
default: string;
}

function updateAndCopyPackageJson() {
const pkg = fs.readJSONSync('package.json');

pkg.exports = entries.reduce<Record<string, { import: string; require: string; default: string; types: string }>>(
pkg.exports = entries.reduce<Record<string, { import: Export; require: Export; default: Export; }>>(
(acc, entry) => {
const exportsEntry = entry === 'index' ? '.' : './' + entry.replace(/\/index$/, '');
const importEntry = `./${entry}.mjs`;
const requireEntry = `./${entry}.cjs`;
const typesEntry = `./${entry}.d.ts`;
const importTypesEntry = `./${entry}.d.mts`;
const requireTypesEntry = `./${entry}.d.cts`;
acc[exportsEntry] = {
types: typesEntry,
import: importEntry,
require: requireEntry,
default: importEntry,
import: {
types: importTypesEntry,
default: importEntry,
},
require: {
types: requireTypesEntry,
default: requireEntry,
},
default: {
types: importTypesEntry,
default: importEntry,
},
};
return acc;
},
Expand All @@ -36,15 +50,17 @@ await concurrently([
name: 'esm',
},
{
command: `tsc -p tsconfig.esm.json --declaration --outDir dist-dts --emitDeclarationOnly &&
resolve-tspaths --out dist-dts &&
rollup --config rollup.dts.config.ts --configPlugin typescript`,
name: 'dts',
command: `tsc -p tsconfig.cjs.json --declaration --outDir dist-dcts --emitDeclarationOnly && resolve-tspaths --out dist-dcts && rollup --config rollup.dcts.config.ts --configPlugin typescript`,
name: 'dcts',
},
{
command: `tsc -p tsconfig.esm.json --declaration --outDir dist-dmts --emitDeclarationOnly && resolve-tspaths --out dist-dmts && rollup --config rollup.dmts.config.ts --configPlugin typescript`,
name: 'dmts',
},
], {
killOthers: 'failure',
}).result.catch(() => process.exit(1));
fs.copySync('../README.md', 'dist.new/README.md');
updateAndCopyPackageJson();
fs.removeSync('dist');
fs.renameSync('dist.new', 'dist');
fs.renameSync('dist.new', 'dist');
24 changes: 17 additions & 7 deletions drizzle-zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Generate Zod schemas from Drizzle ORM schemas",
"type": "module",
"scripts": {
"build": "rimraf dist && rollup --config rollup.config.ts --configPlugin typescript && resolve-tspaths && cp README.md package.json dist/",
"build": "scripts/build.ts",
"b": "pnpm build",
"test:types": "cd tests && tsc",
"pack": "(cd dist && npm pack --pack-destination ..) && rm -f package.tgz && mv *.tgz package.tgz",
Expand All @@ -13,15 +13,23 @@
},
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.mjs",
"require": "./index.cjs",
"default": "./index.mjs"
"import": {
"types": "./index.d.mts",
"default": "./index.mjs"
},
"require": {
"types": "./index.d.cts",
"default": "./index.cjs"
},
"default": {
"types": "./index.d.mts",
"default": "./index.mjs"
}
}
},
"main": "./index.cjs",
"module": "./index.mjs",
"types": "./index.d.ts",
"types": "./index.d.cts",
"publishConfig": {
"provenance": true
},
Expand Down Expand Up @@ -72,7 +80,9 @@
"drizzle-orm": "link:../drizzle-orm/dist",
"rimraf": "^5.0.0",
"rollup": "^3.20.7",
"rollup-plugin-dts": "^5.3.0",
"tsx": "^3.12.2",
"zod": "^3.20.2"
"zod": "^3.20.2",
"zx": "^7.2.2"
}
}
26 changes: 26 additions & 0 deletions drizzle-zod/rollup.cjs.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { defineConfig } from 'rollup';

export default defineConfig([
{
input: 'src/index.ts',
output: {
format: 'cjs',
dir: 'dist.new',
entryFileNames: '[name].cjs',
chunkFileNames: '[name]-[hash].cjs',
sourcemap: true,
},
external: [
/^drizzle-orm\/?/,
'zod',
],
plugins: [
typescript({
tsconfig: 'tsconfig.cjs.json',
}),
terser(),
],
},
]);
35 changes: 0 additions & 35 deletions drizzle-zod/rollup.config.ts

This file was deleted.

19 changes: 19 additions & 0 deletions drizzle-zod/rollup.dcts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from 'rollup';
import dts from 'rollup-plugin-dts';

export default defineConfig([
{
input: 'src/index.ts',
output: {
dir: 'dist.new',
entryFileNames: '[name].d.cts',
},
external: [
/^drizzle-orm\/?/,
'zod',
],
plugins: [
dts(),
],
},
]);
19 changes: 19 additions & 0 deletions drizzle-zod/rollup.dmts.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from 'rollup';
import dts from 'rollup-plugin-dts';

export default defineConfig([
{
input: 'src/index.ts',
output: {
dir: 'dist.new',
entryFileNames: '[name].d.mts',
},
external: [
/^drizzle-orm\/?/,
'zod',
],
plugins: [
dts(),
],
},
]);
26 changes: 26 additions & 0 deletions drizzle-zod/rollup.esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { defineConfig } from 'rollup';

export default defineConfig([
{
input: 'src/index.ts',
output: {
format: 'esm',
dir: 'dist.new',
entryFileNames: '[name].mjs',
chunkFileNames: '[name]-[hash].mjs',
sourcemap: true,
},
external: [
/^drizzle-orm\/?/,
'zod',
],
plugins: [
typescript({
tsconfig: 'tsconfig.esm.json',
}),
terser(),
],
},
]);
27 changes: 27 additions & 0 deletions drizzle-zod/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env -S pnpm tsx
import 'zx/globals';
import concurrently from 'concurrently';

await concurrently([
{
command: 'rollup --config rollup.cjs.config.ts --configPlugin typescript',
name: 'cjs',
},
{
command: 'rollup --config rollup.esm.config.ts --configPlugin typescript',
name: 'esm',
},
{
command: `tsc -p tsconfig.cjs.json --declaration --outDir dist-dcts --emitDeclarationOnly && resolve-tspaths --out dist-dcts && rollup --config rollup.dcts.config.ts --configPlugin typescript`,
name: 'dcts',
},
{
command: `tsc -p tsconfig.esm.json --declaration --outDir dist-dmts --emitDeclarationOnly && resolve-tspaths --out dist-dmts && rollup --config rollup.dmts.config.ts --configPlugin typescript`,
name: 'dmts',
},
], {
killOthers: 'failure',
}).result.catch(() => process.exit(1));
fs.copySync('../README.md', 'dist.new/README.md');
fs.removeSync('dist');
fs.renameSync('dist.new', 'dist');
File renamed without changes.
8 changes: 8 additions & 0 deletions drizzle-zod/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"incremental": true
},
"include": ["src"]
}
2 changes: 0 additions & 2 deletions drizzle-zod/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"baseUrl": ".",
"declaration": true,
"paths": {
"~/*": ["src/*"]
}
Expand Down
Loading

0 comments on commit 1467275

Please sign in to comment.