-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: making optional peer dependencies actually optional
@vue/server-renderer and @vue/compiler-dom were both required in the sense that they will throw regardless of whether or not your are using functionality from those libraries if they weren't installed. I don't believe this was intentional so I have made them optional and they will now only throw if you try to use them and the package isn't available.
- Loading branch information
1 parent
f0001de
commit 4212a60
Showing
4 changed files
with
33 additions
and
2 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 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,23 @@ | ||
import { createRequire } from 'module' | ||
|
||
// @ts-ignore - cjs build will complain about import.meta not being allowed, but its not actually used as require will be defined there | ||
const require = globalThis.require ? globalThis.require : createRequire(import.meta.url); | ||
|
||
type StringLiteral<T> = T extends string ? string extends T ? never : T : never; | ||
export function requireOptional<T extends string>(packageName: StringLiteral<T>): unknown { | ||
try { | ||
return require(packageName); | ||
} catch { | ||
throw new Error( | ||
`The optional peer-dependency ${packageName} is needed for this test and was not found. Please ensure that it has been installed as a dev-dependency and retry.` | ||
); | ||
} | ||
} | ||
|
||
export function requireVueCompilerDom() { | ||
return requireOptional('@vue/compiler-dom') as typeof import('@vue/compiler-dom'); | ||
} | ||
|
||
export function requireVueServerRenderer() { | ||
return requireOptional('@vue/server-renderer') as typeof import('@vue/server-renderer'); | ||
} |