Skip to content

Commit

Permalink
refactor!: remove vite config builtin aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 7, 2024
1 parent 80b8c8b commit 9b6f1b9
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 61 deletions.
22 changes: 1 addition & 21 deletions src/client/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,11 @@
*/

import { join } from 'node:path'
import type { AliasOptions, ConfigEnv, Plugin, UserConfig } from 'vite'
import type { ConfigEnv, Plugin, UserConfig } from 'vite'

import { addTrailingSlash } from '../utils.js'
import type { PluginFullOptions } from './types.js'

/**
* Resolve the `config.resolve.alias` value
*
* Basically we are merging the user defined alias with the
* default alias.
*/
export function resolveAlias(config: UserConfig): AliasOptions {
const defaultAlias = { '@/': `/resources/js/` }

if (Array.isArray(config.resolve?.alias)) {
return [
...(config.resolve?.alias ?? []),
...Object.entries(defaultAlias).map(([find, replacement]) => ({ find, replacement })),
]
}

return { ...defaultAlias, ...config.resolve?.alias }
}

/**
* Resolve the `config.base` value
*/
Expand All @@ -58,7 +39,6 @@ export function configHook(
): UserConfig {
const config: UserConfig = {
publicDir: userConfig.publicDir ?? false,
resolve: { alias: resolveAlias(userConfig) },
base: resolveBase(userConfig, options, command),

build: {
Expand Down
40 changes: 0 additions & 40 deletions tests/client/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,6 @@ test.group('Vite plugin', () => {
await assert.fileContains('public/assets/foo.json', 'resources/js/app.ts')
})

test('user aliases are preserved', async ({ assert }) => {
const plugin = adonisjs({ entrypoints: ['./resources/js/app.ts'] })[1] as Plugin

// @ts-ignore
const config = plugin!.config!(
{ resolve: { alias: { '@test/': '/test/', '@foo': '/foo/' } } },
{ command: 'build' }
)

assert.deepEqual(config.resolve.alias, {
'@/': '/resources/js/',
'@test/': '/test/',
'@foo': '/foo/',
})
})

test('flatten existing aliases', async ({ assert }) => {
const plugin = adonisjs({ entrypoints: ['./resources/js/app.ts'] })[1] as Plugin

// @ts-ignore
const config = plugin!.config!(
{ resolve: { alias: [{ find: '@test/', replacement: '/test/' }] } },
{ command: 'build' }
)

assert.deepEqual(config.resolve.alias, [
{ find: '@test/', replacement: '/test/' },
{ find: '@/', replacement: '/resources/js/' },
])
})

test('user should be able to override the default alias', async ({ assert }) => {
const plugin = adonisjs({ entrypoints: ['./resources/js/app.ts'] })[1] as Plugin

// @ts-ignore
const config = plugin!.config!({ resolve: { alias: { '@/': '/test/' } } }, { command: 'build' })

assert.deepEqual(config.resolve.alias, { '@/': '/test/' })
})

test('define the asset url', async ({ assert }) => {
const plugin = adonisjs({
entrypoints: ['./resources/js/app.ts'],
Expand Down

0 comments on commit 9b6f1b9

Please sign in to comment.