Skip to content

fix: Replace unplugin hoisting error with a warning #1366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2025
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
4 changes: 2 additions & 2 deletions packages/unplugin-typegpu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ const typegpu: UnpluginInstance<Options, false> = createUnplugin(
.slice(0, def.start)
.search(new RegExp(`(?<![\\w_.])${name}(?![\\w_])`)) !== -1
) {
throw new Error(
console.warn(
`File ${id}: function "${name}", containing ${
removeJsImplementation ? 'kernel' : 'kernel & js'
} directive, is referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`,
} directive, might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`,
);
}

Expand Down
21 changes: 15 additions & 6 deletions packages/unplugin-typegpu/test/kernel-and-js-directive.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { babelTransform, rollupTransform } from './transform.ts';

describe('[BABEL] "kernel & js" directive', () => {
Expand Down Expand Up @@ -229,6 +229,16 @@ describe('[BABEL] "kernel & js" directive', () => {
});

describe('[ROLLUP] "kernel & js" directive', () => {
let consoleWarnSpy: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
consoleWarnSpy.mockRestore();
});

it('makes plugin transpile marked arrow functions', async () => {
const code = `\
import tgpu from 'typegpu';
Expand Down Expand Up @@ -469,11 +479,10 @@ describe('[ROLLUP] "kernel & js" directive', () => {
};
`;

expect(rollupTransform(code))
.rejects
.toThrowErrorMatchingInlineSnapshot(
`[Error: File virtual:code: function "add", containing kernel & js directive, is referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.]`,
);
await rollupTransform(code);
expect(consoleWarnSpy).toHaveBeenCalledWith(
`File virtual:code: function "add", containing kernel & js directive, might have been referenced before its usage. Function statements are no longer hoisted after being transformed by the plugin.`,
);
});

it('parses when no typegpu import', async () => {
Expand Down