wxt 0.18.14 → 0.19.0-alpha1

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 (260) hide show
  1. package/README.md +1 -1
  2. package/bin/wxt.mjs +1 -1
  3. package/dist/browser/chrome.d.ts +17 -0
  4. package/dist/browser/chrome.mjs +7 -0
  5. package/dist/browser/index.d.ts +4 -0
  6. package/dist/browser/index.mjs +1 -0
  7. package/dist/browser/webextension-polyfill.d.ts +18 -0
  8. package/dist/browser/webextension-polyfill.mjs +2 -0
  9. package/dist/builtin-modules/index.d.ts +2 -0
  10. package/dist/builtin-modules/index.mjs +2 -0
  11. package/dist/builtin-modules/unimport.d.ts +8 -0
  12. package/dist/builtin-modules/unimport.mjs +99 -0
  13. package/dist/cli/cli-utils.d.ts +25 -0
  14. package/dist/cli/cli-utils.mjs +58 -0
  15. package/dist/cli/commands.d.ts +2 -0
  16. package/dist/cli/commands.mjs +104 -0
  17. package/dist/cli/index.d.ts +1 -0
  18. package/dist/cli/index.mjs +11 -0
  19. package/dist/client/app-config.d.ts +2 -0
  20. package/dist/client/app-config.mjs +4 -0
  21. package/dist/client/content-scripts/content-script-context.d.ts +114 -0
  22. package/dist/client/content-scripts/content-script-context.mjs +169 -0
  23. package/dist/client/content-scripts/custom-events.d.ts +10 -0
  24. package/dist/client/content-scripts/custom-events.mjs +13 -0
  25. package/dist/client/content-scripts/index.d.ts +2 -0
  26. package/dist/client/content-scripts/index.mjs +2 -0
  27. package/dist/client/content-scripts/location-watcher.d.ts +12 -0
  28. package/dist/client/content-scripts/location-watcher.mjs +22 -0
  29. package/dist/client/content-scripts/ui/index.d.ts +23 -0
  30. package/dist/client/content-scripts/ui/index.mjs +188 -0
  31. package/dist/{client.d.ts → client/content-scripts/ui/types.d.ts} +15 -43
  32. package/dist/client/content-scripts/ui/types.mjs +0 -0
  33. package/dist/client/index.d.ts +7 -0
  34. package/dist/client/index.mjs +2 -0
  35. package/dist/core/build.d.ts +15 -0
  36. package/dist/core/build.mjs +6 -0
  37. package/dist/core/builders/vite/index.d.ts +3 -0
  38. package/dist/core/builders/vite/index.mjs +297 -0
  39. package/dist/core/builders/vite/plugins/bundleAnalysis.d.ts +7 -0
  40. package/dist/core/builders/vite/plugins/bundleAnalysis.mjs +15 -0
  41. package/dist/core/builders/vite/plugins/cssEntrypoints.d.ts +13 -0
  42. package/dist/core/builders/vite/plugins/cssEntrypoints.mjs +22 -0
  43. package/dist/core/builders/vite/plugins/defineImportMeta.d.ts +14 -0
  44. package/dist/core/builders/vite/plugins/defineImportMeta.mjs +13 -0
  45. package/dist/core/builders/vite/plugins/devHtmlPrerender.d.ts +7 -0
  46. package/dist/core/builders/vite/plugins/devHtmlPrerender.mjs +140 -0
  47. package/dist/core/builders/vite/plugins/devServerGlobals.d.ts +6 -0
  48. package/dist/core/builders/vite/plugins/devServerGlobals.mjs +15 -0
  49. package/dist/core/builders/vite/plugins/download.d.ts +10 -0
  50. package/dist/core/builders/vite/plugins/download.mjs +14 -0
  51. package/dist/core/builders/vite/plugins/entrypointGroupGlobals.d.ts +6 -0
  52. package/dist/core/builders/vite/plugins/entrypointGroupGlobals.mjs +16 -0
  53. package/dist/core/builders/vite/plugins/extensionApiMock.d.ts +11 -0
  54. package/dist/core/builders/vite/plugins/extensionApiMock.mjs +26 -0
  55. package/dist/core/builders/vite/plugins/globals.d.ts +3 -0
  56. package/dist/core/builders/vite/plugins/globals.mjs +15 -0
  57. package/dist/core/builders/vite/plugins/index.d.ts +17 -0
  58. package/dist/core/builders/vite/plugins/index.mjs +17 -0
  59. package/dist/core/builders/vite/plugins/multipageMove.d.ts +20 -0
  60. package/dist/core/builders/vite/plugins/multipageMove.mjs +59 -0
  61. package/dist/core/builders/vite/plugins/noopBackground.d.ts +6 -0
  62. package/dist/core/builders/vite/plugins/noopBackground.mjs +17 -0
  63. package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.d.ts +6 -0
  64. package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +12 -0
  65. package/dist/core/builders/vite/plugins/resolveAppConfig.d.ts +6 -0
  66. package/dist/core/builders/vite/plugins/resolveAppConfig.mjs +26 -0
  67. package/dist/core/builders/vite/plugins/resolveExtensionApi.d.ts +10 -0
  68. package/dist/core/builders/vite/plugins/resolveExtensionApi.mjs +15 -0
  69. package/dist/core/builders/vite/plugins/resolveVirtualModules.d.ts +6 -0
  70. package/dist/core/builders/vite/plugins/resolveVirtualModules.mjs +30 -0
  71. package/dist/core/builders/vite/plugins/tsconfigPaths.d.ts +3 -0
  72. package/dist/core/builders/vite/plugins/tsconfigPaths.mjs +12 -0
  73. package/dist/core/builders/vite/plugins/wxtPluginLoader.d.ts +6 -0
  74. package/dist/core/builders/vite/plugins/wxtPluginLoader.mjs +56 -0
  75. package/dist/core/clean.d.ts +21 -0
  76. package/dist/core/clean.mjs +38 -0
  77. package/dist/core/create-server.d.ts +11 -0
  78. package/dist/core/create-server.mjs +206 -0
  79. package/dist/core/define-config.d.ts +2 -0
  80. package/dist/core/define-config.mjs +3 -0
  81. package/dist/core/define-runner-config.d.ts +2 -0
  82. package/dist/core/define-runner-config.mjs +3 -0
  83. package/dist/core/index.d.ts +8 -0
  84. package/dist/core/index.mjs +8 -0
  85. package/dist/core/initialize.d.ts +5 -0
  86. package/dist/core/initialize.mjs +128 -0
  87. package/dist/core/package-managers/bun.d.ts +2 -0
  88. package/dist/core/package-managers/bun.mjs +19 -0
  89. package/dist/core/package-managers/index.d.ts +2 -0
  90. package/dist/core/package-managers/index.mjs +65 -0
  91. package/dist/core/package-managers/npm.d.ts +17 -0
  92. package/dist/core/package-managers/npm.mjs +58 -0
  93. package/dist/core/package-managers/pnpm.d.ts +2 -0
  94. package/dist/core/package-managers/pnpm.mjs +21 -0
  95. package/dist/core/package-managers/types.d.ts +2 -0
  96. package/dist/core/package-managers/types.mjs +0 -0
  97. package/dist/core/package-managers/yarn.d.ts +2 -0
  98. package/dist/core/package-managers/yarn.mjs +31 -0
  99. package/dist/core/prepare.d.ts +2 -0
  100. package/dist/core/prepare.mjs +8 -0
  101. package/dist/core/runners/index.d.ts +2 -0
  102. package/dist/core/runners/index.mjs +12 -0
  103. package/dist/core/runners/manual.d.ts +5 -0
  104. package/dist/core/runners/manual.mjs +16 -0
  105. package/dist/core/runners/safari.d.ts +5 -0
  106. package/dist/core/runners/safari.mjs +16 -0
  107. package/dist/core/runners/web-ext.d.ts +5 -0
  108. package/dist/core/runners/web-ext.mjs +78 -0
  109. package/dist/core/runners/wsl.d.ts +5 -0
  110. package/dist/core/runners/wsl.mjs +16 -0
  111. package/dist/core/utils/arrays.d.ts +13 -0
  112. package/dist/{chunk-BERPNPEZ.js → core/utils/arrays.mjs} +6 -10
  113. package/dist/core/utils/building/build-entrypoints.d.ts +3 -0
  114. package/dist/core/utils/building/build-entrypoints.mjs +47 -0
  115. package/dist/core/utils/building/detect-dev-changes.d.ts +57 -0
  116. package/dist/core/utils/building/detect-dev-changes.mjs +93 -0
  117. package/dist/core/utils/building/find-entrypoints.d.ts +5 -0
  118. package/dist/core/utils/building/find-entrypoints.mjs +385 -0
  119. package/dist/core/utils/building/generate-wxt-dir.d.ts +5 -0
  120. package/dist/core/utils/building/generate-wxt-dir.mjs +190 -0
  121. package/dist/core/utils/building/group-entrypoints.d.ts +8 -0
  122. package/dist/core/utils/building/group-entrypoints.mjs +37 -0
  123. package/dist/core/utils/building/import-entrypoint.d.ts +16 -0
  124. package/dist/core/utils/building/import-entrypoint.mjs +97 -0
  125. package/dist/core/utils/building/index.d.ts +9 -0
  126. package/dist/core/utils/building/index.mjs +9 -0
  127. package/dist/core/utils/building/internal-build.d.ts +12 -0
  128. package/dist/core/utils/building/internal-build.mjs +112 -0
  129. package/dist/core/utils/building/rebuild.d.ts +23 -0
  130. package/dist/core/utils/building/rebuild.mjs +39 -0
  131. package/dist/core/utils/building/resolve-config.d.ts +11 -0
  132. package/dist/core/utils/building/resolve-config.mjs +365 -0
  133. package/dist/core/utils/cache.d.ts +8 -0
  134. package/dist/core/utils/cache.mjs +21 -0
  135. package/dist/core/utils/cli.d.ts +3 -0
  136. package/dist/core/utils/cli.mjs +26 -0
  137. package/dist/core/utils/constants.d.ts +5 -0
  138. package/dist/core/utils/constants.mjs +1 -0
  139. package/dist/core/utils/content-scripts.d.ts +11 -0
  140. package/dist/core/utils/content-scripts.mjs +60 -0
  141. package/dist/core/utils/content-security-policy.d.ts +14 -0
  142. package/dist/core/utils/content-security-policy.mjs +39 -0
  143. package/dist/core/utils/entrypoints.d.ts +25 -0
  144. package/dist/core/utils/entrypoints.mjs +31 -0
  145. package/dist/core/utils/eslint.d.ts +1 -0
  146. package/dist/core/utils/eslint.mjs +11 -0
  147. package/dist/core/utils/fs.d.ts +13 -0
  148. package/dist/core/utils/fs.mjs +15 -0
  149. package/dist/core/utils/globals.d.ts +11 -0
  150. package/dist/core/utils/globals.mjs +53 -0
  151. package/dist/core/utils/i18n.d.ts +11 -0
  152. package/dist/core/utils/i18n.mjs +35 -0
  153. package/dist/core/utils/log/index.d.ts +4 -0
  154. package/dist/core/utils/log/index.mjs +4 -0
  155. package/dist/core/utils/log/printBuildSummary.d.ts +2 -0
  156. package/dist/core/utils/log/printBuildSummary.mjs +32 -0
  157. package/dist/core/utils/log/printFileList.d.ts +1 -0
  158. package/dist/core/utils/log/printFileList.mjs +42 -0
  159. package/dist/core/utils/log/printHeader.d.ts +1 -0
  160. package/dist/core/utils/log/printHeader.mjs +7 -0
  161. package/dist/core/utils/log/printTable.d.ts +1 -0
  162. package/dist/core/utils/log/printTable.mjs +22 -0
  163. package/dist/core/utils/manifest.d.ts +44 -0
  164. package/dist/core/utils/manifest.mjs +512 -0
  165. package/dist/core/utils/network.d.ts +7 -0
  166. package/dist/core/utils/network.mjs +38 -0
  167. package/dist/core/utils/package.d.ts +6 -0
  168. package/dist/core/utils/package.mjs +14 -0
  169. package/dist/core/utils/paths.d.ts +11 -0
  170. package/dist/core/utils/paths.mjs +10 -0
  171. package/dist/core/utils/strings.d.ts +14 -0
  172. package/dist/core/utils/strings.mjs +18 -0
  173. package/dist/core/utils/testing/fake-objects.d.ts +4520 -0
  174. package/dist/core/utils/testing/fake-objects.mjs +323 -0
  175. package/dist/core/utils/time.d.ts +9 -0
  176. package/dist/core/utils/time.mjs +17 -0
  177. package/dist/core/utils/transform.d.ts +9 -0
  178. package/dist/core/utils/transform.mjs +17 -0
  179. package/dist/core/utils/types.d.ts +10 -0
  180. package/dist/core/utils/types.mjs +0 -0
  181. package/dist/core/utils/validation.d.ts +15 -0
  182. package/dist/core/utils/validation.mjs +55 -0
  183. package/dist/core/utils/virtual-modules.d.ts +22 -0
  184. package/dist/core/utils/virtual-modules.mjs +14 -0
  185. package/dist/core/utils/wsl.d.ts +4 -0
  186. package/dist/core/utils/wsl.mjs +4 -0
  187. package/dist/core/wxt.d.ts +19 -0
  188. package/dist/core/wxt.mjs +41 -0
  189. package/dist/core/zip.d.ts +7 -0
  190. package/dist/core/zip.mjs +137 -0
  191. package/dist/index.d.ts +4 -79
  192. package/dist/index.mjs +3 -0
  193. package/dist/modules.d.ts +10 -20
  194. package/dist/{chunk-6XSIWUWF.js → modules.mjs} +7 -16
  195. package/dist/sandbox/define-app-config.d.ts +19 -0
  196. package/dist/sandbox/define-app-config.mjs +3 -0
  197. package/dist/sandbox/define-background.d.ts +3 -0
  198. package/dist/sandbox/define-background.mjs +4 -0
  199. package/dist/sandbox/define-content-script.d.ts +2 -0
  200. package/dist/sandbox/define-content-script.mjs +3 -0
  201. package/dist/sandbox/define-unlisted-script.d.ts +3 -0
  202. package/dist/sandbox/define-unlisted-script.mjs +4 -0
  203. package/dist/sandbox/define-wxt-plugin.d.ts +2 -0
  204. package/dist/sandbox/define-wxt-plugin.mjs +3 -0
  205. package/dist/sandbox/dev-server-websocket.d.ts +21 -0
  206. package/dist/sandbox/dev-server-websocket.mjs +37 -0
  207. package/dist/sandbox/index.d.ts +11 -0
  208. package/dist/sandbox/index.mjs +6 -0
  209. package/dist/sandbox/utils/logger.d.ts +9 -0
  210. package/dist/sandbox/utils/logger.mjs +15 -0
  211. package/dist/storage.d.ts +13 -15
  212. package/dist/{storage.js → storage.mjs} +5 -34
  213. package/dist/testing/fake-browser.d.ts +1 -0
  214. package/dist/testing/fake-browser.mjs +1 -0
  215. package/dist/testing/index.d.ts +5 -0
  216. package/dist/testing/index.mjs +2 -0
  217. package/dist/{testing.d.ts → testing/wxt-vitest-plugin.d.ts} +3 -15
  218. package/dist/testing/wxt-vitest-plugin.mjs +26 -0
  219. package/dist/{index-CM35KuSH.d.cts → types.d.ts} +119 -250
  220. package/dist/types.mjs +0 -0
  221. package/dist/version.d.ts +1 -0
  222. package/dist/version.mjs +1 -0
  223. package/dist/virtual/{background-entrypoint.js → background-entrypoint.mjs} +31 -40
  224. package/dist/virtual/{content-script-isolated-world-entrypoint.js → content-script-isolated-world-entrypoint.mjs} +9 -13
  225. package/dist/virtual/{content-script-main-world-entrypoint.js → content-script-main-world-entrypoint.mjs} +9 -14
  226. package/dist/virtual/mock-browser.mjs +6 -0
  227. package/dist/virtual/{reload-html.js → reload-html.mjs} +8 -9
  228. package/dist/virtual/{unlisted-script-entrypoint.js → unlisted-script-entrypoint.mjs} +8 -12
  229. package/package.json +51 -57
  230. package/dist/browser.d.ts +0 -18
  231. package/dist/browser.js +0 -6
  232. package/dist/chunk-BM6QYGAW.js +0 -1063
  233. package/dist/chunk-FNTE2L27.js +0 -7
  234. package/dist/chunk-KPD5J7PZ.js +0 -1065
  235. package/dist/chunk-QGM4M3NI.js +0 -37
  236. package/dist/chunk-SGKCDMVR.js +0 -38
  237. package/dist/chunk-ZONHOHIX.js +0 -3608
  238. package/dist/cli.d.ts +0 -2
  239. package/dist/cli.js +0 -4429
  240. package/dist/client.js +0 -424
  241. package/dist/define-app-config-bg54F_lV.d.ts +0 -294
  242. package/dist/execa-4UBDUBJZ.js +0 -7244
  243. package/dist/execa-QLUM2B3W.js +0 -7245
  244. package/dist/index-CM35KuSH.d.ts +0 -1401
  245. package/dist/index.cjs +0 -14464
  246. package/dist/index.d.cts +0 -81
  247. package/dist/index.js +0 -696
  248. package/dist/modules.cjs +0 -96
  249. package/dist/modules.d.cts +0 -119
  250. package/dist/modules.js +0 -17
  251. package/dist/prompt-25QIVJDC.js +0 -755
  252. package/dist/prompt-7BMKNSWS.js +0 -754
  253. package/dist/sandbox.d.ts +0 -16
  254. package/dist/sandbox.js +0 -36
  255. package/dist/storage.cjs +0 -439
  256. package/dist/storage.d.cts +0 -200
  257. package/dist/testing.cjs +0 -2806
  258. package/dist/testing.d.cts +0 -30
  259. package/dist/testing.js +0 -40
  260. package/dist/virtual/mock-browser.js +0 -6
