提交 f6d4cba2 编写于 作者: Q qiang

fix: static copy with path like glob

上级 fb6fe2ba
import { pathToGlob } from '../src/utils'
describe('pathToGlob', () => {
it('path in unix', () => {
expect(pathToGlob('/test', '**/*.js')).toBe('/test/**/*.js')
expect(pathToGlob('/test(1', '**/*.js')).toBe('/test[(]1/**/*.js')
expect(pathToGlob('/test(1', '**/*.js', { escape: true })).toBe(
'/test\\(1/**/*.js'
)
})
it('path in windows', () => {
expect(pathToGlob('C:\\\\test\\test', '**/*.js', { windows: true })).toBe(
'C:/test/test/**/*.js'
)
expect(pathToGlob('C:\\\\test\\test(1', '**/*.js', { windows: true })).toBe(
'C:/test/test[(]1/**/*.js'
)
expect(
pathToGlob('C:\\\\test\\test(1', '**/*.js', {
windows: true,
escape: true,
})
).toBe('C:/test/test[(]1/**/*.js')
})
})
......@@ -91,3 +91,19 @@ export function normalizeParsePlugins(
if (isTS) plugins.push('typescript', 'decorators-legacy')
return plugins
}
export function pathToGlob(
pathString: string,
glob: string,
options: { windows?: boolean; escape?: boolean } = {}
): string {
const isWindows =
'windows' in options ? options.windows : /^win/.test(process.platform)
const useEscape = options.escape
const str = isWindows ? pathString.replace(/\\/g, '/') : pathString
let safeStr = str.replace(
/[\\*?[\]{}()!]/g,
isWindows || !useEscape ? '[$&]' : '\\$&'
)
return path.posix.join(safeStr, glob)
}
......@@ -2,6 +2,7 @@ import fs from 'fs-extra'
import path from 'path'
import { FSWatcher, watch, WatchOptions } from 'chokidar'
import { isArray } from '@vue/shared'
import { pathToGlob } from './utils'
type FileTransform = (source: Buffer, filename: string) => void | string
export interface FileWatcherOptions {
src: string | string[]
......@@ -30,7 +31,11 @@ export class FileWatcher {
if (!this.watcher) {
const copy = this.copy.bind(this)
const remove = this.remove.bind(this)
this.watcher = watch(this.src, watchOptions)
// escape chokidar cwd
const src = this.src.map((src) =>
pathToGlob(path.resolve(watchOptions.cwd), src)
)
this.watcher = watch(src, watchOptions)
.on('add', copy)
.on('addDir', copy)
.on('change', copy)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册