-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
141 additions
and
119 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { isAbsolute } from 'path' | ||
|
||
import { wrapError } from '../../../error/wrap.js' | ||
|
||
import { resolveModuleName } from './module.js' | ||
|
||
// `pluginConfig[pluginProp]` can be: | ||
// - The direct value | ||
// - This is useful for programmatic usage, | ||
// - For example, by exposing to plugin consumers a function like: | ||
// (pluginConfig) => ({ plugin, pluginConfig }) | ||
// which is passed as argument to this library | ||
// - A builtin identifier among `opts.builtins` | ||
// - A file path starting with . or / | ||
// - A Node module prefixed with `modulePrefix` (which is optional) | ||
export const isModuleLocation = function (location, modulePrefix, builtins) { | ||
return ( | ||
modulePrefix !== undefined && | ||
!isInlineLocation(location) && | ||
!isBuiltinLocation(location, builtins) && | ||
!isPathLocation(location, builtins) | ||
) | ||
} | ||
|
||
export const isPathLocation = function (location, builtins) { | ||
return ( | ||
!isInlineLocation(location) && | ||
!isBuiltinLocation(location, builtins) && | ||
(location.startsWith('.') || isAbsolute(location)) | ||
) | ||
} | ||
|
||
export const isBuiltinLocation = function (location, builtins) { | ||
return builtins[location] !== undefined | ||
} | ||
|
||
export const isInlineLocation = function (location) { | ||
return typeof location !== 'string' | ||
} | ||
|
||
export const resolveModuleLocation = function ({ | ||
location, | ||
modulePrefix, | ||
builtins, | ||
cwd, | ||
}) { | ||
try { | ||
return resolveModuleName(location, modulePrefix, cwd) | ||
} catch (error) { | ||
throw wrapError( | ||
error, | ||
`must be ${getBuiltinsError(builtins)} | ||
This Node module was not found, please ensure it is installed.\n`, | ||
) | ||
} | ||
} | ||
|
||
const getBuiltinsError = function (builtins) { | ||
const builtinNames = Object.keys(builtins) | ||
return builtinNames.length === 0 | ||
? 'a valid package name.' | ||
: `either: | ||
- a valid package name | ||
- a builtin plugin among: ${builtinNames.join(', ')}` | ||
} |
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,31 +1,31 @@ | ||
import { createRequire } from 'module' | ||
|
||
// Resolve Node module ids to absolute file paths. | ||
// Resolve Node module names to absolute file paths. | ||
// We enforce a npm package naming convention for all plugins. | ||
// TODO: use import.meta.resolve() when available | ||
export const resolveModuleName = function (id, modulePrefix, cwd) { | ||
const moduleName = getModuleName(id, modulePrefix) | ||
export const resolveModuleName = function (name, modulePrefix, cwd) { | ||
const moduleName = getModuleName(name, modulePrefix) | ||
return createRequire(`${cwd}/`).resolve(moduleName) | ||
} | ||
|
||
// We allow module names to be either: | ||
// - id -> {modulePrefix}-{id} | ||
// - @scope/id -> @scope/{modulePrefix}-{id} | ||
const getModuleName = function (id, modulePrefix) { | ||
const [scope, scopedName] = getModuleScope(id) | ||
// - name -> {modulePrefix}-{name} | ||
// - @scope/name -> @scope/{modulePrefix}-{name} | ||
const getModuleName = function (name, modulePrefix) { | ||
const [scope, scopedName] = getModuleScope(name) | ||
return `${scope}${modulePrefix}-${scopedName}` | ||
} | ||
|
||
const getModuleScope = function (id) { | ||
if (!id.startsWith('@')) { | ||
return ['', id] | ||
const getModuleScope = function (name) { | ||
if (!name.startsWith('@')) { | ||
return ['', name] | ||
} | ||
|
||
const slashIndex = id.indexOf('/') | ||
const slashIndex = name.indexOf('/') | ||
|
||
if (slashIndex === -1) { | ||
return ['', id] | ||
return ['', name] | ||
} | ||
|
||
return [id.slice(0, slashIndex + 1), id.slice(slashIndex + 1)] | ||
return [name.slice(0, slashIndex + 1), name.slice(slashIndex + 1)] | ||
} |