@freshpack/runtime 1.0.1 → 1.0.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @freshpack/runtime
2
2
 
3
+ ## 1.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - workaround for webpack 112 bug
8
+
9
+ - Updated dependencies:
10
+ - @freshpack/webpack@1.0.2
11
+
3
12
  ## 1.0.1
4
13
 
5
14
  ### Patch Changes
package/client-hmr.js CHANGED
@@ -1,95 +1,88 @@
1
1
  /// <reference types="webpack/module" />
2
+
2
3
  let lastHash;
3
4
  function upToDate() {
4
- return lastHash.indexOf(__webpack_hash__) >= 0;
5
+ return lastHash.indexOf(__webpack_hash__) >= 0;
5
6
  }
6
7
  function logUpdate(updatedModules, renewedModules) {
7
- const unacceptedModules = updatedModules.filter((moduleId) => {
8
- return renewedModules && renewedModules.indexOf(moduleId) < 0;
8
+ const unacceptedModules = updatedModules.filter(moduleId => {
9
+ return renewedModules && renewedModules.indexOf(moduleId) < 0;
10
+ });
11
+ if (unacceptedModules.length > 0) {
12
+ console.warn("[HMR]", "The following modules couldn't be hot updated: (They would need a full reload!)", unacceptedModules);
13
+ }
14
+ if (!renewedModules || renewedModules.length === 0) {
15
+ console.info("[HMR]", "Nothing hot updated.");
16
+ } else {
17
+ console.info("[HMR]", "Updated modules:");
18
+ renewedModules.forEach(function (moduleId) {
19
+ if (typeof moduleId === "string" && moduleId.indexOf("!") !== -1) {
20
+ const parts = moduleId.split("!");
21
+ console.info("[HMR]", "-", parts.pop());
22
+ console.info("[HMR]", " ", "-", moduleId);
23
+ } else {
24
+ console.info("[HMR]", "-", moduleId);
25
+ }
9
26
  });
10
- if (unacceptedModules.length > 0) {
11
- console.warn("[HMR]", "The following modules couldn't be hot updated: (They would need a full reload!)", unacceptedModules);
12
- }
13
- if (!renewedModules || renewedModules.length === 0) {
14
- console.info("[HMR]", "Nothing hot updated.");
15
- }
16
- else {
17
- console.info("[HMR]", "Updated modules:");
18
- renewedModules.forEach(function (moduleId) {
19
- if (typeof moduleId === "string" && moduleId.indexOf("!") !== -1) {
20
- const parts = moduleId.split("!");
21
- console.info("[HMR]", "-", parts.pop());
22
- console.info("[HMR]", " ", "-", moduleId);
23
- }
24
- else {
25
- console.info("[HMR]", "-", moduleId);
26
- }
27
- });
28
- const numberIds = renewedModules.every((moduleId) => {
29
- return typeof moduleId === "number";
30
- });
31
- if (numberIds) {
32
- console.info("[HMR]", 'Consider using the optimization.moduleIds: "named" for module names.');
33
- }
27
+ const numberIds = renewedModules.every(moduleId => {
28
+ return typeof moduleId === "number";
29
+ });
30
+ if (numberIds) {
31
+ console.info("[HMR]", 'Consider using the optimization.moduleIds: "named" for module names.');
34
32
  }
33
+ }
35
34
  }
36
35
  async function checkUpdates(fromUpdate = false) {
37
- try {
38
- const updatedModules = await import.meta.webpackHot.check();
39
- if (!updatedModules) {
40
- if (fromUpdate)
41
- console.info("[HMR]", "Update applied");
42
- else
43
- console.warn("[HMR]", "Promised update was not found");
44
- return;
45
- }
46
- const renewedModules = await import.meta.webpackHot.apply({
47
- ignoreUnaccepted: true,
48
- onUnaccepted(data) {
49
- console.warn("[HMR]", "Update was not accepted:", data.chain.join(" => "));
50
- },
51
- });
52
- logUpdate(updatedModules, renewedModules);
53
- if (!upToDate()) {
54
- await checkUpdates(true);
55
- }
36
+ try {
37
+ const updatedModules = await import.meta.webpackHot.check();
38
+ if (!updatedModules) {
39
+ if (fromUpdate) console.info("[HMR]", "Update applied");else console.warn("[HMR]", "Promised update was not found");
40
+ return;
41
+ }
42
+ const renewedModules = await import.meta.webpackHot.apply({
43
+ ignoreUnaccepted: true,
44
+ onUnaccepted(data) {
45
+ console.warn("[HMR]", "Update was not accepted:", data.chain.join(" => "));
46
+ }
47
+ });
48
+ logUpdate(updatedModules, renewedModules);
49
+ if (!upToDate()) {
50
+ await checkUpdates(true);
56
51
  }
57
- catch (e) {
58
- const status = import.meta.webpackHot.status();
59
- if (["abort", "fail"].indexOf(status) >= 0) {
60
- console.warn("[HMR]", "Can't apply update, restart required!", e);
61
- }
62
- else {
63
- console.warn("[HMR]", "Update failed", e);
64
- }
52
+ } catch (e) {
53
+ const status = import.meta.webpackHot.status();
54
+ if (["abort", "fail"].indexOf(status) >= 0) {
55
+ console.warn("[HMR]", "Can't apply update, restart required!", e);
56
+ } else {
57
+ console.warn("[HMR]", "Update failed", e);
65
58
  }
59
+ }
66
60
  }
67
61
  async function signalled() {
68
- if (import.meta.webpackHot.status() !== "idle") {
69
- console.warn("[HMR]", `Bundle update, but state is not idle: ${import.meta.webpackHot.status()}`);
70
- return;
71
- }
72
- await checkUpdates();
62
+ if (import.meta.webpackHot.status() !== "idle") {
63
+ console.warn("[HMR]", `Bundle update, but state is not idle: ${import.meta.webpackHot.status()}`);
64
+ return;
65
+ }
66
+ await checkUpdates();
73
67
  }
74
68
  if (import.meta.webpackHot) {
75
- const ws = new WebSocket("/_freshpack_hmr");
76
- ws.addEventListener("open", () => {
77
- console.info("[HMR]", "opened");
78
- });
79
- ws.addEventListener("message", (msg) => {
80
- const data = JSON.parse(msg.data);
81
- if (data === "invalid") {
82
- console.info("[HMR]", "webpack is working...");
83
- }
84
- else if ("done" in data) {
85
- const hash = data.done.hash;
86
- lastHash = hash;
87
- console.info("[HMR]", "webpack rebuilt", hash);
88
- signalled();
89
- }
90
- });
91
- ws.addEventListener("close", () => {
92
- console.info("[HMR]", "closed");
93
- });
69
+ const ws = new WebSocket("/_freshpack_hmr");
70
+ ws.addEventListener("open", () => {
71
+ console.info("[HMR]", "opened");
72
+ });
73
+ ws.addEventListener("message", msg => {
74
+ const data = JSON.parse(msg.data);
75
+ if (data === "invalid") {
76
+ console.info("[HMR]", "webpack is working...");
77
+ } else if ("done" in data) {
78
+ const hash = data.done.hash;
79
+ lastHash = hash;
80
+ console.info("[HMR]", "webpack rebuilt", hash);
81
+ signalled();
82
+ }
83
+ });
84
+ ws.addEventListener("close", () => {
85
+ console.info("[HMR]", "closed");
86
+ });
94
87
  }
95
- export {};
88
+ //# sourceMappingURL=client-hmr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["client-hmr.ts"],"sourcesContent":["/// <reference types=\"webpack/module\" />\n\nlet lastHash: string | undefined;\nfunction upToDate() {\n return lastHash!.indexOf(__webpack_hash__) >= 0;\n}\n\nfunction logUpdate(\n updatedModules: (string | number)[],\n renewedModules: (string | number)[] | null,\n) {\n const unacceptedModules = updatedModules.filter((moduleId) => {\n return renewedModules && renewedModules.indexOf(moduleId) < 0;\n });\n\n if (unacceptedModules.length > 0) {\n console.warn(\n \"[HMR]\",\n \"The following modules couldn't be hot updated: (They would need a full reload!)\",\n unacceptedModules,\n );\n }\n\n if (!renewedModules || renewedModules.length === 0) {\n console.info(\"[HMR]\", \"Nothing hot updated.\");\n } else {\n console.info(\"[HMR]\", \"Updated modules:\");\n renewedModules.forEach(function (moduleId) {\n if (typeof moduleId === \"string\" && moduleId.indexOf(\"!\") !== -1) {\n const parts = moduleId.split(\"!\");\n console.info(\"[HMR]\", \"-\", parts.pop());\n console.info(\"[HMR]\", \" \", \"-\", moduleId);\n } else {\n console.info(\"[HMR]\", \"-\", moduleId);\n }\n });\n const numberIds = renewedModules.every((moduleId) => {\n return typeof moduleId === \"number\";\n });\n if (numberIds) {\n console.info(\n \"[HMR]\",\n 'Consider using the optimization.moduleIds: \"named\" for module names.',\n );\n }\n }\n}\n\nasync function checkUpdates(fromUpdate = false) {\n try {\n const updatedModules = await import.meta.webpackHot.check();\n if (!updatedModules) {\n if (fromUpdate) console.info(\"[HMR]\", \"Update applied\");\n else console.warn(\"[HMR]\", \"Promised update was not found\");\n return;\n }\n const renewedModules = await import.meta.webpackHot.apply({\n ignoreUnaccepted: true,\n onUnaccepted(data) {\n console.warn(\n \"[HMR]\",\n \"Update was not accepted:\",\n data.chain.join(\" => \"),\n );\n },\n });\n\n logUpdate(updatedModules, renewedModules);\n\n if (!upToDate()) {\n await checkUpdates(true);\n }\n } catch (e) {\n const status = import.meta.webpackHot.status();\n if ([\"abort\", \"fail\"].indexOf(status) >= 0) {\n console.warn(\"[HMR]\", \"Can't apply update, restart required!\", e);\n } else {\n console.warn(\"[HMR]\", \"Update failed\", e);\n }\n }\n}\n\nasync function signalled() {\n if (import.meta.webpackHot.status() !== \"idle\") {\n console.warn(\n \"[HMR]\",\n `Bundle update, but state is not idle: ${import.meta.webpackHot.status()}`,\n );\n return;\n }\n await checkUpdates();\n}\n\nif (import.meta.webpackHot) {\n const ws = new WebSocket(\"/_freshpack_hmr\");\n ws.addEventListener(\"open\", () => {\n console.info(\"[HMR]\", \"opened\");\n });\n ws.addEventListener(\"message\", (msg) => {\n const data = JSON.parse(msg.data);\n if (data === \"invalid\") {\n console.info(\"[HMR]\", \"webpack is working...\");\n } else if (\"done\" in data) {\n const hash = data.done.hash;\n lastHash = hash;\n console.info(\"[HMR]\", \"webpack rebuilt\", hash);\n signalled();\n }\n });\n ws.addEventListener(\"close\", () => {\n console.info(\"[HMR]\", \"closed\");\n });\n}\n"],"mappings":"AAAA;;AAEA,IAAI,QAA4B;AAChC,SAAS,QAAQ,GAAG;EAClB,OAAO,QAAQ,CAAE,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC;AACjD;AAEA,SAAS,SAAS,CAChB,cAAmC,EACnC,cAA0C,EAC1C;EACA,MAAM,iBAAiB,GAAG,cAAc,CAAC,MAAM,CAAE,QAAQ,IAAK;IAC5D,OAAO,cAAc,IAAI,cAAc,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC;EAC/D,CAAC,CAAC;EAEF,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;IAChC,OAAO,CAAC,IAAI,CACV,OAAO,EACP,iFAAiF,EACjF,iBACF,CAAC;EACH;EAEA,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE;IAClD,OAAO,CAAC,IAAI,CAAC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I,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;IACjC,IAAI,IAAI,KAAK,SAAS,EAAE;MACtB,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,uBAAuB,CAAC;IAChD,CAAC,MAAM,IAAI,MAAM,IAAI,IAAI,EAAE;MACzB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI;MAC3B,QAAQ,GAAG,IAAI;MACf,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,iBAAiB,EAAE,IAAI,CAAC;MAC9C,SAAS,CAAC,CAAC;IACb;EACF,CAAC,CAAC;EACF,EAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,MAAM;IACjC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC;EACjC,CAAC,CAAC;AACJ","ignoreList":[]}
package/internal.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export let unmapping = undefined;
2
2
  export function setUnmapping(newUnmapping) {
3
- unmapping = newUnmapping;
3
+ unmapping = newUnmapping;
4
4
  }
5
+ //# sourceMappingURL=internal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["internal.ts"],"sourcesContent":["export let unmapping: Record<string, URL | string> | undefined = undefined;\n\nexport function setUnmapping(newUnmapping: Record<string, URL | string>) {\n unmapping = newUnmapping;\n}\n"],"mappings":"AAAA,OAAO,IAAI,SAAmD,GAAG,SAAS;AAE1E,OAAO,SAAS,YAAY,CAAC,YAA0C,EAAE;EACvE,SAAS,GAAG,YAAY;AAC1B","ignoreList":[]}
package/mod.js CHANGED
@@ -1,18 +1,19 @@
1
1
  import { unmapping } from "./internal.js";
2
2
  const IS_BROWSER = typeof document !== "undefined";
3
3
  export function unmap(name, url) {
4
- if (IS_BROWSER) {
5
- console.error("browser unmapping should be done using webpack plugin");
6
- return url.href;
7
- }
8
- if (!unmapping) {
9
- console.error("missing unmapping registration");
10
- return url.href;
11
- }
12
- const unmapped = unmapping[name];
13
- if (!unmapped) {
14
- console.error("missing unmapping registration for", name);
15
- return url.href;
16
- }
17
- return unmapped.toString();
4
+ if (IS_BROWSER) {
5
+ console.error("browser unmapping should be done using webpack plugin");
6
+ return url.href;
7
+ }
8
+ if (!unmapping) {
9
+ console.error("missing unmapping registration");
10
+ return url.href;
11
+ }
12
+ const unmapped = unmapping[name];
13
+ if (!unmapped) {
14
+ console.error("missing unmapping registration for", name);
15
+ return url.href;
16
+ }
17
+ return unmapped.toString();
18
18
  }
19
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["mod.ts"],"sourcesContent":["import { unmapping } from \"./internal.ts\";\n\nconst IS_BROWSER = typeof document !== \"undefined\";\n\nexport function unmap(name: string, url: URL): string {\n if (IS_BROWSER) {\n console.error(\"browser unmapping should be done using webpack plugin\");\n return url.href;\n }\n if (!unmapping) {\n console.error(\"missing unmapping registration\");\n return url.href;\n }\n const unmapped = unmapping[name];\n if (!unmapped) {\n console.error(\"missing unmapping registration for\", name);\n return url.href;\n }\n return unmapped.toString();\n}\n"],"mappings":"AAAA,SAAS,SAAS,QAAQ,eAAe;AAEzC,MAAM,UAAU,GAAG,OAAO,QAAQ,KAAK,WAAW;AAElD,OAAO,SAAS,KAAK,CAAC,IAAY,EAAE,GAAQ,EAAU;EACpD,IAAI,UAAU,EAAE;IACd,OAAO,CAAC,KAAK,CAAC,uDAAuD,CAAC;IACtE,OAAO,GAAG,CAAC,IAAI;EACjB;EACA,IAAI,CAAC,SAAS,EAAE;IACd,OAAO,CAAC,KAAK,CAAC,gCAAgC,CAAC;IAC/C,OAAO,GAAG,CAAC,IAAI;EACjB;EACA,MAAM,QAAQ,GAAG,SAAS,CAAC,IAAI,CAAC;EAChC,IAAI,CAAC,QAAQ,EAAE;IACb,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,IAAI,CAAC;IACzD,OAAO,GAAG,CAAC,IAAI;EACjB;EACA,OAAO,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC5B","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freshpack/runtime",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -22,18 +22,22 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@freshpack/core": "3.0.2",
25
- "@freshpack/webpack": "1.0.1",
25
+ "@freshpack/webpack": "1.0.2",
26
26
  "memfs": "^4.80.0",
27
- "webpack": "^5.111.1"
27
+ "webpack": "^5.112.0"
28
28
  },
29
29
  "files": [
30
30
  "client-hmr.d.ts",
31
31
  "client-hmr.js",
32
+ "client-hmr.js.map",
32
33
  "internal.d.ts",
33
34
  "internal.js",
35
+ "internal.js.map",
34
36
  "mod.d.ts",
35
37
  "mod.js",
38
+ "mod.js.map",
36
39
  "server-middleware.d.ts",
37
- "server-middleware.js"
40
+ "server-middleware.js",
41
+ "server-middleware.js.map"
38
42
  ]
39
43
  }
@@ -4,73 +4,80 @@ import { assertUnmapCompilation } from "@freshpack/webpack/unmap";
4
4
  import { setUnmapping } from "./internal.js";
5
5
  import * as memfs from "memfs";
6
6
  export function webpackHandler(app, compiler) {
7
- compiler.outputFileSystem = memfs.createFsFromVolume(new memfs.Volume());
8
- const hmrWatchers = new Set();
9
- const broadcast = (v) => {
10
- const json = JSON.stringify(v);
11
- for (const watcher of hmrWatchers) {
12
- watcher.send(json);
13
- }
14
- };
15
- compiler.hooks.failed.tap("FreshpackHMR", (e) => {
16
- console.error("webpack failed", e);
7
+ compiler.outputFileSystem = memfs.createFsFromVolume(new memfs.Volume());
8
+ const hmrWatchers = new Set();
9
+ const broadcast = v => {
10
+ const json = JSON.stringify(v);
11
+ for (const watcher of hmrWatchers) {
12
+ watcher.send(json);
13
+ }
14
+ };
15
+ compiler.hooks.failed.tap("FreshpackHMR", e => {
16
+ console.error("webpack failed", e);
17
+ });
18
+ compiler.hooks.invalid.tap("FreshpackHMR", () => {
19
+ console.error("webpack invalid");
20
+ broadcast("invalid");
21
+ });
22
+ // deno-ignore: require-await
23
+ compiler.hooks.done.tapPromise({
24
+ name: "FreshpackHMR",
25
+ before: "FreshpackCompilingWait"
26
+ }, async stats => {
27
+ console.info(stats.toString(compiler.options.stats));
28
+ assertFreshCompilation(stats.compilation);
29
+ assertUnmapCompilation(stats.compilation);
30
+ setBuildCache(app, stats.compilation.prodBuildCache,
31
+ // development cache works not the way we want to
32
+ "production");
33
+ setUnmapping(stats.compilation.unmapping);
34
+ broadcast({
35
+ done: {
36
+ hash: stats.hash
37
+ }
17
38
  });
18
- compiler.hooks.invalid.tap("FreshpackHMR", () => {
19
- console.error("webpack invalid");
20
- broadcast("invalid");
39
+ });
40
+ if (compiler.options.watch) {
41
+ compiler.watch(compiler.options.watchOptions, () => {
42
+ // Failure/success is handled by hooks
21
43
  });
22
- // deno-ignore: require-await
23
- compiler.hooks.done.tapPromise({
24
- name: "FreshpackHMR",
25
- before: "FreshpackCompilingWait",
26
- }, async (stats) => {
27
- console.info(stats.toString(compiler.options.stats));
28
- assertFreshCompilation(stats.compilation);
29
- assertUnmapCompilation(stats.compilation);
30
- setBuildCache(app, stats.compilation.prodBuildCache,
31
- // development cache works not the way we want to
32
- "production");
33
- setUnmapping(stats.compilation.unmapping);
34
- broadcast({ done: { hash: stats.hash } });
44
+ } else {
45
+ console.warn("watching is disabled, server won't be reloaded automatically!");
46
+ compiler.compile(() => {
47
+ // Failure/success is handled by hooks
35
48
  });
36
- if (compiler.options.watch) {
37
- compiler.watch(compiler.options.watchOptions, () => {
38
- // Failure/success is handled by hooks
39
- });
49
+ }
50
+ return ctx => {
51
+ if (ctx.url.pathname.endsWith("/_freshpack_hmr")) {
52
+ const {
53
+ response,
54
+ socket
55
+ } = Deno.upgradeWebSocket(ctx.req);
56
+ socket.addEventListener("open", () => {
57
+ hmrWatchers.add(socket);
58
+ });
59
+ socket.addEventListener("close", () => {
60
+ hmrWatchers.delete(socket);
61
+ });
62
+ return Promise.resolve(response);
40
63
  }
41
- else {
42
- console.warn("watching is disabled, server won't be reloaded automatically!");
43
- compiler.compile(() => {
44
- // Failure/success is handled by hooks
45
- });
64
+ if (ctx.req.method !== "GET" && ctx.req.method !== "HEAD") {
65
+ return ctx.next();
46
66
  }
47
- return (ctx) => {
48
- if (ctx.url.pathname.endsWith("/_freshpack_hmr")) {
49
- const { response, socket } = Deno.upgradeWebSocket(ctx.req);
50
- socket.addEventListener("open", () => {
51
- hmrWatchers.add(socket);
52
- });
53
- socket.addEventListener("close", () => {
54
- hmrWatchers.delete(socket);
55
- });
56
- return Promise.resolve(response);
57
- }
58
- if (ctx.req.method !== "GET" && ctx.req.method !== "HEAD") {
59
- return ctx.next();
60
- }
61
- return ctx.next();
62
- };
67
+ return ctx.next();
68
+ };
63
69
  }
64
70
  export function mkCompilingWaiter(compiler) {
65
- let waiter = Promise.withResolvers();
66
- compiler.hooks.failed.tap("FreshpackCompilingWait", () => {
67
- waiter.resolve();
68
- });
69
- compiler.hooks.invalid.tap("FreshpackCompilingWait", () => {
70
- waiter = Promise.withResolvers();
71
- });
72
- compiler.hooks.done.tap("FreshpackCompilingWait", () => {
73
- waiter.resolve();
74
- });
75
- return () => waiter.promise;
71
+ let waiter = Promise.withResolvers();
72
+ compiler.hooks.failed.tap("FreshpackCompilingWait", () => {
73
+ waiter.resolve();
74
+ });
75
+ compiler.hooks.invalid.tap("FreshpackCompilingWait", () => {
76
+ waiter = Promise.withResolvers();
77
+ });
78
+ compiler.hooks.done.tap("FreshpackCompilingWait", () => {
79
+ waiter.resolve();
80
+ });
81
+ return () => waiter.promise;
76
82
  }
83
+ //# sourceMappingURL=server-middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["server-middleware.ts"],"sourcesContent":["import type { App, Context, Middleware } from \"@freshpack/core\";\nimport { setBuildCache } from \"@freshpack/core/internal\";\nimport type Webpack from \"webpack\";\nimport { assertFreshCompilation } from \"@freshpack/webpack/fresh\";\nimport { assertUnmapCompilation } from \"@freshpack/webpack/unmap\";\nimport { setUnmapping } from \"./internal.ts\";\nimport * as memfs from \"memfs\";\n\nexport function webpackHandler<State>(\n app: App<State>,\n compiler: Webpack.Compiler,\n): (ctx: Context<State>) => Promise<Response | Middleware<State>> {\n compiler.outputFileSystem = memfs.createFsFromVolume(\n new memfs.Volume(),\n ) as any;\n\n const hmrWatchers = new Set<WebSocket>();\n const broadcast = (v: unknown) => {\n const json = JSON.stringify(v);\n for (const watcher of hmrWatchers) {\n watcher.send(json);\n }\n };\n\n compiler.hooks.failed.tap(\"FreshpackHMR\", (e) => {\n console.error(\"webpack failed\", e);\n });\n compiler.hooks.invalid.tap(\"FreshpackHMR\", () => {\n console.error(\"webpack invalid\");\n broadcast(\"invalid\");\n });\n // deno-ignore: require-await\n compiler.hooks.done.tapPromise({\n name: \"FreshpackHMR\",\n before: \"FreshpackCompilingWait\",\n }, async (stats) => {\n console.info(stats.toString(compiler.options.stats));\n\n assertFreshCompilation(stats.compilation);\n assertUnmapCompilation(stats.compilation);\n\n setBuildCache(\n app as App<unknown>,\n stats.compilation.prodBuildCache,\n // development cache works not the way we want to\n \"production\",\n );\n setUnmapping(stats.compilation.unmapping);\n broadcast({ done: { hash: stats.hash } });\n });\n\n if (compiler.options.watch) {\n compiler.watch(compiler.options.watchOptions, () => {\n // Failure/success is handled by hooks\n });\n } else {\n console.warn(\n \"watching is disabled, server won't be reloaded automatically!\",\n );\n compiler.compile(() => {\n // Failure/success is handled by hooks\n });\n }\n\n return (ctx) => {\n if (ctx.url.pathname.endsWith(\"/_freshpack_hmr\")) {\n const { response, socket } = Deno.upgradeWebSocket(ctx.req);\n\n socket.addEventListener(\"open\", () => {\n hmrWatchers.add(socket);\n });\n socket.addEventListener(\"close\", () => {\n hmrWatchers.delete(socket);\n });\n return Promise.resolve(response);\n }\n if (ctx.req.method !== \"GET\" && ctx.req.method !== \"HEAD\") {\n return ctx.next();\n }\n return ctx.next();\n };\n}\n\nexport function mkCompilingWaiter(\n compiler: Webpack.Compiler,\n): () => Promise<void> {\n let waiter = Promise.withResolvers<void>();\n\n compiler.hooks.failed.tap(\"FreshpackCompilingWait\", () => {\n waiter.resolve();\n });\n compiler.hooks.invalid.tap(\"FreshpackCompilingWait\", () => {\n waiter = Promise.withResolvers();\n });\n compiler.hooks.done.tap(\"FreshpackCompilingWait\", () => {\n waiter.resolve();\n });\n\n return () => waiter.promise;\n}\n"],"mappings":"AACA,SAAS,aAAa,QAAQ,0BAA0B;AAExD,SAAS,sBAAsB,QAAQ,0BAA0B;AACjE,SAAS,sBAAsB,QAAQ,0BAA0B;AACjE,SAAS,YAAY,QAAQ,eAAe;AAC5C,OAAO,KAAK,KAAK,MAAM,OAAO;AAE9B,OAAO,SAAS,cAAc,CAC5B,GAAe,EACf,QAA0B,EACsC;EAChE,QAAQ,CAAC,gBAAgB,GAAG,KAAK,CAAC,kBAAkB,CAClD,IAAI,KAAK,CAAC,MAAM,CAAC,CACnB,CAAQ;EAER,MAAM,WAAW,GAAG,IAAI,GAAG,CAAY,CAAC;EACxC,MAAM,SAAS,GAAI,CAAU,IAAK;IAChC,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IAC9B,KAAK,MAAM,OAAO,IAAI,WAAW,EAAE;MACjC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;IACpB;EACF,CAAC;EAED,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,cAAc,EAAG,CAAC,IAAK;IAC/C,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE,CAAC,CAAC;EACpC,CAAC,CAAC;EACF,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM;IAC/C,OAAO,CAAC,KAAK,CAAC,iBAAiB,CAAC;IAChC,SAAS,CAAC,SAAS,CAAC;EACtB,CAAC,CAAC;EACF;EACA,QAAQ,CAAC,KAAK,CAAC,IAAI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