Skip to content

Commit 6a669dc

Browse files
fix(cli): allow to load cjs file, and force cjs for cli for node 18+ (wtho#426)
1 parent 21cd1ea commit 6a669dc

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

lib/bin/bulma-css-vars.js renamed to lib/bin/bulma-css-vars.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
const { runCli, runCliInit } = require('../dist/cli.js')
2+
const { runCli, runCliInit } = require('../dist/cjs/cli.js')
33

44
const cwd = process.cwd()
55

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"/*.sass"
2828
],
2929
"bin": {
30-
"bulma-css-vars": "bin/bulma-css-vars.js"
30+
"bulma-css-vars": "bin/bulma-css-vars.cjs"
3131
},
3232
"scripts": {
3333
"build": "tsc -p tsconfig.json && tsc -p tsconfig-esm.json && webpack",

lib/src/cli.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as path from 'path'
2+
import { existsSync } from 'fs'
23
import { defaultOptions } from './default-options'
34
import { BulmaCssVarsOptions, ColorCallSet } from './types'
45
import { getUsedVariables } from './find-used-vars'
@@ -14,22 +15,41 @@ import { getCssFallbacks } from './css-post-processor'
1415
import { compileSass } from './compile-sass'
1516

1617
const configFileName = 'bulma-css-vars.config.js'
18+
const cjsConfigFileName = 'bulma-css-vars.config.cjs'
1719
const mainSassFileName = 'src/main.scss'
1820

1921
const configFilePathAtCwd = (cwd: string) => path.join(cwd, configFileName)
22+
const cjsConfigFilePathAtCwd = (cwd: string) => path.join(cwd, cjsConfigFileName)
2023
const mainSassFilePathAtCwd = (cwd: string) => path.join(cwd, mainSassFileName)
2124

2225
async 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,

lib/tsconfig-esm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"module": "es2015",
4+
"module": "es2020",
55
"outDir": "./dist/esm"
66
}
77
}

lib/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"target": "es2015",
1010
"noImplicitAny": true,
1111
"lib": ["es2019", "dom"],
12-
"downlevelIteration": true
12+
"downlevelIteration": true,
13+
"skipLibCheck": true
1314
},
1415
"include": ["./src/**/*"],
1516
"exclude": ["node_modules", "dist", "**/__mocks__/**/*", "./src/**/*.test.ts"]

0 commit comments

Comments
 (0)