diff --git a/README.md b/README.md index 086f63b..087d60e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ import { hsl, rgb } from 'bulma-css-vars' const appColors = { black: hsl(0, 0, 4), 'scheme-main': rgb(200, 105, 84), - red: { r: 255, b: 0, g: 0} + red: { r: 255, g: 0, b: 0 }, primary: '#663423', blue: 'blue', } diff --git a/lib/bin/bulma-css-vars.js b/lib/bin/bulma-css-vars.cjs similarity index 74% rename from lib/bin/bulma-css-vars.js rename to lib/bin/bulma-css-vars.cjs index 0a31b45..97a53ad 100755 --- a/lib/bin/bulma-css-vars.js +++ b/lib/bin/bulma-css-vars.cjs @@ -1,5 +1,5 @@ #!/usr/bin/env node -const { runCli, runCliInit } = require('../dist/cli.js') +const { runCli, runCliInit } = require('../dist/cjs/cli.js') const cwd = process.cwd() diff --git a/lib/package.json b/lib/package.json index 4277fd5..4e5e86a 100644 --- a/lib/package.json +++ b/lib/package.json @@ -27,7 +27,7 @@ "/*.sass" ], "bin": { - "bulma-css-vars": "bin/bulma-css-vars.js" + "bulma-css-vars": "bin/bulma-css-vars.cjs" }, "scripts": { "build": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json && webpack", diff --git a/lib/src/cli.ts b/lib/src/cli.ts index 33ad95e..6fd4e21 100644 --- a/lib/src/cli.ts +++ b/lib/src/cli.ts @@ -1,4 +1,5 @@ import * as path from 'path' +import { existsSync } from 'fs' import { defaultOptions } from './default-options' import { BulmaCssVarsOptions, ColorCallSet } from './types' import { getUsedVariables } from './find-used-vars' @@ -14,22 +15,41 @@ import { getCssFallbacks } from './css-post-processor' import { compileSass } from './compile-sass' const configFileName = 'bulma-css-vars.config.js' +const cjsConfigFileName = 'bulma-css-vars.config.cjs' const mainSassFileName = 'src/main.scss' const configFilePathAtCwd = (cwd: string) => path.join(cwd, configFileName) +const cjsConfigFilePathAtCwd = (cwd: string) => path.join(cwd, cjsConfigFileName) const mainSassFilePathAtCwd = (cwd: string) => path.join(cwd, mainSassFileName) async function validateOptions(cwd: string) { const configFilePath = configFilePathAtCwd(cwd) + const cjsConfigFilePath = cjsConfigFilePathAtCwd(cwd) let loadedOptions = {} - try { - loadedOptions = require(configFilePath) - } catch (err) { + + if (existsSync(cjsConfigFilePath)) { + try { + loadedOptions = await import(cjsConfigFilePath).then(m => m.default || m) + } catch (err) { + throw new Error( + `Unable to parse configuration file at '${cjsConfigFilePath}': ${err}` + ) + } + } else if (existsSync(configFilePath)) { + try { + loadedOptions = await import(configFilePath).then(m => m.default || m) + } catch (err) { + throw new Error( + `Unable to parse cjs configuration file at '${configFilePath}': ${err}` + ) + } + } else { throw new Error( `Required config file '${configFileName}' was not found at ${configFilePath}` ) } + const options: BulmaCssVarsOptions = { ...defaultOptions, ...loadedOptions, diff --git a/lib/tsconfig-esm.json b/lib/tsconfig-esm.json index bc41dbc..f99918a 100644 --- a/lib/tsconfig-esm.json +++ b/lib/tsconfig-esm.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "module": "es2015", + "module": "es2020", "outDir": "./dist/esm" } } diff --git a/lib/tsconfig.json b/lib/tsconfig.json index 5cacb85..0ac3e44 100644 --- a/lib/tsconfig.json +++ b/lib/tsconfig.json @@ -9,7 +9,8 @@ "target": "es2015", "noImplicitAny": true, "lib": ["es2019", "dom"], - "downlevelIteration": true + "downlevelIteration": true, + "skipLibCheck": true }, "include": ["./src/**/*"], "exclude": ["node_modules", "dist", "**/__mocks__/**/*", "./src/**/*.test.ts"]