diff --git a/.changeset/query-before-extension.md b/.changeset/query-before-extension.md new file mode 100644 index 00000000..9e33a154 --- /dev/null +++ b/.changeset/query-before-extension.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-solid': patch +--- + +Compile modules whose id has the extension after the query, such as unplugin-icons' `~icons/mdi/folder?width=2em.tsx`. Since 2.11.2 these were left to Vite's default JSX transform, which produced React code (#206). Custom `extensions` are now also matched when the query contains a dot. 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/index.ts b/src/index.ts index 82770246..286a23d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -178,7 +178,18 @@ export interface Options { function getExtension(filename: string): string { const index = filename.lastIndexOf('.'); - return index < 0 ? '' : filename.substring(index).replace(/\?.+$/, ''); + return index < 0 ? '' : filename.substring(index); +} + +/** + * The path the transform compiles for `id`. This is normally `id` without its + * query. Some plugins put the extension after the query instead, such as + * unplugin-icons' `~icons/mdi/folder?width=2em.tsx`. Then the full id is used, + * as it was before queries were stripped. #206 + */ +function getTransformPath(id: string, isHandled: (path: string) => boolean): string { + const cleanId = id.replace(/\?.*$/, ''); + return cleanId !== id && !isHandled(cleanId) && isHandled(id) ? id : cleanId; } function containsSolidField(fields: Record) { const keys = Object.keys(fields); @@ -357,7 +368,6 @@ export default function solidPlugin(options: Partial = {}): Plugin { const isSsr = this.environment ? this.environment.config.consumer === 'server' : Boolean(transformOptions?.ssr); - const currentFileExtension = getExtension(id); const extensionsToWatch = options.extensions || []; const allExtensions = extensionsToWatch.map((extension) => @@ -369,7 +379,13 @@ export default function solidPlugin(options: Partial = {}): Plugin { return null; } - id = id.replace(/\?.*$/, ''); + id = getTransformPath( + id, + (path) => /\.[mc]?[tj]sx$/i.test(path) || allExtensions.includes(getExtension(path)), + ); + // Read the extension from the query-free path, so a dot inside the + // query (`a.mdx?x=1.5`) is not taken as the extension. + const currentFileExtension = getExtension(id); if (!(/\.[mc]?[tj]sx$/i.test(id) || allExtensions.includes(currentFileExtension))) { return null; diff --git a/src/transform-ids.test.ts b/src/transform-ids.test.ts new file mode 100644 index 00000000..4a056a15 --- /dev/null +++ b/src/transform-ids.test.ts @@ -0,0 +1,69 @@ +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 { createServer, type Plugin, type ViteDevServer } from 'vite'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import solidPlugin from './index.js'; + +let root: string; +let server: ViteDevServer; + +// Resolves ids the way unplugin-icons does: the extension is appended after +// the query, giving `~icons/mdi/folder?width=2em.tsx`. +const iconsPlugin: Plugin = { + name: 'test-icons', + enforce: 'pre', + resolveId(id) { + if (!id.startsWith('~icons/')) return null; + return id.endsWith('.tsx') ? id : `${id}.tsx`; + }, + load(id) { + if (id.startsWith('~icons/')) { + return 'export default (props = {}) => ;'; + } + }, +}; + +beforeAll(async () => { + root = await realpath(await mkdtemp(path.join(tmpdir(), 'solid-transform-ids-'))); + // The modules import solid-js/web from this repo's install. + await symlink( + fileURLToPath(new URL('../node_modules', import.meta.url)), + path.join(root, 'node_modules'), + 'dir', + ); + await writeFile(path.join(root, 'package.json'), JSON.stringify({ name: 'app', type: 'module' })); + await mkdir(path.join(root, 'src')); + await writeFile(path.join(root, 'src/Comp.custom'), 'export const Comp = () =>
custom
;\n'); + server = await createServer({ + root, + configFile: false, + appType: 'custom', + logLevel: 'silent', + server: { middlewareMode: true, hmr: false, watch: null }, + optimizeDeps: { noDiscovery: true, include: [] }, + plugins: [solidPlugin({ extensions: ['.custom'] }), iconsPlugin], + }); +}); + +afterAll(async () => { + await server?.close(); + await rm(root, { recursive: true, force: true }); +}); + +describe('transform ids with queries', () => { + // #206: since queries were stripped first, this id lost its extension and + // Vite compiled the JSX for React instead. + it('compiles JSX when the extension follows the query', async () => { + const result = await server.environments.client.transformRequest('~icons/mdi/folder?width=2em'); + expect(result?.code).toContain('_$template('); + expect(result?.code).not.toMatch(/react/i); + }); + + it('reads a custom extension from the path, not from a dot in the query', async () => { + const result = await server.environments.client.transformRequest('/src/Comp.custom?x=1.5'); + expect(result?.code).toContain('_$template('); + expect(result?.code).not.toContain('
custom
'); + }); +}); 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', + }, +});