@tileflow/vite 0.1.0-alpha.26 → 0.1.0-alpha.27

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 CHANGED
@@ -52,21 +52,29 @@ component can use `source={{map: 'madrid'}}`; it reads `/tileflow/manifest.json`
52
52
  a Tileflow map, not a renderer or direct style. Do not also run a CLI build that overwrites the same
53
53
  delivery manifest. Config loading executes trusted imports and is not a sandbox.
54
54
 
55
- ## Configure the worker
55
+ ## MapLibre's worker
56
56
 
57
- In a React application's client entry, before mounting an interactive map:
57
+ `tileflow()` also gives interactive maps MapLibre's worker, in development and production builds.
58
+ Each client module that imports `@tileflow/dom`, `@tileflow/react`, `@tileflow/vue`,
59
+ `@tileflow/svelte` or `@tileflow/elements` imports the worker of the MapLibre that package renders
60
+ with, the way Vite delivers workers, so the application needs no worker setup. Import
61
+ `maplibre-gl/dist/maplibre-gl.css` in the client.
62
+
63
+ An application that renders maps without `tileflow()`, for example from a CLI build or a hosted
64
+ manifest, adds only the worker:
65
+
66
+ <!-- docs:check -->
58
67
 
59
68
  ```ts
60
- import {configureTileflowMapLibre} from '@tileflow/react';
61
- import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url';
69
+ import {defineConfig} from 'vite';
70
+ import {tileflowMapLibreWorker} from '@tileflow/vite';
62
71
 
63
- configureTileflowMapLibre({workerUrl});
72
+ export default defineConfig({plugins: [tileflowMapLibreWorker()]});
64
73
  ```
65
74
 
66
- Vue and Svelte import the configuration function from their own adapter. This URL import is
67
- Vite-specific. Keep the worker and main module from the same installed MapLibre version, and import
68
- `maplibre-gl/dist/maplibre-gl.css` in the client. The browser needs the emitted worker closure, not
69
- a CDN worker from another version.
75
+ An explicit `configureTileflowMapLibre({workerUrl})` call still decides, and MapLibre's worker is
76
+ set once. Once a map has started with the plugin's worker, configuring a different one throws. To
77
+ deliver the worker yourself, turn the plugin's off with `tileflow({mapLibreWorker: false})`.
70
78
 
71
79
  ## Serve under a subpath
72
80
 
package/dist/index.d.ts CHANGED
@@ -13,8 +13,27 @@ type TileflowVitePluginOptions = {
13
13
  * transport used to hydrate exact locked pins. No build resolves a catalog head.
14
14
  */
15
15
  icons?: TileflowIconResolutionOptions;
16
+ /**
17
+ * Give interactive maps the application's own MapLibre worker, in development and production.
18
+ * On by default; `false` leaves the worker to an explicit `configureTileflowMapLibre` call.
19
+ */
20
+ mapLibreWorker?: boolean;
16
21
  overwriteHostedManifest?: boolean;
17
22
  };
18
- declare function tileflow(options?: TileflowVitePluginOptions): Plugin;
23
+ /**
24
+ * Prepare Tileflow maps for a Vite application and, unless `mapLibreWorker` is `false`, give its
25
+ * interactive maps MapLibre's worker.
26
+ */
27
+ declare function tileflow(options?: TileflowVitePluginOptions): Plugin[];
28
+ /**
29
+ * Give interactive Tileflow maps the application's own MapLibre worker, for an application that
30
+ * renders maps without `tileflow()`, such as one that only shows hosted maps.
31
+ *
32
+ * Every client module that imports `@tileflow/dom`, `@tileflow/react`, `@tileflow/vue`,
33
+ * `@tileflow/svelte` or `@tileflow/elements` also imports the worker of the MapLibre that package
34
+ * renders with, the way Vite delivers a worker (`?worker&url`), and offers it as the default. An
35
+ * explicit `configureTileflowMapLibre` call still decides, and the worker is configured once.
36
+ */
37
+ declare function tileflowMapLibreWorker(): Plugin;
19
38
 
