wxt 0.7.3 → 0.7.4

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.cjs CHANGED
@@ -960,62 +960,119 @@ function resolvePerBrowserOption(option, browser) {
960
960
  // src/core/vite-plugins/devHtmlPrerender.ts
961
961
  var import_linkedom = require("linkedom");
962
962
  var import_path2 = require("path");
963
+ var reactRefreshPreamble = "";
963
964
  function devHtmlPrerender(config) {
964
- return {
965
- apply: "build",
966
- name: "wxt:dev-html-prerender",
967
- config() {
968
- return {
969
- resolve: {
970
- alias: {
971
- "@wxt/reload-html": (0, import_path2.resolve)(
972
- config.root,
973
- "node_modules/wxt/dist/virtual-modules/reload-html.js"
974
- )
965
+ const htmlReloadId = "@wxt/reload-html";
966
+ const resolvedHtmlReloadId = (0, import_path2.resolve)(
967
+ config.root,
968
+ "node_modules/wxt/dist/virtual-modules/reload-html.js"
969
+ );
970
+ const virtualReactRefreshId = "@wxt/virtual-react-refresh";
971
+ const resolvedVirtualReactRefreshId = "\0" + virtualReactRefreshId;
972
+ return [
973
+ {
974
+ apply: "build",
975
+ name: "wxt:dev-html-prerender",
976
+ config() {
977
+ return {
978
+ resolve: {
979
+ alias: {
980
+ [htmlReloadId]: resolvedHtmlReloadId
981
+ }
975
982
  }
983
+ };
984
+ },
985
+ // Convert scripts like src="./main.tsx" -> src="http://localhost:3000/entrypoints/popup/main.tsx"
986
+ // before the paths are replaced with their bundled path
987
+ transform(code, id) {
988
+ const server = config.server;
989
+ if (config.command !== "serve" || server == null || !id.endsWith(".html"))
990
+ return;
991
+ const { document } = (0, import_linkedom.parseHTML)(code);
992
+ const pointToDevServer = (querySelector, attr) => {
993
+ document.querySelectorAll(querySelector).forEach((element) => {
994
+ const src = element.getAttribute(attr);
995
+ if (!src)
996
+ return;
997
+ if ((0, import_path2.isAbsolute)(src)) {
998
+ element.setAttribute(attr, server.origin + src);
999
+ } else if (src.startsWith(".")) {
1000
+ const abs = (0, import_path2.resolve)((0, import_path2.dirname)(id), src);
1001
+ const pathname = (0, import_path2.relative)(config.root, abs);
1002
+ element.setAttribute(attr, `${server.origin}/${pathname}`);
1003
+ }
1004
+ });
1005
+ };
1006
+ pointToDevServer("script[type=module]", "src");
1007
+ pointToDevServer("link[rel=stylesheet]", "href");
1008
+ const reloader = document.createElement("script");
1009
+ reloader.src = htmlReloadId;
1010
+ reloader.type = "module";
1011
+ document.head.appendChild(reloader);
1012
+ const newHtml = document.toString();
1013
+ config.logger.debug("transform " + id);
1014
+ config.logger.debug("Old HTML:\n" + code);
1015
+ config.logger.debug("New HTML:\n" + newHtml);
1016
+ return newHtml;
1017
+ },
1018
+ // Pass the HTML through the dev server to add dev-mode specific code
1019
+ async transformIndexHtml(html, ctx) {
1020
+ const server = config.server;
1021
+ if (config.command !== "serve" || server == null)
1022
+ return;
1023
+ const originalUrl = `${server.origin}${ctx.path}`;
1024
+ const name = getEntrypointName(config.entrypointsDir, ctx.filename);
1025
+ const url2 = `${server.origin}/${name}.html`;
1026
+ const serverHtml = await server.transformIndexHtml(
1027
+ url2,
1028
+ html,
1029
+ originalUrl
1030
+ );
1031
+ const { document } = (0, import_linkedom.parseHTML)(serverHtml);
1032
+ const reactRefreshScript = Array.from(
1033
+ document.querySelectorAll("script[type=module]")
1034
+ ).find((script) => script.innerHTML.includes("@react-refresh"));
1035
+ if (reactRefreshScript) {
1036
+ reactRefreshPreamble = reactRefreshScript.innerHTML;
1037
+ const virtualScript = document.createElement("script");
1038
+ virtualScript.type = "module";
1039
+ virtualScript.src = `${server.origin}/${virtualReactRefreshId}`;
1040
+ reactRefreshScript.replaceWith(virtualScript);
976
1041
  }
977
- };
1042
+ const viteClientScript = document.querySelector(
1043
+ "script[src='/@vite/client']"
1044
+ );
1045
+ if (viteClientScript) {
1046
+ viteClientScript.src = `${server.origin}${viteClientScript.src}`;
1047
+ }
1048
+ const newHtml = document.toString();
1049
+ config.logger.debug("transformIndexHtml " + ctx.filename);
1050
+ config.logger.debug("Old HTML:\n" + html);
1051
+ config.logger.debug("New HTML:\n" + newHtml);
1052
+ return newHtml;
1053
+ }
978
1054
  },
979
- async transform(html, id) {
980
- const server = config.server;
981
- if (config.command !== "serve" || server == null || !id.endsWith(".html"))
982
- return;
983
- const originalUrl = `${server.origin}${id}`;
984
- const name = getEntrypointName(config.entrypointsDir, id);
985
- const url2 = `${server.origin}/${name}.html`;
986
- const serverHtml = await server.transformIndexHtml(
987
- url2,
988
- html,
989
- originalUrl
990
- );
991
- const { document } = (0, import_linkedom.parseHTML)(serverHtml);
992
- const pointToDevServer = (querySelector, attr) => {
993
- document.querySelectorAll(querySelector).forEach((element) => {
994
- const src = element.getAttribute(attr);
995
- if (!src)
996
- return;
997
- if ((0, import_path2.isAbsolute)(src)) {
998
- element.setAttribute(attr, server.origin + src);
999
- } else if (src.startsWith(".")) {
1000
- const abs = (0, import_path2.resolve)((0, import_path2.dirname)(id), src);
1001
- const pathname = (0, import_path2.relative)(config.root, abs);
1002
- element.setAttribute(attr, `${server.origin}/${pathname}`);
1003
- }
1004
- });
1005
- };
1006
- pointToDevServer("script[type=module]", "src");
1007
- pointToDevServer("link[rel=stylesheet]", "href");
1008
- const reloader = document.createElement("script");
1009
- reloader.src = "@wxt/reload-html";
1010
- reloader.type = "module";
1011
- document.head.appendChild(reloader);
1012
- const newHtml = document.toString();
1013
- config.logger.debug("Transformed " + id);
1014
- config.logger.debug("Old HTML:\n" + html);
1015
- config.logger.debug("New HTML:\n" + newHtml);
1016
- return newHtml;
1055
+ {
1056
+ name: "wxt:virtualize-react-refresh",
1057
+ apply: "serve",
1058
+ resolveId(id) {
1059
+ if (id === `/${virtualReactRefreshId}`) {
1060
+ return resolvedVirtualReactRefreshId;
1061
+ }
1062
+ if (id.startsWith("/chunks/")) {
1063
+ return "\0noop";
1064
+ }
1065
+ },
1066
+ load(id) {
1067
+ if (id === resolvedVirtualReactRefreshId) {
1068
+ return reactRefreshPreamble;
1069
+ }
1070
+ if (id === "\0noop") {
1071
+ return "";
1072
+ }
1073
+ }
1017
1074
  }
1018
- };
1075
+ ];
1019
1076
  }
1020
1077
 
1021
1078
  // src/core/vite-plugins/devServerGlobals.ts
@@ -4507,7 +4564,7 @@ async function clean(root = process.cwd()) {
4507
4564
  }
4508
4565
 
4509
4566
  // package.json
4510
- var version2 = "0.7.3";
4567
+ var version2 = "0.7.4";
4511
4568
 
4512
4569
  // src/core/utils/defineConfig.ts
4513
4570
  function defineConfig(config) {