Skip to content

Commit

Permalink
Update load-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 2, 2023
1 parent 12356af commit 2389f36
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
30 changes: 17 additions & 13 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

import assert from 'node:assert/strict'
import path from 'node:path'
import {pathToFileURL} from 'node:url'
import {fileURLToPath, pathToFileURL} from 'node:url'
import structuredClone from '@ungap/structured-clone'
import createDebug from 'debug'
import isPlainObj from 'is-plain-obj'
Expand Down Expand Up @@ -421,11 +421,11 @@ async function merge(target, raw, options) {
*/
async function addModule(id, value) {
/** @type {string} */
let fp
let fileUrl

try {
fp = await resolvePlugin(id, {
cwd: options.root,
fileUrl = await resolvePlugin(id, {
from: pathToFileURL(options.root) + '/',
prefix: options.prefix
})
} catch (error) {
Expand All @@ -435,7 +435,7 @@ async function merge(target, raw, options) {
return
}

const result = await loadFromAbsolutePath(fp, options.root)
const result = await loadFromAbsolutePath(fileUrl, options.root)

try {
if (typeof result === 'function') {
Expand All @@ -445,14 +445,17 @@ async function merge(target, raw, options) {
} else {
// Assume preset.
const preset = /** @type {Preset} */ (result)
await merge(target, preset, {...options, root: path.dirname(fp)})
await merge(target, preset, {
...options,
root: path.dirname(fileURLToPath(fileUrl))
})
}
} catch (error) {
throw new Error(
'Expected preset or plugin, not `' +
result +
'`, at `' +
path.relative(options.root, fp) +
path.relative(options.root, fileURLToPath(fileUrl)) +
'`',
{cause: error}
)
Expand Down Expand Up @@ -518,17 +521,17 @@ function find(entries, plugin) {
}

/**
* @param {string} fp
* @param {string} fileUrl
* Specifier.
* @param {string} base
* Base.
* @returns {Promise<unknown>}
* Result.
*/
async function loadFromAbsolutePath(fp, base) {
async function loadFromAbsolutePath(fileUrl, base) {
try {
/** @type {ImportResult} */
const result = await import(pathToFileURL(fp).href)
const result = await import(fileUrl)

if (!('default' in result)) {
throw new Error(
Expand All @@ -538,8 +541,9 @@ async function loadFromAbsolutePath(fp, base) {

return result.default
} catch (error) {
throw new Error('Cannot import `' + path.relative(base, fp) + '`', {
cause: error
})
throw new Error(
'Cannot import `' + path.relative(base, fileURLToPath(fileUrl)) + '`',
{cause: error}
)
}
}
3 changes: 2 additions & 1 deletion lib/file-set-pipeline/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @typedef {import('../index.js').VFileReporter} VFileReporter
*/

import {pathToFileURL} from 'node:url'
import {loadPlugin} from 'load-plugin'
import {reporter} from 'vfile-reporter'

Expand Down Expand Up @@ -35,7 +36,7 @@ export async function log(context, settings) {
// Assume a valid reporter.
const result = /** @type {VFileReporter} */ (
await loadPlugin(settings.reporter, {
cwd: settings.cwd,
from: pathToFileURL(settings.cwd) + '/',
prefix: 'vfile-reporter'
})
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ignore": "^5.0.0",
"is-empty": "^1.0.0",
"is-plain-obj": "^4.0.0",
"load-plugin": "^5.0.0",
"load-plugin": "^6.0.0",
"parse-json": "^7.0.0",
"trough": "^2.0.0",
"unist-util-inspect": "^8.0.0",
Expand Down

0 comments on commit 2389f36

Please sign in to comment.