Skip to content

Commit

Permalink
feat(plugin): 支持自定义域名 (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: chenqun1 <[email protected]>
  • Loading branch information
blacksunset and chenqun1 authored Jul 21, 2021
1 parent c1480a7 commit fd6f58e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/plugin/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path')

module.exports = {
rootDir: path.resolve(__dirname, './'),
transform: {
'^.+\\.[t|j]sx?$': 'ts-jest',
},
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node'
}
7 changes: 6 additions & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"debug": "^4.3.1"
},
"devDependencies": {
"rimraf": "^3.0.2"
"@types/jest": "^26.0.24",
"jest": "^27.0.6",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.3",
"typescript": "^4.3.5",
"vite": "^2.4.3"
}
}
38 changes: 38 additions & 0 deletions packages/plugin/src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import ViteCertPlugin from '../index'
import Mkcert from '../mkcert/index'


const install = jest.fn()

jest.mock('../mkcert/index', () => {
return class {
static create() {
return new this()
}
install(...args: any[]) {
install(...args)
}
public async init() {}
}
})

describe('custom hostnames', () => {
test('should install with custom hostnames', async () => {

const plugin = ViteCertPlugin({
hostnames: ['www.test.com']
})

await (plugin as any).config({
server: {
https: true
}
})

expect(install).toHaveBeenCalledTimes(1)

const args = install.mock.calls[0][0]
expect(JSON.stringify(args)).toMatch('www.test.com')
// expect((args as string[]).includes('www.test.com')).toBeTruthy()
})
})
3 changes: 2 additions & 1 deletion packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const plugin = (options?: ViteCertificateOptions): Plugin => {

await mkcert.init()

const certificate = await mkcert.install(['localhost', ...ips])
const hostnames = Array.from(new Set(['localhost', ...ips, ...(options?.hostnames || [])]))
const certificate = await mkcert.install(hostnames)

return {
server: {
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin/src/mkcert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export type MkcertOptions = {
* @default none
*/
mkcertPath?: string

/**
* Custom hostnames
*
* @default []
*/
hostnames?: string[]
}

export type MkcertProps = MkcertOptions & {
Expand Down

0 comments on commit fd6f58e

Please sign in to comment.