From ed58a6e8164c384dd159c52622c42d64d6054672 Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Wed, 28 Jul 2021 11:08:02 +0200 Subject: [PATCH] feat: bundle module and use package exports - use esbuild to bundle module in esm and cjs format - bundle dependencies to reduce module size - use package exports to automatically select the appropriate version - add build step to prepublishOnly - add build step to ci --- .github/workflows/ci.yml | 2 ++ .gitignore | 3 +++ package.json | 25 +++++++++++++++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f64a50f15..7b598f176 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,3 +38,5 @@ jobs: - run: npm install - run: npm test -- --colors + + - run: npm run build diff --git a/.gitignore b/.gitignore index c90aca99f..3151e2a17 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,9 @@ node_modules # Babel-compiled files lib +# esbuild-compiled files +dist + # Ignore package manager lock files package-lock.json yarn.lock diff --git a/package.json b/package.json index 4ea91fa5c..7f4726dce 100644 --- a/package.json +++ b/package.json @@ -2,22 +2,32 @@ "name": "node-fetch", "version": "3.0.0-beta.10", "description": "A light-weight module that brings Fetch API to node.js", - "main": "./src/index.js", + "main": "./dist/index.mjs", "sideEffects": false, "type": "module", "files": [ - "src", + "dist", "@types/index.d.ts" ], "types": "./@types/index.d.ts", "engines": { "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, + "exports": { + ".": { + "import": "./dist/index.mjs", + "require": "./dist/index.cjs" + } + }, "scripts": { + "build": "npm run build:esm && npm run build:cjs", + "build:esm": "esbuild src/index.js --bundle --platform=node --format=esm --outfile=dist/index.mjs", + "build:cjs": "esbuild src/index.js --bundle --platform=node --format=cjs --outfile=dist/index.cjs", "test": "mocha", "coverage": "c8 report --reporter=text-lcov | coveralls", "test-types": "tsd", - "lint": "xo" + "lint": "xo", + "prepublishOnly": "npm run build" }, "repository": { "type": "git", @@ -53,7 +63,10 @@ "chai-iterator": "^3.0.2", "chai-string": "^1.5.0", "coveralls": "^3.1.0", + "data-uri-to-buffer": "^3.0.1", "delay": "^5.0.0", + "esbuild": "^0.12.16", + "fetch-blob": "^3.1.2", "form-data": "^4.0.0", "formdata-node": "^3.5.4", "mocha": "^8.3.2", @@ -61,10 +74,6 @@ "tsd": "^0.14.0", "xo": "^0.39.1" }, - "dependencies": { - "data-uri-to-buffer": "^3.0.1", - "fetch-blob": "^3.1.2" - }, "tsd": { "cwd": "@types", "compilerOptions": { @@ -115,4 +124,4 @@ ] }, "runkitExampleFilename": "example.js" -} +} \ No newline at end of file