Skip to content

Commit 655a73e

Browse files
committed
build: setup library building task
1 parent 993cc98 commit 655a73e

16 files changed

+10380
-4584
lines changed

.eslintignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules
2+
dist
3+
lib

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
node_modules
21
.idea
32
.DS_Store
43
.vscode
4+
node_modules/
5+
dist/

.prettierignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules
2+
dist
3+
lib

babel.config.cjs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = (api) => {
2+
api.cache(true);
3+
const targets = { ie: 11 };
4+
5+
const plugins = [
6+
[
7+
"@babel/plugin-transform-runtime",
8+
{
9+
corejs: 3,
10+
},
11+
],
12+
];
13+
14+
return {
15+
presets: [
16+
[
17+
"@babel/preset-typescript",
18+
{
19+
onlyRemoveTypeImports: true,
20+
},
21+
],
22+
[
23+
"@babel/preset-env",
24+
{
25+
targets,
26+
},
27+
],
28+
],
29+
plugins,
30+
comments: false,
31+
sourceMaps: true,
32+
};
33+
};

build/Awaited4-2.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Awaited<T> = T extends null | undefined
2+
? T // special case for `null | undefined` when not in `--strictNullChecks` mode
3+
: T extends object & { then(onfulfilled: infer F): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
4+
? F extends (value: infer V) => any // if the argument to `then` is callable, extracts the argument
5+
? Awaited<V> // recursively unwrap the value
6+
: never // the argument to `then` was not callable
7+
: T; // non-object or non-thenable
8+
9+
export default Awaited;

build/compile-types.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
mv ./src/types/Awaited.ts ./build/Awaited4-5.ts
3+
mv ./build/Awaited4-2.ts ./src/types/Awaited.ts
4+
5+
npm i [email protected] -D --legacy-peer-deps
6+
npm run compile:types4-2
7+
8+
mv ./src/types/Awaited.ts ./build/Awaited4-2.ts
9+
mv ./build/Awaited4-5.ts ./src/types/Awaited.ts
10+
11+
npm i typescript@beta -D --legacy-peer-deps
12+
npm run compile:types4-5

build/generateExports.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import glob from "glob";
2+
import { readFile, writeFile } from "fs/promises";
3+
import { drop, map, not, pipe, reduce } from "../src/index";
4+
5+
const SOURCE_DIR = "./src";
6+
const OUTPUT_DIR = "./dist";
7+
const CJS_ROOT_DIR = `${OUTPUT_DIR}/cjs`;
8+
const ESM_ROOT_DIR = `${OUTPUT_DIR}/esm`;
9+
const ESM5_ROOT_DIR = `${OUTPUT_DIR}/esm5`;
10+
11+
const conditionalRootIndex = {
12+
import: `${ESM_ROOT_DIR}/index.js`,
13+
require: `${CJS_ROOT_DIR}/index.js`,
14+
};
15+
16+
const conditionalRootIndexLazy = {
17+
import: `${ESM_ROOT_DIR}/Lazy/index.js`,
18+
require: `${CJS_ROOT_DIR}/Lazy/index.js`,
19+
};
20+
21+
const defaultSubPathExports = {
22+
"./package.json": "./package.json",
23+
".": conditionalRootIndex,
24+
"./index": conditionalRootIndex,
25+
"./index.js": conditionalRootIndex,
26+
"./Lazy": conditionalRootIndexLazy,
27+
"./Lazy/index": conditionalRootIndexLazy,
28+
"./Lazy/index.js": conditionalRootIndexLazy,
29+
"./esm5": `${ESM5_ROOT_DIR}/index.js`,
30+
"./esm5/index": `${ESM5_ROOT_DIR}/index.js`,
31+
"./esm5/index.js": `${ESM5_ROOT_DIR}/index.js`,
32+
"./esm5/Lazy": `${ESM5_ROOT_DIR}/Lazy/index.js`,
33+
"./esm5/Lazy/index": `${ESM5_ROOT_DIR}/Lazy/index.js`,
34+
"./esm5/Lazy/index.js": `${ESM5_ROOT_DIR}/Lazy/index.js`,
35+
};
36+
37+
const searchFiles = (pattern: string): Promise<string[]> =>
38+
new Promise((resolve, reject) => {
39+
glob(pattern, function (err, files) {
40+
if (err) reject(err);
41+
else resolve(files);
42+
});
43+
});
44+
45+
async function generateExports() {
46+
const fileNames = await Promise.all([
47+
searchFiles(`${SOURCE_DIR}/*.ts`),
48+
searchFiles(`${SOURCE_DIR}/_internal/*.ts`),
49+
searchFiles(`${SOURCE_DIR}/Lazy/*.ts`),
50+
]).then((lists) =>
51+
lists
52+
.flat()
53+
.filter((a) => not(a.endsWith("index.ts")))
54+
.map((fileName) =>
55+
[...drop(2, fileName.split("/"))].join("/").replace(".ts", ""),
56+
),
57+
);
58+
59+
const subPathExports = pipe(
60+
fileNames,
61+
map((name) => {
62+
const conditionalSubPaths = {
63+
import: `${ESM_ROOT_DIR}/${name}.js`,
64+
require: `${CJS_ROOT_DIR}/${name}.js`,
65+
};
66+
return {
67+
[`./${name}`]: conditionalSubPaths,
68+
[`./${name}.js`]: conditionalSubPaths,
69+
[`./esm5/${name}`]: `${ESM5_ROOT_DIR}/${name}.js`,
70+
[`./esm5/${name}.js`]: `${ESM5_ROOT_DIR}/${name}.js`,
71+
};
72+
}),
73+
(iter) => reduce((acc, field) => Object.assign(acc, field), {}, iter),
74+
);
75+
76+
const packageJsonObject = JSON.parse(
77+
String(await readFile("./package.json")),
78+
);
79+
80+
await writeFile(
81+
"./package.json",
82+
JSON.stringify(
83+
Object.assign(packageJsonObject, {
84+
exports: {
85+
...defaultSubPathExports,
86+
...subPathExports,
87+
},
88+
}),
89+
),
90+
);
91+
}
92+
93+
(async function main() {
94+
await Promise.all([
95+
// Add package.json file to cjs directory
96+
writeFile(`${CJS_ROOT_DIR}/package.json`, '{ "type": "commonjs" }'),
97+
// Generate and add 'exports' field to root package.json
98+
generateExports(),
99+
]);
100+
})();

0 commit comments

Comments
 (0)