Skip to content

Commit f5fca07

Browse files
committed
refactor: loadConfigFile
1 parent fe668d5 commit f5fca07

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/main.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { includesAny, getPluginFunction } from "./utils"
1+
import { includesAny, getPluginFunction, loadConfigFile } from "./utils"
22

33
import type resolve from "@rollup/plugin-node-resolve"
44
type RollupResolveOptions = Parameters<typeof resolve>[0]
@@ -33,9 +33,6 @@ type RollupAscOptions = Parameters<typeof asc>[0] & Record<string, any>
3333
import type visualizer from "rollup-plugin-visualizer"
3434
type RollupVisualizerOptions = Parameters<typeof visualizer>[0]
3535

36-
import { existsSync } from "fs"
37-
import { join } from "path"
38-
3936
export type Plugin =
4037
| "js"
4138
| "ts"
@@ -208,16 +205,9 @@ export function createPlugins(
208205
: {}
209206
) as RollupTerserOptions
210207
if (typeof configDir === "string") {
211-
const terserConfigFile = join(configDir, ".terserrc.js")
212-
if (existsSync(terserConfigFile)) {
213-
const loadedTerserConfigFile = require(terserConfigFile) as { default: RollupTerserOptions } | RollupTerserOptions
214-
if (loadedTerserConfigFile !== undefined) {
215-
if ("default" in loadedTerserConfigFile) {
216-
terserOptions = loadedTerserConfigFile.default
217-
} else {
218-
terserOptions = loadedTerserConfigFile
219-
}
220-
}
208+
const maybeConfig = loadConfigFile(configDir, [".terserrc.js", ".terserrc"])
209+
if (maybeConfig !== null) {
210+
terserOptions = maybeConfig as RollupTerserOptions
221211
}
222212
}
223213
pushPlugin(["terser"], ["rollup-plugin-terser", "terser"], terserOptions, process.env.NODE_ENV === "production")

src/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,23 @@ export function getPluginFunction(modul: any, prop?: string) {
3030
return pluginFunction
3131
}
3232
}
33+
34+
import { existsSync } from "fs"
35+
import { join } from "path"
36+
37+
export function loadConfigFile(configDir: string, configFiles: Array<string>) {
38+
for (const configFile of configFiles) {
39+
const terserConfigFile = join(configDir, configFile)
40+
if (existsSync(terserConfigFile)) {
41+
const loadedTerserConfigFile = require(terserConfigFile) as { default: Record<any, any> } | Record<any, any>
42+
if (loadedTerserConfigFile !== undefined) {
43+
if ("default" in loadedTerserConfigFile) {
44+
return loadedTerserConfigFile.default
45+
} else {
46+
return loadedTerserConfigFile
47+
}
48+
}
49+
}
50+
}
51+
return null
52+
}

0 commit comments

Comments
 (0)