-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
308 additions
and
547 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
// https://nuxt.com/docs/guide/concepts/typescript | ||
"extends": "./.nuxt/tsconfig.json" | ||
"extends": "./.nuxt/tsconfig.json", | ||
"compilerOptions": { | ||
"paths": { | ||
"components/*": ["components/*"] | ||
} | ||
} | ||
} |
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,37 @@ | ||
import {createAlias} from '../alias' | ||
import {WorkspaceInfo} from '../types' | ||
import {describe, expect, it} from 'vitest' | ||
|
||
describe('alias', () => { | ||
it('should create vite alias', () => { | ||
const info: WorkspaceInfo[] = [ | ||
{ | ||
alias: [[/^components\//u, 'components/']], | ||
path: /^\/foo\/bar/u, | ||
}, | ||
] | ||
const alias = createAlias(info) | ||
|
||
expect(alias.replacement).toBe('$1') | ||
expect(alias.find).toBeInstanceOf(RegExp) | ||
if (alias.find instanceof RegExp) { | ||
expect(alias.find.test('virtual:foo')).toBeFalsy() | ||
expect(alias.find.test('./foo/bar')).toBeFalsy() | ||
expect(alias.find.test('../foo/bar')).toBeFalsy() | ||
expect(alias.find.test('/foo/bar')).toBeFalsy() | ||
expect(alias.find.test('@vite/env')).toBeFalsy() | ||
expect(alias.find.test('@vite/client')).toBeFalsy() | ||
expect(alias.find.test('app/foo/bar')).toBeTruthy() | ||
} | ||
|
||
expect(alias.customResolver).toBeInstanceOf(Function) | ||
if (typeof alias.customResolver === 'function') { | ||
const customResolver: any = alias.customResolver | ||
expect(customResolver('components/Foo.vue', undefined)).toBe(null) | ||
expect(customResolver('store/Foo.vue', '/foo/bar/john/Bar.vue')).toBe(null) | ||
expect(customResolver('components/Foo.vue', '/foo/bar/john/Bar.vue')).toBe( | ||
'/foo/bar/components/Foo.vue', | ||
) | ||
} | ||
}) | ||
}) |
7 changes: 7 additions & 0 deletions
7
packages/vite-monorepo-alias/src/__tests__/apps/john/tsconfig.json
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"src/*": ["src/*"] | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/vite-monorepo-alias/src/__tests__/get-typescript-alias.spec.ts
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,45 @@ | ||
import {afterEach, describe, expect, it, vi} from 'vitest' | ||
import {getTypescriptAlias} from '../get-typescript-alias' | ||
import {parseWorkspacePath} from '../parse-workspace-path' | ||
import {join} from 'node:path' | ||
|
||
vi.mock('../parse-workspace-path') | ||
|
||
describe('get-typescript-alias', () => { | ||
afterEach(() => { | ||
vi.mocked(parseWorkspacePath).mockRestore() | ||
}) | ||
const setup = () => { | ||
vi.mocked(parseWorkspacePath).mockImplementation((_, path) => path) | ||
|
||
return {} | ||
} | ||
it('should get typescript alias', async () => { | ||
const prepare = setup() | ||
const alias = await getTypescriptAlias([ | ||
join(__dirname, 'packages/*'), | ||
join(__dirname, 'apps/*'), | ||
]) | ||
|
||
expect(alias).toEqual([ | ||
{ | ||
alias: [['src/*', 'src/']], | ||
path: join(__dirname, 'apps', 'john'), | ||
}, | ||
{ | ||
alias: [['components/*', 'components/']], | ||
path: join(__dirname, 'packages', 'foo'), | ||
}, | ||
{ | ||
alias: [['components/*', 'components/']], | ||
path: join(__dirname, 'packages', 'bar'), | ||
}, | ||
]) | ||
|
||
expect(parseWorkspacePath).toHaveBeenCalledWith('', join(__dirname, 'packages', 'foo')) | ||
expect(parseWorkspacePath).toHaveBeenCalledWith('', join(__dirname, 'packages', 'bar')) | ||
expect(parseWorkspacePath).toHaveBeenCalledWith('', join(__dirname, 'apps', 'john')) | ||
expect(parseWorkspacePath).toHaveBeenCalledWith('', join('src/*')) | ||
expect(parseWorkspacePath).toHaveBeenCalledWith('', join('components/*')) | ||
}) | ||
}) |
15 changes: 15 additions & 0 deletions
15
packages/vite-monorepo-alias/src/__tests__/normalize-workspace-info-options.spec.ts
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,15 @@ | ||
import {describe, expect, it, vi} from 'vitest' | ||
import {normalizeWorkspaceInfoOptions} from '../normalize-workspace-info-options' | ||
|
||
describe('normalize-workspace-info-options', () => { | ||
it('should normalize workspace info options', () => { | ||
expect( | ||
normalizeWorkspaceInfoOptions({ | ||
root: '/foo/bar', | ||
workspaces: ['packages/*', 'apps/*'], | ||
}), | ||
).toEqual({ | ||
workspaces: ['/foo/bar/packages/*', '/foo/bar/apps/*'], | ||
}) | ||
}) | ||
}) |
7 changes: 7 additions & 0 deletions
7
packages/vite-monorepo-alias/src/__tests__/packages/bar/tsconfig.json
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"components/*": ["components/*"] | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/vite-monorepo-alias/src/__tests__/packages/foo/tsconfig.json
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"components/*": ["components/*"] | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/vite-monorepo-alias/src/__tests__/parse-workspace-path.spec.ts
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,20 @@ | ||
import {parseWorkspacePath} from '../parse-workspace-path' | ||
import {describe, expect, it} from 'vitest' | ||
|
||
describe('parse-workspace-path', () => { | ||
it('should return regexp', () => { | ||
const regexp = parseWorkspacePath('/foo/bar', 'packages/hello') | ||
|
||
expect(regexp.test('/foo/bar/packages/hello/foo')).toBeTruthy() | ||
expect(regexp.test('/foo/john/packages/hello/foo')).toBeFalsy() | ||
expect(regexp.test('/foo/bar/apps/hello/bar')).toBeFalsy() | ||
}) | ||
|
||
it('should return alias regexp', () => { | ||
const regexp = parseWorkspacePath('', 'components/*') | ||
|
||
expect('components/hello'.replace(regexp, '')).toBe('hello') | ||
expect(regexp.test('store/hello')).toBeFalsy() | ||
expect(regexp.test('foo/components/hello')).toBeFalsy() | ||
}) | ||
}) |
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,37 @@ | ||
import {Alias} from 'vite' | ||
import {WorkspaceInfo} from './types' | ||
|
||
export const createAlias = (workspaces: WorkspaceInfo[]): Alias => { | ||
return { | ||
customResolver(source: string, importer: undefined | string) { | ||
if (!importer) { | ||
return null | ||
} | ||
|
||
const workspaceInfo = workspaces.find((packageInfo) => { | ||
return packageInfo.path.test(importer) | ||
}) | ||
|
||
if (!workspaceInfo) { | ||
return null | ||
} | ||
|
||
const rootPath = importer.match(workspaceInfo.path)?.[0] ?? '/' | ||
|
||
const alias = workspaceInfo.alias.find(([pattern]) => { | ||
return pattern.test(source) | ||
}) | ||
|
||
if (!alias) { | ||
return null | ||
} | ||
|
||
const replacedSource = source.replace(alias[0], alias[1]) | ||
|
||
return `${rootPath}/${replacedSource}` | ||
}, | ||
// eslint-disable-next-line prefer-named-capture-group,require-unicode-regexp | ||
find: /^(?!virtual:|\/|\.\/|\.\.\/|@vite\/env|@vite\/client)(.*)/, | ||
replacement: '$1', | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
...ges/vite-monorepo-alias/src/change-path-delimiter/__tests__/change-path-delimiter.spec.ts
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/vite-monorepo-alias/src/change-path-delimiter/index.ts
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
packages/vite-monorepo-alias/src/depub-separator/__tests__/index.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
import {glob} from 'glob' | ||
import {readFileSync} from 'node:fs' | ||
import {join} from 'node:path' | ||
import {WorkspaceInfo} from 'src/types' | ||
import {readConfigFile} from 'typescript' | ||
import {parseWorkspacePath} from './parse-workspace-path' | ||
const WildCard = /\/\*$/u | ||
|
||
/** | ||
* todo finding exact alias is yet | ||
* @param workspacePath | ||
*/ | ||
export const getTypescriptAlias = async (workspacePath: string[]): Promise<WorkspaceInfo[]> => { | ||
const paths = await glob(workspacePath) | ||
return paths.map((path) => { | ||
const {config} = readConfigFile(join(path, 'tsconfig.json'), (path) => | ||
readFileSync(path).toString(), | ||
) | ||
const configPaths: Record<string, string[]> = config.compilerOptions?.paths ?? {} | ||
|
||
return { | ||
alias: Object.entries(configPaths).map(([key, value]) => { | ||
return [parseWorkspacePath('', key), value[0].replace(WildCard, '/')] | ||
}), | ||
path: parseWorkspacePath('', path), | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './vite-alias' | ||
export * from './alias' | ||
export * from './workspace-info' |
21 changes: 21 additions & 0 deletions
21
packages/vite-monorepo-alias/src/normalize-workspace-info-options.ts
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,21 @@ | ||
import {parseWorkspacePath} from './parse-workspace-path' | ||
import {join} from 'node:path' | ||
|
||
export interface CreateWorkspaceInfoOptions { | ||
root?: string | ||
workspaces: string[] | ||
} | ||
export interface NormalizedCreateWorkspaceInfoOptions { | ||
workspaces: string[] | ||
} | ||
|
||
export const normalizeWorkspaceInfoOptions = ( | ||
options: CreateWorkspaceInfoOptions, | ||
): NormalizedCreateWorkspaceInfoOptions => { | ||
const {workspaces, root = process.cwd()} = options | ||
return { | ||
workspaces: workspaces.map((info) => { | ||
return join(root, info) | ||
}), | ||
} | ||
} |
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,5 @@ | ||
import {join} from 'node:path' | ||
|
||
export const parseWorkspacePath = (root: string, path: string) => { | ||
return RegExp(`^${join(root, path.replace(/\/*$/u, '/'))}`, 'u') | ||
} |
26 changes: 0 additions & 26 deletions
26
packages/vite-monorepo-alias/src/resolve-url/__tests__/index.spec.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.