From 4c7af4ea3a2155d8f8f8bb8febda33cc680c943d Mon Sep 17 00:00:00 2001 From: hwillson Date: Tue, 24 Sep 2019 07:36:25 -0400 Subject: [PATCH] Adjust publish process to use "dist" as the project root These changes include a new script that is used to prepare the "dist" directory for publishing. This includes: - Copying a modified package.json into "dist", after it has been adjusted (remove "./dist" directory prefixes, remove un-needed package.json properties, and remove the private flag). - Copying the README.md into "dist". This script is now called as part of the "predeploy" script. The "deploy" script will now also cd into "dist" before publishing, so only the contents in "dist" will be published to npm (and "dist" will essentially serve as the published package root). --- config/prepareDist.js | 39 +++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++------------ 2 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 config/prepareDist.js diff --git a/config/prepareDist.js b/config/prepareDist.js new file mode 100644 index 00000000000..6daa4608646 --- /dev/null +++ b/config/prepareDist.js @@ -0,0 +1,39 @@ +// The Apollo Client source that is published to npm is located in the +// "dist" directory. This utility script is called just before deploying +// Apollo Client, to make sure the "dist" directory is prepared for publishing. +// +// This script will: +// +// - Copy the current root package.json into "dist" after adjusting it for +// publishing. +// - Copy the root README.md into "dist" + +const packageJson = require('../package.json'); +const fs = require('fs'); + +// The root package.json is marked as private to prevent publishing +// from happening in the root of the project. This sets the package back to +// public so it can be published from the "dist" directory. +packageJson.private = false; + +// Remove package.json items that we don't need to publish +delete packageJson.scripts; +delete packageJson.bundlesize; + +// The root package.json points to the CJS/ESM source in "dist", to support +// on-going package development (e.g. running tests, supporting npm link, etc.). +// When publishing from "dist" however, we need to update the package.json +// to point to the files within the same directory. +const distPackageJson = JSON.stringify( + packageJson, + (_key, value) => ( + typeof value === 'string' ? value.replace(/\.\/dist\//, '') : value + ), + 2 +); + +// Save the modified package.json to "dist" +fs.writeFileSync(`${__dirname}/../dist/package.json`, distPackageJson); + +// Copy the README into "dist" +fs.copyFileSync(`${__dirname}/../README.md`, `${__dirname}/../dist/README.md`); diff --git a/package.json b/package.json index 0fc2e12ee8f..80d87e1b5a5 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "@apollo/client", "version": "3.0.0-alpha.4", "description": "A fully-featured caching GraphQL client.", + "private": true, "keywords": [ "apollo", "graphql", @@ -12,11 +13,11 @@ ], "author": "opensource@apollographql.com", "license": "MIT", - "main": "./lib/apollo-client.cjs.js", - "module": "./lib/index.js", - "types": "./lib/index.d.ts", + "main": "./dist/apollo-client.cjs.js", + "module": "./dist/index.js", + "types": "./dist/index.d.ts", "sideEffects": [ - "./lib/cache/inmemory/fixPolyfills.js" + "./dist/cache/inmemory/fixPolyfills.js" ], "repository": { "type": "git", @@ -31,7 +32,7 @@ "build": "npx tsc", "postbuild": "npm run bundle", "watch": "npx tsc-watch --onSuccess \"npm run postbuild\"", - "clean": "npx rimraf -r lib coverage", + "clean": "npx rimraf -r dist coverage", "test": "jest --config ./config/jest.config.js", "test:ci": "npm run coverage -- --ci --maxWorkers=2 --reporters=default --reporters=jest-junit", "test:watch": "jest --config ./config/jest.config.js --watch", @@ -39,13 +40,14 @@ "coverage": "npx jest --config ./config/jest.config.js --verbose --coverage", "coverage:upload": "npx codecov", "bundlesize": "npm run build && bundlesize", - "predeploy": "npm run build", - "deploy": "npm publish --tag alpha" + "prepdist": "node ./config/prepareDist.js", + "predeploy": "npm run build && npm run prepdist", + "deploy": "cd dist && npm publish --tag alpha" }, "bundlesize": [ { "name": "apollo-client", - "path": "./lib/apollo-client.cjs.min.js", + "path": "./dist/apollo-client.cjs.min.js", "maxSize": "21 kB" } ], @@ -98,8 +100,5 @@ }, "publishConfig": { "access": "public" - }, - "files": [ - "lib" - ] + } }