-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support loading esm and ts files from json config (#254)
* feat: support loading esm and ts files from .jsonrc * restore existing logic * add types for lilconfig options * simplify
- Loading branch information
Showing
12 changed files
with
145 additions
and
62 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,39 @@ | ||
// eslint-disable-next-line n/no-deprecated-api | ||
const { createRequire, createRequireFromPath } = require('node:module') | ||
// @ts-check | ||
const { createRequire } = require('node:module') | ||
|
||
function req (name, rootFile) { | ||
let create = createRequire || createRequireFromPath | ||
let require = create(rootFile) | ||
return require(name) | ||
const TS_EXT_RE = /\.(c|m)?ts$/ | ||
|
||
/** @type {import('jiti').default | null} */ | ||
let jiti = null | ||
|
||
/** | ||
* @param {string} name | ||
* @param {string} rootFile | ||
* @returns {Promise<any>} | ||
*/ | ||
async function req(name, rootFile = __filename) { | ||
let __require = createRequire(rootFile) | ||
let url = __require.resolve(name) | ||
|
||
try { | ||
return (await import(url)).default | ||
} catch (err) { | ||
if (!TS_EXT_RE.test(url)) { | ||
/* c8 ignore start */ | ||
throw err | ||
} | ||
if (!jiti) { | ||
try { | ||
jiti = (await import('jiti')).default | ||
} catch (jitiErr) { | ||
throw new Error( | ||
`'jiti' is required for the TypeScript configuration files. Make sure it is installed\nError: ${jitiErr.message}` | ||
) | ||
} | ||
/* c8 ignore stop */ | ||
} | ||
return jiti(rootFile, { interopDefault: true })(name) | ||
} | ||
} | ||
|
||
module.exports = req |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"plugins": { | ||
"./postcss/1.js": {}, | ||
"./postcss/2.cjs": {}, | ||
"./postcss/3.mjs": {}, | ||
"./postcss/4.ts": {}, | ||
"./postcss/5.cts": {}, | ||
"./postcss/6.mts": {} | ||
} | ||
} |
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,8 @@ | ||
module.exports = function plugin() { | ||
return { | ||
postcssPlugin: '1' | ||
} | ||
} | ||
|
||
module.exports.postcss = true | ||
module.exports.path = __filename |
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,8 @@ | ||
module.exports = function plugin() { | ||
return { | ||
postcssPlugin: '2' | ||
} | ||
} | ||
|
||
module.exports.postcss = true | ||
module.exports.path = __filename |
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,9 @@ | ||
let plugin = () => { | ||
return { | ||
postcssPlugin: '3' | ||
} | ||
} | ||
plugin.postcss = true | ||
plugin.path = import.meta.url | ||
|
||
export default plugin |
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,9 @@ | ||
let plugin: any = () => { | ||
return { | ||
postcssPlugin: '4' | ||
} | ||
} | ||
plugin.postcss = true | ||
plugin.path = __filename | ||
|
||
export = plugin |
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,9 @@ | ||
let plugin: any = () => { | ||
return { | ||
postcssPlugin: '5' | ||
} | ||
} | ||
plugin.postcss = true | ||
plugin.path = __filename | ||
|
||
export = plugin |
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,10 @@ | ||
let plugin: any = () => { | ||
return { | ||
postcssPlugin: '6' | ||
} | ||
} | ||
plugin.postcss = true | ||
// @ts-ignore | ||
plugin.path = import.meta.url | ||
|
||
export default plugin |