This repository has been archived by the owner on Nov 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
166 lines (151 loc) · 4.65 KB
/
build.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/** Build script for all packages */
const { rollup } = require("rollup");
const beautifyJson = require("json-beautify");
const camelCase = require("lodash.camelcase");
const path = require("path");
const fs = require("fs");
const resolve = require("@rollup/plugin-node-resolve");
const terser = require("rollup-plugin-terser").terser;
const replace = require("@rollup/plugin-replace");
const typescript = require("@rollup/plugin-typescript");
// const typescriptApi = require("typescript");
const allPackagesPeerDepsMap = {
react: "React",
"@reffect/core": "reffect"
};
const INPUT_FILENAME = "index.ts";
const paths = {
project: process.cwd(),
packageJson: path.resolve(process.cwd(), "package.json")
};
// JS object of package.json file
const packageJson = JSON.parse(fs.readFileSync(paths.packageJson).toString());
// converts package name to file name (example: "@reffect/logger" -> "reffect-logger")
const fileName = packageJson.name
.replace("@", "")
.replace("/", "-")
.replace("core", "")
.replace(/-$/g, "");
// array of dependencies names
const deps = Object.keys(packageJson.dependencies || {});
// array of peer dependencies names
const peerDeps = Object.keys(packageJson.peerDependencies || {});
// array of all dependencies names (except dev dependencies)
const allDeps = [...deps, ...peerDeps].filter((value, index, arr) => arr.indexOf(value) === index);
const createBuildConfig = (outputFormat, commonOutput = {}, plugins = []) => {
const outputFileName = `${fileName}.${outputFormat}.js`;
const inputPathToFile = path.join(paths.project, INPUT_FILENAME);
const outputPathToFile = path.join(paths.project, outputFileName);
return {
outputFormat,
outputFileName,
inputPathToFile,
outputPathToFile,
rollupConfig: {
input: inputPathToFile,
output: {
file: outputPathToFile,
format: outputFormat,
sourcemap: true,
...commonOutput
},
external: id => allDeps.includes(id),
context: paths.project,
plugins: [
terser({
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false,
arguments: true,
unsafe_Function: true,
module: true,
passes: 15
}
}),
typescript({
allowJs: false,
allowSyntheticDefaultImports: true,
lib: ["dom", "esnext"],
module: "es2015",
target: "esnext",
moduleResolution: "node",
strict: true,
noImplicitAny: true,
noImplicitReturns: true,
noImplicitThis: true,
noUnusedLocals: true,
sourceMap: false,
strictNullChecks: true,
suppressImplicitAnyIndexErrors: true,
exclude: ["node_modules", "**/*.spec.ts"]
}),
...plugins
]
}
};
};
const buildConfigsMap = {
// Common JS builds
cjs: createBuildConfig("cjs"),
// EcmaScript builds
es: createBuildConfig("es"),
// UMD builds
umd: createBuildConfig(
"umd",
{
name: camelCase(fileName),
globals: peerDeps.reduce(
(globalsMap, peerDep) =>
allPackagesPeerDepsMap[peerDep]
? {
...globalsMap,
[peerDep]: allPackagesPeerDepsMap[peerDep]
}
: globalsMap,
{}
)
},
[
replace({
// TODO: probably it should be sets from package config (?)
// currently this variable contains in @reffect/logger only.
"process.env.NODE_ENV": JSON.stringify("development")
}),
resolve({ jsnext: true })
]
)
};
// automate update package json object (for cases when needs to add/update property in package.json file of the package)
fs.writeFileSync(
paths.packageJson,
beautifyJson(
Object.assign(packageJson, {
/** --- specific package.json fields for dev. usage --- */
source: INPUT_FILENAME,
main: buildConfigsMap.cjs.outputFileName,
typings: INPUT_FILENAME,
"umd:main": buildConfigsMap.umd.outputFileName,
"jsnext:main": buildConfigsMap.es.outputFileName,
module: buildConfigsMap.es.outputFileName,
/** --------------------------------------------------- */
/** list of files which contains in installed package */
files: ["LICENSE", INPUT_FILENAME, `${fileName}.*.js`, `${fileName}.*.js.map`]
}),
// replacer
null,
// spaces
2,
// columns limit
35
)
);
Promise.all(
Object.values(buildConfigsMap).map(async ({ rollupConfig }) =>
(await rollup(rollupConfig)).write(rollupConfig.output)
)
).catch(e => {
console.error(e);
process.exit(1);
});