Skip to content

Commit

Permalink
fix: making optional peer dependencies actually optional
Browse files Browse the repository at this point in the history
@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
thebanjomatic committed Sep 25, 2023
1 parent f0001de commit 4212a60
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@
"vuex": "4.1.0"
},
"peerDependencies": {
"@vue/compiler-dom": "^3.0.1",
"@vue/server-renderer": "^3.0.1",
"vue": "^3.0.1"
},
"peerDependenciesMeta": {
"@vue/compiler-dom": {
"optional": true
},
"@vue/server-renderer": {
"optional": true
}
Expand Down
4 changes: 3 additions & 1 deletion src/renderToString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { renderToString as baseRenderToString } from '@vue/server-renderer'
import { DefineComponent } from 'vue'
import { createInstance } from './createInstance'
import { ComponentMountingOptions } from './mount'
import { RenderMountingOptions } from './types'
import { requireVueServerRenderer } from './utils/requireOptionalPeer'

export function renderToString<
T,
Expand All @@ -22,6 +22,8 @@ export function renderToString<
): Promise<string>

export function renderToString(component: any, options?: any): Promise<string> {
const {renderToString: baseRenderToString} = requireVueServerRenderer()

if (options?.attachTo) {
console.warn('attachTo option is not available for renderToString')
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/compileSlots.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { compile } from '@vue/compiler-dom'
import * as vue from 'vue'
import { requireVueCompilerDom } from './requireOptionalPeer'

export function processSlot(source = '', Vue = vue) {
const {compile} = requireVueCompilerDom();

let template = source.trim()
const hasWrappingTemplate = template && template.startsWith('<template')

Expand Down
23 changes: 23 additions & 0 deletions src/utils/requireOptionalPeer.ts
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');
}

0 comments on commit 4212a60

Please sign in to comment.