dewdrop-sdk 0.8.0 → 0.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dewdrop-sdk",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "The Vite plugin that builds a three.js game for Dewdrop.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/build.js CHANGED
@@ -34,9 +34,12 @@ export const ASSETS_DIR = 'ui';
34
34
  // have before the entry ran: on Dewdrop the game starts from the entry
35
35
  // rather than the page, so the page's head (the stylesheets Vite linked,
36
36
  // the title) and its body (a loading screen, a HUD's containers) reach the
37
- // document through the entry, the page's scripts dropped.
37
+ // document through the entry, the page's scripts dropped. The block is one
38
+ // line, every line break of the page escaped in its string, so the entry's
39
+ // code starts a line down.
38
40
  export function openPage(html) {
39
- return `{ const page = new DOMParser().parseFromString(${JSON.stringify(html)}, 'text/html'); `
41
+ const literal = JSON.stringify(html).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
42
+ return `{ const page = new DOMParser().parseFromString(${literal}, 'text/html'); `
40
43
  + `for (const script of page.querySelectorAll('script')) script.remove(); `
41
44
  + `document.head.append(...page.head.childNodes); document.body.append(...page.body.childNodes); }\n`;
42
45
  }
package/src/vite.js CHANGED
@@ -4,7 +4,8 @@
4
4
  // output, no module preload, a root base, the files Vite emits under
5
5
  // `ui/`, the built page opened in the document by the entry, and the
6
6
  // build's record beside it, `dewdrop-build.json`; a game with a backend
7
- // gets a second chunk beside it, `backend.js`. Vite's dev
7
+ // gets a second chunk beside it, `backend.js`. Each chunk's source map is
8
+ // written beside it and named by none. Vite's dev
8
9
  // server is refused, since a game plays on Dewdrop alone: the refusal
9
10
  // says where. The plugin imports nothing from Vite itself, so it works
10
11
  // linked from a checkout as well as installed, on Vite 3 through 8.
@@ -15,6 +16,23 @@ import { ON_DEWDROP } from './message.js';
15
16
 
16
17
  const BACKEND_SOURCES = ['backend.js', 'backend.ts', 'src/backend.js', 'src/backend.ts'];
17
18
 
19
+ // Every build writes source maps beside its chunks, `main.js.map` and
20
+ // `backend.js.map`, for the developer's own tools to read positions in the
21
+ // built code back to the source by; the chunks name no map, so nothing
22
+ // that runs them looks one up.
23
+ const SOURCE_MAPS = 'hidden';
24
+
25
+ // An emitted file's text, given as a string or as bytes.
26
+ const text = (source) => source instanceof Uint8Array ? new TextDecoder().decode(source) : String(source);
27
+
28
+ // A source map's JSON with its mappings moved down a line, for a chunk
29
+ // given a line above its code.
30
+ function shiftMap(json) {
31
+ const map = JSON.parse(json);
32
+ map.mappings = `;${map.mappings}`;
33
+ return JSON.stringify(map);
34
+ }
35
+
18
36
  // The imports Dewdrop supplies, left in the output: three and the paths
19
37
  // into it, and the SDK by its name. Dewdrop supplies nothing else, a path
20
38
  // into the SDK included, so the build takes every other import in.
@@ -47,6 +65,7 @@ export default function dew() {
47
65
  build: {
48
66
  modulePreload: false,
49
67
  assetsDir: ASSETS_DIR,
68
+ sourcemap: SOURCE_MAPS,
50
69
  rollupOptions: {
51
70
  external: isRuntimeModule,
52
71
  output: outputOptions(major),
@@ -65,6 +84,7 @@ export default function dew() {
65
84
  build: {
66
85
  emptyOutDir: false,
67
86
  copyPublicDir: false,
87
+ sourcemap: SOURCE_MAPS,
68
88
  rollupOptions: {
69
89
  input: entry,
70
90
  output: outputOptions(major, BACKEND_FILE),
@@ -92,8 +112,15 @@ export default function dew() {
92
112
  this.emitFile({ type: 'asset', fileName: BUILD_RECORD_FILE, source: buildRecord() });
93
113
  const page = bundle[PAGE_FILE];
94
114
  if (page?.type !== 'asset') return;
95
- const html = page.source instanceof Uint8Array ? new TextDecoder().decode(page.source) : String(page.source);
96
- chunk.code = openPage(html) + chunk.code;
115
+ chunk.code = openPage(text(page.source)) + chunk.code;
116
+ // The page opens on a line of its own above the entry's code, so the
117
+ // entry's map moves down a line: an empty line first in its mappings.
118
+ // Rollup from 3 on and Rolldown have emitted the map as a file of the
119
+ // bundle by now, so that file moves; Rollup 2, under Vite 3, writes
120
+ // the file later from the chunk's map, so that moves as well.
121
+ if (chunk.map) chunk.map.mappings = `;${chunk.map.mappings}`;
122
+ const map = bundle[chunk.sourcemapFileName ?? `${ENTRY_FILE}.map`];
123
+ if (map?.type === 'asset') map.source = shiftMap(text(map.source));
97
124
  },
98
125
  },
99
126
  };