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
2 changes: 2 additions & 0 deletions packages/unplugin-typegpu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@
"acorn": "^8.14.1",
"rolldown": "1.0.0-beta.33",
"rollup": "~4.37.0",
"webpack": "^5.0.0",
"webpack-virtual-modules": "^0.6.2",
"tsdown": "catalog:build",
"typescript": "catalog:types"
}
Expand Down
250 changes: 249 additions & 1 deletion packages/unplugin-typegpu/test/aliasing.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { babelTransform, rollupTransform } from './transform.ts';
import { babelTransform, rollupTransform, webpackTransform } from './transform.ts';

describe('[BABEL] tgpu alias gathering', () => {
it('works with default import named not tgpu', () => {
Expand Down Expand Up @@ -144,3 +144,251 @@ describe('[ROLLUP] tgpu alias gathering', () => {
`);
});
});

describe('[WEBPACK] tgpu alias gathering', () => {
it('works with default import named not tgpu', async () => {
const code = `\
import hello from 'typegpu';

const increment = hello.fn([])(() => {
});

console.log(increment);
`;

expect(await webpackTransform(code)).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_typegpu__ from "typegpu";
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/***/ ((module) => {

module.exports = __WEBPACK_EXTERNAL_MODULE_typegpu__;

/***/ })
/******/ ]);
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
(() => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var typegpu__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


const increment = typegpu__WEBPACK_IMPORTED_MODULE_0__["default"].fn([])((($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (() => {
}), {
v: 1,
name: undefined,
ast: {"params":[],"body":[0,[]],"externalNames":[]},
externals: () => ({}),
}) && $.f)({})));

console.log(increment);

})();

"
`);
});

it('works with aliased tgpu import', async () => {
const code = `\
import { tgpu as t } from 'typegpu';

const increment = t.fn([])(() => {
});

console.log(increment);
`;

expect(await webpackTransform(code)).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_typegpu__ from "typegpu";
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/***/ ((module) => {

module.exports = __WEBPACK_EXTERNAL_MODULE_typegpu__;

/***/ })
/******/ ]);
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
(() => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var typegpu__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


const increment = typegpu__WEBPACK_IMPORTED_MODULE_0__.tgpu.fn([])((($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (() => {
}), {
v: 1,
name: undefined,
ast: {"params":[],"body":[0,[]],"externalNames":[]},
externals: () => ({}),
}) && $.f)({})));

console.log(increment);

})();

"
`);
});

it('works with namespace import', async () => {
const code = `\
import * as t from 'typegpu';

const increment = t.tgpu.fn([])(() => {
});

console.log(increment);
`;

expect(await webpackTransform(code)).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_typegpu__ from "typegpu";
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/***/ ((module) => {

module.exports = __WEBPACK_EXTERNAL_MODULE_typegpu__;

/***/ })
/******/ ]);
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
(() => {
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var typegpu__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);


const increment = typegpu__WEBPACK_IMPORTED_MODULE_0__.tgpu.fn([])((($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (() => {
}), {
v: 1,
name: undefined,
ast: {"params":[],"body":[0,[]],"externalNames":[]},
externals: () => ({}),
}) && $.f)({})));

console.log(increment);

})();

"
`);
});
});
Loading
Loading