forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepareDist.js
105 lines (93 loc) · 3.48 KB
/
prepareDist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// The Apollo Client source that is published to npm is located in the
// "dist" directory. This utility script is called when building 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 supporting files from the root into "dist" (e.g. `README.MD`,
// `LICENSE`, etc.).
// - Create a new `package.json` for each sub-set bundle we support, and
// store it in the appropriate dist sub-directory.
const fs = require("fs");
const path = require("path");
const recast = require("recast");
const distRoot = `${__dirname}/../dist`;
/* @apollo/client */
const packageJson = require("../package.json");
const entryPoints = require("./entryPoints.js");
// Enable default interpretation of .js files as ECMAScript modules. We don't
// put this in the source ../package.json file because it interferes with tools
// like ts-node, which we use to run various ../config/*.ts scripts.
// TODO(benjamn) Fully diagnose that interference.
packageJson.type = "module";
// 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;
delete packageJson.engines;
// 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) => {
if (typeof value === "string" && value.startsWith("./dist/")) {
const parts = value.split("/");
parts.splice(1, 1); // remove dist
return parts.join("/");
}
return value;
},
2
) + "\n";
// Save the modified package.json to "dist"
fs.writeFileSync(`${distRoot}/package.json`, distPackageJson);
// Copy supporting files into "dist"
const srcDir = `${__dirname}/..`;
const destDir = `${srcDir}/dist`;
fs.copyFileSync(`${srcDir}/README.md`, `${destDir}/README.md`);
fs.copyFileSync(`${srcDir}/LICENSE`, `${destDir}/LICENSE`);
// Create individual bundle package.json files, storing them in their
// associated dist directory. This helps provide a way for the Apollo Client
// core to be used without React, as well as AC's cache, utilities, SSR,
// components, HOC, and various links to be used by themselves, via CommonJS
// entry point files that only include the exports needed for each bundle.
entryPoints.forEach(function buildPackageJson({
dirs,
bundleName = dirs[dirs.length - 1],
sideEffects = false,
}) {
if (!dirs.length) return;
fs.writeFileSync(
path.join(distRoot, ...dirs, "package.json"),
JSON.stringify(
{
name: path.posix.join("@apollo", "client", ...dirs),
type: "module",
main: `${bundleName}.cjs`,
module: "index.js",
types: "index.d.ts",
sideEffects,
},
null,
2
) + "\n"
);
});
entryPoints.forEach(function buildCts({
dirs,
bundleName = dirs[dirs.length - 1],
}) {
if (!dirs.length) return;
fs.writeFileSync(
path.join(distRoot, ...dirs, `${bundleName}.d.cts`),
'export * from "./index.d.ts";\n'
);
});