@@ -0,0 +1,297 @@
1
+ import * as wxtPlugins from "./plugins/index.mjs";
2
+ import {
3
+ getEntrypointBundlePath,
4
+ isHtmlEntrypoint
5
+ } from "../../utils/entrypoints.mjs";
6
+ import { toArray } from "../../utils/arrays.mjs";
7
+ import { safeVarName } from "../../utils/strings.mjs";
8
+ import { importEntrypointFile } from "../../utils/building/index.mjs";
9
+ import { ViteNodeServer } from "vite-node/server";
10
+ import { ViteNodeRunner } from "vite-node/client";
11
+ import { installSourcemapsSupport } from "vite-node/source-map";
12
+ export async function createViteBuilder(wxtConfig, hooks, server) {
13
+ const vite = await import("vite");
14
+ const getBaseConfig = async () => {
15
+ const config = await wxtConfig.vite(wxtConfig.env);
16
+ config.root = wxtConfig.root;
17
+ config.configFile = false;
18
+ config.logLevel = "warn";
19
+ config.mode = wxtConfig.mode;
20
+ config.build ??= {};
21
+ config.publicDir = wxtConfig.publicDir;
22
+ config.build.copyPublicDir = false;
23
+ config.build.outDir = wxtConfig.outDir;
24
+ config.build.emptyOutDir = false;
25
+ if (config.build.minify == null && wxtConfig.command === "serve") {
26
+ config.build.minify = false;
27
+ }
28
+ if (config.build.sourcemap == null && wxtConfig.command === "serve") {
29
+ config.build.sourcemap = "inline";
30
+ }
31
+ config.plugins ??= [];
32
+ config.plugins.push(
33
+ wxtPlugins.download(wxtConfig),
34
+ wxtPlugins.devHtmlPrerender(wxtConfig, server),
35
+ wxtPlugins.resolveVirtualModules(wxtConfig),
36
+ wxtPlugins.devServerGlobals(wxtConfig, server),
37
+ wxtPlugins.tsconfigPaths(wxtConfig),
38
+ wxtPlugins.noopBackground(),
39
+ wxtPlugins.globals(wxtConfig),
40
+ wxtPlugins.resolveExtensionApi(wxtConfig),
41
+ wxtPlugins.defineImportMeta(),
42
+ wxtPlugins.wxtPluginLoader(wxtConfig),
43
+ wxtPlugins.resolveAppConfig(wxtConfig)
44
+ );
45
+ if (wxtConfig.analysis.enabled) {
46
+ config.plugins.push(wxtPlugins.bundleAnalysis(wxtConfig));
47
+ }
48
+ return config;
49
+ };
50
+ const getLibModeConfig = (entrypoint) => {
51
+ const entry = getRollupEntry(entrypoint);
52
+ const plugins = [
53
+ wxtPlugins.entrypointGroupGlobals(entrypoint)
54
+ ];
55
+ if (entrypoint.type === "content-script-style" || entrypoint.type === "unlisted-style") {
56
+ plugins.push(wxtPlugins.cssEntrypoints(entrypoint, wxtConfig));
57
+ }
58
+ const iifeReturnValueName = safeVarName(entrypoint.name);
59
+ const libMode = {
60
+ mode: wxtConfig.mode,
61
+ plugins,
62
+ esbuild: {
63
+ // Add a footer with the returned value so it can return values to `scripting.executeScript`
64
+ // Footer is added apart of esbuild to make sure it's not minified. It
65
+ // get's removed if added to `build.rollupOptions.output.footer`
66
+ // See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value
67
+ footer: iifeReturnValueName + ";"
68
+ },
69
+ build: {
70
+ lib: {
71
+ entry,
72
+ formats: ["iife"],
73
+ name: iifeReturnValueName,
74
+ fileName: entrypoint.name
75
+ },
76
+ rollupOptions: {
77
+ output: {
78
+ // There's only a single output for this build, so we use the desired bundle path for the
79
+ // entry output (like "content-scripts/overlay.js")
80
+ entryFileNames: getEntrypointBundlePath(
81
+ entrypoint,
82
+ wxtConfig.outDir,
83
+ ".js"
84
+ ),
85
+ // Output content script CSS to `content-scripts/`, but all other scripts are written to
86
+ // `assets/`.
87
+ assetFileNames: ({ name }) => {
88
+ if (entrypoint.type === "content-script" && name?.endsWith("css")) {
89
+ return `content-scripts/${entrypoint.name}.[ext]`;
90
+ } else {
91
+ return `assets/${entrypoint.name}.[ext]`;
92
+ }
93
+ }
94
+ }
95
+ }
96
+ },
97
+ define: {
98
+ // See https://github.com/aklinker1/vite-plugin-web-extension/issues/96
99
+ "process.env.NODE_ENV": JSON.stringify(wxtConfig.mode)
100
+ }
101
+ };
102
+ return libMode;
103
+ };
104
+ const getMultiPageConfig = (entrypoints) => {
105
+ const htmlEntrypoints = new Set(
106
+ entrypoints.filter(isHtmlEntrypoint).map((e) => e.name)
107
+ );
108
+ return {
109
+ mode: wxtConfig.mode,
110
+ plugins: [
111
+ wxtPlugins.multipageMove(entrypoints, wxtConfig),
112
+ wxtPlugins.entrypointGroupGlobals(entrypoints)
113
+ ],
114
+ build: {
115
+ rollupOptions: {
116
+ input: entrypoints.reduce((input, entry) => {
117
+ input[entry.name] = getRollupEntry(entry);
118
+ return input;
119
+ }, {}),
120
+ output: {
121
+ // Include a hash to prevent conflicts
122
+ chunkFileNames: "chunks/[name]-[hash].js",
123
+ entryFileNames: ({ name }) => {
124
+ if (htmlEntrypoints.has(name)) return "chunks/[name]-[hash].js";
125
+ return "[name].js";
126
+ },
127
+ // We can't control the "name", so we need a hash to prevent conflicts
128
+ assetFileNames: "assets/[name]-[hash].[ext]"
129
+ }
130
+ }
131
+ }
132
+ };
133
+ };
134
+ const getCssConfig = (entrypoint) => {
135
+ return {
136
+ mode: wxtConfig.mode,
137
+ plugins: [wxtPlugins.entrypointGroupGlobals(entrypoint)],
138
+ build: {
139
+ rollupOptions: {
140
+ input: {
141
+ [entrypoint.name]: entrypoint.inputPath
142
+ },
143
+ output: {
144
+ assetFileNames: () => {
145
+ if (entrypoint.type === "content-script-style") {
146
+ return `content-scripts/${entrypoint.name}.[ext]`;
147
+ } else {
148
+ return `assets/${entrypoint.name}.[ext]`;
149
+ }
150
+ }
151
+ }
152
+ }
153
+ }
154
+ };
155
+ };
156
+ return {
157
+ name: "Vite",
158
+ version: vite.version,
159
+ async importEntrypoint(path) {
160
+ switch (wxtConfig.experimental.entrypointImporter) {
161
+ default:
162
+ case "jiti": {
163
+ return await importEntrypointFile(path);
164
+ }
165
+ case "vite-runtime": {
166
+ const baseConfig = await getBaseConfig();
167
+ const envConfig = {
168
+ plugins: [
169
+ wxtPlugins.extensionApiMock(wxtConfig),
170
+ wxtPlugins.removeEntrypointMainFunction(wxtConfig, path)
171
+ ]
172
+ };
173
+ const config = vite.mergeConfig(baseConfig, envConfig);
174
+ const server2 = await vite.createServer(config);
175
+ await server2.listen();
176
+ const runtime = await vite.createViteRuntime(server2, { hmr: false });
177
+ const module = await runtime.executeUrl(path);
178
+ await server2.close();
179
+ return module.default;
180
+ }
181
+ case "vite-node": {
182
+ const baseConfig = await getBaseConfig();
183
+ baseConfig.optimizeDeps ??= {};
184
+ baseConfig.optimizeDeps.noDiscovery = true;
185
+ baseConfig.optimizeDeps.include = [];
186
+ const envConfig = {
187
+ plugins: [
188
+ wxtPlugins.extensionApiMock(wxtConfig),
189
+ wxtPlugins.removeEntrypointMainFunction(wxtConfig, path)
190
+ ]
191
+ };
192
+ const config = vite.mergeConfig(baseConfig, envConfig);
193
+ const server2 = await vite.createServer(config);
194
+ await server2.pluginContainer.buildStart({});
195
+ const node = new ViteNodeServer(
196
+ // @ts-ignore: Some weird type error...
197
+ server2
198
+ );
199
+ installSourcemapsSupport({
200
+ getSourceMap: (source) => node.getSourceMap(source)
201
+ });
202
+ const runner = new ViteNodeRunner({
203
+ root: server2.config.root,
204
+ base: server2.config.base,
205
+ // when having the server and runner in a different context,
206
+ // you will need to handle the communication between them
207
+ // and pass to this function
208
+ fetchModule(id) {
209
+ return node.fetchModule(id);
210
+ },
211
+ resolveId(id, importer) {
212
+ return node.resolveId(id, importer);
213
+ }
214
+ });
215
+ const res = await runner.executeFile(path);
216
+ await server2.close();
217
+ return res.default;
218
+ }
219
+ }
220
+ },
221
+ async build(group) {
222
+ let entryConfig;
223
+ if (Array.isArray(group)) entryConfig = getMultiPageConfig(group);
224
+ else if (group.inputPath.endsWith(".css"))
225
+ entryConfig = getCssConfig(group);
226
+ else entryConfig = getLibModeConfig(group);
227
+ const buildConfig = vite.mergeConfig(await getBaseConfig(), entryConfig);
228
+ await hooks.callHook(
229
+ "vite:build:extendConfig",
230
+ toArray(group),
231
+ buildConfig
232
+ );
233
+ const result = await vite.build(buildConfig);
234
+ return {
235
+ entrypoints: group,
236
+ chunks: getBuildOutputChunks(result)
237
+ };
238
+ },
239
+ async createServer(info) {
240
+ const serverConfig = {
241
+ server: {
242
+ port: info.port,
243
+ strictPort: true,
244
+ host: info.hostname,
245
+ origin: info.origin
246
+ }
247
+ };
248
+ const baseConfig = await getBaseConfig();
249
+ const finalConfig = vite.mergeConfig(baseConfig, serverConfig);
250
+ await hooks.callHook("vite:devServer:extendConfig", finalConfig);
251
+ const viteServer = await vite.createServer(finalConfig);
252
+ const server2 = {
253
+ async listen() {
254
+ await viteServer.listen(info.port);
255
+ },
256
+ async close() {
257
+ await viteServer.close();
258
+ },
259
+ transformHtml(...args) {
260
+ return viteServer.transformIndexHtml(...args);
261
+ },
262
+ ws: {
263
+ send(message, payload) {
264
+ return viteServer.ws.send(message, payload);
265
+ },
266
+ on(message, cb) {
267
+ viteServer.ws.on(message, cb);
268
+ }
269
+ },
270
+ watcher: viteServer.watcher
271
+ };
272
+ return server2;
273
+ }
274
+ };
275
+ }
276
+ function getBuildOutputChunks(result) {
277
+ if ("on" in result) throw Error("wxt does not support vite watch mode.");
278
+ if (Array.isArray(result)) return result.flatMap(({ output }) => output);
279
+ return result.output;
280
+ }
281
+ function getRollupEntry(entrypoint) {
282
+ let virtualEntrypointType;
283
+ switch (entrypoint.type) {
284
+ case "background":
285
+ case "unlisted-script":
286
+ virtualEntrypointType = entrypoint.type;
287
+ break;
288
+ case "content-script":
289
+ virtualEntrypointType = entrypoint.options.world === "MAIN" ? "content-script-main-world" : "content-script-isolated-world";
290
+ break;
291
+ }
292
+ if (virtualEntrypointType) {
293
+ const moduleId = `virtual:wxt-${virtualEntrypointType}-entrypoint`;
294
+ return `${moduleId}?${entrypoint.inputPath}`;
295
+ }
296
+ return entrypoint.inputPath;
297
+ }
@@ -0,0 +1,7 @@
1
+ import type * as vite from 'vite';
2
+ import { ResolvedConfig } from '../../../../types';
3
+ export declare function bundleAnalysis(config: ResolvedConfig): vite.Plugin;
4
+ /**
5
+ * @deprecated FOR TESTING ONLY.
6
+ */
7
+ export declare function resetBundleIncrement(): void;
@@ -0,0 +1,15 @@
1
+ import { visualizer } from "@aklinker1/rollup-plugin-visualizer";
2
+ import path from "node:path";
3
+ let increment = 0;
4
+ export function bundleAnalysis(config) {
5
+ return visualizer({
6
+ template: "raw-data",
7
+ filename: path.resolve(
8
+ config.analysis.outputDir,
9
+ `${config.analysis.outputName}-${increment++}.json`
10
+ )
11
+ });
12
+ }
13
+ export function resetBundleIncrement() {
14
+ increment = 0;
15
+ }
@@ -0,0 +1,13 @@
1
+ import type * as vite from 'vite';
2
+ import { Entrypoint, ResolvedConfig } from '../../../../types';
3
+ /**
4
+ * Rename CSS entrypoint outputs to ensure a JS file is not generated, and that the CSS file is
5
+ * placed in the correct place.
6
+ *
7
+ * It:
8
+ * 1. Renames CSS files to their final paths
9
+ * 2. Removes the JS file that get's output by lib mode
10
+ *
11
+ * THIS PLUGIN SHOULD ONLY BE APPLIED TO CSS LIB MODE BUILDS. It should not be added to every build.
12
+ */
13
+ export declare function cssEntrypoints(entrypoint: Entrypoint, config: ResolvedConfig): vite.Plugin;
@@ -0,0 +1,22 @@
1
+ import { getEntrypointBundlePath } from "../../../utils/entrypoints.mjs";
2
+ export function cssEntrypoints(entrypoint, config) {
3
+ return {
4
+ name: "wxt:css-entrypoint",
5
+ config() {
6
+ return {
7
+ build: {
8
+ rollupOptions: {
9
+ output: {
10
+ assetFileNames: () => getEntrypointBundlePath(entrypoint, config.outDir, ".css")
11
+ }
12
+ }
13
+ }
14
+ };
15
+ },
16
+ generateBundle(_, bundle) {
17
+ Object.keys(bundle).forEach((file) => {
18
+ if (file.endsWith(".js")) delete bundle[file];
19
+ });
20
+ }
21
+ };
22
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Overrides definitions for `import.meta.*`
3
+ *
4
+ * - `import.meta.url`: Without this, background service workers crash trying to access
5
+ * `document.location`, see https://github.com/wxt-dev/wxt/issues/392
6
+ */
7
+ export declare function defineImportMeta(): {
8
+ name: string;
9
+ config(): {
10
+ define: {
11
+ 'import.meta.url': string;
12
+ };
13
+ };
14
+ };
@@ -0,0 +1,13 @@
1
+ export function defineImportMeta() {
2
+ return {
3
+ name: "wxt:define",
4
+ config() {
5
+ return {
6
+ define: {
7
+ // This works for all extension contexts, including background service worker
8
+ "import.meta.url": "self.location.href"
9
+ }
10
+ };
11
+ }
12
+ };
13
+ }
@@ -0,0 +1,7 @@
1
+ import type * as vite from 'vite';
2
+ import { ResolvedConfig, WxtDevServer } from '../../../../types';
3
+ /**
4
+ * Pre-renders the HTML entrypoints when building the extension to connect to the dev server.
5
+ */
6
+ export declare function devHtmlPrerender(config: ResolvedConfig, server: WxtDevServer | undefined): vite.PluginOption;
7
+ export declare function pointToDevServer(config: ResolvedConfig, server: WxtDevServer, id: string, document: Document, querySelector: string, attr: string): void;
@@ -0,0 +1,140 @@
1
+ import { getEntrypointName } from "../../../utils/entrypoints.mjs";
2
+ import { parseHTML } from "linkedom";
3
+ import { dirname, relative, resolve } from "node:path";
4
+ import { normalizePath } from "../../../utils/paths.mjs";
5
+ import { murmurHash } from "ohash";
6
+ const inlineScriptContents = {};
7
+ export function devHtmlPrerender(config, server) {
8
+ const htmlReloadId = "@wxt/reload-html";
9
+ const resolvedHtmlReloadId = resolve(
10
+ config.wxtModuleDir,
11
+ "dist/virtual/reload-html.mjs"
12
+ );
13
+ const virtualInlineScript = "virtual:wxt-inline-script";
14
+ const resolvedVirtualInlineScript = "\0" + virtualInlineScript;
15
+ return [
16
+ {
17
+ apply: "build",
18
+ name: "wxt:dev-html-prerender",
19
+ config() {
20
+ return {
21
+ resolve: {
22
+ alias: {
23
+ [htmlReloadId]: resolvedHtmlReloadId
24
+ }
25
+ }
26
+ };
27
+ },
28
+ // Convert scripts like src="./main.tsx" -> src="http://localhost:3000/entrypoints/popup/main.tsx"
29
+ // before the paths are replaced with their bundled path
30
+ transform(code, id) {
31
+ if (config.command !== "serve" || server == null || !id.endsWith(".html"))
32
+ return;
33
+ const { document } = parseHTML(code);
34
+ const _pointToDevServer = (querySelector, attr) => pointToDevServer(config, server, id, document, querySelector, attr);
35
+ _pointToDevServer("script[type=module]", "src");
36
+ _pointToDevServer("link[rel=stylesheet]", "href");
37
+ const reloader = document.createElement("script");
38
+ reloader.src = htmlReloadId;
39
+ reloader.type = "module";
40
+ document.head.appendChild(reloader);
41
+ const newHtml = document.toString();
42
+ config.logger.debug("transform " + id);
43
+ config.logger.debug("Old HTML:\n" + code);
44
+ config.logger.debug("New HTML:\n" + newHtml);
45
+ return newHtml;
46
+ },
47
+ // Pass the HTML through the dev server to add dev-mode specific code
48
+ async transformIndexHtml(html, ctx) {
49
+ if (config.command !== "serve" || server == null) return;
50
+ const originalUrl = `${server.origin}${ctx.path}`;
51
+ const name = getEntrypointName(config.entrypointsDir, ctx.filename);
52
+ const url = `${server.origin}/${name}.html`;
53
+ const serverHtml = await server.transformHtml(url, html, originalUrl);
54
+ const { document } = parseHTML(serverHtml);
55
+ const inlineScripts = document.querySelectorAll("script:not([src])");
56
+ inlineScripts.forEach((script) => {
57
+ const textContent = script.textContent ?? "";
58
+ const hash = murmurHash(textContent);
59
+ inlineScriptContents[hash] = textContent;
60
+ const virtualScript = document.createElement("script");
61
+ virtualScript.type = "module";
62
+ virtualScript.src = `${server.origin}/@id/${virtualInlineScript}?${hash}`;
63
+ script.replaceWith(virtualScript);
64
+ });
65
+ const viteClientScript = document.querySelector(
66
+ "script[src='/@vite/client']"
67
+ );
68
+ if (viteClientScript) {
69
+ viteClientScript.src = `${server.origin}${viteClientScript.src}`;
70
+ }
71
+ const newHtml = document.toString();
72
+ config.logger.debug("transformIndexHtml " + ctx.filename);
73
+ config.logger.debug("Old HTML:\n" + html);
74
+ config.logger.debug("New HTML:\n" + newHtml);
75
+ return newHtml;
76
+ }
77
+ },
78
+ {
79
+ name: "wxt:virtualize-react-refresh",
80
+ apply: "serve",
81
+ resolveId(id) {
82
+ if (id.startsWith(virtualInlineScript)) {
83
+ return "\0" + id;
84
+ }
85
+ if (id.startsWith("/chunks/")) {
86
+ return "\0noop";
87
+ }
88
+ },
89
+ load(id) {
90
+ if (id.startsWith(resolvedVirtualInlineScript)) {
91
+ const hash = Number(id.substring(id.indexOf("?") + 1));
92
+ return inlineScriptContents[hash];
93
+ }
94
+ if (id === "\0noop") {
95
+ return "";
96
+ }
97
+ }
98
+ }
99
+ ];
100
+ }
101
+ export function pointToDevServer(config, server, id, document, querySelector, attr) {
102
+ document.querySelectorAll(querySelector).forEach((element) => {
103
+ const src = element.getAttribute(attr);
104
+ if (!src || isUrl(src)) return;
105
+ let resolvedAbsolutePath;
106
+ const matchingAlias = Object.entries(config.alias).find(
107
+ ([key]) => src.startsWith(key)
108
+ );
109
+ if (matchingAlias) {
110
+ const [alias, replacement] = matchingAlias;
111
+ resolvedAbsolutePath = resolve(
112
+ config.root,
113
+ src.replace(alias, replacement)
114
+ );
115
+ } else {
116
+ resolvedAbsolutePath = resolve(dirname(id), src);
117
+ }
118
+ if (resolvedAbsolutePath) {
119
+ const relativePath = normalizePath(
120
+ relative(config.root, resolvedAbsolutePath)
121
+ );
122
+ if (relativePath.startsWith(".")) {
123
+ let path = normalizePath(resolvedAbsolutePath);
124
+ if (!path.startsWith("/")) path = "/" + path;
125
+ element.setAttribute(attr, `${server.origin}/@fs${path}`);
126
+ } else {
127
+ const url = new URL(relativePath, server.origin);
128
+ element.setAttribute(attr, url.href);
129
+ }
130
+ }
131
+ });
132
+ }
133
+ function isUrl(str) {
134
+ try {
135
+ new URL(str);
136
+ return true;
137
+ } catch {
138
+ return false;
139
+ }
140
+ }
@@ -0,0 +1,6 @@
1
+ import { Plugin } from 'vite';
2
+ import { ResolvedConfig, WxtDevServer } from '../../../../types';
3
+ /**
4
+ * Defines global constants about the dev server. Helps scripts connect to the server's web socket.
5
+ */
6
+ export declare function devServerGlobals(config: ResolvedConfig, server: WxtDevServer | undefined): Plugin;
@@ -0,0 +1,15 @@
1
+ export function devServerGlobals(config, server) {
2
+ return {
3
+ name: "wxt:dev-server-globals",
4
+ config() {
5
+ if (server == null || config.command == "build") return;
6
+ return {
7
+ define: {
8
+ __DEV_SERVER_PROTOCOL__: JSON.stringify("ws:"),
9
+ __DEV_SERVER_HOSTNAME__: JSON.stringify(server.hostname),
10
+ __DEV_SERVER_PORT__: JSON.stringify(server.port)
11
+ }
12
+ };
13
+ }
14
+ };
15
+ }
@@ -0,0 +1,10 @@
1
+ import { Plugin } from 'vite';
2
+ import { ResolvedConfig } from '../../../../types';
3
+ /**
4
+ * Downloads any URL imports, like Google Analytics, into virtual modules so they are bundled with
5
+ * the extension instead of depending on remote code at runtime.
6
+ *
7
+ * @example
8
+ * import "url:https://google-tagmanager.com/gtag?id=XYZ";
9
+ */
10
+ export declare function download(config: ResolvedConfig): Plugin;
@@ -0,0 +1,14 @@
1
+ import { fetchCached } from "../../../utils/network.mjs";
2
+ export function download(config) {
3
+ return {
4
+ name: "wxt:download",
5
+ resolveId(id) {
6
+ if (id.startsWith("url:")) return "\0" + id;
7
+ },
8
+ async load(id) {
9
+ if (!id.startsWith("\0url:")) return;
10
+ const url = id.replace("\0url:", "");
11
+ return await fetchCached(url, config);
12
+ }
13
+ };
14
+ }
@@ -0,0 +1,6 @@
1
+ import type * as vite from 'vite';
2
+ import { EntrypointGroup } from '../../../../types';
3
+ /**
4
+ * Define a set of global variables specific to an entrypoint.
5
+ */
6
+ export declare function entrypointGroupGlobals(entrypointGroup: EntrypointGroup): vite.PluginOption;
@@ -0,0 +1,16 @@
1
+ import { getEntrypointGlobals } from "../../../utils/globals.mjs";
2
+ export function entrypointGroupGlobals(entrypointGroup) {
3
+ return {
4
+ name: "wxt:entrypoint-group-globals",
5
+ config() {
6
+ const define = {};
7
+ let name = Array.isArray(entrypointGroup) ? "html" : entrypointGroup.name;
8
+ for (const global of getEntrypointGlobals(name)) {
9
+ define[`import.meta.env.${global.name}`] = JSON.stringify(global.value);
10
+ }
11
+ return {
12
+ define
13
+ };
14
+ }
15
+ };
16
+ }
@@ -0,0 +1,11 @@
1
+ import type * as vite from 'vite';
2
+ import { ResolvedConfig } from '../../../../types';
3
+ /**
4
+ * Mock `webextension-polyfill`, `wxt/browser`, and `wxt/browser/*` by inlining
5
+ * all dependencies that import them and adding a custom alias so that Vite
6
+ * resolves to a mocked version of the module.
7
+ *
8
+ * TODO: Detect non-wxt dependencies (like `@webext-core/*`) that import `webextension-polyfill` via
9
+ * `npm list` and inline them automatically.
10
+ */
11
+ export declare function extensionApiMock(config: ResolvedConfig): vite.PluginOption;
@@ -0,0 +1,26 @@
1
+ import path from "node:path";
2
+ export function extensionApiMock(config) {
3
+ return {
4
+ name: "wxt:extension-api-mock",
5
+ config() {
6
+ const replacement = path.resolve(
7
+ config.wxtModuleDir,
8
+ "dist/virtual/mock-browser"
9
+ );
10
+ return {
11
+ resolve: {
12
+ alias: [
13
+ { find: "webextension-polyfill", replacement },
14
+ // wxt/browser, wxt/browser/...
15
+ { find: /^wxt\/browser.*/, replacement }
16
+ ]
17
+ },
18
+ ssr: {
19
+ // Inline all WXT modules so vite processes them so the aliases can
20
+ // be resolved
21
+ noExternal: ["wxt"]
22
+ }
23
+ };
24
+ }
25
+ };
26
+ }
@@ -0,0 +1,3 @@
1
+ import type * as vite from 'vite';
2
+ import { ResolvedConfig } from '../../../../types';
3
+ export declare function globals(config: ResolvedConfig): vite.PluginOption;
@@ -0,0 +1,15 @@
1
+ import { getGlobals } from "../../../utils/globals.mjs";
2
+ export function globals(config) {
3
+ return {
4
+ name: "wxt:globals",
5
+ config() {
6
+ const define = {};
7
+ for (const global of getGlobals(config)) {
8
+ define[`import.meta.env.${global.name}`] = JSON.stringify(global.value);
9
+ }
10
+ return {
11
+ define
12
+ };
13
+ }
14
+ };
15
+ }