enforcePolicy = (specifier, referrerResourceId, wrappedRequire) => {
  if (typeof specifier === 'undefined') {
    throw Error(`Requested specifier is undefined`)
  }
  // skip enforcing what we determined at build time we cannot
  if (
    LAVAMOAT.unenforceable.includes(specifier) ||
    // implicitly allow all for root
    referrerResourceId === LAVAMOAT.root
  ) {
    return wrappedRequire()
  }

  const referrerPolicy = LAVAMOAT.policy.resources[referrerResourceId] || {}
  if (referrerPolicy.builtin && LAVAMOAT.externals[specifier]) {
    const builtinName = LAVAMOAT.externals[specifier]
    if (referrerPolicy.builtin[builtinName]) {
      return wrappedRequire()
    }
    if (
      builtinName &&
      !builtinName.includes('.') &&
      keys(referrerPolicy.builtin).some((key) =>
        key.startsWith(`${builtinName}.`)
      )
    ) {
      // create minimal selection if it's a builtin and not allowed as a whole, but with subpaths
      return getBuiltinForConfig(
        wrappedRequire(),
        builtinName,
        referrerPolicy.builtin
      )
    }
  }
  const requestedResourceId = findResourceId(specifier)
  if (!requestedResourceId) {
    if (LAVAMOAT.ctxm.includes(specifier)) {
      throw Error(
        `'${referrerResourceId}' is trying to load a module dynamically from a dependency it has not been allowed using the following Webpack Context Module: '${specifier}'`
      )
    }
    throw Error(
      `Requested specifier '${specifier}' is not allowed as a builtin and is not a known dependency of '${referrerResourceId}'; regenerate policy or add it to policy-override.json.`
    )
  }
  // allow imports internal to the package
  if (requestedResourceId === referrerResourceId) {
    return wrappedRequire()
  }
  if (referrerPolicy.packages?.[requestedResourceId]) {
    return wrappedRequire()
  }

  // This error message does not refer to specifier directly so it won't be confusing for a ContextModule either.
  throw Error(
    `Policy does not allow importing ${requestedResourceId} from ${referrerResourceId}`
  )
}
Enforces the policy for resource imports.

@template {object} T
@param {string} specifier - The ID of the requested resource.
@param {string} referrerResourceId - The ID of the referrer resource.
@param {() => T} wrappedRequire - The wrapped **webpack_require** function.
@returns {Partial<T>} The result of the wrapped **webpack_require** function.
@throws {Error} Throws an error if the policy does not allow importing the
  requested resource from the referrer resource.