Skip to content
Draft
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
7 changes: 4 additions & 3 deletions apps/typegpu-docs/src/components/ExampleView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { ControlPanel } from './ControlPanel.tsx';
import { Button } from './design/Button.tsx';
import { Snackbar } from './design/Snackbar.tsx';
import { openInStackBlitz } from './stackblitz/openInStackBlitz.ts';
import { importMap } from '../utils/importmap/importmap.ts';

console.log(importMap());

type Props = {
example: Example;
Expand Down Expand Up @@ -171,9 +174,7 @@ export function ExampleView({ example }: Props) {
</div>

<div className='absolute right-0 z-5 md:top-15 md:right-8'>
<Button
onClick={() => openInStackBlitz(example)}
>
<Button onClick={() => openInStackBlitz(example)}>
<span className='font-bold'>Edit on</span>
<img
src='https://developer.stackblitz.com/img/logo/stackblitz-logo-black_blue.svg'
Expand Down
32 changes: 2 additions & 30 deletions apps/typegpu-docs/src/utils/examples/exampleContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,7 @@ import type {
ExampleSrcFile,
ThumbnailPair,
} from './types.ts';

function extractUrlFromViteImport(
importFn: () => void,
): [URL | undefined, boolean] {
const filePath = String(importFn);
const match = filePath.match(/\(\)\s*=>\s*import\("([^"]+)"\)/);

if (match?.[1]) {
const isRelative = match[1].startsWith('./');
return [new URL(match[1], window.location.origin), isRelative];
}

return [undefined, false];
}

function noCacheImport<T>(
importFn: () => Promise<T>,
): Promise<T> {
const [url, isRelative] = extractUrlFromViteImport(importFn);

if (!url) {
throw new Error(`Could not no-cache-import using ${importFn}`);
}

url.searchParams.append('update', Date.now().toString());
return import(
/* @vite-ignore */ `${isRelative ? '.' : ''}${url.pathname}${url.search}`
);
}
import { noCacheImport } from '../import-helpers.ts';

const contentExamplesPath = '../../content/examples/';

Expand Down Expand Up @@ -135,7 +107,7 @@ export const examples = R.pipe(
key,
metadata: value,
tsFiles: readonlyTsFiles[key] ?? [],
tsImport: () => noCacheImport(tsFilesImportFunctions[key]),
tsImport: () => noCacheImport(tsFilesImportFunctions[key], import.meta.url),
htmlFile: htmlFiles[key]?.[0] ?? '',
thumbnails: thumbnailFiles[key],
}) satisfies Example,
Expand Down
29 changes: 29 additions & 0 deletions apps/typegpu-docs/src/utils/import-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Extracts the imported URL as-in
*/
export function extractUrlFromViteImport(
importFn: () => void,
): string | undefined {
const match = String(importFn).match(/import\(["']([^"']+)["']\)/);

if (match?.[1]) {
return match[1];
}

return undefined;
}

export function noCacheImport<T>(
importFn: () => Promise<T>,
baseUrl?: string,
): Promise<T> {
const href = extractUrlFromViteImport(importFn);

if (!href) {
throw new Error(`Could not no-cache-import using ${importFn}`);
}

const url = new URL(href, baseUrl);
url.searchParams.append('update', Date.now().toString());
return import(/* @vite-ignore */ url.href);
}
53 changes: 53 additions & 0 deletions apps/typegpu-docs/src/utils/importmap/importmap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { extractUrlFromViteImport } from '../import-helpers.ts';

function importToUrl(importFn: () => unknown, options: { hasDefaultExport?: boolean }): string {
const href = extractUrlFromViteImport(importFn);
if (!href) {
throw new Error(`Could not retrieve URL from import: ${importFn}`);
}
const absoluteUrl = new URL(href, import.meta.url);
const importFnCode = String(importFn);
console.log(absoluteUrl.href, importFnCode);
let interModuleCode: string;
// For some reason, Vite introduces indirection when importing some
// modules (e.g. "typegpu", but not "typegpu/data")
// The following conditions are meant to work around these
// code transformations.
if (importFnCode.includes('.then')) {
// The import code resembles something like:
// () => import(...).then(foo=>foo.bar)
const indirectionProperty = '???';
interModuleCode = `\
import mod from '${absoluteUrl.href}'
export
`;
if (options.hasDefaultExport) {
interModuleCode += '';
}
// interModuleCode = `...`;
} else {
interModuleCode = '';
}

// console.log(interModuleCode);

const userBlob = new Blob([interModuleCode], { type: 'text/javascript' });
const userModuleUrl = URL.createObjectURL(userBlob);

// const module = await import(userModuleUrl);

// URL.revokeObjectURL(userModuleUrl);
// return module;

// console.log(userModuleUrl);

return userModuleUrl;
}

export function importMap(): Record<string, string> {
return {
'typegpu': importToUrl(() => import('./typegpu-reexport.ts'), { hasDefaultExport: true }),
'typegpu/data': importToUrl(() => import('./typegpu-data-reexport.ts'), {}),
'typegpu/std': importToUrl(() => import('./typegpu-std-reexport.ts'), {}),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'typegpu/data';
2 changes: 2 additions & 0 deletions apps/typegpu-docs/src/utils/importmap/typegpu-reexport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from 'typegpu';
export { default } from 'typegpu';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'typegpu/std';
Loading