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,57 @@
1
+ import { BuildOutput, BuildStepOutput, EntrypointGroup } from '../../../types';
2
+ /**
3
+ * Compare the changed files vs the build output and determine what kind of reload needs to happen:
4
+ *
5
+ * - Do nothing
6
+ * - CSS or JS file associated with an HTML page is changed - this is handled automatically by the
7
+ * dev server
8
+ * - Change isn't used by any of the entrypoints
9
+ * - Reload Content script
10
+ * - CSS or JS file associated with a content script
11
+ * - Background script will be told to reload the content script
12
+ * - Reload HTML file
13
+ * - HTML file itself is saved - HMR doesn't handle this because the HTML pages are pre-rendered
14
+ * - Chrome is OK reloading the page when the HTML file is changed without reloading the whole
15
+ * extension. Not sure about firefox, this might need to change to an extension reload
16
+ * - Reload extension
17
+ * - Background script is changed
18
+ * - Manifest is different
19
+ * - Restart browser
20
+ * - Config file changed (wxt.config.ts, .env, web-ext.config.ts, etc)
21
+ */
22
+ export declare function detectDevChanges(changedFiles: string[], currentOutput: BuildOutput): DevModeChange;
23
+ /**
24
+ * Contains information about what files changed, what needs rebuilt, and the type of reload that is
25
+ * required.
26
+ */
27
+ export type DevModeChange = NoChange | HtmlReload | ExtensionReload | ContentScriptReload | FullRestart | BrowserRestart;
28
+ interface NoChange {
29
+ type: 'no-change';
30
+ }
31
+ interface RebuildChange {
32
+ /**
33
+ * The list of entrypoints that need rebuilt.
34
+ */
35
+ rebuildGroups: EntrypointGroup[];
36
+ /**
37
+ * The previous output stripped of any files are going to change.
38
+ */
39
+ cachedOutput: BuildOutput;
40
+ }
41
+ interface FullRestart {
42
+ type: 'full-restart';
43
+ }
44
+ interface BrowserRestart {
45
+ type: 'browser-restart';
46
+ }
47
+ interface HtmlReload extends RebuildChange {
48
+ type: 'html-reload';
49
+ }
50
+ interface ExtensionReload extends RebuildChange {
51
+ type: 'extension-reload';
52
+ }
53
+ interface ContentScriptReload extends RebuildChange {
54
+ type: 'content-script-reload';
55
+ changedSteps: BuildStepOutput[];
56
+ }
57
+ export {};
@@ -0,0 +1,93 @@
1
+ import { every, some } from "../../utils/arrays.mjs";
2
+ import { normalizePath } from "../../utils/paths.mjs";
3
+ import { wxt } from "../../wxt.mjs";
4
+ export function detectDevChanges(changedFiles, currentOutput) {
5
+ const isConfigChange = some(
6
+ changedFiles,
7
+ (file) => file === wxt.config.userConfigMetadata.configFile
8
+ );
9
+ if (isConfigChange) return { type: "full-restart" };
10
+ const isRunnerChange = some(
11
+ changedFiles,
12
+ (file) => file === wxt.config.runnerConfig.configFile
13
+ );
14
+ if (isRunnerChange) return { type: "browser-restart" };
15
+ const changedSteps = new Set(
16
+ changedFiles.flatMap(
17
+ (changedFile) => findEffectedSteps(changedFile, currentOutput)
18
+ )
19
+ );
20
+ if (changedSteps.size === 0) {
21
+ const hasPublicChange = some(
22
+ changedFiles,
23
+ (file) => file.startsWith(wxt.config.publicDir)
24
+ );
25
+ if (hasPublicChange) {
26
+ return {
27
+ type: "extension-reload",
28
+ rebuildGroups: [],
29
+ cachedOutput: currentOutput
30
+ };
31
+ } else {
32
+ return { type: "no-change" };
33
+ }
34
+ }
35
+ const unchangedOutput = {
36
+ manifest: currentOutput.manifest,
37
+ steps: [],
38
+ publicAssets: [...currentOutput.publicAssets]
39
+ };
40
+ const changedOutput = {
41
+ manifest: currentOutput.manifest,
42
+ steps: [],
43
+ publicAssets: []
44
+ };
45
+ for (const step of currentOutput.steps) {
46
+ if (changedSteps.has(step)) {
47
+ changedOutput.steps.push(step);
48
+ } else {
49
+ unchangedOutput.steps.push(step);
50
+ }
51
+ }
52
+ const isOnlyHtmlChanges = changedFiles.length > 0 && every(changedFiles, (file) => file.endsWith(".html"));
53
+ if (isOnlyHtmlChanges) {
54
+ return {
55
+ type: "html-reload",
56
+ cachedOutput: unchangedOutput,
57
+ rebuildGroups: changedOutput.steps.map((step) => step.entrypoints)
58
+ };
59
+ }
60
+ const isOnlyContentScripts = changedOutput.steps.length > 0 && every(
61
+ changedOutput.steps.flatMap((step) => step.entrypoints),
62
+ (entry) => entry.type === "content-script"
63
+ );
64
+ if (isOnlyContentScripts) {
65
+ return {
66
+ type: "content-script-reload",
67
+ cachedOutput: unchangedOutput,
68
+ changedSteps: changedOutput.steps,
69
+ rebuildGroups: changedOutput.steps.map((step) => step.entrypoints)
70
+ };
71
+ }
72
+ return {
73
+ type: "extension-reload",
74
+ cachedOutput: unchangedOutput,
75
+ rebuildGroups: changedOutput.steps.map((step) => step.entrypoints)
76
+ };
77
+ }
78
+ function findEffectedSteps(changedFile, currentOutput) {
79
+ const changes = [];
80
+ const changedPath = normalizePath(changedFile);
81
+ const isChunkEffected = (chunk) => (
82
+ // If it's an HTML file with the same path, is is effected because HTML files need to be re-rendered
83
+ // - fileName is normalized, relative bundle path, "<entrypoint-name>.html"
84
+ chunk.type === "asset" && changedPath.replace("/index.html", ".html").endsWith(chunk.fileName) || // If it's a chunk that depends on the changed file, it is effected
85
+ // - moduleIds are absolute, normalized paths
86
+ chunk.type === "chunk" && chunk.moduleIds.includes(changedPath)
87
+ );
88
+ for (const step of currentOutput.steps) {
89
+ const effectedChunk = step.chunks.find((chunk) => isChunkEffected(chunk));
90
+ if (effectedChunk) changes.push(step);
91
+ }
92
+ return changes;
93
+ }
@@ -0,0 +1,5 @@
1
+ import { Entrypoint } from '../../../types';
2
+ /**
3
+ * Return entrypoints and their configuration by looking through the project's files.
4
+ */
5
+ export declare function findEntrypoints(): Promise<Entrypoint[]>;
@@ -0,0 +1,385 @@
1
+ import { relative, resolve } from "path";
2
+ import fs from "fs-extra";
3
+ import { minimatch } from "minimatch";
4
+ import { parseHTML } from "linkedom";
5
+ import JSON5 from "json5";
6
+ import glob from "fast-glob";
7
+ import {
8
+ getEntrypointName,
9
+ resolvePerBrowserOptions
10
+ } from "../../utils/entrypoints.mjs";
11
+ import { VIRTUAL_NOOP_BACKGROUND_MODULE_ID } from "../../utils/constants.mjs";
12
+ import { CSS_EXTENSIONS_PATTERN } from "../../utils/paths.mjs";
13
+ import pc from "picocolors";
14
+ import { wxt } from "../../wxt.mjs";
15
+ export async function findEntrypoints() {
16
+ await fs.mkdir(wxt.config.wxtDir, { recursive: true });
17
+ await fs.writeJson(resolve(wxt.config.wxtDir, "tsconfig.json"), {});
18
+ const relativePaths = await glob(Object.keys(PATH_GLOB_TO_TYPE_MAP), {
19
+ cwd: wxt.config.entrypointsDir
20
+ });
21
+ relativePaths.sort();
22
+ const pathGlobs = Object.keys(PATH_GLOB_TO_TYPE_MAP);
23
+ const entrypointInfos = relativePaths.reduce((results, relativePath) => {
24
+ const inputPath = resolve(wxt.config.entrypointsDir, relativePath);
25
+ const name = getEntrypointName(wxt.config.entrypointsDir, inputPath);
26
+ const matchingGlob = pathGlobs.find(
27
+ (glob2) => minimatch(relativePath, glob2)
28
+ );
29
+ if (matchingGlob) {
30
+ const type = PATH_GLOB_TO_TYPE_MAP[matchingGlob];
31
+ results.push({
32
+ name,
33
+ inputPath,
34
+ type,
35
+ skipped: wxt.config.filterEntrypoints != null && !wxt.config.filterEntrypoints.has(name)
36
+ });
37
+ }
38
+ return results;
39
+ }, []);
40
+ preventNoEntrypoints(entrypointInfos);
41
+ preventDuplicateEntrypointNames(entrypointInfos);
42
+ let hasBackground = false;
43
+ const entrypoints = await Promise.all(
44
+ entrypointInfos.map(async (info) => {
45
+ const { type } = info;
46
+ switch (type) {
47
+ case "popup":
48
+ return await getPopupEntrypoint(info);
49
+ case "sidepanel":
50
+ return await getSidepanelEntrypoint(info);
51
+ case "options":
52
+ return await getOptionsEntrypoint(info);
53
+ case "background":
54
+ hasBackground = true;
55
+ return await getBackgroundEntrypoint(info);
56
+ case "content-script":
57
+ return await getContentScriptEntrypoint(info);
58
+ case "unlisted-page":
59
+ return await getUnlistedPageEntrypoint(info);
60
+ case "unlisted-script":
61
+ return await getUnlistedScriptEntrypoint(info);
62
+ case "content-script-style":
63
+ return {
64
+ ...info,
65
+ type,
66
+ outputDir: resolve(wxt.config.outDir, CONTENT_SCRIPT_OUT_DIR),
67
+ options: {
68
+ include: void 0,
69
+ exclude: void 0
70
+ }
71
+ };
72
+ default:
73
+ return {
74
+ ...info,
75
+ type,
76
+ outputDir: wxt.config.outDir,
77
+ options: {
78
+ include: void 0,
79
+ exclude: void 0
80
+ }
81
+ };
82
+ }
83
+ })
84
+ );
85
+ if (wxt.config.command === "serve" && !hasBackground) {
86
+ entrypoints.push(
87
+ await getBackgroundEntrypoint({
88
+ inputPath: VIRTUAL_NOOP_BACKGROUND_MODULE_ID,
89
+ name: "background",
90
+ type: "background",
91
+ skipped: false
92
+ })
93
+ );
94
+ }
95
+ wxt.logger.debug("All entrypoints:", entrypoints);
96
+ const skippedEntrypointNames = entrypointInfos.filter((item) => item.skipped).map((item) => item.name);
97
+ if (skippedEntrypointNames.length) {
98
+ wxt.logger.warn(
99
+ `Filter excluded the following entrypoints:
100
+ ${skippedEntrypointNames.map((item) => `${pc.dim("-")} ${pc.cyan(item)}`).join("\n")}`
101
+ );
102
+ }
103
+ const targetEntrypoints = entrypoints.filter((entry) => {
104
+ const { include, exclude } = entry.options;
105
+ if (include?.length && exclude?.length) {
106
+ wxt.logger.warn(
107
+ `The ${entry.name} entrypoint lists both include and exclude, but only one can be used per entrypoint. Entrypoint ignored.`
108
+ );
109
+ return false;
110
+ }
111
+ if (exclude?.length && !include?.length) {
112
+ return !exclude.includes(wxt.config.browser);
113
+ }
114
+ if (include?.length && !exclude?.length) {
115
+ return include.includes(wxt.config.browser);
116
+ }
117
+ if (skippedEntrypointNames.includes(entry.name)) {
118
+ return false;
119
+ }
120
+ return true;
121
+ });
122
+ wxt.logger.debug(`${wxt.config.browser} entrypoints:`, targetEntrypoints);
123
+ await wxt.hooks.callHook("entrypoints:resolved", wxt, targetEntrypoints);
124
+ return targetEntrypoints;
125
+ }
126
+ function preventDuplicateEntrypointNames(files) {
127
+ const namesToPaths = files.reduce(
128
+ (map, { name, inputPath }) => {
129
+ map[name] ??= [];
130
+ map[name].push(inputPath);
131
+ return map;
132
+ },
133
+ {}
134
+ );
135
+ const errorLines = Object.entries(namesToPaths).reduce(
136
+ (lines, [name, absolutePaths]) => {
137
+ if (absolutePaths.length > 1) {
138
+ lines.push(`- ${name}`);
139
+ absolutePaths.forEach((absolutePath) => {
140
+ lines.push(` - ${relative(wxt.config.root, absolutePath)}`);
141
+ });
142
+ }
143
+ return lines;
144
+ },
145
+ []
146
+ );
147
+ if (errorLines.length > 0) {
148
+ const errorContent = errorLines.join("\n");
149
+ throw Error(
150
+ `Multiple entrypoints with the same name detected, only one entrypoint for each name is allowed.
151
+
152
+ ${errorContent}`
153
+ );
154
+ }
155
+ }
156
+ function preventNoEntrypoints(files) {
157
+ if (files.length === 0) {
158
+ throw Error(`No entrypoints found in ${wxt.config.entrypointsDir}`);
159
+ }
160
+ }
161
+ async function getPopupEntrypoint(info) {
162
+ const options = await getHtmlEntrypointOptions(
163
+ info,
164
+ {
165
+ browserStyle: "browse_style",
166
+ exclude: "exclude",
167
+ include: "include",
168
+ defaultIcon: "default_icon",
169
+ defaultTitle: "default_title",
170
+ mv2Key: "type"
171
+ },
172
+ {
173
+ defaultTitle: (document) => document.querySelector("title")?.textContent || void 0
174
+ },
175
+ {
176
+ defaultTitle: (content) => content,
177
+ mv2Key: (content) => content === "page_action" ? "page_action" : "browser_action"
178
+ }
179
+ );
180
+ return {
181
+ type: "popup",
182
+ name: "popup",
183
+ options: resolvePerBrowserOptions(options, wxt.config.browser),
184
+ inputPath: info.inputPath,
185
+ outputDir: wxt.config.outDir,
186
+ skipped: info.skipped
187
+ };
188
+ }
189
+ async function getOptionsEntrypoint(info) {
190
+ const options = await getHtmlEntrypointOptions(
191
+ info,
192
+ {
193
+ browserStyle: "browse_style",
194
+ chromeStyle: "chrome_style",
195
+ exclude: "exclude",
196
+ include: "include",
197
+ openInTab: "open_in_tab"
198
+ }
199
+ );
200
+ return {
201
+ type: "options",
202
+ name: "options",
203
+ options: resolvePerBrowserOptions(options, wxt.config.browser),
204
+ inputPath: info.inputPath,
205
+ outputDir: wxt.config.outDir,
206
+ skipped: info.skipped
207
+ };
208
+ }
209
+ async function getUnlistedPageEntrypoint(info) {
210
+ const options = await getHtmlEntrypointOptions(info, {
211
+ exclude: "exclude",
212
+ include: "include"
213
+ });
214
+ return {
215
+ type: "unlisted-page",
216
+ name: info.name,
217
+ inputPath: info.inputPath,
218
+ outputDir: wxt.config.outDir,
219
+ options,
220
+ skipped: info.skipped
221
+ };
222
+ }
223
+ async function getUnlistedScriptEntrypoint({
224
+ inputPath,
225
+ name,
226
+ skipped
227
+ }) {
228
+ const defaultExport = await wxt.builder.importEntrypoint(inputPath);
229
+ if (defaultExport == null) {
230
+ throw Error(
231
+ `${name}: Default export not found, did you forget to call "export default defineUnlistedScript(...)"?`
232
+ );
233
+ }
234
+ const { main: _, ...options } = defaultExport;
235
+ return {
236
+ type: "unlisted-script",
237
+ name,
238
+ inputPath,
239
+ outputDir: wxt.config.outDir,
240
+ options: resolvePerBrowserOptions(options, wxt.config.browser),
241
+ skipped
242
+ };
243
+ }
244
+ async function getBackgroundEntrypoint({
245
+ inputPath,
246
+ name,
247
+ skipped
248
+ }) {
249
+ let options = {};
250
+ if (inputPath !== VIRTUAL_NOOP_BACKGROUND_MODULE_ID) {
251
+ const defaultExport = await wxt.builder.importEntrypoint(inputPath);
252
+ if (defaultExport == null) {
253
+ throw Error(
254
+ `${name}: Default export not found, did you forget to call "export default defineBackground(...)"?`
255
+ );
256
+ }
257
+ const { main: _, ...moduleOptions } = defaultExport;
258
+ options = moduleOptions;
259
+ }
260
+ if (wxt.config.manifestVersion !== 3) {
261
+ delete options.type;
262
+ }
263
+ return {
264
+ type: "background",
265
+ name,
266
+ inputPath,
267
+ outputDir: wxt.config.outDir,
268
+ options: resolvePerBrowserOptions(options, wxt.config.browser),
269
+ skipped
270
+ };
271
+ }
272
+ async function getContentScriptEntrypoint({
273
+ inputPath,
274
+ name,
275
+ skipped
276
+ }) {
277
+ const defaultExport = await wxt.builder.importEntrypoint(inputPath);
278
+ if (defaultExport == null) {
279
+ throw Error(
280
+ `${name}: Default export not found, did you forget to call "export default defineContentScript(...)"?`
281
+ );
282
+ }
283
+ const { main: _, ...options } = defaultExport;
284
+ if (options == null) {
285
+ throw Error(
286
+ `${name}: Default export not found, did you forget to call "export default defineContentScript(...)"?`
287
+ );
288
+ }
289
+ return {
290
+ type: "content-script",
291
+ name,
292
+ inputPath,
293
+ outputDir: resolve(wxt.config.outDir, CONTENT_SCRIPT_OUT_DIR),
294
+ options: resolvePerBrowserOptions(options, wxt.config.browser),
295
+ skipped
296
+ };
297
+ }
298
+ async function getSidepanelEntrypoint(info) {
299
+ const options = await getHtmlEntrypointOptions(
300
+ info,
301
+ {
302
+ browserStyle: "browse_style",
303
+ exclude: "exclude",
304
+ include: "include",
305
+ defaultIcon: "default_icon",
306
+ defaultTitle: "default_title",
307
+ openAtInstall: "open_at_install"
308
+ },
309
+ {
310
+ defaultTitle: (document) => document.querySelector("title")?.textContent || void 0
311
+ },
312
+ {
313
+ defaultTitle: (content) => content
314
+ }
315
+ );
316
+ return {
317
+ type: "sidepanel",
318
+ name: info.name,
319
+ options: resolvePerBrowserOptions(options, wxt.config.browser),
320
+ inputPath: info.inputPath,
321
+ outputDir: wxt.config.outDir,
322
+ skipped: info.skipped
323
+ };
324
+ }
325
+ async function getHtmlEntrypointOptions(info, keyMap, queries, parsers) {
326
+ const content = await fs.readFile(info.inputPath, "utf-8");
327
+ const { document } = parseHTML(content);
328
+ const options = {};
329
+ const defaultQuery = (manifestKey) => document.querySelector(`meta[name='manifest.${manifestKey}']`)?.getAttribute("content");
330
+ Object.entries(keyMap).forEach(([_key, manifestKey]) => {
331
+ const key = _key;
332
+ const content2 = queries?.[key] ? queries[key](document, manifestKey) : defaultQuery(manifestKey);
333
+ if (content2) {
334
+ try {
335
+ options[key] = (parsers?.[key] ?? JSON5.parse)(content2);
336
+ } catch (err) {
337
+ wxt.logger.fatal(
338
+ `Failed to parse meta tag content. Usually this means you have invalid JSON5 content (content=${content2})`,
339
+ err
340
+ );
341
+ }
342
+ }
343
+ });
344
+ return options;
345
+ }
346
+ const PATH_GLOB_TO_TYPE_MAP = {
347
+ "sandbox.html": "sandbox",
348
+ "sandbox/index.html": "sandbox",
349
+ "*.sandbox.html": "sandbox",
350
+ "*.sandbox/index.html": "sandbox",
351
+ "bookmarks.html": "bookmarks",
352
+ "bookmarks/index.html": "bookmarks",
353
+ "history.html": "history",
354
+ "history/index.html": "history",
355
+ "newtab.html": "newtab",
356
+ "newtab/index.html": "newtab",
357
+ "sidepanel.html": "sidepanel",
358
+ "sidepanel/index.html": "sidepanel",
359
+ "*.sidepanel.html": "sidepanel",
360
+ "*.sidepanel/index.html": "sidepanel",
361
+ "devtools.html": "devtools",
362
+ "devtools/index.html": "devtools",
363
+ "background.[jt]s": "background",
364
+ "background/index.[jt]s": "background",
365
+ [VIRTUAL_NOOP_BACKGROUND_MODULE_ID]: "background",
366
+ "content.[jt]s?(x)": "content-script",
367
+ "content/index.[jt]s?(x)": "content-script",
368
+ "*.content.[jt]s?(x)": "content-script",
369
+ "*.content/index.[jt]s?(x)": "content-script",
370
+ [`content.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
371
+ [`*.content.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
372
+ [`content/index.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
373
+ [`*.content/index.${CSS_EXTENSIONS_PATTERN}`]: "content-script-style",
374
+ "popup.html": "popup",
375
+ "popup/index.html": "popup",
376
+ "options.html": "options",
377
+ "options/index.html": "options",
378
+ "*.html": "unlisted-page",
379
+ "*/index.html": "unlisted-page",
380
+ "*.[jt]s?(x)": "unlisted-script",
381
+ "*/index.[jt]s?(x)": "unlisted-script",
382
+ [`*.${CSS_EXTENSIONS_PATTERN}`]: "unlisted-style",
383
+ [`*/index.${CSS_EXTENSIONS_PATTERN}`]: "unlisted-style"
384
+ };
385
+ const CONTENT_SCRIPT_OUT_DIR = "content-scripts";
@@ -0,0 +1,5 @@
1
+ import { Entrypoint } from '../../../types';
2
+ /**
3
+ * Generate and write all the files inside the `InternalConfig.typesDir` directory.
4
+ */
5
+ export declare function generateTypesDir(entrypoints: Entrypoint[]): Promise<void>;