@tileflow/vite 0.1.0-alpha.1 → 0.1.0-alpha.11
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/dist/index.js +64 -51
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { resolve } from "path";
|
|
2
3
|
import {
|
|
3
4
|
createTileflowBuildArtifacts,
|
|
4
5
|
createTileflowDevRequestHandler,
|
|
6
|
+
createTileflowNodeRequest,
|
|
5
7
|
defaultTileflowConfigPath,
|
|
6
|
-
|
|
8
|
+
getTileflowAssetBasePath,
|
|
9
|
+
getTileflowAssetFileName,
|
|
10
|
+
getTileflowWatchPaths,
|
|
11
|
+
isTileflowRequestUrl,
|
|
12
|
+
joinTileflowPublicUrl,
|
|
13
|
+
normalizeTileflowBasePath,
|
|
14
|
+
writeTileflowNodeResponse
|
|
7
15
|
} from "@tileflow/dev";
|
|
8
|
-
import { resolve } from "path";
|
|
9
16
|
function tileflow(options = {}) {
|
|
10
17
|
const basePath = normalizeTileflowBasePath(options.base ?? "/tileflow");
|
|
11
18
|
const configPath = options.config ?? defaultTileflowConfigPath;
|
|
@@ -18,6 +25,7 @@ function tileflow(options = {}) {
|
|
|
18
25
|
configureServer(server) {
|
|
19
26
|
const root = server.config.root;
|
|
20
27
|
const configFile = resolve(root, configPath);
|
|
28
|
+
const watchedIconPaths = /* @__PURE__ */ new Set();
|
|
21
29
|
const handler = createTileflowDevRequestHandler({
|
|
22
30
|
basePath,
|
|
23
31
|
config: configPath,
|
|
@@ -28,21 +36,47 @@ function tileflow(options = {}) {
|
|
|
28
36
|
},
|
|
29
37
|
tileBaseUrl: options.tileBaseUrl
|
|
30
38
|
});
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
const refreshWatchedIconPaths = async () => {
|
|
40
|
+
try {
|
|
41
|
+
const watchPaths = await getTileflowWatchPaths({
|
|
42
|
+
config: configPath,
|
|
43
|
+
cwd: root
|
|
44
|
+
});
|
|
45
|
+
for (const watchPath of watchPaths) {
|
|
46
|
+
if (!watchedIconPaths.has(watchPath)) {
|
|
47
|
+
server.watcher.add(watchPath);
|
|
48
|
+
watchedIconPaths.add(watchPath);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const message = error instanceof Error ? error.message : "Unknown Tileflow error";
|
|
53
|
+
server.config.logger.warn(`[tileflow] Unable to watch icon paths: ${message}`);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const reloadIfTileflowInput = (file) => {
|
|
33
57
|
if (file === configFile) {
|
|
58
|
+
void refreshWatchedIconPaths();
|
|
34
59
|
server.ws.send({ type: "full-reload" });
|
|
60
|
+
return;
|
|
35
61
|
}
|
|
36
|
-
|
|
62
|
+
if (isWatchedIconPath(file, watchedIconPaths)) {
|
|
63
|
+
server.ws.send({ type: "full-reload" });
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
server.watcher.add(configFile);
|
|
67
|
+
void refreshWatchedIconPaths();
|
|
68
|
+
server.watcher.on("add", reloadIfTileflowInput);
|
|
69
|
+
server.watcher.on("change", reloadIfTileflowInput);
|
|
70
|
+
server.watcher.on("unlink", reloadIfTileflowInput);
|
|
37
71
|
server.middlewares.use(async (request, response, next) => {
|
|
38
|
-
if (!
|
|
72
|
+
if (!isTileflowRequestUrl(request.url, basePath)) {
|
|
39
73
|
next();
|
|
40
74
|
return;
|
|
41
75
|
}
|
|
42
76
|
try {
|
|
43
|
-
const tileflowRequest =
|
|
77
|
+
const tileflowRequest = createTileflowNodeRequest(request);
|
|
44
78
|
const tileflowResponse = await handler(tileflowRequest);
|
|
45
|
-
await
|
|
79
|
+
await writeTileflowNodeResponse(response, tileflowResponse);
|
|
46
80
|
} catch (error) {
|
|
47
81
|
next(error);
|
|
48
82
|
}
|
|
@@ -57,71 +91,50 @@ function tileflow(options = {}) {
|
|
|
57
91
|
return;
|
|
58
92
|
}
|
|
59
93
|
const artifacts = await createTileflowBuildArtifacts({
|
|
94
|
+
assetBaseUrl: joinTileflowPublicUrl(config.base, basePath),
|
|
60
95
|
config: configPath,
|
|
61
96
|
cwd: config.root,
|
|
62
|
-
styleBaseUrl:
|
|
97
|
+
styleBaseUrl: joinTileflowPublicUrl(config.base, basePath),
|
|
63
98
|
tileBaseUrl: options.tileBaseUrl
|
|
64
99
|
});
|
|
65
|
-
const assetBase =
|
|
100
|
+
const assetBase = getTileflowAssetBasePath(basePath);
|
|
66
101
|
this.emitFile({
|
|
67
|
-
fileName:
|
|
102
|
+
fileName: getTileflowAssetFileName(assetBase, "manifest.json"),
|
|
68
103
|
source: `${JSON.stringify(artifacts.manifest, null, 2)}
|
|
69
104
|
`,
|
|
70
105
|
type: "asset"
|
|
71
106
|
});
|
|
72
107
|
for (const [mapName, style] of Object.entries(artifacts.styles)) {
|
|
73
108
|
this.emitFile({
|
|
74
|
-
fileName:
|
|
109
|
+
fileName: getTileflowAssetFileName(assetBase, `styles/${mapName}.json`),
|
|
75
110
|
source: `${JSON.stringify(style, null, 2)}
|
|
76
111
|
`,
|
|
77
112
|
type: "asset"
|
|
78
113
|
});
|
|
79
114
|
}
|
|
115
|
+
for (const asset of artifacts.assets) {
|
|
116
|
+
this.emitFile({
|
|
117
|
+
fileName: getTileflowAssetFileName(assetBase, asset.fileName),
|
|
118
|
+
source: asset.source,
|
|
119
|
+
type: "asset"
|
|
120
|
+
});
|
|
121
|
+
}
|
|
80
122
|
}
|
|
81
123
|
};
|
|
82
124
|
}
|
|
83
125
|
var index_default = tileflow;
|
|
84
|
-
function
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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}`;
|
|
126
|
+
function isWatchedIconPath(file, watchedIconPaths) {
|
|
127
|
+
const normalizedFile = normalizePath(file);
|
|
128
|
+
for (const watchPath of watchedIconPaths) {
|
|
129
|
+
const normalizedWatchPath = normalizePath(watchPath);
|
|
130
|
+
if (normalizedFile === normalizedWatchPath || normalizedFile.startsWith(`${normalizedWatchPath}/`)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
117
133
|
}
|
|
118
|
-
return
|
|
119
|
-
}
|
|
120
|
-
function assetBasePath(basePath) {
|
|
121
|
-
return basePath.replace(/^\/+|\/+$/g, "");
|
|
134
|
+
return false;
|
|
122
135
|
}
|
|
123
|
-
function
|
|
124
|
-
return
|
|
136
|
+
function normalizePath(value) {
|
|
137
|
+
return value.replace(/\\/g, "/");
|
|
125
138
|
}
|
|
126
139
|
export {
|
|
127
140
|
index_default as default,
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tileflow/vite",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.11",
|
|
4
4
|
"description": "Vite plugin for Tileflow map style development and static artifacts.",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
6
7
|
"homepage": "https://tileflow.dev",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
@@ -35,7 +36,7 @@
|
|
|
35
36
|
"access": "public"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@tileflow/dev": "0.1.0-alpha.
|
|
39
|
+
"@tileflow/dev": "0.1.0-alpha.11"
|
|
39
40
|
},
|
|
40
41
|
"peerDependencies": {
|
|
41
42
|
"vite": ">=5"
|