@tileflow/vite 0.1.0-alpha.0

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 ADDED
@@ -0,0 +1,15 @@
1
+ # @tileflow/vite
2
+
3
+ Vite plugin for local Tileflow style development and static build artifacts.
4
+
5
+ ```ts
6
+ import react from "@vitejs/plugin-react";
7
+ import { tileflow } from "@tileflow/vite";
8
+ import { defineConfig } from "vite";
9
+
10
+ export default defineConfig({
11
+ plugins: [react(), tileflow()],
12
+ });
13
+ ```
14
+
15
+ Docs: https://tileflow.dev/docs
@@ -0,0 +1,11 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ type TileflowVitePluginOptions = {
4
+ base?: string;
5
+ config?: string;
6
+ emitBuildArtifacts?: boolean;
7
+ tileBaseUrl?: string;
8
+ };
9
+ declare function tileflow(options?: TileflowVitePluginOptions): Plugin;
10
+
11
+ export { type TileflowVitePluginOptions, tileflow as default, tileflow };
package/dist/index.js ADDED
@@ -0,0 +1,129 @@
1
+ // src/index.ts
2
+ import {
3
+ createTileflowBuildArtifacts,
4
+ createTileflowDevRequestHandler,
5
+ defaultTileflowConfigPath,
6
+ normalizeTileflowBasePath
7
+ } from "@tileflow/dev";
8
+ import { resolve } from "path";
9
+ function tileflow(options = {}) {
10
+ const basePath = normalizeTileflowBasePath(options.base ?? "/tileflow");
11
+ const configPath = options.config ?? defaultTileflowConfigPath;
12
+ let viteConfig = null;
13
+ return {
14
+ name: "tileflow:vite",
15
+ configResolved(config) {
16
+ viteConfig = config;
17
+ },
18
+ configureServer(server) {
19
+ const root = server.config.root;
20
+ const configFile = resolve(root, configPath);
21
+ const handler = createTileflowDevRequestHandler({
22
+ basePath,
23
+ config: configPath,
24
+ cwd: root,
25
+ onError(error) {
26
+ const message = error instanceof Error ? error.message : "Unknown Tileflow error";
27
+ server.config.logger.error(`[tileflow] ${message}`);
28
+ },
29
+ tileBaseUrl: options.tileBaseUrl
30
+ });
31
+ server.watcher.add(configFile);
32
+ server.watcher.on("change", (file) => {
33
+ if (file === configFile) {
34
+ server.ws.send({ type: "full-reload" });
35
+ }
36
+ });
37
+ server.middlewares.use(async (request, response, next) => {
38
+ if (!isTileflowRequest(request.url, basePath)) {
39
+ next();
40
+ return;
41
+ }
42
+ try {
43
+ const tileflowRequest = toWebRequest(request);
44
+ const tileflowResponse = await handler(tileflowRequest);
45
+ await writeNodeResponse(response, tileflowResponse);
46
+ } catch (error) {
47
+ next(error);
48
+ }
49
+ });
50
+ },
51
+ async generateBundle() {
52
+ if (options.emitBuildArtifacts === false) {
53
+ return;
54
+ }
55
+ const config = viteConfig;
56
+ if (!config) {
57
+ return;
58
+ }
59
+ const artifacts = await createTileflowBuildArtifacts({
60
+ config: configPath,
61
+ cwd: config.root,
62
+ styleBaseUrl: joinPublicUrl(config.base, basePath),
63
+ tileBaseUrl: options.tileBaseUrl
64
+ });
65
+ const assetBase = assetBasePath(basePath);
66
+ this.emitFile({
67
+ fileName: assetFileName(assetBase, "manifest.json"),
68
+ source: `${JSON.stringify(artifacts.manifest, null, 2)}
69
+ `,
70
+ type: "asset"
71
+ });
72
+ for (const [mapName, style] of Object.entries(artifacts.styles)) {
73
+ this.emitFile({
74
+ fileName: assetFileName(assetBase, `styles/${mapName}.json`),
75
+ source: `${JSON.stringify(style, null, 2)}
76
+ `,
77
+ type: "asset"
78
+ });
79
+ }
80
+ }
81
+ };
82
+ }
83
+ var index_default = tileflow;
84
+ function isTileflowRequest(url, basePath) {
85
+ const pathname = (url ?? "/").split("?")[0] ?? "/";
86
+ if (!basePath) {
87
+ return pathname === "/manifest.json" || pathname === "/style.json" || pathname === "/" || pathname.startsWith("/styles/");
88
+ }
89
+ return pathname === basePath || pathname.startsWith(`${basePath}/`);
90
+ }
91
+ function toWebRequest(request) {
92
+ const protocol = getHeader(request, "x-forwarded-proto") ?? "http";
93
+ const host = getHeader(request, "host") ?? "localhost";
94
+ return new Request(`${protocol}://${host}${request.url ?? "/"}`, {
95
+ method: request.method
96
+ });
97
+ }
98
+ function getHeader(request, name) {
99
+ const value = request.headers[name];
100
+ return Array.isArray(value) ? value[0] : value;
101
+ }
102
+ async function writeNodeResponse(response, tileflowResponse) {
103
+ response.statusCode = tileflowResponse.status;
104
+ tileflowResponse.headers.forEach((value, key) => {
105
+ response.setHeader(key, value);
106
+ });
107
+ response.end(Buffer.from(await tileflowResponse.arrayBuffer()));
108
+ }
109
+ function joinPublicUrl(viteBase, basePath) {
110
+ const trimmedBase = viteBase.replace(/\/+$/g, "");
111
+ const trimmedPath = basePath.replace(/^\/+|\/+$/g, "");
112
+ if (!trimmedPath) {
113
+ return trimmedBase || "";
114
+ }
115
+ if (!trimmedBase || trimmedBase === ".") {
116
+ return `/${trimmedPath}`;
117
+ }
118
+ return `${trimmedBase}/${trimmedPath}`;
119
+ }
120
+ function assetBasePath(basePath) {
121
+ return basePath.replace(/^\/+|\/+$/g, "");
122
+ }
123
+ function assetFileName(basePath, fileName) {
124
+ return basePath ? `${basePath}/${fileName}` : fileName;
125
+ }
126
+ export {
127
+ index_default as default,
128
+ tileflow
129
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@tileflow/vite",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Vite plugin for Tileflow map style development and static artifacts.",
5
+ "type": "module",
6
+ "homepage": "https://tileflow.dev",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/tileflow/tileflow.git",
10
+ "directory": "packages/vite"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/tileflow/tileflow/issues"
14
+ },
15
+ "keywords": [
16
+ "tileflow",
17
+ "vite",
18
+ "maps",
19
+ "maplibre",
20
+ "plugin"
21
+ ],
22
+ "main": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {
38
+ "@tileflow/dev": "0.1.0-alpha.0"
39
+ },
40
+ "peerDependencies": {
41
+ "vite": ">=5"
42
+ },
43
+ "devDependencies": {
44
+ "vite": "^8.0.16"
45
+ },
46
+ "scripts": {
47
+ "build": "tsup src/index.ts --format esm --dts --clean --external vite --platform node",
48
+ "typecheck": "tsc -p tsconfig.json --noEmit",
49
+ "lint": "tsc -p tsconfig.json --noEmit"
50
+ }
51
+ }