Skip to content

Commit

Permalink
Merge pull request #64 from atom-community/config-files
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jul 19, 2021
2 parents 6769854 + f5fca07 commit ab7f4a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { includesAny, getPluginFunction } from "./utils"
import { includesAny, getPluginFunction, loadConfigFile } from "./utils"

import type resolve from "@rollup/plugin-node-resolve"
type RollupResolveOptions = Parameters<typeof resolve>[0]
Expand Down Expand Up @@ -68,6 +68,8 @@ export function createPlugins(
inputPluginsNames: Array<Plugin> = ["ts", "js", "json", "coffee"],
extraPlugins?: Array<any>
) {
const configDir = require.main?.filename?.replace(/node_modules.*/, "")

let plugins = []

// language specific
Expand Down Expand Up @@ -188,9 +190,7 @@ export function createPlugins(
)

// terser
pushPlugin(
["terser"],
["rollup-plugin-terser", "terser"],
let terserOptions = (
process.env.NODE_ENV === "production"
? {
ecma: 2018,
Expand All @@ -202,9 +202,15 @@ export function createPlugins(
comments: false,
},
}
: {},
process.env.NODE_ENV === "production"
)
: {}
) as RollupTerserOptions
if (typeof configDir === "string") {
const maybeConfig = loadConfigFile(configDir, [".terserrc.js", ".terserrc"])
if (maybeConfig !== null) {
terserOptions = maybeConfig as RollupTerserOptions
}
}
pushPlugin(["terser"], ["rollup-plugin-terser", "terser"], terserOptions, process.env.NODE_ENV === "production")

// utility function that pushes a plugin
function pushPlugin(
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ export function getPluginFunction(modul: any, prop?: string) {
return pluginFunction
}
}

import { existsSync } from "fs"
import { join } from "path"

export function loadConfigFile(configDir: string, configFiles: Array<string>) {
for (const configFile of configFiles) {
const terserConfigFile = join(configDir, configFile)
if (existsSync(terserConfigFile)) {
const loadedTerserConfigFile = require(terserConfigFile) as { default: Record<any, any> } | Record<any, any>
if (loadedTerserConfigFile !== undefined) {
if ("default" in loadedTerserConfigFile) {
return loadedTerserConfigFile.default
} else {
return loadedTerserConfigFile
}
}
}
}
return null
}

0 comments on commit ab7f4a9

Please sign in to comment.