Explorar el Código

fix(lazer): fix package build

Connor Prussin hace 1 año
padre
commit
4c907a37e9

+ 0 - 26
.github/workflows/ci-lazer-sdk-js.yml

@@ -1,26 +0,0 @@
-name: "Lazer JS SDK Test Suite"
-on:
-  push:
-    branches:
-      - main
-    paths:
-      - lazer/sdk/js/**
-  pull_request:
-    paths:
-      - lazer/sdk/js/**
-
-jobs:
-  lazer-js-sdk-test-suite:
-    name: Lazer JS SDK Test Suite
-    runs-on: ubuntu-22.04
-    defaults:
-      run:
-        working-directory: lazer/sdk/js
-    steps:
-      - uses: actions/checkout@v4
-      - uses: actions/setup-node@v4
-        with:
-          node-version: 20.18.0
-      - run: npm install --global pnpm@9.13.2
-      - run: pnpm install --frozen-lockfile
-      - run: pnpm run test

+ 46 - 0
README.md

@@ -112,6 +112,52 @@ after `--`, for instance you could run `pnpm test -- --concurrency 2`.
 - `pnpm start:prod`: Run production builds and start production mode servers in
   parallel.
 
+#### Building a new package
+
+New packages should be configured with a few requirements in mind:
+
+1. You need to make sure `package.json` scripts are named such that that
+   turborepo will hook into them at the expected times.
+
+   - See [turbo.json](./turbo.json) to see the base configuration and ensure you
+     use script names that match the tasks that turborepo is configured to run.
+   - You don't need to define scripts for all the tasks that are in the root
+     `turbo.json` config, just define the ones that make sense for your project.
+   - You shouldn't define scripts for tasks that just trigger other tasks;
+     instead configure turborepo dependencies to trigger the tasks. This will
+     allow turborepo to cache and parallelize things more effectively.
+   - In general, `build`, `fix`, and `test` are the main turborepo task graph
+     entry points, and any sub-tasks that apply to your package should be
+     configured as dependencies somewhere in the graph rooted at one of those.
+   - If you need sub-tasks or a task configuration that only apply to your or a
+     small few packages, add a package-scoped `turbo.json`, see [the one in the
+     insights app](./apps/insights/turbo.json) as an example.
+
+2. Make sure to configure the tools you need and hook up the relevant checks to
+   turborepo via the `test` root task so they run on CI. The most common tools
+   (eslint, prettier, jest, and typescript) are trivial to configure correctly
+   using shared configs, see the relevant config files [in the insights
+   app](./apps/insights) as a starting point.
+
+3. If you are writing a package that will be published:
+
+   - Make sure you are dual-exporting cjs and esm correctly, see [how the lazer
+     sdk package builds](./lazer/sdk/js/package.json) (in particular look at the
+     `build:cjs` and `build:esm` tasks) for an example for how to do this
+
+   - Ensure you have properly configured [subpath
+     exports](https://nodejs.org/api/packages.html#subpath-exports) to reference
+     the esm and cjs outputs so that your package can be consumed correctly in
+     both environments. Again, see [the lazer sdk](./lazer/sdk/js/package.json)
+     as an example for doing this correctly.
+
+   - Ensure you have set a `main` and `types` property on your `package.json`
+     pointing to your cjs entrypoint for use in older javascript environments.
+
+   - Ensure you configure the `files` property on your `package.json` to include
+     all output files and to exclude source files & tooling configuration. This
+     will result in smaller package sizes.
+
 ## Audit / Feature Status
 
 ⚠ **This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or

+ 19 - 7
lazer/sdk/js/package.json

@@ -1,21 +1,33 @@
 {
   "name": "@pythnetwork/pyth-lazer-sdk",
-  "version": "0.1.0",
+  "version": "0.1.1",
   "description": "Pyth Lazer SDK",
   "publishConfig": {
     "access": "public"
   },
   "files": [
-    "lib/**/*"
+    "dist/**/*"
   ],
+  "main": "./dist/cjs/index.js",
+  "types": "./dist/cjs/index.d.ts",
+  "exports": {
+    "import": {
+      "types": "./dist/esm/index.d.ts",
+      "default": "./dist/esm/index.js"
+    },
+    "require": {
+      "types": "./dist/cjs/index.d.ts",
+      "default": "./dist/cjs/index.js"
+    }
+  },
   "scripts": {
-    "build:cjs": "tsc --project tsconfig.json --verbatimModuleSyntax false --module commonjs --outDir ./dist/cjs && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
-    "build:esm": "tsc --project tsconfig.json --outDir ./dist/esm && echo '{\"type\":\"module\"}' > dist/esm/package.json",
+    "build:cjs": "tsc --project tsconfig.build.json --verbatimModuleSyntax false --module commonjs --outDir ./dist/cjs && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
+    "build:esm": "tsc --project tsconfig.build.json --outDir ./dist/esm && echo '{\"type\":\"module\"}' > dist/esm/package.json",
+    "fix:lint": "eslint --fix .",
+    "test:lint": "eslint .",
+    "test:types": "tsc",
     "example": "node --loader ts-node/esm examples/index.js",
-    "test": "pnpm run test:lint && pnpm run build:cjs && pnpm run build:esm",
     "doc": "typedoc --out docs/typedoc src",
-    "test:lint": "eslint .",
-    "fix:lint": "eslint --fix .",
     "publish": "pnpm run script -- publish"
   },
   "devDependencies": {

+ 9 - 0
lazer/sdk/js/tsconfig.build.json

@@ -0,0 +1,9 @@
+{
+  "extends": "./tsconfig.json",
+  "compilerOptions": {
+    "noEmit": false,
+    "incremental": false,
+    "declaration": true
+  },
+  "exclude": ["node_modules", "dist", "examples/"]
+}

+ 2 - 1
lazer/sdk/js/tsconfig.json

@@ -1,3 +1,4 @@
 {
-  "extends": "@cprussin/tsconfig/base.json"
+  "extends": "@cprussin/tsconfig/base.json",
+  "exclude": ["node_modules", "dist"]
 }

+ 7 - 1
turbo.json

@@ -4,7 +4,7 @@
   "ui": "tui",
   "tasks": {
     "build": {
-      "dependsOn": ["^build"],
+      "dependsOn": ["^build", "build:cjs", "build:esm"],
       "inputs": [
         "$TURBO_DEFAULT$",
         "!README.md",
@@ -16,6 +16,12 @@
       ],
       "outputs": ["dist/**", "lib/**"]
     },
+    "build:cjs": {
+      "outputs": ["dist/cjs/**"]
+    },
+    "build:esm": {
+      "outputs": ["dist/esm/**"]
+    },
     "fix": {
       "dependsOn": ["fix:lint", "fix:format"],
       "cache": false