From 9ffe0e5989a36c8871a01f8aa767d8a7d57f089a Mon Sep 17 00:00:00 2001 From: Alexandre Date: Fri, 19 Mar 2021 21:46:13 +0100 Subject: [PATCH] :bug: Force alias to be arrays to properly support config merging fix #3 --- playground/assets/test.txt | 1 + playground/index.tsx | 6 +++++- playground/tsconfig.json | 7 ++++++- playground/vite.config.ts | 7 +++++++ src/index.ts | 23 +++++++++++++++++++++-- 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 playground/assets/test.txt diff --git a/playground/assets/test.txt b/playground/assets/test.txt new file mode 100644 index 0000000..e1923af --- /dev/null +++ b/playground/assets/test.txt @@ -0,0 +1 @@ +Hello world!!! diff --git a/playground/index.tsx b/playground/index.tsx index 3c5ad87..81fb0ed 100644 --- a/playground/index.tsx +++ b/playground/index.tsx @@ -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[] = [ { diff --git a/playground/tsconfig.json b/playground/tsconfig.json index b7b50a4..dc6abb7 100644 --- a/playground/tsconfig.json +++ b/playground/tsconfig.json @@ -8,6 +8,11 @@ "moduleResolution": "node", "jsx": "preserve", "jsxImportSource": "solid-js", - "types": ["vite/client"] + "types": ["vite/client"], + "baseUrl": ".", + "paths": { + "@/*": ["./pages/*"], + "@@/*": ["./assets/*"] + } } } diff --git a/playground/vite.config.ts b/playground/vite.config.ts index f32f91b..2068560 100644 --- a/playground/vite.config.ts +++ b/playground/vite.config.ts @@ -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' + } + } }); diff --git a/src/index.ts b/src/index.ts index f5aac65..70453ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -20,7 +20,7 @@ export default function solidPlugin(options: Partial = {}): Plugin { name: 'solid', enforce: 'pre', - config(userConfig, { command }) { + config(userConfig, { command }): UserConfig { const replaceDev = options.dev !== false; const alias = @@ -28,6 +28,12 @@ export default function solidPlugin(options: Partial = {}): Plugin { ? [{ 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. @@ -91,3 +97,16 @@ export default function solidPlugin(options: Partial = {}): 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 })) +} +