Skip to content

Commit

Permalink
feat: validate options
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Mar 31, 2023
1 parent 1105a73 commit e2d074f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
38 changes: 37 additions & 1 deletion __test__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import path from 'path'
import { describe, expect, it, test } from 'vitest'
import { eq, extractHashFromFileName, getContentHash, isPublicTypescript, linebreak, setEol } from '../src/utils'
import {
eq,
extractHashFromFileName,
getContentHash,
isPublicTypescript,
linebreak,
setEol,
validateOptions,
} from '../src/utils'
import { getGlobalConfig, setGlobalConfig } from '../src/utils/globalConfig'

describe('vite-plugin-public-typescript', () => {
Expand Down Expand Up @@ -78,4 +86,32 @@ describe('vite-plugin-public-typescript', () => {
expect(hash1).toBe('1234')
expect(hash2).toBe('1234')
})

test('should validate options success', () => {
const opts = {
inputDir: 'publicTypescript',
outputDir: '/',
manifestName: 'manifest',
hash: true,
ssrBuild: false,
esbuildOptions: {},
sideEffects: false,
}

expect(() => validateOptions(opts)).not.toThrowError()
})

test('should validate options throw error', () => {
const opts = {
inputDir: '/publicTypescript/foo',
outputDir: 'js',
manifestName: 'manifest',
hash: true,
ssrBuild: false,
esbuildOptions: {},
sideEffects: false,
}

expect(() => validateOptions(opts)).toThrowError('dir')
})
})
1 change: 0 additions & 1 deletion playground/spa/public/js/haha.bdaaba63.js

This file was deleted.

1 change: 0 additions & 1 deletion playground/spa/public/js/index.93041bfa.js

This file was deleted.

1 change: 0 additions & 1 deletion playground/spa/public/js/test.b7d6e86f.js

This file was deleted.

6 changes: 3 additions & 3 deletions playground/spa/publicTypescript/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"haha": "js/haha.bdaaba63.js",
"index": "js/index.93041bfa.js",
"test": "js/test.b7d6e86f.js"
"haha": "/haha.bdaaba63.js",
"index": "/index.93041bfa.js",
"test": "/test.b7d6e86f.js"
}
2 changes: 1 addition & 1 deletion playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
inputDir: 'publicTypescript',
manifestName: 'manifest',
hash: true,
outputDir: 'js',
outputDir: '/',
}),

{
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import glob from 'tiny-glob'
import type { BuildOptions } from 'esbuild'
import Watcher from 'watcher'
import fs from 'fs-extra'
import { TS_EXT, debug, eq, isEmptyObject, isPublicTypescript, reloadPage } from './utils'
import { TS_EXT, debug, eq, isEmptyObject, isPublicTypescript, reloadPage, validateOptions } from './utils'
import { build, deleteOldJsFile, esbuildTypescript } from './utils/build'
import { ManifestCache } from './utils/manifestCache'
import { getGlobalConfig, setGlobalConfig } from './utils/globalConfig'
Expand All @@ -25,6 +25,7 @@ export interface VPPTPluginOptions {
inputDir?: string
/**
* @description output public javascript dir, relative to `publicDir`
* @note outputDir should start with '/'
* @default '/'
*/
outputDir?: string
Expand Down Expand Up @@ -75,6 +76,8 @@ export function publicTypescript(options: VPPTPluginOptions = {}) {
...options,
}

validateOptions(opts)

debug('options:', opts)

let config: ResolvedConfig
Expand Down
24 changes: 24 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,27 @@ export function extractHashFromFileName(filename: string, hash: VPPTPluginOption
}
return ''
}

export function validateOptions(options: Required<VPPTPluginOptions>) {
const { outputDir } = options
// ensure outputDir is Dir
if (!/^\/([a-zA-Z0-9]+\/)*[a-zA-Z0-9]*$/.test(outputDir)) {
throw new Error(`outputDir must be a directory, but got ${outputDir}`)
} else {
if (outputDir.length > 1 && outputDir.endsWith('/')) {
// remove last slash
options.outputDir = outputDir.replace(/\/$/, '')
}
}

// ensure inputDir is Dir
const { inputDir } = options
if (!/^([a-zA-Z0-9]+\/)*[a-zA-Z0-9]*$/.test(inputDir)) {
throw new Error(`inputDir must be a directory, but got ${inputDir}`)
} else {
if (inputDir.endsWith('/')) {
// remove last slash
options.inputDir = inputDir.replace(/\/$/, '')
}
}
}

0 comments on commit e2d074f

Please sign in to comment.