-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Create typegpu babel plugin #804
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cf43de3
Create typegpu babel plugin
mhawryluk fcc4554
Update lock
mhawryluk 7b2e9fd
Create common `plugin-typegpu` package for all build plugins
mhawryluk 4b07354
Extract more common implementation parts of both plugins
mhawryluk a222586
Small refactor
mhawryluk 864e770
Rename plugin to unplugin
mhawryluk d08c2bf
Apply suggestions from code review
mhawryluk 5217aed
Try to fix ci error
mhawryluk df8cb4b
Revert
mhawryluk 28cda84
Merge branch 'main' into feat/rn-plugin
mhawryluk 4c5c200
Fix object value in parsers
mhawryluk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<div align="center"> | ||
|
||
# plugin-typegpu | ||
|
||
🚧 **Under Construction** 🚧 - [GitHub](https://github.com/software-mansion/TypeGPU/tree/main/packages/plugin) | ||
|
||
</div> | ||
|
||
Build plugins for [TypeGPU](https://typegpu.com), transpiling JavaScript functions into WGSL at build-time. | ||
|
||
Currently supported: | ||
* babel | ||
```js | ||
// babel.config.js (React Native with Expo) | ||
|
||
module.exports = function (api) { | ||
api.cache(true); | ||
return { | ||
presets: ['babel-preset-expo'], | ||
plugins: ['plugin-typegpu/babel'] | ||
}; | ||
}; | ||
|
||
``` | ||
|
||
* rollup | ||
|
||
```ts | ||
// vite.config.js | ||
|
||
import { defineConfig } from 'vite'; | ||
import typegpu from 'plugin-typegpu/rollup'; | ||
|
||
export default defineConfig({ | ||
plugins: [typegpu()], | ||
}); | ||
|
||
``` | ||
|
||
## Getting Started | ||
|
||
```sh | ||
npm install plugin-typegpu | ||
``` | ||
|
||
## TypeGPU is created by Software Mansion | ||
|
||
[![swm](https://logo.swmansion.com/logo?color=white&variant=desktop&width=150&tag=typegpu-github 'Software Mansion')](https://swmansion.com) | ||
|
||
Since 2012 [Software Mansion](https://swmansion.com) is a software agency with experience in building web and mobile apps. We are Core React Native Contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product – [Hire us](https://swmansion.com/contact/projects?utm_source=typegpu&utm_medium=readme). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
"name": "plugin-typegpu", | ||
"version": "0.0.0", | ||
"description": "Build plugins for TypeGPU, transpiling JavaScript functions into WGSL at build-time.", | ||
"keywords": [ | ||
"rollup-plugin", | ||
"babel-plugin", | ||
"vite-plugin", | ||
"typegpu", | ||
"gpgpu", | ||
"webgpu", | ||
"wgpu", | ||
"wgsl", | ||
"typescript", | ||
"shaders" | ||
], | ||
"license": "MIT", | ||
"type": "module", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.js", | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"./rollup": { | ||
"types": "./dist/rollup.d.ts", | ||
"module": "./dist/rollup.js", | ||
"import": "./dist/rollup.js", | ||
"default": "./dist/rollup.cjs" | ||
}, | ||
"./babel": { | ||
"types": "./dist/babel.d.ts", | ||
"module": "./dist/babel.js", | ||
"import": "./dist/babel.js", | ||
"default": "./dist/babel.cjs" | ||
} | ||
}, | ||
"sideEffects": false, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/software-mansion/TypeGPU.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/software-mansion/TypeGPU/issues" | ||
}, | ||
"homepage": "https://typegpu.com", | ||
"scripts": { | ||
"dev:watch": "DEV=true tsup --watch", | ||
"dev:build": "DEV=true tsup", | ||
"build": "tsup", | ||
"test:types": "pnpm tsc --p ./tsconfig.json --noEmit", | ||
"publish": "echo \"Use pnpm prepare-package instead!\" && exit 1", | ||
"prepare-package": "tgpu-dev-cli prepack" | ||
}, | ||
"dependencies": { | ||
"tinyest-for-wgsl": "workspace:~0.1.0-alpha.0", | ||
"estree-walker": "^3.0.3", | ||
"magic-string": "^0.30.11", | ||
"@babel/standalone": "^7.26.6" | ||
}, | ||
"devDependencies": { | ||
"@babel/template": "^7.25.9", | ||
"@babel/types": "^7.26.5", | ||
"@typegpu/tgpu-dev-cli": "workspace:*", | ||
"@types/babel__standalone": "^7.1.9", | ||
"@types/babel__template": "^7.4.4", | ||
"@types/babel__traverse": "^7.20.6", | ||
"acorn": "^8.12.1", | ||
"rollup": "~4.12.0", | ||
"tsup": "^8.0.2", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import * as Babel from '@babel/standalone'; | ||
import type TemplateGenerator from '@babel/template'; | ||
import type { TraverseOptions } from '@babel/traverse'; | ||
import type * as t from '@babel/types'; | ||
import { transpileFn } from 'tinyest-for-wgsl'; | ||
import { | ||
type Context, | ||
type TypegpuPluginOptions, | ||
embedJSON, | ||
gatherTgpuAliases, | ||
isDoesCall, | ||
shouldSkipFile, | ||
} from './common'; | ||
|
||
// NOTE: @babel/standalone does expose internal packages, as specified in the docs, but the | ||
// typing for @babel/standalone does not expose them. | ||
const template = ( | ||
Babel as unknown as { packages: { template: typeof TemplateGenerator } } | ||
).packages.template; | ||
const types = (Babel as unknown as { packages: { types: typeof t } }).packages | ||
.types; | ||
|
||
function functionVisitor(ctx: Context): TraverseOptions { | ||
return { | ||
ImportDeclaration(path) { | ||
gatherTgpuAliases(path.node, ctx); | ||
}, | ||
|
||
CallExpression(path) { | ||
const node = path.node; | ||
|
||
if (isDoesCall(node, ctx)) { | ||
const implementation = node.arguments[0]; | ||
|
||
if ( | ||
implementation && | ||
!(implementation.type === 'TemplateLiteral') && | ||
!(implementation.type === 'StringLiteral') | ||
) { | ||
const { argNames, body, externalNames } = transpileFn(implementation); | ||
|
||
path.replaceWith( | ||
types.callExpression(node.callee, [ | ||
types.callExpression(template.expression('tgpu.__assignAst')(), [ | ||
implementation, | ||
template.expression`${embedJSON({ argNames, body, externalNames })}`(), | ||
externalNames.length > 0 | ||
? template.expression`{${externalNames.join(', ')}}`() | ||
: template.expression`undefined`(), | ||
]), | ||
]), | ||
); | ||
|
||
path.skip(); | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
export default function () { | ||
return { | ||
visitor: { | ||
Program(path, state) { | ||
// @ts-ignore | ||
const code: string | undefined = state.file?.code; | ||
// @ts-ignore | ||
const options: TypegpuPluginOptions | undefined = state.opts; | ||
// @ts-ignore | ||
const id: string | undefined = state.filename; | ||
|
||
if (shouldSkipFile(options, id, code)) { | ||
return; | ||
} | ||
|
||
const ctx: Context = { | ||
tgpuAliases: new Set(['tgpu']), | ||
}; | ||
|
||
path.traverse(functionVisitor(ctx)); | ||
}, | ||
} satisfies TraverseOptions, | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're planning to rewrite the plugin in
unplugin
(at least the parts that will run on rollup and rollup-adjacent bundlers), we should IMO go with the nameunplugin-typegpu
.https://github.com/unplugin/unplugin-starter/tree/main
It will be less ambiguous what the plugin is for, and will be easier to find 🙌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, could we call the directory
unplugin-typegpu
instead of matching the current convention and calling itunplugin
? I feel like for packages living under the@typegpu
namespace it would be fine, but this will make it more clear which package it represents.