Skip to content

Commit fe668d5

Browse files
committed
feat: support loading .terserrc.js
1 parent 6769854 commit fe668d5

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/main.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ 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+
3639
export type Plugin =
3740
| "js"
3841
| "ts"
@@ -68,6 +71,8 @@ export function createPlugins(
6871
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
6972
extraPlugins?: Array<any>
7073
) {
74+
const configDir = require.main?.filename?.replace(/node_modules.*/, "")
75+
7176
let plugins = []
7277

7378
// language specific
@@ -188,9 +193,7 @@ export function createPlugins(
188193
)
189194

190195
// terser
191-
pushPlugin(
192-
["terser"],
193-
["rollup-plugin-terser", "terser"],
196+
let terserOptions = (
194197
process.env.NODE_ENV === "production"
195198
? {
196199
ecma: 2018,
@@ -202,9 +205,22 @@ export function createPlugins(
202205
comments: false,
203206
},
204207
}
205-
: {},
206-
process.env.NODE_ENV === "production"
207-
)
208+
: {}
209+
) as RollupTerserOptions
210+
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+
}
221+
}
222+
}
223+
pushPlugin(["terser"], ["rollup-plugin-terser", "terser"], terserOptions, process.env.NODE_ENV === "production")
208224

209225
// utility function that pushes a plugin
210226
function pushPlugin(

0 commit comments

Comments
 (0)