Skip to content

Commit

Permalink
ci: fix cjs+esm build outputs (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad authored Jul 25, 2023
1 parent a27f222 commit 132f479
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 44 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,6 @@ Otherwise, consider sending me an email ([email protected]) or [message me on Dis
### Current Sponsors 🥰
| [tmm](https://github.com/tmm) | [xrexy](https://github.com/xrexy) | [thomasballinger](https://github.com/thomasballinger) | [codeandcats](https://github.com/codeandcats) |
| ------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------- |
| <img height="64px" src="https://avatars.githubusercontent.com/u/6759464"> | <img height="64px" src="https://avatars.githubusercontent.com/u/71969236"> | <img height="64px" src="https://avatars.githubusercontent.com/u/458879"> | <img height="64px" src="https://avatars.githubusercontent.com/u/6035934"> |
| [tmm](https://github.com/tmm) | [xrexy](https://github.com/xrexy) | [thomasballinger](https://github.com/thomasballinger) | [codeandcats](https://github.com/codeandcats) | [jacksteamdev](https://github.com/jacksteamdev) |
| ------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| <img height="64px" src="https://avatars.githubusercontent.com/u/6759464"> | <img height="64px" src="https://avatars.githubusercontent.com/u/71969236"> | <img height="64px" src="https://avatars.githubusercontent.com/u/458879"> | <img height="64px" src="https://avatars.githubusercontent.com/u/6035934"> | <img height="64px" src="https://avatars.githubusercontent.com/u/23390212"> |
2 changes: 1 addition & 1 deletion dev/arktype.io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "docusaurus start",
"build": "node --loader ts-node/esm ../scripts/docgen.ts && pnpm docusaurus build --out-dir dist && ts-node ../scripts/copyCNAME.ts ",
"build": "node --loader ts-node/esm ../scripts/docgen.ts && pnpm docusaurus build --out-dir dist && node --loader ts-node/esm ../scripts/copyCNAME.ts ",
"typecheck": "tsc",
"serve": "docusaurus serve --dir dist"
},
Expand Down
10 changes: 7 additions & 3 deletions dev/attest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
"url": "https://arktype.io"
},
"type": "module",
"main": "./dist/main.js",
"types": "./dist/main.d.ts",
"main": "./dist/cjs/main.js",
"types": "./dist/types/main.d.ts",
"exports": {
".": "./dist/main.js"
".": {
"types": "./dist/types/main.d.ts",
"import": "./dist/mjs/main.js",
"require": "./dist/cjs/main.js"
}
},
"files": [
"dist",
Expand Down
5 changes: 5 additions & 0 deletions dev/configs/.changeset/six-trees-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"arktype": patch
---

ci: revert to dual cjs+esm build output
96 changes: 65 additions & 31 deletions dev/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,71 @@
import { cpSync, rmSync } from "node:fs"
import { renameSync, rmSync } from "node:fs"
import { join } from "node:path"
import { readJson, shell, writeJson } from "../attest/src/main.js"
import * as process from "node:process"
import {
getSourceFilePaths,
readJson,
shell,
writeJson
} from "../attest/src/main.js"
import { repoDirs } from "./common.js"

const packageRoot = process.cwd()
const outRoot = join(packageRoot, "dist")
const packageJson = readJson(join(packageRoot, "package.json"))

console.log(`🔨 Building ${packageJson.name}...`)
rmSync(outRoot, { recursive: true, force: true })
const tsConfigData = readJson(join(repoDirs.configs, "tsconfig.json"))
const tempTsConfig = join(packageRoot, "tsconfig.temp.json")
writeJson(tempTsConfig, {
...tsConfigData,
include: ["src"],
compilerOptions: {
...tsConfigData.compilerOptions,
noEmit: false,
module: "commonjs",
outDir: "dist"
const isTestBuild = process.argv.includes("--test")

const isProd = () => process.argv.includes("--prod") || !!process.env.CI

const inFiles = getSourceFilePaths(
isTestBuild ? repoDirs.root : repoDirs.srcRoot
)

const successMessage = `📦 Successfully built arktype!`

const arktypeTsc = () => {
console.log(`🔨 Building arktype...`)
rmSync(repoDirs.outRoot, { recursive: true, force: true })
if (!isTestBuild) {
buildTypes()
}
transpile()
console.log(successMessage)
}

const buildTypes = () => {
process.stdout.write("⏳ Building types...".padEnd(successMessage.length))
const tsConfigData = readJson(join(repoDirs.root, "tsconfig.json"))
const tempTsConfig = join(repoDirs.root, "tsconfig.temp.json")
try {
writeJson(tempTsConfig, { ...tsConfigData, include: ["src"] })
shell(
`pnpm tsc --project ${tempTsConfig} --outDir ${repoDirs.outRoot} --noEmit false --emitDeclarationOnly`
)
renameSync(join(repoDirs.outRoot, "src"), repoDirs.typesOut)
} finally {
rmSync(tempTsConfig, { force: true })
}
})
try {
shell(`pnpm tsc --project ${tempTsConfig}`)
const outSrc = join(outRoot, "src")
// not sure which setting to change to get it to compile here in the first place
cpSync(outSrc, outRoot, {
recursive: true,
force: true
process.stdout.write(`✅\n`)
}

const transpile = () => {
process.stdout.write(`⌛ Transpiling...`.padEnd(successMessage.length))
swc("mjs")
swc("cjs")
process.stdout.write("✅\n")
}

const swc = (kind: "mjs" | "cjs") => {
const kindOutDir = join(repoDirs.outRoot, kind)
let cmd = `pnpm swc -d ${kindOutDir} -C jsc.target=es2020 -q `
if (kind === "cjs") {
cmd += `-C module.type=commonjs `
}
if (!isProd()) {
cmd += `--source-maps inline `
}
cmd += inFiles.join(" ")
shell(cmd)
writeJson(join(kindOutDir, "package.json"), {
type: kind === "cjs" ? "commonjs" : "module"
})
rmSync(outSrc, { recursive: true, force: true })
writeJson(join(outRoot, "package.json"), { type: "commonjs" })
} finally {
rmSync(tempTsConfig, { force: true })
}
console.log(`📦 Successfully built ${packageJson.name}!`)

arktypeTsc()
8 changes: 7 additions & 1 deletion dev/scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const arktypeIo = join(dev, "arktype.io")
const docsDir = join(arktypeIo, "docs")
const srcRoot = join(root, "src")
const outRoot = join(root, "dist")
const typesOut = join(outRoot, "types")
const mjsOut = join(outRoot, "mjs")
const cjsOut = join(outRoot, "cjs")

export const repoDirs = {
root,
Expand All @@ -18,5 +21,8 @@ export const repoDirs = {
arktypeIo,
docsDir,
srcRoot,
outRoot
outRoot,
typesOut,
mjsOut,
cjsOut
}
2 changes: 1 addition & 1 deletion dev/scripts/docgen/api/extractApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const extractApi = (project: Project, packageRoot: string) => {
subpath: ".",
exports: extractExportsFromDts(
project.addSourceFileAtPath(
join(packageRoot, "dist", "main.d.ts")
join(packageRoot, "dist", "types", "main.d.ts")
)
)
}
Expand Down
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
"url": "https://github.com/arktypeio/arktype.git"
},
"type": "module",
"main": "./dist/main.js",
"types": "./dist/main.d.ts",
"main": "./dist/cjs/main.js",
"types": "./dist/types/main.d.ts",
"exports": {
".": "./dist/main.js",
"./internal/*": "./dist/*"
".": {
"types": "./dist/types/main.d.ts",
"import": "./dist/mjs/main.js",
"require": "./dist/cjs/main.js"
},
"./internal/*": {
"types": "./dist/types/*",
"import": "./dist/mjs/*",
"require": "./dist/cjs/*"
}
},
"files": [
"dist",
Expand Down

0 comments on commit 132f479

Please sign in to comment.