Skip to content

Commit

Permalink
🐛 Force alias to be arrays to properly support config merging fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
amoutonbrady committed Mar 19, 2021
1 parent af0ab70 commit 9ffe0e5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions playground/assets/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!!!
6 changes: 5 additions & 1 deletion playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { MetaProvider } from 'solid-meta';
import { createApp } from 'solid-utils';
import { Router, Route, RouteDefinition, Link } from 'solid-app-router';

import Home from './pages';
import test from '@@/test.txt?raw'
import Home from '@/index';

// This should log Hello World
console.log(test)

const routes: RouteDefinition[] = [
{
Expand Down
7 changes: 6 additions & 1 deletion playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"moduleResolution": "node",
"jsx": "preserve",
"jsxImportSource": "solid-js",
"types": ["vite/client"]
"types": ["vite/client"],
"baseUrl": ".",
"paths": {
"@/*": ["./pages/*"],
"@@/*": ["./assets/*"]
}
}
}
7 changes: 7 additions & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import solid from '../src';
import { defineConfig } from 'vite';
import { resolve } from 'path'

export default defineConfig({
plugins: [solid()],
resolve: {
alias: {
'@': '/pages',
'@@': '/assets'
}
}
});
23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, UserConfig } from 'vite';
import { Plugin, UserConfig, AliasOptions, Alias } from 'vite';
import { readFileSync } from 'fs';
import { transformAsync, TransformOptions } from '@babel/core';
import { mergeAndConcat } from 'merge-anything';
Expand All @@ -20,14 +20,20 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
name: 'solid',
enforce: 'pre',

config(userConfig, { command }) {
config(userConfig, { command }): UserConfig {
const replaceDev = options.dev !== false;

const alias =
command === 'serve' && replaceDev
? [{ find: /^solid-js$/, replacement: 'solid-js/dev' }]
: [];

// TODO: remove when fully removed from vite
userConfig.alias = normalizeAliases(userConfig.alias)

if (!userConfig.resolve) userConfig.resolve = {}
userConfig.resolve.alias = normalizeAliases(userConfig.resolve?.alias)

return mergeAndConcat(userConfig, {
/**
* We only need esbuild on .ts or .js files.
Expand Down Expand Up @@ -91,3 +97,16 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
},
};
}

/**
* This basically normalize all aliases of the config into
* the array format of the alias.
*
* eg: alias: { '@': 'src/' } => [{ find: '@', replacement: 'src/' }]
*/
function normalizeAliases(alias: AliasOptions = []): Alias[] {
return Array.isArray(alias)
? alias
: Object.entries(alias).map(([find, replacement]) => ({ find, replacement }))
}

0 comments on commit 9ffe0e5

Please sign in to comment.