-
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
9 changed files
with
242 additions
and
205 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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { isAbsolute } from 'path' | ||
|
||
// `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 getLocationInfo = function ( | ||
pluginConfig, | ||
{ pluginProp, builtins }, | ||
) { | ||
const originalLocation = pluginConfig[pluginProp] | ||
const locationType = getLocationType(originalLocation, builtins) | ||
return { originalLocation, locationType } | ||
} | ||
|
||
const getLocationType = function (originalLocation, builtins) { | ||
if (typeof originalLocation !== 'string') { | ||
return 'inline' | ||
} | ||
|
||
if (builtins[originalLocation] !== undefined) { | ||
return 'builtin' | ||
} | ||
|
||
if (isPathLocation(originalLocation)) { | ||
return 'path' | ||
} | ||
|
||
return 'module' | ||
} | ||
|
||
const isPathLocation = function (originalLocation) { | ||
return originalLocation.startsWith('.') || isAbsolute(originalLocation) | ||
} |
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,82 @@ | ||
import { validateObjectOrString } from '../../normalize/validate/complex.js' | ||
import { | ||
validateFileExists, | ||
validateRegularFile, | ||
} from '../../normalize/validate/fs.js' | ||
import { validateDefinedString } from '../../normalize/validate/simple.js' | ||
|
||
import { CoreError, ConsumerError } from './error.js' | ||
import { resolveModuleLocation } from './module.js' | ||
import { safeNormalizeConfig } from './normalize.js' | ||
|
||
// Normalize a single `pluginConfig` | ||
export const normalizeLocation = async function ( | ||
pluginConfig, | ||
locationType, | ||
{ name, cwd, pluginProp, builtins, modulePrefix }, | ||
) { | ||
const locationDefinitions = getLocationDefinitions(locationType, pluginProp) | ||
const { [pluginProp]: location, ...pluginConfigA } = | ||
await safeNormalizeConfig(pluginConfig, locationDefinitions, { | ||
context: { locationType, builtins, modulePrefix }, | ||
cwd, | ||
prefix: `${name}.`, | ||
UserErrorType: ConsumerError, | ||
SystemErrorType: CoreError, | ||
}) | ||
return { pluginConfig: pluginConfigA, location } | ||
} | ||
|
||
const getLocationDefinitions = function (locationType, pluginProp) { | ||
return [ | ||
{ name: pluginProp, ...normalizeLocationProp }, | ||
{ name: pluginProp, ...NORMALIZE_LOCATIONS[locationType] }, | ||
] | ||
} | ||
|
||
export const getExampleLocation = function (value, { context: { builtins } }) { | ||
const builtinNames = Object.keys(builtins) | ||
return builtinNames.length !== 0 && builtinNames[0].trim() !== '' | ||
? builtinNames[0] | ||
: undefined | ||
} | ||
|
||
const normalizeLocationProp = { | ||
required: true, | ||
example: getExampleLocation, | ||
} | ||
|
||
const normalizeInlineLocation = { | ||
validate: validateObjectOrString, | ||
} | ||
|
||
const normalizeBuiltinLocation = {} | ||
|
||
const normalizePathLocation = { | ||
path: true, | ||
async validate(location) { | ||
await validateFileExists(location) | ||
await validateRegularFile(location) | ||
}, | ||
} | ||
|
||
const normalizeModuleLocation = { | ||
validate(value, { context: { modulePrefix } }) { | ||
validateDefinedString(value) | ||
validateHasModulePrefix(modulePrefix) | ||
}, | ||
transform: resolveModuleLocation, | ||
} | ||
|
||
const validateHasModulePrefix = function (modulePrefix) { | ||
if (modulePrefix === undefined) { | ||
throw new Error('must start with . or / when it is a file path.') | ||
} | ||
} | ||
|
||
const NORMALIZE_LOCATIONS = { | ||
inline: normalizeInlineLocation, | ||
builtin: normalizeBuiltinLocation, | ||
path: normalizePathLocation, | ||
module: normalizeModuleLocation, | ||
} |
Oops, something went wrong.