diff --git a/packages/api/package.json b/packages/api/package.json index f09221f..e686f29 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -2,8 +2,6 @@ "name": "@overlap/serverless-tools-api", "description": "Tools and utilities for working with APIGateway.", "version": "0.1.0", - "main": "./src/index.js", - "types": "./src/index.d.ts", "repository": { "type": "git", "url": "https://github.com/overlap-dev/serverless-tools", diff --git a/packages/api/project.json b/packages/api/project.json index f43ca5b..8e4a22c 100644 --- a/packages/api/project.json +++ b/packages/api/project.json @@ -3,7 +3,7 @@ "$schema": "../../node_modules/nx/schemas/project-schema.json", "sourceRoot": "packages/api/src", "targets": { - "build": { + "build-ts": { "executor": "@nx/js:tsc", "outputs": ["{options.outputPath}"], "options": { @@ -13,6 +13,14 @@ "assets": ["packages/api/*.md"] } }, + "build": { + "executor": "nx:run-commands", + "outputs": ["{workspaceRoot}/dist/packages/api"], + "options": { + "command": "node tools/scripts/bundle-api.mjs" + }, + "dependsOn": ["build-ts"] + }, "bump": { "executor": "nx:run-commands", "options": { diff --git a/packages/api/tsconfig.json b/packages/api/tsconfig.json index 1166bc9..b5cfc10 100644 --- a/packages/api/tsconfig.json +++ b/packages/api/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.base.json", "compilerOptions": { "module": "esnext", - "moduleResolution": "bundler", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, diff --git a/tools/scripts/bundle-api.mjs b/tools/scripts/bundle-api.mjs new file mode 100644 index 0000000..5144089 --- /dev/null +++ b/tools/scripts/bundle-api.mjs @@ -0,0 +1,49 @@ +import { readFile, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import commonjs from '@rollup/plugin-commonjs'; +import json from '@rollup/plugin-json'; +import { nodeResolve } from '@rollup/plugin-node-resolve'; +import { rollup } from 'rollup'; + +const workspaceRoot = process.cwd(); +const distDir = join(workspaceRoot, 'dist/packages/api'); +const inputFile = join(distDir, 'src/index.js'); +const packageJsonPath = join(distDir, 'package.json'); + +const bundle = await rollup({ + input: inputFile, + external: () => false, + plugins: [nodeResolve({ preferBuiltins: true }), commonjs(), json()], +}); + +await bundle.write({ + file: join(distDir, 'index.esm.js'), + format: 'esm', + sourcemap: true, +}); + +await bundle.write({ + file: join(distDir, 'index.cjs.js'), + format: 'cjs', + sourcemap: true, +}); + +await bundle.close(); + +const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8')); + +packageJson.main = './index.cjs.js'; +packageJson.module = './index.esm.js'; +packageJson.types = './src/index.d.ts'; +packageJson.exports = { + '.': { + types: './src/index.d.ts', + require: './index.cjs.js', + import: './index.esm.js', + default: './index.cjs.js', + }, + './package.json': './package.json', +}; +delete packageJson.type; + +await writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);