Skip to content

Commit b5b4fc3

Browse files
authored
fix: issues caused by the new build method (#122)
* fix(ecs): change tsup config to handle wasm * fix(graphics): change unhandled Graphics import
1 parent 5271432 commit b5b4fc3

7 files changed

Lines changed: 26 additions & 70 deletions

File tree

example/pong/src/components.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Graphics } from "@nanoforge-dev/graphics-2d";
1+
import { type Circle, type Rect } from "@nanoforge-dev/graphics-2d";
22
import type { InputEnum } from "@nanoforge-dev/input";
33

44
import { layer } from "./index";
@@ -38,19 +38,19 @@ export class Hitbox {
3838

3939
export class CircleComponent {
4040
name = "CircleComponent";
41-
component: Graphics.Circle;
41+
component: Circle;
4242

43-
constructor(component: Graphics.Circle) {
43+
constructor(component: Circle) {
4444
this.component = component;
4545
layer.add(this.component);
4646
}
4747
}
4848

4949
export class RectangleComponent {
5050
name = "RectangleComponent";
51-
component: Graphics.Rect;
51+
component: Rect;
5252

53-
constructor(component: Graphics.Rect) {
53+
constructor(component: Rect) {
5454
this.component = component;
5555
layer.add(this.component);
5656
}

example/pong/src/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { AssetManagerLibrary } from "@nanoforge-dev/asset-manager/src";
12
import { type IRunOptions } from "@nanoforge-dev/common";
23
import { NanoforgeFactory } from "@nanoforge-dev/core";
34
import { ECSLibrary } from "@nanoforge-dev/ecs";
4-
import { Graphics, Graphics2DLibrary } from "@nanoforge-dev/graphics-2d";
5+
import { Circle, Graphics2DLibrary, Layer, Rect } from "@nanoforge-dev/graphics-2d";
56
import { InputEnum } from "@nanoforge-dev/input";
7+
import { InputLibrary } from "@nanoforge-dev/input/src";
68
import { SoundLibrary } from "@nanoforge-dev/sound";
79

810
import {
@@ -18,15 +20,19 @@ import { bounce, controlPlayer, drawCircle, move, moveRectangle } from "./system
1820

1921
export const app = NanoforgeFactory.createClient();
2022

21-
export const layer = new Graphics.Layer();
23+
export const layer = new Layer();
2224

2325
export const main = async (options: IRunOptions) => {
26+
const assetManager = new AssetManagerLibrary();
2427
const graphics = new Graphics2DLibrary();
2528
const ecsLibrary = new ECSLibrary();
29+
const inputLibrary = new InputLibrary();
2630
const sounds = new SoundLibrary();
2731

32+
app.useAssetManager(assetManager);
2833
app.useGraphics(graphics);
2934
app.useComponentSystem(ecsLibrary);
35+
app.useInput(inputLibrary);
3036
app.useSound(sounds);
3137

3238
await app.init(options);
@@ -47,7 +53,7 @@ export const main = async (options: IRunOptions) => {
4753
registry.addComponent(
4854
ball,
4955
new CircleComponent(
50-
new Graphics.Circle({
56+
new Circle({
5157
radius: 70,
5258
fill: "red",
5359
}),
@@ -61,7 +67,7 @@ export const main = async (options: IRunOptions) => {
6167
registry.addComponent(player1, new Controller(InputEnum.KeyW, InputEnum.KeyS));
6268
registry.addComponent(
6369
player1,
64-
new RectangleComponent(new Graphics.Rect({ fill: "blue", width: 50, height: 500 })),
70+
new RectangleComponent(new Rect({ fill: "blue", width: 50, height: 500 })),
6571
);
6672

6773
const player2 = registry.spawnEntity();
@@ -71,7 +77,7 @@ export const main = async (options: IRunOptions) => {
7177
registry.addComponent(player2, new Controller(InputEnum.ArrowUp, InputEnum.ArrowDown));
7278
registry.addComponent(
7379
player2,
74-
new RectangleComponent(new Graphics.Rect({ fill: "blue", width: 50, height: 500 })),
80+
new RectangleComponent(new Rect({ fill: "blue", width: 50, height: 500 })),
7581
);
7682

7783
registry.addSystem(move);

packages/ecs/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"Tchips <leo.outmizguine@epitech.eu>"
1919
],
2020
"files": [
21-
"dist",
22-
"lib"
21+
"dist"
2322
],
2423
"main": "./dist/index.cjs",
2524
"module": "./dist/index.js",

packages/ecs/tsup.config.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,3 @@
1-
import fs from "node:fs";
2-
import path from "node:path";
3-
41
import { createTsupConfig } from "../../tsup.config.js";
52

6-
const wasmPlugin = {
7-
name: "wasm",
8-
setup(build) {
9-
// Resolve ".wasm" files to a path with a namespace
10-
build.onResolve({ filter: /\.wasm$/ }, (args) => {
11-
// If this is the import inside the stub module, import the
12-
// binary itself. Put the path in the "wasm-binary" namespace
13-
// to tell our binary load callback to load the binary file.
14-
if (args.namespace === "wasm-stub") {
15-
return {
16-
path: args.path,
17-
namespace: "wasm-binary",
18-
};
19-
}
20-
21-
// Otherwise, generate the JavaScript stub module for this
22-
// ".wasm" file. Put it in the "wasm-stub" namespace to tell
23-
// our stub load callback to fill it with JavaScript.
24-
//
25-
// Resolve relative paths to absolute paths here since this
26-
// resolve callback is given "resolveDir", the directory to
27-
// resolve imports against.
28-
if (args.resolveDir === "") {
29-
return; // Ignore unresolvable paths
30-
}
31-
return {
32-
path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
33-
namespace: "wasm-stub",
34-
};
35-
});
36-
37-
// Virtual modules in the "wasm-stub" namespace are filled with
38-
// the JavaScript code for compiling the WebAssembly binary. The
39-
// binary itself is imported from a second virtual module.
40-
build.onLoad({ filter: /.*/, namespace: "wasm-stub" }, async (args) => ({
41-
contents: `import wasm from ${JSON.stringify(args.path)}
42-
export default (imports) =>
43-
WebAssembly.instantiate(wasm, imports).then(
44-
result => result.instance.exports)`,
45-
}));
46-
47-
// Virtual modules in the "wasm-binary" namespace contain the
48-
// actual bytes of the WebAssembly file. This uses esbuild's
49-
// built-in "binary" loader instead of manually embedding the
50-
// binary data inside JavaScript code ourselves.
51-
build.onLoad({ filter: /.*/, namespace: "wasm-binary" }, async (args) => ({
52-
contents: await fs.promises.readFile(args.path),
53-
loader: "binary",
54-
}));
55-
},
56-
};
57-
58-
export default [createTsupConfig({ esbuildPlugins: [wasmPlugin] })];
3+
export default [createTsupConfig()];

packages/graphics-2d/src/graphics-2d.library.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseGraphicsLibrary, type InitContext } from "@nanoforge-dev/common";
22

3-
import { Graphics } from ".";
3+
import * as Graphics from "./exports/konva";
44

55
export class Graphics2DLibrary extends BaseGraphicsLibrary {
66
private _stage?: Graphics.Stage;

packages/graphics-2d/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { Graphics2DLibrary } from "./graphics-2d.library";
2-
export * as Graphics from "./exports/konva";
2+
export * from "./exports/konva";

tsup.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ export function createTsupConfig({
2121
keepNames = true,
2222
dts = true,
2323
sourcemap = true,
24+
esbuildOptions = (options) => {
25+
options.assetNames = "assets/[name]";
26+
},
2427
esbuildPlugins = [],
2528
treeshake = false,
2629
outDir = "dist",
30+
loader = { ".wasm": "copy" },
2731
}: Options = {}) {
2832
return defineConfig({
2933
entry,
@@ -42,8 +46,10 @@ export function createTsupConfig({
4246
keepNames,
4347
dts,
4448
sourcemap,
49+
esbuildOptions,
4550
esbuildPlugins,
4651
treeshake,
4752
outDir,
53+
loader,
4854
});
4955
}

0 commit comments

Comments
 (0)