-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.node.common.config.mjs
170 lines (127 loc) · 5 KB
/
rollup.node.common.config.mjs
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
import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonJS from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import path from "path"
import fs from "fs"
import * as console from "colorful-cli-logger"
console.setVerbose(true)
import nodeJsBuiltins from "builtins"
const nonPrefixedBuiltinModules = [...nodeJsBuiltins()]
const prefixedBuiltinModules = []
for (const mod of nonPrefixedBuiltinModules) {
if (!mod.startsWith("node:")) prefixedBuiltinModules.push("node:"+mod)
}
const builtinModules = new Set([...nonPrefixedBuiltinModules, ...prefixedBuiltinModules])
const isCjsRegex = /(((module)((\[[`"'])|\.))?exports([`"']\])?((\.|(\[[`"']))[^\s\n"`']+([`"']\])?)?\s*=\s*[^\s\n]+)|require\(["'`][^\s\n"'`]+["'`]\)/gm
const config = {
input: './repl/src/repl.ts',
output: {
file: 'repl/dist/circClone-repl.js',
format: 'cjs',
sourcemap: false,
exports: "named",
interop: "auto"
},
plugins: [
typescript({tsconfig: "./tsconfig.json", noEmitOnError: false, sourceMap: false}),
// these resolve options do not matter if resolveOnly is used. ModulesOnly does currently not prefer cjs exports if they exists, instead it finds the first esm version and transpiles it, which we want to avoid if we can.
resolve({modulesOnly: true, preferBuiltins: true, resolveOnly(mod) {
if (builtinModules.has(mod)) return false
// this can happen when a module defines custom imports (e.g. chalk) in its package.json. We have no way tho to know here from where it was imported, so we just assume it was imported from a esm module and that it is esm as well. The only case that is known to not be covered here is when this module itself uses custom imports in package.json, to include this we would have to check if the given module is in local in this package. But I don't think that is worth the effort.
if (!fs.existsSync(path.join("node_modules", mod))) {
console.warn("may be resolved sub module:", mod)
return true
}
const json = JSON.parse(fs.readFileSync(path.join("node_modules", mod, "package.json"), "utf8"))
if (json.type === "module") {
console.error("resolved module:", mod)
return true
}
const vals = getSpecificJsonProps(json, [
["exports", ".", "node", "require"],
["exports", ".", "require", "node"],
["exports", ".", "require", "default"],
["exports", ".", "require", "default"],
["exports", "node", "require"],
["exports", "require", "node"],
["exports", "require", "default"],
["exports", "require", "default"],
["exports", "require"],
["exports", "node"],
["exports", "default"],
["exports", ".", "default"],
"exports",
"main"
])
for (let val of vals) {
if (typeof val !== "string") continue
if (val.endsWith(".mjs")) continue
if (fs.existsSync(path.join("node_modules", mod, val))) {
if (fs.statSync(path.join("node_modules", mod, val)).isDirectory()) val = path.join(val, "index.js")
else if (!val.endsWith(".js")) val = val + ".js"
if (!fs.existsSync(path.join("node_modules", mod, val))) continue
}
if (!val.endsWith(".js")) continue
const fileContent = fs.readFileSync(path.join("node_modules", mod, val), "utf8")
const isCjs = isCjsRegex.test(fileContent)
if (isCjs) return false
}
console.error("resolved module:", mod)
return true
}}),
// do i even need this?
commonJS({
include: 'node_modules/**'
}),
json()
]
};
export default config
// console.log('resolved modules:', resolved)
function filterJsonProps(packageJsonParsed, whiteListOfPackageJsonProps/* (string | string[])[] */) {
const ob = {}
for (const keys of whiteListOfPackageJsonProps) {
let keysAr = typeof keys === "string" ? keys.split(".") : keys
let local = packageJsonParsed
let failed = false
for (const key of keysAr) {
if (local[key] !== undefined) {
local = local[key]
}
else {
failed = true
break
}
}
if (failed) continue
let localCopy = ob
for (let i = 0; i < keysAr.length-1; i++) {
const key = keysAr[i];
if (localCopy[key] === undefined) localCopy[key] = {}
localCopy = localCopy[key]
}
localCopy[keysAr[keysAr.length-1]] = local
}
return ob
}
function getSpecificJsonProps(packageJsonParsed, whiteListOfPackageJsonProps/* (string | string[])[] */) {
const ar = []
for (const keys of whiteListOfPackageJsonProps) {
let keysAr = typeof keys === "string" ? keys.split(".") : keys
let local = packageJsonParsed
let failed = false
for (const key of keysAr) {
if (local[key] !== undefined) {
local = local[key]
}
else {
failed = true
break
}
}
if (failed) continue
ar.push(local)
}
return ar
}