-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
executable file
·79 lines (70 loc) · 2.08 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
#!/usr/bin/env node
const { execSync } = require("child_process")
const fs = require("fs-extra")
const { rollup } = require("rollup")
const { builtinModules } = require("module")
const alias = require("@rollup/plugin-alias")
const { nodeResolve } = require("@rollup/plugin-node-resolve")
const pkg = require("node-fetch/package.json")
async function main() {
await fs.remove("lib")
const NODE_FETCH = "node_modules/node-fetch"
delete pkg.dependencies["fetch-blob"]
delete pkg.dependencies["data-uri-to-buffer"]
const bundle = await rollup({
input: `${NODE_FETCH}/src/index.js`,
external: Object.keys(pkg.dependencies)
.concat(builtinModules)
.concat("node-domexception"),
plugins: [
alias({
entries: [
{
find: /^node:(.+)$/,
replacement: "$1",
},
],
}),
nodeResolve(),
],
})
await bundle.write({
dir: "lib",
format: "cjs",
exports: "named",
sourcemap: true,
banner: `/* eslint-disable */`,
footer: `/* eslint-enable */`,
})
pkg.main = "index.js"
pkg.name = "node-fetch-commonjs"
pkg.version = "3.3.1"
pkg.type = "commonjs"
pkg.repository.url = execSync("git config --get remote.origin.url").toString().trim()
const fetchPkg = require("fetch-blob/package.json")
pkg.dependencies ??= {}
Object.assign(pkg.dependencies, fetchPkg.dependencies)
delete pkg.dependencies["formdata-polyfill"]
delete pkg.homepage
delete pkg.files
delete pkg.scripts
delete pkg.type
delete pkg.types
delete pkg.devDependencies
delete pkg.bugs
delete pkg.tsd
delete pkg.xo
delete pkg.runkitExampleFilename
await fs.copy(`${NODE_FETCH}/LICENSE.md`, "lib/LICENSE.md")
await fs.copy("README.md", "lib/README.md")
await fs.copy(`${NODE_FETCH}/@types/index.d.ts`, "lib/index.d.ts")
await fs.writeJSON("lib/package.json", pkg, { spaces: 2 })
const source = await fs.readFile("lib/index.js", "utf-8")
await fs.writeFile(
"lib/index.js",
source.replace(/('use strict';)/, "exports = module.exports = fetch;\n$1")
)
}
main().catch(err => {
throw err
})