File tree 2 files changed +22
-1
lines changed
2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 1
1
export const IGNORE_ANNOTATION = "purifycss ignore"
2
+ export const CONFIG_FILENAME = "purgecss.config.js"
2
3
3
4
// Error Message
5
+ export const ERROR_CONFIG_FILE_LOADING = "Error loading the config file"
4
6
export const ERROR_MISSING_CONTENT = "No content provided."
5
7
export const ERROR_MISSING_CSS = "No css provided."
6
8
export const ERROR_EXTRACTER_FAILED =
Original file line number Diff line number Diff line change 2
2
import type { ExtractersObj , Options } from "./../flow/index.js"
3
3
4
4
import fs from "fs"
5
+ import path from "path"
5
6
import glob from "glob"
6
7
import defaultOptions from "./constants/defaultOptions"
7
8
import postcss from "postcss"
8
9
import selectorParser from "postcss-selector-parser"
9
10
import {
11
+ CONFIG_FILENAME ,
12
+ ERROR_CONFIG_FILE_LOADING ,
10
13
ERROR_MISSING_CONTENT ,
11
14
ERROR_MISSING_CSS ,
12
15
ERROR_EXTRACTER_FAILED ,
@@ -28,12 +31,28 @@ class Purgecss {
28
31
options : Options
29
32
selectors : Set < string >
30
33
31
- constructor ( options : Options ) {
34
+ constructor ( options : Options | string ) {
35
+ if ( typeof options === "string" || typeof options === "undefined" )
36
+ options = this . loadConfigFile ( options )
32
37
this . checkOptions ( options )
33
38
this . options = Object . assign ( defaultOptions , options )
34
39
this . selectors = new Set ( )
35
40
}
36
41
42
+ loadConfigFile ( configFile ?: string ) {
43
+ const pathConfig = typeof configFile === "undefined"
44
+ ? CONFIG_FILENAME
45
+ : configFile
46
+ let options
47
+ try {
48
+ const t = path . resolve ( process . cwd ( ) , pathConfig )
49
+ options = require ( t )
50
+ } catch ( e ) {
51
+ throw new Error ( ERROR_CONFIG_FILE_LOADING + e . message )
52
+ }
53
+ return options
54
+ }
55
+
37
56
checkOptions ( options : Options ) {
38
57
if ( typeof options !== "object" ) throw new TypeError ( ERROR_OPTIONS_TYPE )
39
58
if ( ! options . content || ! options . content . length )
You can’t perform that action at this time.
0 commit comments