Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(context): Build and publish package as dual esm/cjs #11294

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/context/build.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import { build } from '@redwoodjs/framework-tools'
import { writeFileSync } from 'node:fs'

await build()
import { build, defaultBuildOptions } from '@redwoodjs/framework-tools'

// ESM build
await build({
buildOptions: {
...defaultBuildOptions,
format: 'esm',
},
})

// CJS build
await build({
buildOptions: {
...defaultBuildOptions,
outdir: 'dist/cjs',
},
})

// Place a package.json file with `type: commonjs` in the 'dist/cjs' folder so that
// all files are considered CommonJS modules.
writeFileSync('dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))
33 changes: 31 additions & 2 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,49 @@
"directory": "packages/context"
},
"license": "MIT",
"main": "./dist/index.js",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"default": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
},
"./dist/store": {
"import": {
"types": "./dist/store.d.ts",
"default": "./dist/store.js"
},
"default": {
"types": "./dist/cjs/store.d.ts",
"default": "./dist/cjs/store.js"
}
}
},
"main": "./dist/cjs/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsx ./build.mts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-context.tgz",
"build:types": "tsc --build --verbose",
"build:types": "tsc --build --verbose ./tsconfig.json ./tsconfig.cjs.json",
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"check:attw": "yarn attw -P",
"check:package": "concurrently npm:check:attw yarn:publint",
"prepublishOnly": "NODE_ENV=production yarn build"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.15.4",
"@redwoodjs/framework-tools": "workspace:*",
"concurrently": "8.2.2",
"publint": "0.2.10",
"tsx": "4.17.0",
"typescript": "5.5.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/context/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAsyncStoreInstance } from './store'
import { getAsyncStoreInstance } from './store.js'

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface GlobalContext extends Record<string, unknown> {}
Expand Down
2 changes: 1 addition & 1 deletion packages/context/src/global.api-auto-imports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GlobalContext } from './context'
import type { GlobalContext } from './context.js'

declare global {
const context: GlobalContext
Expand Down
6 changes: 3 additions & 3 deletions packages/context/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './context'
export * from './context.js'
// Note: store is not exported here to discourage direct usage.

import './global.api-auto-imports'
export * from './global.api-auto-imports'
import './global.api-auto-imports.js'
export * from './global.api-auto-imports.js'
2 changes: 1 addition & 1 deletion packages/context/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AsyncLocalStorage } from 'async_hooks'

import type { GlobalContext } from './context'
import type { GlobalContext } from './context.js'

let CONTEXT_STORAGE: AsyncLocalStorage<Map<string, GlobalContext>>

Expand Down
9 changes: 9 additions & 0 deletions packages/context/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/cjs",
"module": "CommonJS",
"moduleResolution": "Node10",
"tsBuildInfoFile": "./tsconfig.cjs.tsbuildinfo"
}
}
6 changes: 4 additions & 2 deletions packages/context/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"rootDir": "src",
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo",
"outDir": "dist"
"outDir": "dist",
"module": "Node16",
"moduleResolution": "Node16",
"tsBuildInfoFile": "./tsconfig.tsbuildinfo"
},
"include": ["src"]
}
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7983,7 +7983,10 @@ __metadata:
version: 0.0.0-use.local
resolution: "@redwoodjs/context@workspace:packages/context"
dependencies:
"@arethetypeswrong/cli": "npm:0.15.4"
"@redwoodjs/framework-tools": "workspace:*"
concurrently: "npm:8.2.2"
publint: "npm:0.2.10"
tsx: "npm:4.17.0"
typescript: "npm:5.5.4"
languageName: unknown
Expand Down
Loading