wxt 0.7.2 → 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
@@ -1867,7 +1924,8 @@ async function importEntrypointFile(path9, config) {
1867
1924
  "node_modules/wxt/dist/virtual-modules/fake-browser.js"
1868
1925
  )
1869
1926
  },
1870
- extensions: [".ts", ".tsx", ".cjs", ".js", ".mjs"],
1927
+ // List of extensions to transform with esbuild
1928
+ extensions: [".ts", ".cts", ".mts", ".tsx", ".js", ".cjs", ".mjs", ".jsx"],
1871
1929
  transform(opts) {
1872
1930
  const isEntrypoint = opts.filename === normalPath;
1873
1931
  return (0, import_esbuild.transformSync)(
@@ -2136,13 +2194,13 @@ var PATH_GLOB_TO_TYPE_MAP = {
2136
2194
  "*.sidepanel/index.html": "sidepanel",
2137
2195
  "devtools.html": "devtools",
2138
2196
  "devtools/index.html": "devtools",
2139
- "background.ts": "background",
2140
- "background/index.ts": "background",
2197
+ "background.[jt]s": "background",
2198
+ "background/index.[jt]s": "background",
2141
2199
  [VIRTUAL_NOOP_BACKGROUND_MODULE_ID]: "background",
2142
- "content.ts?(x)": "content-script",
2143
- "content/index.ts?(x)": "content-script",
2144
- "*.content.ts?(x)": "content-script",
2145
- "*.content/index.ts?(x)": "content-script",
2200
+ "content.[jt]s?(x)": "content-script",
2201
+ "content/index.[jt]s?(x)": "content-script",
2202
+ "*.content.[jt]s?(x)": "content-script",
2203
+ "*.content/index.[jt]s?(x)": "content-script",
2146
2204
  [`content.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
2147
2205
  [`*.content.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
2148
2206
  [`content/index.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
@@ -2153,7 +2211,7 @@ var PATH_GLOB_TO_TYPE_MAP = {
2153
2211
  "options/index.html": "options",
2154
2212
  "*.html": "unlisted-page",
2155
2213
  "*/index.html": "unlisted-page",
2156
- "*.ts": "unlisted-script",
2214
+ "*.[jt]s": "unlisted-script",
2157
2215
  "*/index.ts": "unlisted-script",
2158
2216
  [`*.${CSS_EXTENSIONS_PATTERN}`]: "unlisted-style",
2159
2217
  [`*/index.${CSS_EXTENSIONS_PATTERN}`]: "unlisted-style",
@@ -4506,7 +4564,7 @@ async function clean(root = process.cwd()) {
4506
4564
  }
4507
4565
 
4508
4566
  // package.json
4509
- var version2 = "0.7.2";
4567
+ var version2 = "0.7.4";
4510
4568
 
4511
4569
  // src/core/utils/defineConfig.ts
4512
4570
  function defineConfig(config) {