wxt 0.20.13 → 0.20.15

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 (280) hide show
  1. package/dist/browser.d.mts +20 -0
  2. package/dist/browser.mjs +18 -3
  3. package/dist/builtin-modules/index.mjs +7 -2
  4. package/dist/builtin-modules/unimport.mjs +69 -75
  5. package/dist/cli/cli-utils.mjs +54 -50
  6. package/dist/cli/commands.mjs +78 -103
  7. package/dist/cli/index.d.mts +1 -0
  8. package/dist/cli/index.mjs +9 -4
  9. package/dist/core/{build.d.ts → build.d.mts} +6 -2
  10. package/dist/core/build.mjs +23 -4
  11. package/dist/core/builders/vite/index.mjs +293 -343
  12. package/dist/core/builders/vite/plugins/bundleAnalysis.mjs +11 -12
  13. package/dist/core/builders/vite/plugins/cssEntrypoints.mjs +27 -20
  14. package/dist/core/builders/vite/plugins/defineImportMeta.mjs +17 -12
  15. package/dist/core/builders/vite/plugins/devHtmlPrerender.mjs +105 -136
  16. package/dist/core/builders/vite/plugins/devServerGlobals.mjs +15 -14
  17. package/dist/core/builders/vite/plugins/download.mjs +23 -12
  18. package/dist/core/builders/vite/plugins/entrypointGroupGlobals.mjs +18 -14
  19. package/dist/core/builders/vite/plugins/extensionApiMock.mjs +31 -33
  20. package/dist/core/builders/vite/plugins/globals.mjs +14 -13
  21. package/dist/core/builders/vite/plugins/iifeFooter.mjs +17 -11
  22. package/dist/core/builders/vite/plugins/index.mjs +18 -16
  23. package/dist/core/builders/vite/plugins/noopBackground.mjs +21 -15
  24. package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.mjs +26 -23
  25. package/dist/core/builders/vite/plugins/resolveAppConfig.mjs +26 -24
  26. package/dist/core/builders/vite/plugins/resolveVirtualModules.mjs +28 -26
  27. package/dist/core/builders/vite/plugins/tsconfigPaths.mjs +11 -11
  28. package/dist/core/builders/vite/plugins/wxtPluginLoader.mjs +42 -47
  29. package/dist/core/{clean.d.ts → clean.d.mts} +7 -3
  30. package/dist/core/clean.mjs +38 -35
  31. package/dist/core/{create-server.d.ts → create-server.d.mts} +6 -2
  32. package/dist/core/create-server.mjs +236 -247
  33. package/dist/core/define-config.d.mts +6 -0
  34. package/dist/core/define-config.mjs +6 -2
  35. package/dist/core/define-web-ext-config.d.mts +13 -0
  36. package/dist/core/define-web-ext-config.mjs +16 -7
  37. package/dist/core/generate-wxt-dir.mjs +110 -148
  38. package/dist/core/index.d.mts +9 -0
  39. package/dist/core/index.mjs +12 -8
  40. package/dist/core/initialize.d.mts +8 -0
  41. package/dist/core/initialize.mjs +110 -124
  42. package/dist/core/keyboard-shortcuts.mjs +30 -28
  43. package/dist/core/package-managers/bun.mjs +18 -16
  44. package/dist/core/package-managers/deno.mjs +12 -8
  45. package/dist/core/package-managers/index.mjs +52 -60
  46. package/dist/core/package-managers/npm.mjs +51 -52
  47. package/dist/core/package-managers/pnpm.mjs +21 -18
  48. package/dist/core/package-managers/yarn.mjs +31 -28
  49. package/dist/core/prepare.d.mts +6 -0
  50. package/dist/core/prepare.mjs +12 -7
  51. package/dist/core/resolve-config.mjs +393 -462
  52. package/dist/core/runners/index.mjs +15 -10
  53. package/dist/core/runners/manual.mjs +13 -14
  54. package/dist/core/runners/safari.mjs +13 -14
  55. package/dist/core/runners/web-ext.mjs +62 -83
  56. package/dist/core/runners/wsl.mjs +13 -14
  57. package/dist/core/utils/arrays.mjs +23 -12
  58. package/dist/core/utils/building/build-entrypoints.mjs +44 -40
  59. package/dist/core/utils/building/detect-dev-changes.mjs +84 -105
  60. package/dist/core/utils/building/find-entrypoints.mjs +278 -359
  61. package/dist/core/utils/building/group-entrypoints.mjs +42 -35
  62. package/dist/core/utils/building/index.mjs +8 -6
  63. package/dist/core/utils/building/internal-build.mjs +86 -102
  64. package/dist/core/utils/building/rebuild.mjs +50 -30
  65. package/dist/core/utils/cache.mjs +29 -18
  66. package/dist/core/utils/constants.mjs +9 -1
  67. package/dist/core/utils/content-scripts.mjs +56 -54
  68. package/dist/core/utils/content-security-policy.mjs +36 -39
  69. package/dist/core/utils/entrypoints.mjs +51 -28
  70. package/dist/core/utils/env.mjs +22 -17
  71. package/dist/core/utils/environments/browser-environment.mjs +13 -12
  72. package/dist/core/utils/environments/environment.mjs +36 -33
  73. package/dist/core/utils/environments/extension-environment.mjs +15 -10
  74. package/dist/core/utils/environments/index.mjs +4 -2
  75. package/dist/core/utils/eslint.mjs +11 -10
  76. package/dist/core/utils/fs.mjs +23 -11
  77. package/dist/core/utils/globals.mjs +53 -51
  78. package/dist/core/utils/i18n.mjs +41 -32
  79. package/dist/core/utils/index.mjs +3 -0
  80. package/dist/core/utils/log/index.mjs +6 -4
  81. package/dist/core/utils/log/printBuildSummary.mjs +20 -25
  82. package/dist/core/utils/log/printFileList.mjs +30 -33
  83. package/dist/core/utils/log/printHeader.mjs +9 -5
  84. package/dist/core/utils/log/printTable.mjs +19 -21
  85. package/dist/core/utils/manifest.mjs +366 -479
  86. package/dist/core/utils/minimatch-multiple.mjs +26 -15
  87. package/dist/core/utils/network.mjs +27 -33
  88. package/dist/core/utils/number.mjs +7 -3
  89. package/dist/core/utils/package.mjs +20 -13
  90. package/dist/core/utils/paths.d.mts +8 -0
  91. package/dist/core/utils/paths.mjs +26 -7
  92. package/dist/core/utils/strings.mjs +19 -14
  93. package/dist/core/utils/syntax-errors.mjs +14 -16
  94. package/dist/core/utils/time.mjs +19 -15
  95. package/dist/core/utils/transform.mjs +129 -160
  96. package/dist/core/utils/types.d.mts +6 -0
  97. package/dist/core/utils/validation.mjs +43 -50
  98. package/dist/core/utils/virtual-modules.mjs +20 -12
  99. package/dist/core/utils/wsl.mjs +11 -3
  100. package/dist/core/wxt.mjs +65 -60
  101. package/dist/core/{zip.d.ts → zip.d.mts} +6 -2
  102. package/dist/core/zip.mjs +118 -140
  103. package/dist/index.d.mts +13 -0
  104. package/dist/index.mjs +13 -3
  105. package/dist/{modules.d.ts → modules.d.mts} +14 -16
  106. package/dist/modules.mjs +179 -51
  107. package/dist/testing/fake-browser.d.mts +2 -0
  108. package/dist/testing/fake-browser.mjs +3 -1
  109. package/dist/testing/index.d.mts +3 -0
  110. package/dist/testing/index.mjs +4 -2
  111. package/dist/testing/{wxt-vitest-plugin.d.ts → wxt-vitest-plugin.d.mts} +7 -9
  112. package/dist/testing/wxt-vitest-plugin.mjs +38 -19
  113. package/dist/types.d.mts +1475 -0
  114. package/dist/utils/app-config.d.mts +6 -0
  115. package/dist/utils/app-config.mjs +8 -2
  116. package/dist/utils/content-script-context.d.mts +134 -0
  117. package/dist/utils/content-script-context.mjs +187 -175
  118. package/dist/utils/content-script-ui/iframe.d.mts +42 -0
  119. package/dist/utils/content-script-ui/iframe.mjs +42 -27
  120. package/dist/utils/content-script-ui/integrated.d.mts +37 -0
  121. package/dist/utils/content-script-ui/integrated.mjs +36 -29
  122. package/dist/utils/content-script-ui/shadow-root.d.mts +80 -0
  123. package/dist/utils/content-script-ui/shadow-root.mjs +75 -82
  124. package/dist/utils/content-script-ui/shared.mjs +115 -150
  125. package/dist/utils/content-script-ui/types.d.mts +107 -0
  126. package/dist/utils/content-script-ui/types.mjs +1 -0
  127. package/dist/utils/{define-app-config.d.ts → define-app-config.d.mts} +5 -3
  128. package/dist/utils/define-app-config.mjs +22 -2
  129. package/dist/utils/define-background.d.mts +7 -0
  130. package/dist/utils/define-background.mjs +7 -3
  131. package/dist/utils/define-content-script.d.mts +6 -0
  132. package/dist/utils/define-content-script.mjs +6 -2
  133. package/dist/utils/define-unlisted-script.d.mts +7 -0
  134. package/dist/utils/define-unlisted-script.mjs +7 -3
  135. package/dist/utils/define-wxt-plugin.d.mts +6 -0
  136. package/dist/utils/define-wxt-plugin.mjs +6 -2
  137. package/dist/utils/inject-script.d.mts +41 -0
  138. package/dist/utils/inject-script.mjs +44 -34
  139. package/dist/utils/internal/custom-events.d.mts +9 -0
  140. package/dist/utils/internal/custom-events.mjs +18 -10
  141. package/dist/utils/internal/location-watcher.mjs +23 -20
  142. package/dist/utils/internal/logger.mjs +15 -12
  143. package/dist/utils/match-patterns.d.mts +1 -0
  144. package/dist/utils/match-patterns.mjs +3 -1
  145. package/dist/utils/{split-shadow-root-css.d.ts → split-shadow-root-css.d.mts} +6 -3
  146. package/dist/utils/split-shadow-root-css.mjs +16 -7
  147. package/dist/utils/storage.d.mts +1 -0
  148. package/dist/utils/storage.mjs +3 -1
  149. package/dist/version.d.mts +4 -0
  150. package/dist/version.mjs +5 -1
  151. package/dist/virtual/background-entrypoint.d.mts +4 -0
  152. package/dist/virtual/background-entrypoint.mjs +143 -161
  153. package/dist/virtual/content-script-isolated-world-entrypoint.d.mts +4 -0
  154. package/dist/virtual/content-script-isolated-world-entrypoint.mjs +26 -27
  155. package/dist/virtual/content-script-main-world-entrypoint.d.mts +4 -0
  156. package/dist/virtual/content-script-main-world-entrypoint.mjs +24 -24
  157. package/dist/virtual/mock-browser.d.mts +6 -0
  158. package/dist/virtual/mock-browser.mjs +5 -3
  159. package/dist/virtual/reload-html.d.mts +1 -0
  160. package/dist/virtual/reload-html.mjs +57 -53
  161. package/dist/virtual/unlisted-script-entrypoint.d.mts +4 -0
  162. package/dist/virtual/unlisted-script-entrypoint.mjs +35 -43
  163. package/package.json +56 -50
  164. package/dist/browser.d.ts +0 -30
  165. package/dist/builtin-modules/index.d.ts +0 -2
  166. package/dist/builtin-modules/unimport.d.ts +0 -5
  167. package/dist/cli/cli-utils.d.ts +0 -25
  168. package/dist/cli/commands.d.ts +0 -2
  169. package/dist/cli/index.d.ts +0 -1
  170. package/dist/core/builders/vite/index.d.ts +0 -7
  171. package/dist/core/builders/vite/plugins/bundleAnalysis.d.ts +0 -7
  172. package/dist/core/builders/vite/plugins/cssEntrypoints.d.ts +0 -13
  173. package/dist/core/builders/vite/plugins/defineImportMeta.d.ts +0 -14
  174. package/dist/core/builders/vite/plugins/devHtmlPrerender.d.ts +0 -7
  175. package/dist/core/builders/vite/plugins/devServerGlobals.d.ts +0 -6
  176. package/dist/core/builders/vite/plugins/download.d.ts +0 -10
  177. package/dist/core/builders/vite/plugins/entrypointGroupGlobals.d.ts +0 -6
  178. package/dist/core/builders/vite/plugins/extensionApiMock.d.ts +0 -6
  179. package/dist/core/builders/vite/plugins/globals.d.ts +0 -3
  180. package/dist/core/builders/vite/plugins/iifeFooter.d.ts +0 -8
  181. package/dist/core/builders/vite/plugins/index.d.ts +0 -16
  182. package/dist/core/builders/vite/plugins/noopBackground.d.ts +0 -6
  183. package/dist/core/builders/vite/plugins/removeEntrypointMainFunction.d.ts +0 -6
  184. package/dist/core/builders/vite/plugins/resolveAppConfig.d.ts +0 -6
  185. package/dist/core/builders/vite/plugins/resolveVirtualModules.d.ts +0 -6
  186. package/dist/core/builders/vite/plugins/tsconfigPaths.d.ts +0 -3
  187. package/dist/core/builders/vite/plugins/wxtPluginLoader.d.ts +0 -6
  188. package/dist/core/define-config.d.ts +0 -2
  189. package/dist/core/define-web-ext-config.d.ts +0 -9
  190. package/dist/core/generate-wxt-dir.d.ts +0 -5
  191. package/dist/core/index.d.ts +0 -8
  192. package/dist/core/initialize.d.ts +0 -5
  193. package/dist/core/keyboard-shortcuts.d.ts +0 -12
  194. package/dist/core/package-managers/bun.d.ts +0 -2
  195. package/dist/core/package-managers/deno.d.ts +0 -2
  196. package/dist/core/package-managers/index.d.ts +0 -2
  197. package/dist/core/package-managers/npm.d.ts +0 -17
  198. package/dist/core/package-managers/pnpm.d.ts +0 -2
  199. package/dist/core/package-managers/types.d.ts +0 -2
  200. package/dist/core/package-managers/types.mjs +0 -0
  201. package/dist/core/package-managers/yarn.d.ts +0 -2
  202. package/dist/core/prepare.d.ts +0 -2
  203. package/dist/core/resolve-config.d.ts +0 -11
  204. package/dist/core/runners/index.d.ts +0 -2
  205. package/dist/core/runners/manual.d.ts +0 -5
  206. package/dist/core/runners/safari.d.ts +0 -5
  207. package/dist/core/runners/web-ext.d.ts +0 -5
  208. package/dist/core/runners/wsl.d.ts +0 -5
  209. package/dist/core/utils/arrays.d.ts +0 -13
  210. package/dist/core/utils/building/build-entrypoints.d.ts +0 -3
  211. package/dist/core/utils/building/detect-dev-changes.d.ts +0 -61
  212. package/dist/core/utils/building/find-entrypoints.d.ts +0 -5
  213. package/dist/core/utils/building/group-entrypoints.d.ts +0 -8
  214. package/dist/core/utils/building/index.d.ts +0 -6
  215. package/dist/core/utils/building/internal-build.d.ts +0 -12
  216. package/dist/core/utils/building/rebuild.d.ts +0 -23
  217. package/dist/core/utils/cache.d.ts +0 -8
  218. package/dist/core/utils/cli.d.ts +0 -3
  219. package/dist/core/utils/cli.mjs +0 -26
  220. package/dist/core/utils/constants.d.ts +0 -5
  221. package/dist/core/utils/content-scripts.d.ts +0 -12
  222. package/dist/core/utils/content-security-policy.d.ts +0 -14
  223. package/dist/core/utils/entrypoints.d.ts +0 -31
  224. package/dist/core/utils/env.d.ts +0 -5
  225. package/dist/core/utils/environments/browser-environment.d.ts +0 -3
  226. package/dist/core/utils/environments/environment.d.ts +0 -8
  227. package/dist/core/utils/environments/extension-environment.d.ts +0 -6
  228. package/dist/core/utils/environments/index.d.ts +0 -2
  229. package/dist/core/utils/eslint.d.ts +0 -1
  230. package/dist/core/utils/fs.d.ts +0 -13
  231. package/dist/core/utils/globals.d.ts +0 -11
  232. package/dist/core/utils/i18n.d.ts +0 -11
  233. package/dist/core/utils/log/index.d.ts +0 -4
  234. package/dist/core/utils/log/printBuildSummary.d.ts +0 -2
  235. package/dist/core/utils/log/printFileList.d.ts +0 -1
  236. package/dist/core/utils/log/printHeader.d.ts +0 -1
  237. package/dist/core/utils/log/printTable.d.ts +0 -1
  238. package/dist/core/utils/manifest.d.ts +0 -40
  239. package/dist/core/utils/minimatch-multiple.d.ts +0 -15
  240. package/dist/core/utils/network.d.ts +0 -7
  241. package/dist/core/utils/number.d.ts +0 -1
  242. package/dist/core/utils/package.d.ts +0 -6
  243. package/dist/core/utils/paths.d.ts +0 -11
  244. package/dist/core/utils/strings.d.ts +0 -14
  245. package/dist/core/utils/syntax-errors.d.ts +0 -11
  246. package/dist/core/utils/testing/fake-objects.d.ts +0 -11665
  247. package/dist/core/utils/testing/fake-objects.mjs +0 -326
  248. package/dist/core/utils/time.d.ts +0 -9
  249. package/dist/core/utils/transform.d.ts +0 -11
  250. package/dist/core/utils/types.d.ts +0 -13
  251. package/dist/core/utils/types.mjs +0 -0
  252. package/dist/core/utils/validation.d.ts +0 -15
  253. package/dist/core/utils/virtual-modules.d.ts +0 -22
  254. package/dist/core/utils/wsl.d.ts +0 -4
  255. package/dist/core/wxt.d.ts +0 -24
  256. package/dist/index.d.ts +0 -11
  257. package/dist/testing/fake-browser.d.ts +0 -10
  258. package/dist/testing/index.d.ts +0 -10
  259. package/dist/types.d.ts +0 -1459
  260. package/dist/types.mjs +0 -0
  261. package/dist/utils/app-config.d.ts +0 -2
  262. package/dist/utils/content-script-context.d.ts +0 -134
  263. package/dist/utils/content-script-ui/iframe.d.ts +0 -32
  264. package/dist/utils/content-script-ui/integrated.d.ts +0 -34
  265. package/dist/utils/content-script-ui/shadow-root.d.ts +0 -76
  266. package/dist/utils/content-script-ui/shared.d.ts +0 -5
  267. package/dist/utils/content-script-ui/types.d.ts +0 -104
  268. package/dist/utils/define-background.d.ts +0 -4
  269. package/dist/utils/define-content-script.d.ts +0 -3
  270. package/dist/utils/define-unlisted-script.d.ts +0 -4
  271. package/dist/utils/define-wxt-plugin.d.ts +0 -3
  272. package/dist/utils/inject-script.d.ts +0 -36
  273. package/dist/utils/internal/custom-events.d.ts +0 -10
  274. package/dist/utils/internal/dev-server-websocket.d.ts +0 -21
  275. package/dist/utils/internal/dev-server-websocket.mjs +0 -37
  276. package/dist/utils/internal/location-watcher.d.ts +0 -12
  277. package/dist/utils/internal/logger.d.ts +0 -9
  278. package/dist/utils/match-patterns.d.ts +0 -5
  279. package/dist/utils/storage.d.ts +0 -5
  280. package/dist/version.d.ts +0 -1
