Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 6, 2022
1 parent d16bbe2 commit 4af1ebd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/config/load/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPluginPath } from '../plugin/load.js'
import { getPluginPath } from '../plugin/import.js'
import { CONFIG_PLUGIN_TYPE } from '../plugin/types.js'

// The `config` can be:
Expand Down
74 changes: 74 additions & 0 deletions src/config/plugin/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { createRequire } from 'module'

import { PluginError, UserError } from '../../error/main.js'
import { wrapError } from '../../error/wrap.js'
import { PLUGINS_IMPORT_BASE } from '../normalize/cwd.js'

import { getModuleId } from './id.js'

// Builtin modules are lazy loaded for performance reasons.
// The return value is shallow merged to make it a plain object instead of
// a dynamic `Module` instance.
export const importPlugin = async function ({
id,
type,
selectProp,
modulePrefix,
builtins,
isCombinationDimension,
}) {
const moduleId = getModuleId(id, type, isCombinationDimension)
const builtin = builtins[moduleId]

if (builtin !== undefined) {
return await builtin()
}

const moduleName = `${modulePrefix}${moduleId}`
const pluginPath = safeGetPluginPath({
moduleName,
type,
selectProp,
base: PLUGINS_IMPORT_BASE,
})

try {
return await import(pluginPath)
} catch (error) {
throw wrapError(
error,
`Could not load "${type}" module "${moduleId}"\n\n`,
PluginError,
)
}
}

// Find the local file path of a plugin.
// We enforce a naming convention for all plugins.
// All plugins are Node modules.
// We do not allow npm @scope because:
// - This is simpler for users
// - This prevent the confusion (which could be malicious) created by the
// ambiguity
// TODO: use import.meta.resolve() when available
const safeGetPluginPath = function ({ moduleName, type, selectProp, base }) {
try {
return getPluginPath(moduleName, type, base)
} catch (error) {
throw wrapError(error, `Configuration property "${selectProp}"`, UserError)
}
}

export const getPluginPath = function (moduleName, type, base) {
const { resolve } = createRequire(new URL(base, import.meta.url))

try {
return resolve(moduleName)
} catch (error) {
throw wrapError(
error,
`must be a valid package name: "${moduleName}".
This Node module was not found, please ensure it is installed.\n\n`,
)
}
}
75 changes: 1 addition & 74 deletions src/config/plugin/load.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { createRequire } from 'module'

import { PluginError, UserError } from '../../error/main.js'
import { wrapError } from '../../error/wrap.js'
import { PLUGINS_IMPORT_BASE } from '../normalize/cwd.js'

import { getPluginConfig } from './config.js'
import { getModuleId } from './id.js'
import { importPlugin } from './import.js'
import { normalizePlugin } from './normalize.js'

// Import plugin's code
Expand Down Expand Up @@ -65,70 +59,3 @@ const loadPlugin = async function (
})
return { ...pluginC, config: pluginConfig }
}

// Builtin modules are lazy loaded for performance reasons.
// The return value is shallow merged to make it a plain object instead of
// a dynamic `Module` instance.
const importPlugin = async function ({
id,
type,
selectProp,
modulePrefix,
builtins,
isCombinationDimension,
}) {
const moduleId = getModuleId(id, type, isCombinationDimension)
const builtin = builtins[moduleId]

if (builtin !== undefined) {
return await builtin()
}

const moduleName = `${modulePrefix}${moduleId}`
const pluginPath = safeGetPluginPath({
moduleName,
type,
selectProp,
base: PLUGINS_IMPORT_BASE,
})

try {
return await import(pluginPath)
} catch (error) {
throw wrapError(
error,
`Could not load "${type}" module "${moduleId}"\n\n`,
PluginError,
)
}
}

// Find the local file path of a plugin.
// We enforce a naming convention for all plugins.
// All plugins are Node modules.
// We do not allow npm @scope because:
// - This is simpler for users
// - This prevent the confusion (which could be malicious) created by the
// ambiguity
// TODO: use import.meta.resolve() when available
const safeGetPluginPath = function ({ moduleName, type, selectProp, base }) {
try {
return getPluginPath(moduleName, type, base)
} catch (error) {
throw wrapError(error, `Configuration property "${selectProp}"`, UserError)
}
}

export const getPluginPath = function (moduleName, type, base) {
const { resolve } = createRequire(new URL(base, import.meta.url))

try {
return resolve(moduleName)
} catch (error) {
throw wrapError(
error,
`must be a valid package name: "${moduleName}".
This Node module was not found, please ensure it is installed.\n\n`,
)
}
}

0 comments on commit 4af1ebd

Please sign in to comment.