From 7aecc042bc8e7232112928dded67d947a8e2c790 Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Tue, 13 Feb 2024 10:42:22 +1100 Subject: [PATCH] cjs --- .gitignore | 1 + .scripts/preconstruct.ts | 7 +++++-- .scripts/prepublishOnly.ts | 11 +++++++++++ create-farc/tsconfig.build.json | 1 + package.json | 10 ++++++---- src/jsx/jsx-dev-runtime/package.json | 2 +- src/jsx/jsx-runtime/package.json | 2 +- src/package.json | 13 +++++++++---- src/utils/deserializeJson.ts | 2 +- src/utils/serializeJson.ts | 2 +- tsconfig.base.json | 6 ------ tsconfig.build.json | 9 ++++++--- tsconfig.json | 10 +++++++++- 13 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 .scripts/prepublishOnly.ts diff --git a/.gitignore b/.gitignore index d36353ff..3807a305 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.map coverage _lib +_cjs node_modules tsconfig.*.tsbuildinfo tsconfig.tsbuildinfo diff --git a/.scripts/preconstruct.ts b/.scripts/preconstruct.ts index 956e4439..77068924 100644 --- a/.scripts/preconstruct.ts +++ b/.scripts/preconstruct.ts @@ -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 @@ -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 [ diff --git a/.scripts/prepublishOnly.ts b/.scripts/prepublishOnly.ts new file mode 100644 index 00000000..333babc8 --- /dev/null +++ b/.scripts/prepublishOnly.ts @@ -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)) diff --git a/create-farc/tsconfig.build.json b/create-farc/tsconfig.build.json index 5279b9e9..33ac037a 100644 --- a/create-farc/tsconfig.build.json +++ b/create-farc/tsconfig.build.json @@ -10,6 +10,7 @@ "module": "ESNext", "moduleResolution": "Node", "outDir": "./_lib", + "rootDir": ".", "sourceMap": true } } diff --git a/package.json b/package.json index c1e9972f..c4539f32 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/jsx/jsx-dev-runtime/package.json b/src/jsx/jsx-dev-runtime/package.json index 0c6d3b52..877be901 100644 --- a/src/jsx/jsx-dev-runtime/package.json +++ b/src/jsx/jsx-dev-runtime/package.json @@ -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" } diff --git a/src/jsx/jsx-runtime/package.json b/src/jsx/jsx-runtime/package.json index ef21cfb8..ea2e09cb 100644 --- a/src/jsx/jsx-runtime/package.json +++ b/src/jsx/jsx-runtime/package.json @@ -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" } diff --git a/src/package.json b/src/package.json index a8fa62bb..50463856 100644 --- a/src/package.json +++ b/src/package.json @@ -2,6 +2,7 @@ "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", @@ -9,19 +10,23 @@ "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": { diff --git a/src/utils/deserializeJson.ts b/src/utils/deserializeJson.ts index ca00eec5..8d2233de 100644 --- a/src/utils/deserializeJson.ts +++ b/src/utils/deserializeJson.ts @@ -1,4 +1,4 @@ -import { default as lz } from 'lz-string' +import * as lz from 'lz-string' export function deserializeJson(data = ''): returnType { if (data === 'undefined') return {} as returnType diff --git a/src/utils/serializeJson.ts b/src/utils/serializeJson.ts index dc7bcdce..0c0bf2b9 100644 --- a/src/utils/serializeJson.ts +++ b/src/utils/serializeJson.ts @@ -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)) diff --git a/tsconfig.base.json b/tsconfig.base.json index 7e1a7dea..2695887c 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -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 diff --git a/tsconfig.build.json b/tsconfig.build.json index 2bd43d01..d255911c 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -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" } } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 4d7693fb..2479e72d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"] + }, + } }