@@ -1,356 +1,306 @@
1
- import * as wxtPlugins from "./plugins/index.mjs";
2
- import {
3
- getEntrypointBundlePath,
4
- isHtmlEntrypoint
5
- } from "../../utils/entrypoints.mjs";
1
+ import { normalizePath } from "../../utils/paths.mjs";
2
+ import "../../utils/index.mjs";
3
+ import { getEntrypointBundlePath, isHtmlEntrypoint } from "../../utils/entrypoints.mjs";
4
+ import { devHtmlPrerender } from "./plugins/devHtmlPrerender.mjs";
5
+ import { devServerGlobals } from "./plugins/devServerGlobals.mjs";
6
+ import { download } from "./plugins/download.mjs";
7
+ import { resolveVirtualModules } from "./plugins/resolveVirtualModules.mjs";
8
+ import { tsconfigPaths } from "./plugins/tsconfigPaths.mjs";
9
+ import { noopBackground } from "./plugins/noopBackground.mjs";
10
+ import { cssEntrypoints } from "./plugins/cssEntrypoints.mjs";
11
+ import { bundleAnalysis } from "./plugins/bundleAnalysis.mjs";
12
+ import { globals } from "./plugins/globals.mjs";
13
+ import { entrypointGroupGlobals } from "./plugins/entrypointGroupGlobals.mjs";
14
+ import { defineImportMeta } from "./plugins/defineImportMeta.mjs";
15
+ import { removeEntrypointMainFunction } from "./plugins/removeEntrypointMainFunction.mjs";
16
+ import { wxtPluginLoader } from "./plugins/wxtPluginLoader.mjs";
17
+ import { resolveAppConfig } from "./plugins/resolveAppConfig.mjs";
18
+ import { iifeFooter } from "./plugins/iifeFooter.mjs";
19
+ import "./plugins/index.mjs";
6
20
  import { toArray } from "../../utils/arrays.mjs";