20
- export { type TileflowVitePluginOptions, tileflow as default, tileflow };
39
+ export { type TileflowVitePluginOptions, tileflow as default, tileflow, tileflowMapLibreWorker };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
- import { resolve } from "path";
2
+ import { existsSync, realpathSync } from "fs";
3
+ import { dirname, isAbsolute, join, resolve } from "path";
3
4
  import {
4
5
  assertNoLocalTilesetsForProduction,
5
6
  assertTileflowSelfHostedManifestTarget,
@@ -21,6 +22,73 @@ import {
21
22
  writeTileflowNodeResponse
22
23
  } from "@tileflow/dev/server";
23
24
  function tileflow(options = {}) {
25
+ const plugin = tileflowArtifacts(options);
26
+ return options.mapLibreWorker === false ? [plugin] : [plugin, tileflowMapLibreWorker()];
27
+ }
28
+ function tileflowMapLibreWorker() {
29
+ const workerPaths = /* @__PURE__ */ new Map();
30
+ let root = process.cwd();
31
+ let warned = false;
32
+ return {
33
+ name: "tileflow:maplibre-worker",
34
+ // After framework plugins have turned components into JavaScript, before Vite rewrites imports.
35
+ enforce: "post",
36
+ configResolved(config) {
37
+ root = config.root;
38
+ },
39
+ transform(code, id, transformOptions) {
40
+ if (transformOptions?.ssr || !isApplicationModule(id)) return null;
41
+ const packageName = tileflowBrowserPackageImport.exec(code)?.[2];
42
+ if (packageName === void 0) return null;
43
+ const importerDirectory = isAbsolute(id) ? dirname(stripQuery(id)) : root;
44
+ const cacheKey = `${importerDirectory}\0${packageName}`;
45
+ if (!workerPaths.has(cacheKey)) {
46
+ workerPaths.set(
47
+ cacheKey,
48
+ findMapLibreWorker(`@tileflow/${packageName}`, importerDirectory)
49
+ );
50
+ }
51
+ const workerPath = workerPaths.get(cacheKey);
52
+ if (workerPath === void 0) {
53
+ if (!warned) {
54
+ warned = true;
55
+ this.warn(
56
+ `Unable to find the MapLibre worker of @tileflow/${packageName} from ${id}; configure it with configureTileflowMapLibre.`
57
+ );
58
+ }
59
+ return null;
60
+ }
61
+ const prelude = `import __tileflowMapLibreWorkerUrl from ${JSON.stringify(`${normalizePath(workerPath)}?worker&url`)};globalThis[Symbol.for('tileflow.maplibre.workerUrl')] ??= __tileflowMapLibreWorkerUrl;`;
62
+ return { code: `${prelude}${code}`, map: null };
63
+ }
64
+ };
65
+ }
66
+ var tileflowBrowserPackageImport = /(?:\bfrom|\bimport)\s*\(?\s*(['"])@tileflow\/(dom|elements|react|svelte|vue)(?:\/[^'"]*)?\1/u;
67
+ function isApplicationModule(id) {
68
+ if (id.startsWith("\0") || /[\\/]node_modules[\\/]/u.test(id)) return false;
69
+ return !/[?&](?:worker|sharedworker|worker_file|url|raw|inline)\b/u.test(id);
70
+ }
71
+ function findMapLibreWorker(packageName, fromDirectory) {
72
+ const packageDirectory = findPackageDirectory(packageName, fromDirectory);
73
+ if (packageDirectory === void 0) return void 0;
74
+ const domDirectory = packageName === "@tileflow/dom" ? packageDirectory : findPackageDirectory("@tileflow/dom", packageDirectory) ?? packageDirectory;
75
+ const mapLibreDirectory = findPackageDirectory("maplibre-gl", domDirectory);
76
+ if (mapLibreDirectory === void 0) return void 0;
77
+ const workerPath = join(mapLibreDirectory, "dist", "maplibre-gl-worker.mjs");
78
+ return existsSync(workerPath) ? workerPath : void 0;
79
+ }
80
+ function findPackageDirectory(packageName, fromDirectory) {
81
+ for (let directory = fromDirectory; ; directory = dirname(directory)) {
82
+ const candidate = join(directory, "node_modules", packageName);
83
+ if (existsSync(join(candidate, "package.json"))) return realpathSync(candidate);
84
+ if (dirname(directory) === directory) return void 0;
85
+ }
86
+ }
87
+ function stripQuery(id) {
88
+ const queryIndex = id.indexOf("?");
89
+ return queryIndex === -1 ? id : id.slice(0, queryIndex);
90
+ }
91
+ function tileflowArtifacts(options) {
24
92
  const basePath = normalizeTileflowBasePath(options.base ?? "/tileflow");
25
93
  const configPath = options.config ?? defaultTileflowConfigPath;
26
94
  let viteConfig = null;
@@ -187,5 +255,6 @@ function getViteDevBasePaths(publicBase, basePath) {
187
255
  }
188
256
  export {
189
257
  index_default as default,
190
- tileflow
258
+ tileflow,
259
+ tileflowMapLibreWorker
191
260
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tileflow/vite",
3
- "version": "0.1.0-alpha.26",
3
+ "version": "0.1.0-alpha.27",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Vite plugin for Tileflow map style development and static artifacts.",
6
6
  "engines": {
@@ -41,14 +41,14 @@
41
41
  "access": "public"
42
42
  },
43
43
  "dependencies": {
44
- "@tileflow/dev": ">=0.1.0-alpha.39 <0.1.0-beta.0"
44
+ "@tileflow/dev": ">=0.1.0-alpha.45 <0.1.0-beta.0"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "vite": ">=5 <9"
48
48
  },
49
49
  "devDependencies": {
50
50
  "vite": "^8.3.4",
51
- "@tileflow/maps": "0.1.0-alpha.17"
51
+ "@tileflow/maps": "0.1.0-alpha.22"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "tsup src/index.ts --format esm --dts --clean --external vite --platform node",