forked from mysticatea/eslint-utils
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrollup.config.js
55 lines (52 loc) · 1.81 KB
/
rollup.config.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
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
import fs from "fs"
import path from "path"
import dts from "rollup-plugin-dts"
import sourcemaps from "rollup-plugin-sourcemaps"
import packageInfo from "./package.json"
/**
* Define the output configuration.
* @param {string} ext The extension for generated files.
* @returns {object[]} The output configuration
*/
function config(ext) {
return [
{
input: "src/index.mjs",
output: {
exports: ext === ".mjs" ? undefined : "named",
file: `index${ext}`,
format: ext === ".mjs" ? "es" : "cjs",
sourcemap: true,
},
plugins: [sourcemaps()],
external: Object.keys(packageInfo.dependencies),
},
{
input: "./dist/index.d.ts",
output: {
exports: "named",
file: `index.d${ext.replace(/js$/u, "ts")}`,
format: "es",
},
plugins: [dts()],
},
]
}
/* eslint-disable @eslint-community/mysticatea/node/no-sync */
// Replace extension `.mts` to `.ts` in the `dist/*.d.mts` file name.
// This is needed because rollup-plugin-dts<=v4 doesn't support `.mts` extension.
for (const file of fs.readdirSync(path.resolve("dist"))) {
if (file.endsWith(".d.mts")) {
const content = fs.readFileSync(path.resolve("dist", file), "utf8")
const newContent = content.replace(/\.mjs(['"])/gu, ".js$1")
const newName = file.replace(/\.d\.mts$/u, ".d.ts")
fs.writeFileSync(path.resolve("dist", newName), newContent, "utf8")
fs.unlinkSync(path.resolve("dist", file))
}
}
/* eslint-enable @eslint-community/mysticatea/node/no-sync */
export default [...config(".js"), ...config(".mjs")]