Skip to content

Commit ab7f4a9

Browse files
authored
Merge pull request #64 from atom-community/config-files
2 parents 6769854 + f5fca07 commit ab7f4a9

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/main.ts

Lines changed: 13 additions & 7 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]
@@ -68,6 +68,8 @@ export function createPlugins(
6868
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
6969
extraPlugins?: Array<any>
7070
) {
71+
const configDir = require.main?.filename?.replace(/node_modules.*/, "")
72+
7173
let plugins = []
7274

7375
// language specific
@@ -188,9 +190,7 @@ export function createPlugins(
188190
)
189191

190192
// terser
191-
pushPlugin(
192-
["terser"],
193-
["rollup-plugin-terser", "terser"],
193+
let terserOptions = (
194194
process.env.NODE_ENV === "production"
195195
? {
196196
ecma: 2018,
@@ -202,9 +202,15 @@ export function createPlugins(
202202
comments: false,
203203
},
204204
}
205-
: {},
206-
process.env.NODE_ENV === "production"
207-
)
205+
: {}
206+
) as RollupTerserOptions
207+
if (typeof configDir === "string") {
208+
const maybeConfig = loadConfigFile(configDir, [".terserrc.js", ".terserrc"])
209+
if (maybeConfig !== null) {
210+
terserOptions = maybeConfig as RollupTerserOptions
211+
}
212+
}
213+
pushPlugin(["terser"], ["rollup-plugin-terser", "terser"], terserOptions, process.env.NODE_ENV === "production")
208214

209215
// utility function that pushes a plugin
210216
function pushPlugin(

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)