diff --git a/.changeset/keep-user-alias.md b/.changeset/keep-user-alias.md new file mode 100644 index 00000000..7808dcf8 --- /dev/null +++ b/.changeset/keep-user-alias.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-solid': patch +--- + +The plugin no longer changes `resolve.alias`. An alias object in your config now keeps priority over aliases that later plugins add, such as `vite-plugin-node-polyfills` mapping `path` (#130). diff --git a/.github/workflows/vitest.yml b/.github/workflows/vitest.yml index fc062d92..aad075ac 100644 --- a/.github/workflows/vitest.yml +++ b/.github/workflows/vitest.yml @@ -27,6 +27,9 @@ jobs: - name: Install project dependencies run: pnpm i + - name: Run unit tests + run: pnpm run test:unit + - name: Install playwright browser working-directory: examples/vite-6 run: pnpm exec playwright install chromium diff --git a/package.json b/package.json index a2662f89..cba7d046 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "prepublishOnly": "pnpm build", "release": "pnpm build && changeset publish", "check": "package-check", - "test": "node scripts/test-examples.ts" + "test": "node scripts/test-examples.ts", + "test:unit": "vitest run" }, "repository": { "type": "git", @@ -68,7 +69,8 @@ "rollup-plugin-cleaner": "^1.0.0", "solid-js": "^1.9.3", "typescript": "^5.2.2", - "vite": "^6.0.0" + "vite": "^6.0.0", + "vitest": "^3.0.7" }, "peerDependencies": { "@testing-library/jest-dom": "^5.16.6 || ^5.17.0 || ^6.0.0 || ^7.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index eed12c18..23823a84 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,6 +90,9 @@ importers: vite: specifier: ^6.0.0 version: 6.0.9(@types/node@18.19.66)(lightningcss@1.32.0) + vitest: + specifier: ^3.0.7 + version: 3.0.7(@types/node@18.19.66)(@vitest/browser@3.0.7)(jsdom@26.0.0(supports-color@8.1.1))(lightningcss@1.32.0)(msw@2.7.3(@types/node@18.19.66)(typescript@5.7.2))(supports-color@8.1.1) examples/vite-3: dependencies: @@ -5968,6 +5971,28 @@ snapshots: '@types/node': 18.19.66 optional: true + '@vitest/browser@3.0.7(@types/node@18.19.66)(playwright@1.50.1)(typescript@5.7.2)(vite@6.0.9(@types/node@18.19.66)(lightningcss@1.32.0))(vitest@3.0.7)': + dependencies: + '@testing-library/dom': 10.4.0 + '@testing-library/user-event': 14.6.1(@testing-library/dom@10.4.0) + '@vitest/mocker': 3.0.7(msw@2.7.3(@types/node@18.19.66)(typescript@5.7.2))(vite@6.0.9(@types/node@18.19.66)(lightningcss@1.32.0)) + '@vitest/utils': 3.0.7 + magic-string: 0.30.17 + msw: 2.7.3(@types/node@18.19.66)(typescript@5.7.2) + sirv: 3.0.1 + tinyrainbow: 2.0.0 + vitest: 3.0.7(@types/node@18.19.66)(@vitest/browser@3.0.7)(jsdom@26.0.0(supports-color@8.1.1))(lightningcss@1.32.0)(msw@2.7.3(@types/node@18.19.66)(typescript@5.7.2))(supports-color@8.1.1) + ws: 8.18.1 + optionalDependencies: + playwright: 1.50.1 + transitivePeerDependencies: + - '@types/node' + - bufferutil + - typescript + - utf-8-validate + - vite + optional: true + '@vitest/browser@3.0.7(@types/node@18.19.66)(playwright@1.50.1)(typescript@5.7.2)(vite@6.2.0(@types/node@18.19.66)(lightningcss@1.32.0))(vitest@3.0.7)': dependencies: '@testing-library/dom': 10.4.0 @@ -6038,6 +6063,16 @@ snapshots: chai: 5.2.0 tinyrainbow: 2.0.0 + '@vitest/mocker@3.0.7(msw@2.7.3(@types/node@18.19.66)(typescript@5.7.2))(vite@6.0.9(@types/node@18.19.66)(lightningcss@1.32.0))': + dependencies: + '@vitest/spy': 3.0.7 + estree-walker: 3.0.3 + magic-string: 0.30.17 + optionalDependencies: + msw: 2.7.3(@types/node@18.19.66)(typescript@5.7.2) + vite: 6.0.9(@types/node@18.19.66)(lightningcss@1.32.0) + optional: true + '@vitest/mocker@3.0.7(msw@2.7.3(@types/node@18.19.66)(typescript@5.7.2))(vite@6.2.0(@types/node@18.19.66)(lightningcss@1.32.0))': dependencies: '@vitest/spy': 3.0.7 @@ -8040,7 +8075,7 @@ snapshots: why-is-node-running: 2.3.0 optionalDependencies: '@types/node': 18.19.66 - '@vitest/browser': 3.0.7(@types/node@18.19.66)(playwright@1.50.1)(typescript@5.7.2)(vite@6.2.0(@types/node@18.19.66)(lightningcss@1.32.0))(vitest@3.0.7) + '@vitest/browser': 3.0.7(@types/node@18.19.66)(playwright@1.50.1)(typescript@5.7.2)(vite@6.0.9(@types/node@18.19.66)(lightningcss@1.32.0))(vitest@3.0.7) jsdom: 26.0.0(supports-color@8.1.1) transitivePeerDependencies: - jiti diff --git a/src/alias.test.ts b/src/alias.test.ts new file mode 100644 index 00000000..886c1afa --- /dev/null +++ b/src/alias.test.ts @@ -0,0 +1,78 @@ +import { mkdtemp, mkdir, realpath, rm, symlink, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { build, createServer, type Plugin, type Rollup } from 'vite'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import solidPlugin from './index.js'; + +let root: string; + +beforeAll(async () => { + root = await realpath(await mkdtemp(path.join(tmpdir(), 'solid-alias-'))); + await writeFile(path.join(root, 'package.json'), JSON.stringify({ name: 'app', type: 'module' })); + await writeFile(path.join(root, 'entry.js'), "export { join } from 'path/posix';\n"); + await writeFile(path.join(root, 'user-posix.js'), 'export const join = "user alias";\n'); + // The `path` polyfill has no `posix` subpath, like path-browserify. + await mkdir(path.join(root, 'polyfill')); + await writeFile(path.join(root, 'polyfill/index.js'), 'export const join = "polyfill";\n'); +}); + +afterAll(async () => { + await rm(root, { recursive: true, force: true }); +}); + +describe('resolve.alias', () => { + // #130: the plugin turned the user's alias object into an array. Vite then + // put aliases from later plugins first, so `path` matched `path/posix`. + it('keeps user object aliases ahead of aliases added by later plugins', async () => { + const polyfills: Plugin = { + name: 'test-polyfills', + config: () => ({ resolve: { alias: { path: path.join(root, 'polyfill') } } }), + }; + const result = (await build({ + root, + configFile: false, + logLevel: 'silent', + plugins: [solidPlugin(), polyfills], + resolve: { alias: { 'path/posix': path.join(root, 'user-posix.js') } }, + build: { + write: false, + minify: false, + rollupOptions: { input: path.join(root, 'entry.js'), preserveEntrySignatures: 'strict' }, + }, + })) as Rollup.RollupOutput | Rollup.RollupOutput[]; + const output = Array.isArray(result) ? result[0] : result; + const code = output.output + .filter((chunk): chunk is Rollup.OutputChunk => chunk.type === 'chunk') + .map((chunk) => chunk.code) + .join('\n'); + expect(code).toContain('"user alias"'); + }); + + it('still serves the HMR runtime for the solid-refresh import', async () => { + await symlink( + fileURLToPath(new URL('../node_modules', import.meta.url)), + path.join(root, 'node_modules'), + 'dir', + ); + await writeFile(path.join(root, 'App.tsx'), 'export const App = () =>
app
;\n'); + const server = await createServer({ + root, + configFile: false, + appType: 'custom', + logLevel: 'silent', + server: { middlewareMode: true, ws: false, watch: null }, + optimizeDeps: { noDiscovery: true, include: [] }, + plugins: [solidPlugin()], + }); + try { + const app = await server.transformRequest('/App.tsx'); + expect(app?.code).toMatch(/from ["']\/@solid-refresh["']/); + const runtime = await server.transformRequest('/@solid-refresh'); + expect(runtime?.code).toContain('$$registry'); + } finally { + await server.close(); + } + }); +}); diff --git a/src/index.ts b/src/index.ts index 82770246..78bbd5b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { readFileSync } from 'fs'; import { mergeAndConcat } from 'merge-anything'; import { createRequire } from 'module'; import solidRefresh from 'solid-refresh/babel'; -import type { Alias, AliasOptions, FilterPattern, Plugin } from 'vite'; +import type { FilterPattern, Plugin } from 'vite'; import { createFilter, version } from 'vite'; import { crawlFrameworkPkgs } from 'vitefu'; @@ -225,8 +225,9 @@ export default function solidPlugin(options: Partial = {}): Plugin { projectRoot = userConfig.root; isTestMode = userConfig.mode === 'test'; - if (!userConfig.resolve) userConfig.resolve = {}; - userConfig.resolve.alias = normalizeAliases(userConfig.resolve && userConfig.resolve.alias); + // Leave the user's `resolve.alias` as it is. Turning an object into an + // array changes how Vite merges it with aliases from later plugins: + // their entries then take priority over the user's. #130 solidPkgsConfig = await crawlFrameworkPkgs({ viteUserConfig: userConfig, @@ -287,7 +288,6 @@ export default function solidPlugin(options: Partial = {}): Plugin { ...(userConfig.mode === 'test' && !options.ssr ? ['browser'] : []), ], dedupe: nestedDeps, - alias: [{ find: /^solid-refresh$/, replacement: runtimePublicPath }], }, optimizeDeps: { include: [...nestedDeps, ...solidPkgsConfig.optimizeDeps.include], @@ -346,7 +346,10 @@ export default function solidPlugin(options: Partial = {}): Plugin { }, resolveId(id) { - if (id === runtimePublicPath) return id; + // `solid-refresh` is the import added by the HMR transform. It is + // resolved here rather than through `resolve.alias`, because adding an + // alias array would also change how the user's aliases merge. #130 + if (id === runtimePublicPath || id === 'solid-refresh') return runtimePublicPath; }, load(id) { @@ -456,15 +459,3 @@ 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 })); -} diff --git a/tsconfig.json b/tsconfig.json index 70191cbf..9c12304d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "src", "cypress" ], "exclude": [ - "**/*.spec.ts" + "**/*.spec.ts", "**/*.test.ts" ], "compilerOptions": { "target": "ESNext", diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 00000000..2f1a903a --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,10 @@ +import { defineConfig } from 'vitest/config'; + +// The plugin's own unit tests (`pnpm test:unit`). The example apps under +// examples/* carry their own vitest configs and run through `pnpm test`. +export default defineConfig({ + test: { + include: ['src/**/*.test.ts'], + environment: 'node', + }, +});