Skip to content

Commit 4fb8e5e

Browse files
committed
Finish new with new package "@workadventure/map-starter-kit-core"
1 parent fb0b455 commit 4fb8e5e

34 files changed

Lines changed: 1926 additions & 5773 deletions

README.md

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,43 @@ Uploading a map using the [WA map storage](https://docs.workadventu.re/map-build
2424

2525
```
2626
map-starter-kit/
27-
├── 📁 app/ # Server-side code (Node.js/Express)
28-
│ ├── app.ts # Main Express application
29-
│ └── controller/ # Express route controllers
30-
│ ├── FrontController.ts
31-
│ ├── MapController.ts
32-
│ └── UploaderController.ts
27+
├── 📁 app/ # Server entry point (loads @workadventure/map-starter-kit-core)
28+
│ └── app.ts # Re-exports the Express app from the core package
3329
3430
├── 📁 src/ # Map scripts (Browser/WorkAdventure) ⚠️ REQUIRED
3531
│ └── main.ts # Your map scripts go here
3632
37-
├── 📁 public/ # Static assets (images, CSS, etc.)
38-
│ ├── assets/
39-
│ ├── images/
40-
│ └── styles.css
4133
4234
├── 📁 tilesets/ # Map tileset images (PNG)
4335
4436
├── 📄 *.tmj # Map files (office.tmj, conference.tmj, etc.)
45-
├── 📄 *.html # HTML pages (index.html, step1-git.html, etc.)
4637
├── 📄 vite.config.ts # Vite configuration
4738
└── 📄 package.json # Dependencies and scripts
4839
```
4940

41+
The **server** (Express app, controllers, HTML publishing pages, static assets) is provided by the npm package **`@workadventure/map-starter-kit-core`**. Updating this dependency gives you new publishing UI and server features without changing your maps or config.
42+
5043
### Quick Reference
5144

52-
- *`public/`*: Static files like PDFs or audio files
5345
- *`src/`*: **Map scripts** (MUST be here for compilation) ⚠️
5446
- *`tilesets/`*: All PNG tilesets
55-
- *`app/`*: **Server-side code** (Express.js backend)
47+
- *`app/`*: **Server entry point** – loads the core package; do not add server logic here
5648

5749
> [!TIP]
5850
> - If you want to use more than one map file, just add the new map file in the root folder (we recommend creating a copy of *office.tmj* and editing it to avoid any mistakes).
5951
> - We recommend using **512x512** images for the map thumbnails.
60-
> - If you are going to create custom websites to embed in the map, please reference the HTML files in the `input` option in *vite.config.js*.
52+
> - If you are going to create custom websites to embed in the map, please reference the HTML files in the `input` option in *buildmap.vite.config.js*.
53+
54+
### 📁 Server entry point (`app/`)
6155

62-
### 📁 Server-side Development (`app/`)
56+
The `app/` directory contains only the **entry point** that loads the server from **`@workadventure/map-starter-kit-core`**.
6357

64-
The `app/` directory contains the server-side Express.js application code. See [app/README.md](./app/README.md) for detailed documentation.
58+
- *`app.ts`*: Imports and re-exports the Express app from the core package (for Vite’s server plugin).
6559

66-
- *`app.ts`*: Main Express application entry point
67-
- *`controller/`*: Express controllers for handling routes
68-
- *`FrontController.ts`*: Handles frontend routes and HTML rendering
69-
- *`MapController.ts`*: Handles map-related API endpoints
70-
- *`UploaderController.ts`*: Handles map upload and configuration
60+
The actual server (Express, routes, HTML pages, upload, map storage) lives in the dependency. To get updates to the publishing UI and server behaviour, run `npm update @workadventure/map-starter-kit-core`.
7161

7262
> [!IMPORTANT]
73-
> **Server-side code** should be placed in the `app/` directory. This code runs on the Node.js server and is not compiled by Vite for the browser.
63+
> Do **not** add server logic or new controllers in `app/`. The server is fully provided by the core package.
7464
7565
### 📁 Map Scripts Development (`src/`) ⚠️
7666

@@ -109,7 +99,7 @@ The `src/` directory is where you **MUST** place **all map-related scripts** tha
10999
npm install
110100
```
111101

112-
This will install all required dependencies including Vite, TypeScript, WorkAdventure packages, and Express.js.
102+
This will install all required dependencies, including Vite, TypeScript, WorkAdventure packages, and **`@workadventure/map-starter-kit-core`** (server and publishing UI).
113103

114104
### 🚀 Development
115105

app/README.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/app.ts

Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,4 @@
1-
import express from 'express';
2-
import * as path from "node:path";
3-
import * as fs from "node:fs";
4-
import cors from 'cors';
5-
import { FrontController } from './controller/FrontController';
6-
import { MapController } from './controller/MapController';
7-
import { UploaderController } from './controller/UploaderController';
1+
import core from '@workadventure/map-starter-kit-core/dist/server.js';
82

9-
const app = express();
10-
11-
const corsOptions = {
12-
credentials: true, // Allow sending cookies
13-
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
14-
allowedHeaders: [
15-
'Content-Type',
16-
'Authorization',
17-
'X-Requested-With',
18-
'Accept',
19-
'Origin',
20-
'Access-Control-Request-Method',
21-
'Access-Control-Request-Headers'
22-
],
23-
exposedHeaders: ['Content-Length', 'Content-Type'],
24-
maxAge: 86400, // Cache the OPTIONS requests for 24 hours
25-
};
26-
27-
// Apply the CORS middleware
28-
// The CORS middleware automatically handles the OPTIONS requests (preflight)
29-
app.use(cors(corsOptions));
30-
31-
// Parse JSON bodies
32-
app.use(express.json());
33-
34-
// Configure the static assets for Express
35-
const staticOptions = {
36-
maxAge: '1d', // Cache the files for 1 day
37-
etag: true, // Enable ETag for cache validation
38-
lastModified: true, // Enable Last-Modified header
39-
};
40-
41-
// Serve dist/assets FIRST with explicit MIME type configuration
42-
// This ensures compiled JavaScript files from getMapsScripts are served correctly
43-
// This route must be before express.static('.') to take precedence
44-
app.use('/assets', express.static(path.join(process.cwd(), 'dist', 'assets'), staticOptions));
45-
// Serve the public folder with a custom path
46-
app.use('/public', express.static(path.join(process.cwd(), 'public'), staticOptions));
47-
// Serve the tilesets folder with a longer cache (rarely modified)
48-
app.use('/tilesets', express.static(path.join(process.cwd(), 'tilesets'), {
49-
maxAge: '7d',
50-
etag: true,
51-
lastModified: true,
52-
}));
53-
54-
// Middleware to exclude /src from express.static - let Vite handle TypeScript transformation
55-
// VitePluginNode will automatically add Vite middleware that transforms TypeScript files
56-
const staticMiddleware = express.static('.', staticOptions);
57-
58-
// Middleware to transform and serve TypeScript files as JavaScript
59-
// This bundles the file with its dependencies to resolve npm imports
60-
app.use('/src', async (req, res, next) => {
61-
// Only handle .ts and .tsx files - transform them to JavaScript
62-
if (req.path.endsWith('.ts') || req.path.endsWith('.tsx')) {
63-
try {
64-
// req.path includes /src/, so we need to join it correctly
65-
const filePath = path.join(process.cwd(), 'src', req.path.startsWith('/') ? req.path.slice(1) : req.path);
66-
67-
// Check if file exists
68-
if (!fs.existsSync(filePath)) {
69-
return res.status(404).send('File not found');
70-
}
71-
72-
// Use dynamic import to get esbuild (available via Vite)
73-
const esbuild = await import('esbuild');
74-
75-
// Bundle the TypeScript file with its dependencies
76-
// This resolves npm imports like @workadventure/scripting-api-extra
77-
const result = await esbuild.build({
78-
entryPoints: [filePath],
79-
bundle: true,
80-
format: 'esm',
81-
target: 'esnext',
82-
write: false,
83-
platform: 'browser',
84-
sourcemap: false,
85-
// Externalize WorkAdventure global API (available in the browser)
86-
external: ['WA'],
87-
});
88-
89-
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
90-
return res.send(result.outputFiles[0].text);
91-
} catch (error) {
92-
console.error('Error transforming TypeScript file:', error);
93-
return next(error);
94-
}
95-
}
96-
// For non-TypeScript files in /src, pass to next middleware
97-
next();
98-
});
99-
100-
// Serve static files, but skip /src (handled above)
101-
app.use((req, res, next) => {
102-
// Skip /src requests - they are handled by the transformation middleware above
103-
if (req.path.startsWith('/src/')) {
104-
return next(); // Let the transformation middleware handle it or pass to Vite
105-
}
106-
// For other files, use express.static
107-
staticMiddleware(req, res, next);
108-
});
109-
110-
const controllers = [
111-
new MapController(app),
112-
new FrontController(app),
113-
new UploaderController(app),
114-
];
115-
116-
// Verify and log all controllers created
117-
controllers.forEach(controller => {
118-
console.info(`Controller started: ${controller.constructor.name}`);
119-
});
120-
121-
export default app;
122-
// Export for VitePluginNode compatibility
123-
export const viteNodeApp = app;
3+
export default core.default;
4+
export const viteNodeApp = core.viteNodeApp;

app/controller/FrontController.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)