11import * as path from 'path'
2+ import { existsSync } from 'fs'
23import { defaultOptions } from './default-options'
34import { BulmaCssVarsOptions , ColorCallSet } from './types'
45import { getUsedVariables } from './find-used-vars'
@@ -14,22 +15,41 @@ import { getCssFallbacks } from './css-post-processor'
1415import { compileSass } from './compile-sass'
1516
1617const configFileName = 'bulma-css-vars.config.js'
18+ const cjsConfigFileName = 'bulma-css-vars.config.cjs'
1719const mainSassFileName = 'src/main.scss'
1820
1921const configFilePathAtCwd = ( cwd : string ) => path . join ( cwd , configFileName )
22+ const cjsConfigFilePathAtCwd = ( cwd : string ) => path . join ( cwd , cjsConfigFileName )
2023const mainSassFilePathAtCwd = ( cwd : string ) => path . join ( cwd , mainSassFileName )
2124
2225async function validateOptions ( cwd : string ) {
2326 const configFilePath = configFilePathAtCwd ( cwd )
27+ const cjsConfigFilePath = cjsConfigFilePathAtCwd ( cwd )
2428
2529 let loadedOptions = { }
26- try {
27- loadedOptions = require ( configFilePath )
28- } catch ( err ) {
30+
31+ if ( existsSync ( cjsConfigFilePath ) ) {
32+ try {
33+ loadedOptions = await import ( cjsConfigFilePath ) . then ( m => m . default || m )
34+ } catch ( err ) {
35+ throw new Error (
36+ `Unable to parse configuration file at '${ cjsConfigFilePath } ': ${ err } `
37+ )
38+ }
39+ } else if ( existsSync ( configFilePath ) ) {
40+ try {
41+ loadedOptions = await import ( configFilePath ) . then ( m => m . default || m )
42+ } catch ( err ) {
43+ throw new Error (
44+ `Unable to parse cjs configuration file at '${ configFilePath } ': ${ err } `
45+ )
46+ }
47+ } else {
2948 throw new Error (
3049 `Required config file '${ configFileName } ' was not found at ${ configFilePath } `
3150 )
3251 }
52+
3353 const options : BulmaCssVarsOptions = {
3454 ...defaultOptions ,
3555 ...loadedOptions ,
0 commit comments