Skip to content

Commit

Permalink
Move logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 13, 2022
1 parent e3eccff commit a318961
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
31 changes: 31 additions & 0 deletions src/config/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createRequire } from 'module'

// Resolve Node module ids 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)
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)
return `${scope}${modulePrefix}-${scopedName}`
}

const getModuleScope = function (id) {
if (!id.startsWith('@')) {
return ['', id]
}

const slashIndex = id.indexOf('/')

if (slashIndex === -1) {
return ['', id]
}

return [id.slice(0, slashIndex + 1), id.slice(slashIndex + 1)]
}
31 changes: 2 additions & 29 deletions src/config/plugin/lib/id.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRequire } from 'module'
import { isAbsolute } from 'path'

import { wrapError } from '../../../error/wrap.js'
import { resolveModuleName } from '../../module.js'

// `pluginConfig.id` can be:
// - The direct value
Expand Down Expand Up @@ -37,14 +37,9 @@ export const isInlineId = function (id) {
return typeof id !== 'string'
}

// Resolve Node module ids to absolute file paths.
// We enforce a npm package naming convention for all plugins.
// TODO: use import.meta.resolve() when available
export const resolveModuleId = function ({ id, modulePrefix, builtins, cwd }) {
const moduleName = getModuleName(id, modulePrefix)

try {
return createRequire(cwd).resolve(moduleName)
return resolveModuleName(id, modulePrefix, cwd)
} catch (error) {
throw wrapError(
error,
Expand All @@ -54,28 +49,6 @@ This Node module was not found, please ensure it is installed.\n`,
}
}

// 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)
return `${scope}${modulePrefix}-${scopedName}`
}

const getModuleScope = function (id) {
if (!id.startsWith('@')) {
return ['', id]
}

const slashIndex = id.indexOf('/')

if (slashIndex === -1) {
return ['', id]
}

return [id.slice(0, slashIndex + 1), id.slice(slashIndex + 1)]
}

const getBuiltinsError = function (builtins) {
const builtinIds = Object.keys(builtins)
return builtinIds.length === 0
Expand Down

0 comments on commit a318961

Please sign in to comment.