@workadventure/map-starter-kit-core 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -13
- package/package.json +4 -3
- package/src/server.ts +0 -124
package/README.md
CHANGED
|
@@ -16,7 +16,83 @@ Core app, HTML pages and static assets for the **WorkAdventure Map Starter Kit**
|
|
|
16
16
|
npm install @workadventure/map-starter-kit-core
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
**Peer / consumer:** The built server expects `express` to be available at runtime. For TypeScript consumers, `@types/express`
|
|
19
|
+
**Peer / consumer:** The built server expects `express` to be available at runtime. For TypeScript consumers, install `@types/express` so the exported `Application` type resolves.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Package exports and usage from another package
|
|
24
|
+
|
|
25
|
+
This package is built and published as a **single server bundle**. Consumers must use the built output, not the source, to avoid module resolution errors.
|
|
26
|
+
|
|
27
|
+
### How exports are managed
|
|
28
|
+
|
|
29
|
+
| Field | Role |
|
|
30
|
+
|--------|------|
|
|
31
|
+
| **`main`** | Entry when a tool doesn’t use `exports`. Should point to `dist/server.js` so the bundle is used, not `src/server.ts`. |
|
|
32
|
+
| **`types`** | Tells TypeScript which declaration file to use for the package entry (`dist/server.d.ts`). |
|
|
33
|
+
| **`exports`** | Modern entry map: `"."` and `"./dist/server.js"` resolve to `dist/server.js` with types `dist/server.d.ts`. |
|
|
34
|
+
| **`files`** | What gets published: `dist`, `public`, `README.md`. Source (`src/`) is not published so consumers never resolve to `.ts` files. |
|
|
35
|
+
|
|
36
|
+
Recommended in **package.json** for correct consumption:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"main": "dist/server.js",
|
|
41
|
+
"types": "dist/server.d.ts",
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"types": "./dist/server.d.ts",
|
|
45
|
+
"import": "./dist/server.js",
|
|
46
|
+
"require": "./dist/server.js"
|
|
47
|
+
},
|
|
48
|
+
"./dist/server.js": {
|
|
49
|
+
"types": "./dist/server.d.ts",
|
|
50
|
+
"import": "./dist/server.js",
|
|
51
|
+
"require": "./dist/server.js"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"files": ["dist", "public", "README.md"]
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Using the package in another project
|
|
59
|
+
|
|
60
|
+
**1. Install the package**
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm install @workadventure/map-starter-kit-core
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**2. Import the Express app (ESM)**
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import core from "@workadventure/map-starter-kit-core";
|
|
70
|
+
|
|
71
|
+
const app = core.default; // or core.viteNodeApp (same Express Application)
|
|
72
|
+
|
|
73
|
+
app.listen(3000, () => console.log("Listening on 3000"));
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**3. Or use the explicit entry (same result)**
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import core from "@workadventure/map-starter-kit-core/dist/server.js";
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**4. TypeScript**
|
|
83
|
+
|
|
84
|
+
Types come from `dist/server.d.ts`: the default export is typed as `{ default: Application; viteNodeApp: Application }`. Ensure the consuming project has `express` and `@types/express` so the `Application` type resolves.
|
|
85
|
+
|
|
86
|
+
**5. CommonJS**
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const core = require("@workadventure/map-starter-kit-core");
|
|
90
|
+
const app = core.default;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Important:** Do not import from `@workadventure/map-starter-kit-core/src/server` or rely on `src/` in the package. Only the built `dist/server.js` and its types are the supported contract.
|
|
94
|
+
|
|
95
|
+
---
|
|
20
96
|
|
|
21
97
|
## Usage
|
|
22
98
|
|
|
@@ -25,7 +101,7 @@ npm install @workadventure/map-starter-kit-core
|
|
|
25
101
|
Use the built Express app in your own server:
|
|
26
102
|
|
|
27
103
|
```ts
|
|
28
|
-
import core from "@workadventure/map-starter-kit-core
|
|
104
|
+
import core from "@workadventure/map-starter-kit-core";
|
|
29
105
|
|
|
30
106
|
const app = core.default; // or core.viteNodeApp
|
|
31
107
|
|
|
@@ -33,7 +109,7 @@ const app = core.default; // or core.viteNodeApp
|
|
|
33
109
|
app.listen(3000, () => console.log("Listening on 3000"));
|
|
34
110
|
```
|
|
35
111
|
|
|
36
|
-
Types are provided
|
|
112
|
+
Types are provided via `dist/server.d.ts`: `core` is typed as `{ default: Application; viteNodeApp: Application }`.
|
|
37
113
|
|
|
38
114
|
### Development server (standalone)
|
|
39
115
|
|
|
@@ -61,23 +137,30 @@ npm run build
|
|
|
61
137
|
map-starter-kit-core/
|
|
62
138
|
├── src/
|
|
63
139
|
│ ├── server.ts # Express app entry (CORS, static, routes)
|
|
64
|
-
│ ├──
|
|
65
|
-
│
|
|
66
|
-
│
|
|
67
|
-
│
|
|
68
|
-
│
|
|
69
|
-
│
|
|
70
|
-
|
|
71
|
-
│
|
|
72
|
-
├──
|
|
140
|
+
│ ├── utils/
|
|
141
|
+
│ │ └── getCoreRoot.ts # Resolve core package root (cwd vs package dir)
|
|
142
|
+
│ └── controllers/
|
|
143
|
+
│ ├── FrontController.ts # HTML pages (Mustache): /, step1-git, step2-hosting, step3-*, step4-*
|
|
144
|
+
│ ├── MapController.ts # /maps/list – list .tmj maps with properties
|
|
145
|
+
│ └── UploaderController.ts # /uploader/* – configure, status, maps-storage-list, upload
|
|
146
|
+
├── public/ # Static assets
|
|
147
|
+
│ ├── assets/
|
|
148
|
+
│ │ ├── js/ # Client scripts (e.g. index.js – maps list, Mustache)
|
|
149
|
+
│ │ └── views/ # HTML views (index.html, step1-git.html, …)
|
|
150
|
+
│ ├── images/
|
|
151
|
+
│ └── styles/
|
|
73
152
|
├── types/
|
|
74
153
|
│ └── server.d.ts # Module declaration for dist/server.js (copied to dist on build)
|
|
75
|
-
├── dist/ # Build output (
|
|
154
|
+
├── dist/ # Build output (published)
|
|
155
|
+
│ ├── server.js # Bundled server entry
|
|
156
|
+
│ └── server.d.ts # Types for consumers
|
|
76
157
|
├── vite.config.ts
|
|
77
158
|
├── tsconfig.json
|
|
78
159
|
└── package.json
|
|
79
160
|
```
|
|
80
161
|
|
|
162
|
+
Only `dist`, `public`, and `README.md` are included in the published package (`files`). Source (`src/`) is not published so consumers always get the built bundle.
|
|
163
|
+
|
|
81
164
|
## API / Routes
|
|
82
165
|
|
|
83
166
|
| Method | Path | Description |
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workadventure/map-starter-kit-core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Core app, HTML pages and static assets for the WorkAdventure Map Starter Kit. Update this package to get new UI and server features without touching your maps or config.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "dist/server.js",
|
|
6
|
+
"types": "dist/server.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"dev": "vite",
|
|
8
9
|
"build": "tsc && vite build && node -e \"require('fs').copyFileSync('types/server.d.ts','dist/server.d.ts')\""
|
|
@@ -46,7 +47,7 @@
|
|
|
46
47
|
"require": "./dist/server.js"
|
|
47
48
|
}
|
|
48
49
|
},
|
|
49
|
-
"
|
|
50
|
+
"dependencies": {
|
|
50
51
|
"@types/cors": "^2.8.19",
|
|
51
52
|
"@types/express": "^5.0.6",
|
|
52
53
|
"@types/mustache": "^4.2.6",
|
package/src/server.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
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 { getCoreRoot } from './utils/getCoreRoot.ts';
|
|
6
|
-
import { FrontController } from './controllers/FrontController.ts';
|
|
7
|
-
import { MapController } from './controllers/MapController.ts';
|
|
8
|
-
import { UploaderController } from './controllers/UploaderController.ts';
|
|
9
|
-
|
|
10
|
-
const app = express();
|
|
11
|
-
|
|
12
|
-
const corsOptions = {
|
|
13
|
-
credentials: true, // Allow sending cookies
|
|
14
|
-
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
|
15
|
-
allowedHeaders: [
|
|
16
|
-
'Content-Type',
|
|
17
|
-
'Authorization',
|
|
18
|
-
'X-Requested-With',
|
|
19
|
-
'Accept',
|
|
20
|
-
'Origin',
|
|
21
|
-
'Access-Control-Request-Method',
|
|
22
|
-
'Access-Control-Request-Headers'
|
|
23
|
-
],
|
|
24
|
-
exposedHeaders: ['Content-Length', 'Content-Type'],
|
|
25
|
-
maxAge: 86400, // Cache the OPTIONS requests for 24 hours
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// Apply the CORS middleware
|
|
29
|
-
// The CORS middleware automatically handles the OPTIONS requests (preflight)
|
|
30
|
-
app.use(cors(corsOptions));
|
|
31
|
-
|
|
32
|
-
// Parse JSON bodies
|
|
33
|
-
app.use(express.json());
|
|
34
|
-
|
|
35
|
-
// Configure the static assets for Express
|
|
36
|
-
const staticOptions = {
|
|
37
|
-
maxAge: '1d', // Cache the files for 1 day
|
|
38
|
-
etag: true, // Enable ETag for cache validation
|
|
39
|
-
lastModified: true, // Enable Last-Modified header
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// Serve dist/assets FIRST with explicit MIME type configuration
|
|
43
|
-
// This ensures compiled JavaScript files from getMapsScripts are served correctly
|
|
44
|
-
// This route must be before express.static('.') to take precedence
|
|
45
|
-
app.use('/assets', express.static(path.join(process.cwd(), 'dist', 'assets'), staticOptions));
|
|
46
|
-
// Serve the public folder from core (project root or package root for easy updates)
|
|
47
|
-
app.use('/public', express.static(path.join(getCoreRoot(), 'public'), staticOptions));
|
|
48
|
-
// Serve the tilesets folder with a longer cache (rarely modified)
|
|
49
|
-
app.use('/tilesets', express.static(path.join(process.cwd(), 'tilesets'), {
|
|
50
|
-
maxAge: '7d',
|
|
51
|
-
etag: true,
|
|
52
|
-
lastModified: true,
|
|
53
|
-
}));
|
|
54
|
-
|
|
55
|
-
// Middleware to exclude /src from express.static - let Vite handle TypeScript transformation
|
|
56
|
-
// VitePluginNode will automatically add Vite middleware that transforms TypeScript files
|
|
57
|
-
const staticMiddleware = express.static('.', staticOptions);
|
|
58
|
-
|
|
59
|
-
// Middleware to transform and serve TypeScript files as JavaScript
|
|
60
|
-
// This bundles the file with its dependencies to resolve npm imports
|
|
61
|
-
app.use('/src', async (req, res, next) => {
|
|
62
|
-
// Only handle .ts and .tsx files - transform them to JavaScript
|
|
63
|
-
if (req.path.endsWith('.ts') || req.path.endsWith('.tsx')) {
|
|
64
|
-
try {
|
|
65
|
-
// req.path includes /src/, so we need to join it correctly
|
|
66
|
-
const filePath = path.join(process.cwd(), 'src', req.path.startsWith('/') ? req.path.slice(1) : req.path);
|
|
67
|
-
|
|
68
|
-
// Check if file exists
|
|
69
|
-
if (!fs.existsSync(filePath)) {
|
|
70
|
-
return res.status(404).send('File not found');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Use dynamic import to get esbuild (available via Vite)
|
|
74
|
-
const esbuild = await import('esbuild');
|
|
75
|
-
|
|
76
|
-
// Bundle the TypeScript file with its dependencies
|
|
77
|
-
// This resolves npm imports like @workadventure/scripting-api-extra
|
|
78
|
-
const result = await esbuild.build({
|
|
79
|
-
entryPoints: [filePath],
|
|
80
|
-
bundle: true,
|
|
81
|
-
format: 'esm',
|
|
82
|
-
target: 'esnext',
|
|
83
|
-
write: false,
|
|
84
|
-
platform: 'browser',
|
|
85
|
-
sourcemap: false,
|
|
86
|
-
// Externalize WorkAdventure global API (available in the browser)
|
|
87
|
-
external: ['WA'],
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
|
|
91
|
-
return res.send(result.outputFiles[0].text);
|
|
92
|
-
} catch (error) {
|
|
93
|
-
console.error('Error transforming TypeScript file:', error);
|
|
94
|
-
return next(error);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// For non-TypeScript files in /src, pass to next middleware
|
|
98
|
-
next();
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// Serve static files, but skip /src (handled above)
|
|
102
|
-
app.use((req, res, next) => {
|
|
103
|
-
// Skip /src requests - they are handled by the transformation middleware above
|
|
104
|
-
if (req.path.startsWith('/src/')) {
|
|
105
|
-
return next(); // Let the transformation middleware handle it or pass to Vite
|
|
106
|
-
}
|
|
107
|
-
// For other files, use express.static
|
|
108
|
-
staticMiddleware(req, res, next);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const controllers = [
|
|
112
|
-
new MapController(app),
|
|
113
|
-
new FrontController(app),
|
|
114
|
-
new UploaderController(app),
|
|
115
|
-
];
|
|
116
|
-
|
|
117
|
-
// Verify and log all controllers created
|
|
118
|
-
controllers.forEach(controller => {
|
|
119
|
-
console.info(`Controller started: ${controller.constructor.name}`);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
export default app;
|
|
123
|
-
// Export for VitePluginNode compatibility
|
|
124
|
-
export const viteNodeApp = app;
|