@silkui/vite 0.1.11 → 0.1.13

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +43 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@silkui/vite",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "description": "Vite plugin that builds a ui-js application and reloads it in the native window",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.js CHANGED
@@ -13,6 +13,10 @@ import { writeFileSync, rmSync } from "node:fs";
13
13
  import { builtinModules } from "node:module";
14
14
  import { dirname, join, relative, resolve } from "node:path";
15
15
  import { missingApis, ownerOf, report } from "./apis.js";
16
+
17
+ // The modules of `@silkui/core/globals`, and the globals it defines.
18
+ const CORE_GLOBALS = /[\\/](?:@silkui[\\/]core|packages[\\/]core)[\\/]src[\\/](?:globals|storage|location|indexeddb|worker)\.js$/;
19
+ const PROVIDED_BY_GLOBALS = ["document", "window", "navigator", "location", "history", "MutationObserver", "matchMedia", "Image", "HTMLElement", "localStorage", "sessionStorage", "indexedDB"];
16
20
  import { devPlugins } from "./dev.js";
17
21
  import { resolveOptions } from "./resolve.js";
18
22
  import { compileStylesheet } from "./compiler.js";
@@ -92,12 +96,36 @@ export default function silk(options = {}) {
92
96
  if (watching && options.host && child === null) startHost();
93
97
  },
94
98
  };
99
+ // `new Worker(new URL("./x.js", import.meta.url))`: Vite bundles the worker as a file of
100
+ // its own, which a Silk UI app carrying its bundle in memory does not have beside it. Its
101
+ // code goes into the bundle as a `data:` address instead.
102
+ const workerPlugin = {
103
+ name: "silk:workers",
104
+ apply: "build",
105
+ enforce: "post",
106
+ generateBundle: {
107
+ order: "post",
108
+ handler(_options, output) {
109
+ const app = output[fileName];
110
+ if (!app || app.type !== "chunk") return;
111
+ for (const item of Object.values(output)) {
112
+ if (item === app || !/\.[cm]?js$/.test(item.fileName)) continue;
113
+ const code = item.type === "chunk" ? item.code : typeof item.source === "string" ? item.source : new TextDecoder().decode(item.source);
114
+ const quoted = new RegExp(`(["'\`])[^"'\`]*?${escapeRegExp(item.fileName)}\\1`, "g");
115
+ if (!quoted.test(app.code)) continue;
116
+ app.code = app.code.replace(quoted, () => JSON.stringify("data:text/javascript," + encodeURIComponent(code)));
117
+ delete output[item.fileName];
118
+ }
119
+ },
120
+ },
121
+ };
95
122
  const apis = options.apis ?? "warn";
96
123
  const allow = new Set(options.allow ?? []);
97
124
  let root = "";
98
125
  let serving = false;
99
126
  /** name → { area, owners } */
100
127
  const uses = new Map();
128
+ let browserGlobals = false;
101
129
  const apiPlugin = {
102
130
  name: "silk:apis",
103
131
  enforce: "post",
@@ -109,6 +137,7 @@ export default function silk(options = {}) {
109
137
 
110
138
  buildStart() {
111
139
  uses.clear();
140
+ browserGlobals = false;
112
141
  },
113
142
 
114
143
  // After the other transforms, so what is scanned is JavaScript.
@@ -122,6 +151,12 @@ export default function silk(options = {}) {
122
151
  } catch {
123
152
  return;
124
153
  }
154
+ // `@silkui/core/globals` provides the browser globals, and its own modules test for what
155
+ // they use.
156
+ if (CORE_GLOBALS.test(id.split("?")[0])) {
157
+ browserGlobals ||= /globals\.js$/.test(id.split("?")[0]);
158
+ return;
159
+ }
125
160
  const owner = ownerOf(id, root);
126
161
  for (const { name, area } of missingApis(ast)) {
127
162
  if (serving && (name === "TextEncoder" || name === "TextDecoder")) continue;
@@ -136,6 +171,9 @@ export default function silk(options = {}) {
136
171
  },
137
172
 
138
173
  buildEnd(error) {
174
+ if (browserGlobals) for (const name of PROVIDED_BY_GLOBALS) uses.delete(name);
175
+ // Its Worker runs on the full runtime's worker threads.
176
+ if (browserGlobals && node) uses.delete("Worker");
139
177
  if (error || serving || uses.size === 0) return;
140
178
  if (apis === "error") this.error(report(uses));
141
179
  this.warn(report(uses));
@@ -175,7 +213,7 @@ export default function silk(options = {}) {
175
213
  if (!emittedStylesheet) rmSync(stylesheetPath, { force: true });
176
214
  },
177
215
  };
178
- const plugins = [buildPlugin, silkImports, apiPlugin, cssPlugin, stylesheetDevPlugin({ strict: options.strictStylesheets === true })];
216
+ const plugins = [buildPlugin, workerPlugin, silkImports, apiPlugin, cssPlugin, stylesheetDevPlugin({ strict: options.strictStylesheets === true })];
179
217
  if (node) plugins.push(builtinsPlugin);
180
218
  return [...plugins, ...devPlugins({ entry, port: options.port ?? 0, host: options.host })];
181
219
  }
@@ -209,6 +247,10 @@ const NOT_IN_NODE = new Set(["dom", "storage", "workers"]);
209
247
 
210
248
  const BUILTINS = new Set(builtinModules);
211
249
 
250
+ function escapeRegExp(text) {
251
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
252
+ }
253
+
212
254
  /** `node:<name>` when `id` names a Node builtin, with or without the prefix. */
213
255
  function builtinOf(id) {
214
256
  if (id.startsWith("node:")) return id;