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

refactor(api): Add conditional exports to package.json #11307

Merged
merged 10 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions .changesets/11307.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- refactor(api): Add conditional exports to package.json (#11307) by @Josh-Walker-GM

This change restricts the available imports from this package. You can no longer freely import from within the dist like `@redwoodjs/api/dist/...`. If you were doing so please consult the `@redwoodjs/api` `package.json` file to see what exports are now available.
5 changes: 2 additions & 3 deletions packages/api-server/src/plugins/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
FastifyRequest,
} from 'fastify'
import fastifyRawBody from 'fastify-raw-body'
import type { Plugin } from 'graphql-yoga'

import type { GlobalContext } from '@redwoodjs/context'
import { getAsyncStoreInstance } from '@redwoodjs/context/dist/store'
Expand Down Expand Up @@ -68,8 +67,8 @@ export async function redwoodFastifyGraphQLServer(
if (graphqlOptions?.realtime) {
const { useRedwoodRealtime } = await import('@redwoodjs/realtime')

const originalExtraPlugins: Plugin<any>[] =
graphqlOptions.extraPlugins ?? []
const originalExtraPlugins = graphqlOptions.extraPlugins ?? []
// @ts-expect-error TODO(jgmw): Fix this type issue introduced after switching to Node16 module resolution
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to self

originalExtraPlugins.push(useRedwoodRealtime(graphqlOptions.realtime))
graphqlOptions.extraPlugins = originalExtraPlugins

Expand Down
2 changes: 0 additions & 2 deletions packages/api/cache/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/api/cache/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions packages/api/logger/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/api/logger/package.json

This file was deleted.

27 changes: 21 additions & 6 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
},
"license": "MIT",
"type": "commonjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json",
"./cache": {
"types": "./dist/cache/index.d.ts",
"default": "./dist/cache/index.js"
},
"./logger": {
"types": "./dist/logger/index.d.ts",
"default": "./dist/logger/index.js"
},
"./webhooks": {
"types": "./dist/webhooks/index.d.ts",
"default": "./dist/webhooks/index.js"
}
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
Expand All @@ -17,17 +36,14 @@
"tsc": "./dist/bins/tsc.js"
},
"files": [
"dist",
"cache",
"logger",
"webhooks"
"dist"
],
"scripts": {
"build": "tsx ./build.mts && yarn build:types",
"build:pack": "yarn pack -o redwoodjs-api.tgz",
"build:types": "tsc --build --verbose ./tsconfig.json",
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"check:attw": "yarn attw -P",
"check:attw": "yarn rw-fwtools-attw $(pwd)",
"check:package": "concurrently npm:check:attw yarn:publint",
"prepublishOnly": "NODE_ENV=production yarn build",
"test": "vitest run",
Expand All @@ -44,7 +60,6 @@
"title-case": "3.0.3"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.15.4",
"@redwoodjs/framework-tools": "workspace:*",
"@types/aws-lambda": "8.10.143",
"@types/jsonwebtoken": "9.0.6",
Expand Down
2 changes: 0 additions & 2 deletions packages/api/webhooks/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/api/webhooks/package.json

This file was deleted.

7 changes: 6 additions & 1 deletion packages/framework-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
}
},
"types": "./dist/buildDefaults.d.ts",
"bin": {
"rw-fwtools-attw": "./dist/bins/rw-fwtools-attw.js"
},
"files": [
"dist"
],
Expand All @@ -28,9 +31,11 @@
"build:types": "tsc --build --verbose"
},
"dependencies": {
"@arethetypeswrong/cli": "0.15.4",
"esbuild": "0.23.0",
"fast-glob": "3.3.2",
"fs-extra": "11.2.0"
"fs-extra": "11.2.0",
"zx": "8.1.4"
},
"devDependencies": {
"@types/fs-extra": "11.0.4",
Expand Down
48 changes: 48 additions & 0 deletions packages/framework-tools/src/attw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createRequire } from 'node:module'
import path from 'node:path'

import { $ } from 'zx'

interface Problem {
kind: string
entrypoint?: string
resolutionKind?: string
}

interface Options {
cwd: string
}

export async function attw({ cwd }: Options): Promise<Problem[]> {
// We can't rely on directly running the attw binary because it's not
// a direct dependency of the package that will ultimately use this.
// Instead, we have to do a little work to find the attw binary and run it.
const require = createRequire(import.meta.url)
const pathToAttw = require.resolve('@arethetypeswrong/cli/package.json')
const attwPackageJson = require(pathToAttw)
const relativeBinPath = attwPackageJson.bin.attw
const attwBinPath = path.resolve(path.dirname(pathToAttw), relativeBinPath)

// Run attw via it's CLI
await $({
nothrow: true,
cwd,
})`yarn node ${attwBinPath} -P -f json > .attw.json`

// Read the resulting JSON file
const output = await $`cat .attw.json`
await $`rm .attw.json`
const json = JSON.parse(output.stdout)

// If no errors were found then return early
if (!json.analysis.problems || json.analysis.problems.length === 0) {
return []
}

// We don't care about node10 errors
const problems: Problem[] = json.analysis.problems.filter(
(problem: Problem) => problem.resolutionKind !== 'node10',
)

return problems
}
25 changes: 25 additions & 0 deletions packages/framework-tools/src/bins/rw-fwtools-attw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { attw } from '../attw.js'

async function main() {
const cwd = process.argv[2]
if (!cwd) {
console.error('Usage: rw-fwtools-attw <path-to-package>')
process.exit(1)
}

console.log(`Running attw against: ${cwd}`)

const problems = await attw({ cwd })
if (problems.length > 0) {
console.error('Problems found:')
for (const problem of problems) {
console.error(problem)
}
process.exit(1)
}

console.log('No problems found')
process.exit(0)
}

main()
4 changes: 3 additions & 1 deletion packages/graphql-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
"outDir": "dist",
"module": "Node16",
"moduleResolution": "Node16"
},
"include": ["ambient.d.ts", "src/**/*"],
"references": [{ "path": "../api" }, { "path": "../context" }]
Expand Down
4 changes: 3 additions & 1 deletion packages/mailer/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": "../../../tsconfig.compilerOption.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
"outDir": "dist",
"module": "Node16",
"moduleResolution": "Node16"
},
"include": ["src"]
}
4 changes: 3 additions & 1 deletion packages/testing/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": "../../tsconfig.compilerOption.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
"outDir": "dist",
"module": "Node16",
"moduleResolution": "Node16"
},
"include": ["src"],
"references": [
Expand Down
5 changes: 4 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7215,7 +7215,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@redwoodjs/api@workspace:packages/api"
dependencies:
"@arethetypeswrong/cli": "npm:0.15.4"
"@prisma/client": "npm:5.18.0"
"@redwoodjs/framework-tools": "workspace:*"
"@types/aws-lambda": "npm:8.10.143"
Expand Down Expand Up @@ -8161,12 +8160,16 @@ __metadata:
version: 0.0.0-use.local
resolution: "@redwoodjs/framework-tools@workspace:packages/framework-tools"
dependencies:
"@arethetypeswrong/cli": "npm:0.15.4"
"@types/fs-extra": "npm:11.0.4"
esbuild: "npm:0.23.0"
fast-glob: "npm:3.3.2"
fs-extra: "npm:11.2.0"
tsx: "npm:4.17.0"
typescript: "npm:5.5.4"
zx: "npm:8.1.4"
bin:
rw-fwtools-attw: ./dist/bins/rw-fwtools-attw.js
languageName: unknown
linkType: soft

Expand Down
Loading