7
21
  import { safeVarName } from "../../utils/strings.mjs";
22
+ import { createExtensionEnvironment } from "../../utils/environments/extension-environment.mjs";
23
+ import "../../utils/environments/index.mjs";
24
+ import fs from "fs-extra";
25
+ import { dirname, extname, join, relative } from "node:path";
8
26
  import { ViteNodeServer } from "vite-node/server";
9
27
  import { ViteNodeRunner } from "vite-node/client";
10
28
  import { installSourcemapsSupport } from "vite-node/source-map";
11
- import { createExtensionEnvironment } from "../../utils/environments/index.mjs";
12
- import { dirname, extname, join, relative } from "node:path";
13
- import fs from "fs-extra";
14
- import { normalizePath } from "../../utils/paths.mjs";
15
- export async function createViteBuilder(wxtConfig, hooks, getWxtDevServer) {
16
- const vite = await import("vite");
17
- const getBaseConfig = async (baseConfigOptions) => {
18
- const config = await wxtConfig.vite(wxtConfig.env);
19
- config.root = wxtConfig.root;
20
- config.configFile = false;
21
- config.logLevel = "warn";
22
- config.mode = wxtConfig.mode;
23
- config.envPrefix ??= ["VITE_", "WXT_"];
24
- config.build ??= {};
25
- config.publicDir = wxtConfig.publicDir;
26
- config.build.copyPublicDir = false;
27
- config.build.outDir = wxtConfig.outDir;
28
- config.build.emptyOutDir = false;
29
- if (config.build.minify == null && wxtConfig.command === "serve") {
30
- config.build.minify = false;
31
- }
32
- if (config.build.sourcemap == null && wxtConfig.command === "serve") {
33
- config.build.sourcemap = "inline";
34
- }
35
- config.server ??= {};
36
- config.server.watch = {
37
- ignored: [`${wxtConfig.outBaseDir}/**`, `${wxtConfig.wxtDir}/**`]
38
- };
39
- config.legacy ??= {};
40
- config.legacy.skipWebSocketTokenCheck = true;
41
- const server = getWxtDevServer?.();
42
- config.plugins ??= [];
43
- config.plugins.push(
44
- wxtPlugins.download(wxtConfig),
45
- wxtPlugins.devHtmlPrerender(wxtConfig, server),
46
- wxtPlugins.resolveVirtualModules(wxtConfig),
47
- wxtPlugins.devServerGlobals(wxtConfig, server),
48
- wxtPlugins.tsconfigPaths(wxtConfig),
49
- wxtPlugins.noopBackground(),
50
- wxtPlugins.globals(wxtConfig),
51
- wxtPlugins.defineImportMeta(),
52
- wxtPlugins.wxtPluginLoader(wxtConfig),
53
- wxtPlugins.resolveAppConfig(wxtConfig)
54
- );
55
- if (wxtConfig.analysis.enabled && // If included, vite-node entrypoint loader will increment the
56
- // bundleAnalysis's internal build index tracker, which we don't want
57
- !baseConfigOptions?.excludeAnalysisPlugin) {
58
- config.plugins.push(wxtPlugins.bundleAnalysis(wxtConfig));
59
- }
60
- return config;
61
- };
62
- const getLibModeConfig = (entrypoint) => {
63
- const entry = getRollupEntry(entrypoint);
64
- const plugins = [
65
- wxtPlugins.entrypointGroupGlobals(entrypoint)
66
- ];
67
- const iifeReturnValueName = safeVarName(entrypoint.name);
68
- if (entrypoint.type === "content-script-style" || entrypoint.type === "unlisted-style") {
69
- plugins.push(wxtPlugins.cssEntrypoints(entrypoint, wxtConfig));
70
- }
71
- if (entrypoint.type === "content-script" || entrypoint.type === "unlisted-script") {
72
- plugins.push(wxtPlugins.iifeFooter(iifeReturnValueName));
73
- }
74
- return {
75
- mode: wxtConfig.mode,
76
- plugins,
77
- build: {
78
- lib: {
79
- entry,
80
- formats: ["iife"],
81
- name: iifeReturnValueName,
82
- fileName: entrypoint.name
83
- },
84
- rollupOptions: {
85
- output: {
86
- // There's only a single output for this build, so we use the desired bundle path for the
87
- // entry output (like "content-scripts/overlay.js")
88
- entryFileNames: getEntrypointBundlePath(
89
- entrypoint,
90
- wxtConfig.outDir,
91
- ".js"
92
- ),
93
- // Output content script CSS to `content-scripts/`, but all other scripts are written to
94
- // `assets/`.
95
- assetFileNames: ({ name }) => {
96
- if (entrypoint.type === "content-script" && name?.endsWith("css")) {
97
- return `content-scripts/${entrypoint.name}.[ext]`;
98
- } else {
99
- return `assets/${entrypoint.name}.[ext]`;
100
- }
101
- }
102
- }
103
- }
104
- },
105
- define: {
106
- // See https://github.com/aklinker1/vite-plugin-web-extension/issues/96
107
- "process.env.NODE_ENV": JSON.stringify(wxtConfig.mode)
108
- }
109
- };
110
- };
111
- const getMultiPageConfig = (entrypoints) => {
112
- const htmlEntrypoints = new Set(
113
- entrypoints.filter(isHtmlEntrypoint).map((e) => e.name)
114
- );
115
- return {
116
- mode: wxtConfig.mode,
117
- plugins: [wxtPlugins.entrypointGroupGlobals(entrypoints)],
118
- build: {
119
- rollupOptions: {
120
- input: entrypoints.reduce((input, entry) => {
121
- input[entry.name] = getRollupEntry(entry);
122
- return input;
123
- }, {}),
124
- output: {
125
- // Include a hash to prevent conflicts
126
- chunkFileNames: "chunks/[name]-[hash].js",
127
- entryFileNames: ({ name }) => {
128
- if (htmlEntrypoints.has(name)) return "chunks/[name]-[hash].js";
129
- return "[name].js";
130
- },
131
- // We can't control the "name", so we need a hash to prevent conflicts
132
- assetFileNames: "assets/[name]-[hash].[ext]"
133
- }
134
- }
135
- }
136
- };
137
- };
138
- const getCssConfig = (entrypoint) => {
139
- return {
140
- mode: wxtConfig.mode,
141
- plugins: [wxtPlugins.entrypointGroupGlobals(entrypoint)],
142
- build: {
143
- rollupOptions: {
144
- input: {
145
- [entrypoint.name]: entrypoint.inputPath
146
- },
147
- output: {
148
- assetFileNames: () => {
149
- if (entrypoint.type === "content-script-style") {
150
- return `content-scripts/${entrypoint.name}.[ext]`;
151
- } else {
152
- return `assets/${entrypoint.name}.[ext]`;
153
- }
154
- }
155
- }
156
- }
157
- }
158
- };
159
- };
160
- const createViteNodeImporter = async (paths) => {
161
- const baseConfig = await getBaseConfig({
162
- excludeAnalysisPlugin: true
163
- });
164
- baseConfig.optimizeDeps ??= {};
165
- baseConfig.optimizeDeps.noDiscovery = true;
166
- baseConfig.optimizeDeps.include = [];
167
- const envConfig = {
168
- plugins: paths.map(
169
- (path) => wxtPlugins.removeEntrypointMainFunction(wxtConfig, path)
170
- )
171
- };
172
- const config = vite.mergeConfig(baseConfig, envConfig);
173
- const server = await vite.createServer(config);
174
- await server.pluginContainer.buildStart({});
175
- const node = new ViteNodeServer(
176
- // @ts-ignore: Some weird type error...
177
- server
178
- );
179
- installSourcemapsSupport({
180
- getSourceMap: (source) => node.getSourceMap(source)
181
- });
182
- const runner = new ViteNodeRunner({
183
- root: server.config.root,
184
- base: server.config.base,
185
- // when having the server and runner in a different context,
186
- // you will need to handle the communication between them
187
- // and pass to this function
188
- fetchModule(id) {
189
- return node.fetchModule(id);
190
- },
191
- resolveId(id, importer) {
192
- return node.resolveId(id, importer);
193
- }
194
- });
195
- return { runner, server };
196
- };
197
- const requireDefaultExport = (path, mod) => {
198
- const relativePath = relative(wxtConfig.root, path);
199
- if (mod?.default == null) {
200
- const defineFn = relativePath.includes(".content") ? "defineContentScript" : relativePath.includes("background") ? "defineBackground" : "defineUnlistedScript";
201
- throw Error(
202
- `${relativePath}: Default export not found, did you forget to call "export default ${defineFn}(...)"?`
203
- );
204
- }
205
- };
206
- return {
207
- name: "Vite",
208
- version: vite.version,
209
- async importEntrypoint(path) {
210
- const env = createExtensionEnvironment();
211
- const { runner, server } = await createViteNodeImporter([path]);
212
- const res = await env.run(() => runner.executeFile(path));
213
- await server.close();
214
- requireDefaultExport(path, res);
215
- return res.default;
216
- },
217
- async importEntrypoints(paths) {
218
- const env = createExtensionEnvironment();
219
- const { runner, server } = await createViteNodeImporter(paths);
220
- const res = await env.run(
221
- () => Promise.all(
222
- paths.map(async (path) => {
223
- const mod = await runner.executeFile(path);
224
- requireDefaultExport(path, mod);
225
- return mod.default;
226
- })
227
- )
228
- );
229
- await server.close();
230
- return res;
231
- },
232
- async build(group) {
233
- let entryConfig;
234
- if (Array.isArray(group)) entryConfig = getMultiPageConfig(group);
235
- else if (group.type === "content-script-style" || group.type === "unlisted-style")
236
- entryConfig = getCssConfig(group);
237
- else entryConfig = getLibModeConfig(group);
238
- const buildConfig = vite.mergeConfig(await getBaseConfig(), entryConfig);
239
- await hooks.callHook(
240
- "vite:build:extendConfig",
241
- toArray(group),
242
- buildConfig
243
- );
244
- const result = await vite.build(buildConfig);
245
- const chunks = getBuildOutputChunks(result);
246
- return {
247
- entrypoints: group,
248
- chunks: await moveHtmlFiles(wxtConfig, group, chunks)
249
- };
250
- },
251
- async createServer(info) {
252
- const serverConfig = {
253
- server: {
254
- host: info.host,
255
- port: info.port,
256
- strictPort: true,
257
- origin: info.origin
258
- }
259
- };
260
- const baseConfig = await getBaseConfig();
261
- const finalConfig = vite.mergeConfig(baseConfig, serverConfig);
262
- await hooks.callHook("vite:devServer:extendConfig", finalConfig);
263
- const viteServer = await vite.createServer(finalConfig);
264
- const server = {
265
- async listen() {
266
- await viteServer.listen(info.port);
267
- },
268
- async close() {
269
- await viteServer.close();
270
- },
271
- transformHtml(...args) {
272
- return viteServer.transformIndexHtml(...args);
273
- },
274
- ws: {
275
- send(message, payload) {
276
- return viteServer.ws.send(message, payload);
277
- },
278
- on(message, cb) {
279
- viteServer.ws.on(message, cb);
280
- }
281
- },
282
- watcher: viteServer.watcher,
283
- on(event, cb) {
284
- viteServer.httpServer?.on(event, cb);
285
- }
286
- };
287
- return server;
288
- }
289
- };
29
+
30
+ //#region src/core/builders/vite/index.ts
31
+ async function createViteBuilder(wxtConfig, hooks, getWxtDevServer) {
32
+ const vite = await import("vite");
33
+ /**
34
+ * Returns the base vite config shared by all builds based on the inline and user config.
35
+ */
36
+ const getBaseConfig = async (baseConfigOptions) => {
37
+ const config = await wxtConfig.vite(wxtConfig.env);
38
+ config.root = wxtConfig.root;
39
+ config.configFile = false;
40
+ config.logLevel = "warn";
41
+ config.mode = wxtConfig.mode;
42
+ config.envPrefix ??= ["VITE_", "WXT_"];
43
+ config.build ??= {};
44
+ config.publicDir = wxtConfig.publicDir;
45
+ config.build.copyPublicDir = false;
46
+ config.build.outDir = wxtConfig.outDir;
47
+ config.build.emptyOutDir = false;
48
+ if (config.build.minify == null && wxtConfig.command === "serve") config.build.minify = false;
49
+ if (config.build.sourcemap == null && wxtConfig.command === "serve") config.build.sourcemap = "inline";
50
+ config.server ??= {};
51
+ config.server.watch = { ignored: [`${wxtConfig.outBaseDir}/**`, `${wxtConfig.wxtDir}/**`] };
52
+ config.legacy ??= {};
53
+ config.legacy.skipWebSocketTokenCheck = true;
54
+ const server = getWxtDevServer?.();
55
+ config.plugins ??= [];
56
+ config.plugins.push(download(wxtConfig), devHtmlPrerender(wxtConfig, server), resolveVirtualModules(wxtConfig), devServerGlobals(wxtConfig, server), tsconfigPaths(wxtConfig), noopBackground(), globals(wxtConfig), defineImportMeta(), wxtPluginLoader(wxtConfig), resolveAppConfig(wxtConfig));
57
+ if (wxtConfig.analysis.enabled && !baseConfigOptions?.excludeAnalysisPlugin) config.plugins.push(bundleAnalysis(wxtConfig));
58
+ return config;
59
+ };
60
+ /**
61
+ * Return the basic config for building an entrypoint in [lib mode](https://vitejs.dev/guide/build.html#library-mode).
62
+ */
63
+ const getLibModeConfig = (entrypoint) => {
64
+ const entry = getRollupEntry(entrypoint);
65
+ const plugins = [entrypointGroupGlobals(entrypoint)];
66
+ const iifeReturnValueName = safeVarName(entrypoint.name);
67
+ if (entrypoint.type === "content-script-style" || entrypoint.type === "unlisted-style") plugins.push(cssEntrypoints(entrypoint, wxtConfig));
68
+ if (entrypoint.type === "content-script" || entrypoint.type === "unlisted-script") plugins.push(iifeFooter(iifeReturnValueName));
69
+ return {
70
+ mode: wxtConfig.mode,
71
+ plugins,
72
+ build: {
73
+ lib: {
74
+ entry,
75
+ formats: ["iife"],
76
+ name: iifeReturnValueName,
77
+ fileName: entrypoint.name
78
+ },
79
+ rollupOptions: { output: {
80
+ entryFileNames: getEntrypointBundlePath(entrypoint, wxtConfig.outDir, ".js"),
81
+ assetFileNames: ({ names }) => {
82
+ if (entrypoint.type === "content-script" && names[0].endsWith("css")) return `content-scripts/${entrypoint.name}.[ext]`;
83
+ else return `assets/${entrypoint.name}.[ext]`;
84
+ }
85
+ } }
86
+ },
87
+ define: { "process.env.NODE_ENV": JSON.stringify(wxtConfig.mode) }
88
+ };
89
+ };
90
+ /**
91
+ * Return the basic config for building multiple entrypoints in [multi-page mode](https://vitejs.dev/guide/build.html#multi-page-app).
92
+ */
93
+ const getMultiPageConfig = (entrypoints) => {
94
+ const htmlEntrypoints = new Set(entrypoints.filter(isHtmlEntrypoint).map((e) => e.name));
95
+ return {
96
+ mode: wxtConfig.mode,
97
+ plugins: [entrypointGroupGlobals(entrypoints)],
98
+ build: { rollupOptions: {
99
+ input: entrypoints.reduce((input, entry) => {
100
+ input[entry.name] = getRollupEntry(entry);
101
+ return input;
102
+ }, {}),
103
+ output: {
104
+ chunkFileNames: "chunks/[name]-[hash].js",
105
+ entryFileNames: ({ name }) => {
106
+ if (htmlEntrypoints.has(name)) return "chunks/[name]-[hash].js";
107
+ return "[name].js";
108
+ },
109
+ assetFileNames: "assets/[name]-[hash].[ext]"
110
+ }
111
+ } }
112
+ };
113
+ };
114
+ /**
115
+ * Return the basic config for building a single CSS entrypoint in [multi-page mode](https://vitejs.dev/guide/build.html#multi-page-app).
116
+ */
117
+ const getCssConfig = (entrypoint) => {
118
+ return {
119
+ mode: wxtConfig.mode,
120
+ plugins: [entrypointGroupGlobals(entrypoint)],
121
+ build: { rollupOptions: {
122
+ input: { [entrypoint.name]: entrypoint.inputPath },
123
+ output: { assetFileNames: () => {
124
+ if (entrypoint.type === "content-script-style") return `content-scripts/${entrypoint.name}.[ext]`;
125
+ else return `assets/${entrypoint.name}.[ext]`;
126
+ } }
127
+ } }
128
+ };
129
+ };
130
+ const createViteNodeImporter = async (paths) => {
131
+ const baseConfig = await getBaseConfig({ excludeAnalysisPlugin: true });
132
+ baseConfig.optimizeDeps ??= {};
133
+ baseConfig.optimizeDeps.noDiscovery = true;
134
+ baseConfig.optimizeDeps.include = [];
135
+ const envConfig = { plugins: paths.map((path) => removeEntrypointMainFunction(wxtConfig, path)) };
136
+ const config = vite.mergeConfig(baseConfig, envConfig);
137
+ const server = await vite.createServer(config);
138
+ await server.pluginContainer.buildStart({});
139
+ const node = new ViteNodeServer(server);
140
+ installSourcemapsSupport({ getSourceMap: (source) => node.getSourceMap(source) });
141
+ return {
142
+ runner: new ViteNodeRunner({
143
+ root: server.config.root,
144
+ base: server.config.base,
145
+ fetchModule(id) {
146
+ return node.fetchModule(id);
147
+ },
148
+ resolveId(id, importer) {
149
+ return node.resolveId(id, importer);
150
+ }
151
+ }),
152
+ server
153
+ };
154
+ };
155
+ const requireDefaultExport = (path, mod) => {
156
+ const relativePath = relative(wxtConfig.root, path);
157
+ if (mod?.default == null) {
158
+ const defineFn = relativePath.includes(".content") ? "defineContentScript" : relativePath.includes("background") ? "defineBackground" : "defineUnlistedScript";
159
+ throw Error(`${relativePath}: Default export not found, did you forget to call "export default ${defineFn}(...)"?`);
160
+ }
161
+ };
162
+ return {
163
+ name: "Vite",
164
+ version: vite.version,
165
+ async importEntrypoint(path) {
166
+ const env = createExtensionEnvironment();
167
+ const { runner, server } = await createViteNodeImporter([path]);
168
+ const res = await env.run(() => runner.executeFile(path));
169
+ await server.close();
170
+ requireDefaultExport(path, res);
171
+ return res.default;
172
+ },
173
+ async importEntrypoints(paths) {
174
+ const env = createExtensionEnvironment();
175
+ const { runner, server } = await createViteNodeImporter(paths);
176
+ const res = await env.run(() => Promise.all(paths.map(async (path) => {
177
+ const mod = await runner.executeFile(path);
178
+ requireDefaultExport(path, mod);
179
+ return mod.default;
180
+ })));
181
+ await server.close();
182
+ return res;
183
+ },
184
+ async build(group) {
185
+ let entryConfig;
186
+ if (Array.isArray(group)) entryConfig = getMultiPageConfig(group);
187
+ else if (group.type === "content-script-style" || group.type === "unlisted-style") entryConfig = getCssConfig(group);
188
+ else entryConfig = getLibModeConfig(group);
189
+ const buildConfig = vite.mergeConfig(await getBaseConfig(), entryConfig);
190
+ await hooks.callHook("vite:build:extendConfig", toArray(group), buildConfig);
191
+ return {
192
+ entrypoints: group,
193
+ chunks: await moveHtmlFiles(wxtConfig, group, getBuildOutputChunks(await vite.build(buildConfig)))
194
+ };
195
+ },
196
+ async createServer(info) {
197
+ const serverConfig = { server: {
198
+ host: info.host,
199
+ port: info.port,
200
+ strictPort: true,
201
+ origin: info.origin
202
+ } };
203
+ const baseConfig = await getBaseConfig();
204
+ const finalConfig = vite.mergeConfig(baseConfig, serverConfig);
205
+ await hooks.callHook("vite:devServer:extendConfig", finalConfig);
206
+ const viteServer = await vite.createServer(finalConfig);
207
+ return {
208
+ async listen() {
209
+ await viteServer.listen(info.port);
210
+ },
211
+ async close() {
212
+ await viteServer.close();
213
+ },
214
+ transformHtml(...args) {
215
+ return viteServer.transformIndexHtml(...args);
216
+ },
217
+ ws: {
218
+ send(message, payload) {
219
+ return viteServer.ws.send(message, payload);
220
+ },
221
+ on(message, cb) {
222
+ viteServer.ws.on(message, cb);
223
+ }
224
+ },
225
+ watcher: viteServer.watcher,
226
+ on(event, cb) {
227
+ viteServer.httpServer?.on(event, cb);
228
+ }
229
+ };
230
+ }
231
+ };
290
232
  }
291
233
  function getBuildOutputChunks(result) {
292
- if ("on" in result) throw Error("wxt does not support vite watch mode.");
293
- if (Array.isArray(result)) return result.flatMap(({ output }) => output);
294
- return result.output;
234
+ if ("on" in result) throw Error("wxt does not support vite watch mode.");
235
+ if (Array.isArray(result)) return result.flatMap(({ output }) => output);
236
+ return result.output;
295
237
  }
238
+ /**
239
+ * Returns the input module ID (virtual or real file) for an entrypoint. The returned string should
240
+ * be passed as an input to rollup.
241
+ */
296
242
  function getRollupEntry(entrypoint) {
297
- let virtualEntrypointType;
298
- switch (entrypoint.type) {
299
- case "background":
300
- case "unlisted-script":
301
- virtualEntrypointType = entrypoint.type;
302
- break;
303
- case "content-script":
304
- virtualEntrypointType = entrypoint.options.world === "MAIN" ? "content-script-main-world" : "content-script-isolated-world";
305
- break;
306
- }
307
- if (virtualEntrypointType) {
308
- const moduleId = `virtual:wxt-${virtualEntrypointType}-entrypoint`;
309
- return `${moduleId}?${entrypoint.inputPath}`;
310
- }
311
- return entrypoint.inputPath;
243
+ let virtualEntrypointType;
244
+ switch (entrypoint.type) {
245
+ case "background":
246
+ case "unlisted-script":
247
+ virtualEntrypointType = entrypoint.type;
248
+ break;
249
+ case "content-script":
250
+ virtualEntrypointType = entrypoint.options.world === "MAIN" ? "content-script-main-world" : "content-script-isolated-world";
251
+ break;
252
+ }
253
+ if (virtualEntrypointType) return `${`virtual:wxt-${virtualEntrypointType}-entrypoint`}?${entrypoint.inputPath}`;
254
+ return entrypoint.inputPath;
312
255
  }
256
+ /**
257
+ * Ensures the HTML files output by a multipage build are in the correct location. This does two
258
+ * things:
259
+ *
260
+ * 1. Moves the HTML files to their final location at `<outDir>/<entrypoint.name>.html`.
261
+ * 2. Updates the bundle so it summarizes the files correctly in the returned build output.
262
+ *
263
+ * Assets (JS and CSS) are output to the `<outDir>/assets` directory, and don't need to be modified.
264
+ * HTML files access them via absolute URLs, so we don't need to update any import paths in the HTML
265
+ * files either.
266
+ */
313
267
  async function moveHtmlFiles(config, group, chunks) {
314
- if (!Array.isArray(group)) return chunks;
315
- const entryMap = group.reduce((map, entry) => {
316
- const a = normalizePath(relative(config.root, entry.inputPath));
317
- map[a] = entry;
318
- return map;
319
- }, {});
320
- const movedChunks = await Promise.all(
321
- chunks.map(async (chunk) => {
322
- if (!chunk.fileName.endsWith(".html")) return chunk;
323
- const entry = entryMap[chunk.fileName];
324
- const oldBundlePath = chunk.fileName;
325
- const newBundlePath = getEntrypointBundlePath(
326
- entry,
327
- config.outDir,
328
- extname(chunk.fileName)
329
- );
330
- const oldAbsPath = join(config.outDir, oldBundlePath);
331
- const newAbsPath = join(config.outDir, newBundlePath);
332
- await fs.ensureDir(dirname(newAbsPath));
333
- await fs.move(oldAbsPath, newAbsPath, { overwrite: true });
334
- return {
335
- ...chunk,
336
- fileName: newBundlePath
337
- };
338
- })
339
- );
340
- removeEmptyDirs(config.outDir);
341
- return movedChunks;
268
+ if (!Array.isArray(group)) return chunks;
269
+ const entryMap = group.reduce((map, entry) => {
270
+ const a = normalizePath(relative(config.root, entry.inputPath));
271
+ map[a] = entry;
272
+ return map;
273
+ }, {});
274
+ const movedChunks = await Promise.all(chunks.map(async (chunk) => {
275
+ if (!chunk.fileName.endsWith(".html")) return chunk;
276
+ const entry = entryMap[chunk.fileName];
277
+ const oldBundlePath = chunk.fileName;
278
+ const newBundlePath = getEntrypointBundlePath(entry, config.outDir, extname(chunk.fileName));
279
+ const oldAbsPath = join(config.outDir, oldBundlePath);
280
+ const newAbsPath = join(config.outDir, newBundlePath);
281
+ await fs.ensureDir(dirname(newAbsPath));
282
+ await fs.move(oldAbsPath, newAbsPath, { overwrite: true });
283
+ return {
284
+ ...chunk,
285
+ fileName: newBundlePath
286
+ };
287
+ }));
288
+ await removeEmptyDirs(config.outDir);
289
+ return movedChunks;
342
290
  }
343
- export async function removeEmptyDirs(dir) {
344
- const files = await fs.readdir(dir);
345
- for (const file of files) {
346
- const filePath = join(dir, file);
347
- const stats = await fs.stat(filePath);
348
- if (stats.isDirectory()) {
349
- await removeEmptyDirs(filePath);
350
- }
351
- }
352
- try {
353
- await fs.rmdir(dir);
354
- } catch {
355
- }
291
+ /**
292
+ * Recursively remove all directories that are empty/
293
+ */
294
+ async function removeEmptyDirs(dir) {
295
+ const files = await fs.readdir(dir);
296
+ for (const file of files) {
297
+ const filePath = join(dir, file);
298
+ if ((await fs.stat(filePath)).isDirectory()) await removeEmptyDirs(filePath);
299
+ }
300
+ try {
301
+ await fs.rmdir(dir);
302
+ } catch {}
356
303
  }
304
+
305
+ //#endregion
306
+ export { createViteBuilder };