Skip to content

Commit

Permalink
cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 12, 2024
1 parent c37011e commit 7aecc04
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.map
coverage
_lib
_cjs
node_modules
tsconfig.*.tsbuildinfo
tsconfig.tsbuildinfo
Expand Down
7 changes: 5 additions & 2 deletions .scripts/preconstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ console.log('Setting up packages for development.')

// Get all package.json files
const packagePaths = await glob('**/package.json', {
ignore: ['**/dist/**', '**/node_modules/**'],
ignore: ['**/_cjs/**', '**/_lib/**', '**/node_modules/**'],
})

let count = 0
Expand Down Expand Up @@ -62,7 +62,10 @@ for (const packagePath of packagePaths) {
['default', exports],
['types', exports.replace('.js', '.d.ts')],
]
else entries = Object.entries(exports)
else
entries = Object.entries(exports).filter(
([type]) => type === 'import' || type === 'types',
)

// Link exports to dist locations
for (const [, value] of entries as [
Expand Down
11 changes: 11 additions & 0 deletions .scripts/prepublishOnly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { join } from 'node:path'

const packageJsonPath = join(import.meta.dir, '../src/package.json')
const packageJson = await Bun.file(packageJsonPath).json()

// NOTE: We explicitly don't want to publish the type field.
// We create a separate package.json for `dist/cjs` and `dist/esm` that has the type field.
// biome-ignore lint/performance/noDelete:
delete packageJson.type

Bun.write(packageJsonPath, JSON.stringify(packageJson, null, 2))
1 change: 1 addition & 0 deletions create-farc/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"module": "ESNext",
"moduleResolution": "Node",
"outDir": "./_lib",
"rootDir": ".",
"sourceMap": true
}
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"workspaces": ["create-farc", "examples/*", "src"],
"scripts": {
"dev": "bun run --hot ./examples/_dev/src/index.tsx",
"build": "bun run clean && bun run build:farc && bun run build:create-farc",
"build:farc": "tsc --project ./tsconfig.build.json",
"build:create-farc": "rimraf create-farc/_lib && tsc -p create-farc/tsconfig.build.json",
"build": "bun run clean && bun run build:cjs && bun run build:esm && bun run build:create-farc",
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --moduleResolution node --outDir ./src/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./src/_cjs/package.json",
"build:esm": "tsc --project ./tsconfig.build.json --declaration --declarationMap --outDir ./src/_lib && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./src/_lib/package.json",
"build:create-farc": "rimraf create-farc/_lib && tsc -p ./create-farc/tsconfig.build.json",
"changeset": "changeset",
"changeset:release": "bun run build && changeset publish",
"changeset:version": "changeset version && bun install --lockfile-only",
"clean": "rimraf src/_lib src/tsconfig.build.tsbuildinfo",
"clean": "rimraf src/_lib src/_cjs src/tsconfig.build.tsbuildinfo tsconfig.build.tsbuildinfo",
"prepublishOnly": "bun .scripts/prepublishOnly.ts",
"format": "biome format . --write",
"lint": "biome check . --apply",
"preconstruct": "bun run .scripts/preconstruct.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/jsx/jsx-dev-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"type": "module",
"types": "../../_lib/jsx/jsx-dev-runtime/index.d.ts",
"module": "../../_lib/jsx/jsx-dev-runtime/index.js",
"main": "../../_lib/jsx/jsx-dev-runtime/index.js"
"main": "../../_cjs/jsx/jsx-dev-runtime/index.js"
}
2 changes: 1 addition & 1 deletion src/jsx/jsx-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"type": "module",
"types": "../../_lib/jsx/jsx-runtime/index.d.ts",
"module": "../../_lib/jsx/jsx-runtime/index.js",
"main": "../../_lib/jsx/jsx-runtime/index.js"
"main": "../../_cjs/jsx/jsx-runtime/index.js"
}
13 changes: 9 additions & 4 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
"name": "farc",
"version": "0.0.1",
"type": "module",
"main": "_cjs/index.js",
"module": "_lib/index.js",
"types": "_lib/index.d.ts",
"typings": "_lib/index.d.ts",
"sideEffects": false,
"exports": {
".": {
"types": "./_lib/index.d.ts",
"default": "./_lib/index.js"
"import": "./_lib/index.js",
"default": "./_cjs/index.js"
},
"./jsx": {
"types": "./_lib/jsx/index.d.ts",
"default": "./_lib/jsx/index.js"
"import": "./_lib/jsx/index.js",
"default": "./_cjs/jsx/index.js"
},
"./jsx/jsx-runtime": {
"types": "./_lib/jsx/jsx-runtime/index.d.ts",
"default": "./_lib/jsx/jsx-runtime/index.js"
"import": "./_lib/jsx/jsx-runtime/index.js",
"default": "./_cjs/jsx/jsx-runtime/index.js"
},
"./jsx/jsx-dev-runtime": {
"types": "./_lib/jsx/jsx-dev-runtime/index.d.ts",
"default": "./_lib/jsx/jsx-dev-runtime/index.js"
"import": "./_lib/jsx/jsx-dev-runtime/index.js",
"default": "./_cjs/jsx/jsx-dev-runtime/index.js"
}
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/deserializeJson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { default as lz } from 'lz-string'
import * as lz from 'lz-string'

export function deserializeJson<returnType>(data = ''): returnType {
if (data === 'undefined') return {} as returnType
Expand Down
2 changes: 1 addition & 1 deletion src/utils/serializeJson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { default as lz } from 'lz-string'
import * as lz from 'lz-string'

export function serializeJson(data: unknown = {}) {
return lz.compressToEncodedURIComponent(JSON.stringify(data))
Expand Down
6 changes: 0 additions & 6 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@
"module": "NodeNext",
"target": "ES2021", // Setting this to `ES2021` enables native support for `Node v16+`: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping.
"lib": ["ES2022", "DOM"],
"jsx": "react-jsx",
"jsxImportSource": "farc/jsx",
"types": ["@types/bun", "typed-htmx"],
"paths": {
"farc/jsx/jsx-runtime": ["./src/jsx/jsx-runtime/index.ts"],
"farc/jsx/jsx-dev-runtime": ["./src/jsx/jsx-dev-runtime/index.ts"]
},

// Skip type checking for node modules
"skipLibCheck": true
Expand Down
9 changes: 6 additions & 3 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
"src/**/*.test-d.tsx"
],
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"jsx": "react-jsx",
"jsxImportSource": "farc/jsx",
"paths": {
"farc/jsx/jsx-runtime": ["./src/jsx/jsx-runtime/index.ts"],
"farc/jsx/jsx-dev-runtime": ["./src/jsx/jsx-dev-runtime/index.ts"]
},
"sourceMap": true,
"outDir": "./src/_lib",
"rootDir": "./src"
}
}
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
// This configuration is used for local development and type checking.
"extends": "./tsconfig.base.json",
"include": ["src"]
"include": ["src"],
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "farc/jsx",
"paths": {
"farc/jsx/jsx-runtime": ["./src/jsx/jsx-runtime/index.ts"],
"farc/jsx/jsx-dev-runtime": ["./src/jsx/jsx-dev-runtime/index.ts"]
},
}
}

0 comments on commit 7aecc04

Please sign in to comment.