-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add basic support for esm (#914)
* try load config as cjs and esm files * use new async function * fix tests * run tests in node, not in jsdom environment * Apply suggestions from code review Co-authored-by: Haroen Viaene <[email protected]> * add interop default, for mjs config * fix lint error Co-authored-by: Haroen Viaene <[email protected]> Co-authored-by: shipjs <[email protected]>
- Loading branch information
1 parent
06790d1
commit 42918d5
Showing
11 changed files
with
56 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = { | ||
presets: ['@babel/preset-env'], | ||
presets: [['@babel/preset-env', { targets: { node: 'current' } }]], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/shipjs-lib/src/lib/config/__tests__/cjs/ship.config.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
versionUpdated() {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
import dotenv from 'dotenv'; | ||
import { resolve } from 'path'; | ||
import { existsSync } from 'fs'; | ||
import { promises as fs, constants } from 'fs'; | ||
|
||
import defaultConfig from './defaultConfig'; | ||
import mergeConfig from './mergeConfig'; | ||
dotenv.config(); | ||
|
||
export default function loadConfig(dir = '.', filename = 'ship.config.js') { | ||
const fullPath = resolve(dir, filename); | ||
const userConfig = existsSync(fullPath) ? require(fullPath) : {}; | ||
const exist = (path) => | ||
fs | ||
.access(path, constants.F_OK) | ||
.then(() => path) | ||
.catch(() => false); | ||
|
||
const pickDefault = (obj) => | ||
typeof obj.default === 'object' ? obj.default : obj; | ||
|
||
export default async function loadConfig( | ||
dir = '.', | ||
filename = 'ship.config', | ||
extensions = ['js', 'cjs', 'mjs'] | ||
) { | ||
const fullPaths = extensions.map((ext) => resolve(dir, `${filename}.${ext}`)); | ||
|
||
const fullPath = ( | ||
await Promise.all(fullPaths.map((path) => exist(path))) | ||
).find((path) => path); | ||
|
||
const userConfig = | ||
fullPath && (await exist(fullPath)) | ||
? await import(fullPath).then(pickDefault) | ||
: {}; | ||
|
||
return mergeConfig(defaultConfig, userConfig); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { readFileSync } from 'fs'; | ||
import { promises as fs } from 'fs'; | ||
import { resolve } from 'path'; | ||
import loadConfig from '../config/loadConfig'; | ||
|
||
export default function getAppName(dir = '.') { | ||
const { appName } = loadConfig(dir); | ||
export default async function getAppName(dir = '.') { | ||
const { appName } = await loadConfig(dir); | ||
if (appName) { | ||
return appName; | ||
} | ||
const { name } = JSON.parse(readFileSync(resolve(dir, 'package.json'))); | ||
const { name } = JSON.parse(await fs.readFile(resolve(dir, 'package.json'))); | ||
return name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,4 @@ module.exports = { | |
}, | ||
], | ||
], | ||
plugins: [ | ||
[ | ||
'@babel/plugin-transform-runtime', | ||
{ | ||
regenerator: true, | ||
}, | ||
], | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters