Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ test-results
!.vscode/settings.json
!.vscode/extensions.json
.idea
playground/test-temp-*/
test-temp-*/
159 changes: 0 additions & 159 deletions test/basic/index.test.ts

This file was deleted.

23 changes: 23 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { mkdtemp, rm } from 'node:fs/promises';
import path from 'node:path';
import { promisify } from 'node:util';
import { type Configuration, rspack } from '@rspack/core';
import { expect, test as base } from '@rstest/playwright';

export const test = base.extend<{ tempDir: string }>({
tempDir: async ({ task }, use) => {
const dir = await mkdtemp(
path.join(path.dirname(task.filepath!), 'test-temp-'),
);
try {
await use(dir);
} finally {
await rm(dir, { recursive: true, force: true });
}
},
});

export async function build(config: Configuration) {
const stats = await promisify(rspack)(config);
expect(stats?.toJson({ all: false, errors: true }).errors).toEqual([]);
}
37 changes: 37 additions & 0 deletions test/hmr/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { cp } from 'node:fs/promises';
import path from 'node:path';
import { rspack } from '@rspack/core';
import { RspackDevServer } from '@rspack/dev-server';
import { editFile, getRandomPort } from '@rstackjs/test-utils';
import { expect } from '@rstest/playwright';
import { test } from '../helpers';
import createConfig from './rspack.config';

for (const refresh of ['automatic', 'explicit Babel', 'pre-loader'] as const) {
test(`preserves parent state during HMR with ${refresh} refresh`, async ({
page,
tempDir,
}) => {
const src = path.join(tempDir, 'src');
await cp(path.join(import.meta.dirname, 'src'), src, { recursive: true });
const port = await getRandomPort();
const compiler = rspack({ ...createConfig({ refresh }), context: tempDir });
const server = new RspackDevServer(
{ host: '127.0.0.1', port, hot: true },
compiler,
);
try {
await server.start();
await page.goto(`http://127.0.0.1:${port}`);
const button = page.getByRole('button');
await button.click();
await expect(button).toHaveText('Count: 1');
await editFile(path.join(src, 'Counter.tsx'), (code) =>
code.replace('Count:', 'Updated:'),
);
await expect(button).toHaveText('Updated: 1');
} finally {
await server.stop();
}
});
}
51 changes: 51 additions & 0 deletions test/hmr/rspack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
type Configuration,
HtmlRspackPlugin,
type RuleSetRule,
} from '@rspack/core';

type Refresh = 'automatic' | 'explicit Babel' | 'pre-loader';

export default ({
refresh = 'automatic',
}: { refresh?: Refresh } = {}): Configuration => {
const test = /\.[mc]?[jt]sx$/i;
const rules: RuleSetRule[] = [];
if (refresh === 'pre-loader') {
rules.push({ test, enforce: 'pre', loader: 'solid-loader/refresh' });
}

rules.push(
{
test,
use: [
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
refresh === 'automatic'
? 'solid-loader'
: {
loader: 'solid-loader',
options: {
compiler: 'babel',
refresh: refresh === 'explicit Babel',
},
},
],
},
{
descriptionData: { name: /^@solidjs\/web$/ },
parser: { importDynamic: false },
},
);

return {
context: import.meta.dirname,
mode: 'development',
entry: './src/index.tsx',
resolve: {
extensions: ['.tsx', '...'],
conditionNames: ['solid', 'development', '...'],
},
module: { rules },
plugins: [new HtmlRspackPlugin()],
};
};
3 changes: 3 additions & 0 deletions test/hmr/src/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Counter(props: { count: number; increment: () => void }) {
return <button onClick={props.increment}>Count: {props.count}</button>;
}
10 changes: 10 additions & 0 deletions test/hmr/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { render } from '@solidjs/web';
import { createSignal } from 'solid-js';
import { Counter } from './Counter';

function App() {
const [count, setCount] = createSignal<number>(0);
return <Counter count={count()} increment={() => setCount(count() + 1)} />;
}

render(() => <App />, document.body);
8 changes: 8 additions & 0 deletions test/plain-typescript/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { build, test } from '../helpers';
import config from './rspack.config';

test('builds a TSX component that imports a plain TypeScript module', async ({
tempDir,
}) => {
await build({ ...config, output: { path: tempDir } });
});
23 changes: 23 additions & 0 deletions test/plain-typescript/rspack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Configuration } from '@rspack/core';

export default {
context: import.meta.dirname,
mode: 'production',
entry: './src/index.tsx',
resolve: { conditionNames: ['solid', '...'] },
module: {
rules: [
{
test: /\.[mc]?[jt]sx?$/i,
use: [
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
'solid-loader',
],
},
{
descriptionData: { name: /^@solidjs\/web$/ },
parser: { importDynamic: false },
},
],
},
} satisfies Configuration;
1 change: 1 addition & 0 deletions test/plain-typescript/src/count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const count: number = 42;
4 changes: 4 additions & 0 deletions test/plain-typescript/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { render } from '@solidjs/web';
import { count } from './count.ts';

render(() => <h1>{count}</h1>, document.body);
22 changes: 22 additions & 0 deletions test/ssr/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';
import { expect } from '@rstest/playwright';
import { build, test } from '../helpers';
import createConfig from './rspack.config';

for (const compiler of ['native', 'babel'] as const) {
test(`renders typed SSR output with ${compiler}`, async ({ tempDir }) => {
const config = createConfig({ compiler });
await build({ ...config, output: { ...config.output, path: tempDir } });

const { html } = createRequire(import.meta.url)(
path.join(tempDir, 'app.cjs'),
);
expect(html).toBe('<h1>Hello</h1>');
const map = JSON.parse(
await readFile(path.join(tempDir, 'app.cjs.map'), 'utf8'),
);
expect(map.sources).toContainEqual(expect.stringMatching(/app\.tsx$/));
});
}
31 changes: 31 additions & 0 deletions test/ssr/rspack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Configuration } from '@rspack/core';
import type { SolidCompiler } from 'solid-loader';

export default ({
compiler = 'native',
}: { compiler?: SolidCompiler } = {}): Configuration => ({
context: import.meta.dirname,
mode: 'production',
target: 'node',
entry: './src/app.tsx',
devtool: 'source-map',
output: {
filename: 'app.cjs',
library: { type: 'commonjs2' },
},
resolve: { conditionNames: ['solid', '...'] },
module: {
rules: [
{
test: /\.tsx$/,
use: [
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
{
loader: 'solid-loader',
options: { compiler, solid: { generate: 'ssr' } },
},
],
},
],
},
});
4 changes: 4 additions & 0 deletions test/ssr/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { renderToString } from '@solidjs/web';

const title: string = 'Hello';
export const html = renderToString(() => <h1>{title}</h1>);
Loading