wxt 0.19.6 → 0.19.8

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.
@@ -1,4 +1,3 @@
1
- import type { WxtRuntime, WxtI18n } from './index';
2
1
  /**
3
2
  * EXPERIMENTAL
4
3
  *
@@ -6,9 +5,9 @@ import type { WxtRuntime, WxtI18n } from './index';
6
5
  *
7
6
  * @module wxt/browser/chrome
8
7
  */
9
- export type Chrome = typeof chrome;
10
- export type WxtBrowser = Omit<Chrome, 'runtime' | 'i18n'> & {
11
- runtime: WxtRuntime & Omit<Chrome['runtime'], 'getURL'>;
12
- i18n: WxtI18n & Omit<Chrome['i18n'], 'getMessage'>;
8
+ import type { WxtRuntime, WxtI18n } from './index';
9
+ export type WxtBrowser = Omit<typeof chrome, 'runtime' | 'i18n'> & {
10
+ runtime: WxtRuntime & Omit<(typeof chrome)['runtime'], 'getURL'>;
11
+ i18n: WxtI18n & Omit<(typeof chrome)['i18n'], 'getMessage'>;
13
12
  };
14
13
  export declare const browser: WxtBrowser;
@@ -28,6 +28,10 @@ export async function createViteBuilder(wxtConfig, hooks, server) {
28
28
  if (config.build.sourcemap == null && wxtConfig.command === "serve") {
29
29
  config.build.sourcemap = "inline";
30
30
  }
31
+ config.server ??= {};
32
+ config.server.watch = {
33
+ ignored: [`${wxtConfig.outBaseDir}/**`, `${wxtConfig.wxtDir}/**`]
34
+ };
31
35
  config.plugins ??= [];
32
36
  config.plugins.push(
33
37
  wxtPlugins.download(wxtConfig),
@@ -227,10 +231,7 @@ export async function createViteBuilder(wxtConfig, hooks, server) {
227
231
  port: info.port,
228
232
  strictPort: true,
229
233
  host: info.hostname,
230
- origin: info.origin,
231
- watch: {
232
- ignored: [`${wxtConfig.outBaseDir}/**`, `${wxtConfig.wxtDir}/**`]
233
- }
234
+ origin: info.origin
234
235
  }
235
236
  };
236
237
  const baseConfig = await getBaseConfig();
@@ -1,5 +1,7 @@
1
1
  import path from "node:path";
2
2
  export function extensionApiMock(config) {
3
+ const virtualSetupModule = "virtual:wxt-setup";
4
+ const resolvedVirtualSetupModule = "\0" + virtualSetupModule;
3
5
  return {
4
6
  name: "wxt:extension-api-mock",
5
7
  config() {
@@ -8,6 +10,9 @@ export function extensionApiMock(config) {
8
10
  "dist/virtual/mock-browser"
9
11
  );
10
12
  return {
13
+ test: {
14
+ setupFiles: [virtualSetupModule]
15
+ },
11
16
  resolve: {
12
17
  alias: [
13
18
  { find: "webextension-polyfill", replacement },
@@ -21,6 +26,19 @@ export function extensionApiMock(config) {
21
26
  noExternal: ["wxt"]
22
27
  }
23
28
  };
29
+ },
30
+ resolveId(id) {
31
+ if (id.endsWith(virtualSetupModule)) return resolvedVirtualSetupModule;
32
+ },
33
+ load(id) {
34
+ if (id === resolvedVirtualSetupModule) return setupTemplate;
24
35
  }
25
36
  };
26
37
  }
38
+ const setupTemplate = `
39
+ import { vi } from 'vitest';
40
+ import { fakeBrowser } from 'wxt/testing';
41
+
42
+ vi.stubGlobal("chrome", fakeBrowser);
43
+ vi.stubGlobal("browser", fakeBrowser);
44
+ `;
@@ -73,32 +73,46 @@ export async function initialize(options) {
73
73
  console.log();
74
74
  }
75
75
  async function listTemplates() {
76
- try {
77
- const res = await fetch("https://ungh.cc/repos/wxt-dev/wxt/files/main");
78
- if (res.status >= 300)
79
- throw Error(`Request failed with status ${res.status} ${res.statusText}`);
80
- const data = await res.json();
81
- return data.files.map((item) => item.path.match(/templates\/(.+)\/package\.json/)?.[1]).filter((name) => name != null).map((name) => ({ name, path: `templates/${name}` })).sort((l, r) => {
82
- const lWeight = TEMPLATE_SORT_WEIGHT[l.name] ?? Number.MAX_SAFE_INTEGER;
83
- const rWeight = TEMPLATE_SORT_WEIGHT[r.name] ?? Number.MAX_SAFE_INTEGER;
84
- const diff = lWeight - rWeight;
85
- if (diff !== 0) return diff;
86
- return l.name.localeCompare(r.name);
87
- });
88
- } catch (err) {
89
- consola.error(err);
90
- throw Error(`Failed to load templates`);
91
- }
76
+ const templates = await listTemplatesUngh().catch((err) => {
77
+ consola.debug("Failed to load templates via ungh:", err);
78
+ return listTemplatesGithub();
79
+ });
80
+ return templates.sort((l, r) => {
81
+ const lWeight = TEMPLATE_SORT_WEIGHT[l.name] ?? Number.MAX_SAFE_INTEGER;
82
+ const rWeight = TEMPLATE_SORT_WEIGHT[r.name] ?? Number.MAX_SAFE_INTEGER;
83
+ const diff = lWeight - rWeight;
84
+ if (diff !== 0) return diff;
85
+ return l.name.localeCompare(r.name);
86
+ });
87
+ }
88
+ async function listTemplatesUngh() {
89
+ const res = await fetch("https://ungh.cc/repos/wxt-dev/wxt/files/main");
90
+ if (res.status !== 200)
91
+ throw Error(
92
+ `Request failed with status ${res.status} ${res.statusText}: ${await res.text()}`
93
+ );
94
+ const data = await res.json();
95
+ return data.files.map((item) => item.path.match(/templates\/(.+)\/package\.json/)?.[1]).filter((name) => name != null).map((name) => ({ name, path: `templates/${name}` }));
96
+ }
97
+ async function listTemplatesGithub() {
98
+ const res = await fetch(
99
+ `https://api.github.com/repos/${REPO}/contents/templates`,
100
+ { headers: { Accept: "application/vnd.github+json" } }
101
+ );
102
+ if (res.status !== 200)
103
+ throw Error(
104
+ `Request failed with status ${res.status} ${res.statusText}: ${await res.text()}`
105
+ );
106
+ return await res.json();
92
107
  }
93
108
  async function cloneProject({
94
109
  directory,
95
- template,
96
- packageManager
110
+ template
97
111
  }) {
98
112
  const { default: ora } = await import("ora");
99
113
  const spinner = ora("Downloading template").start();
100
114
  try {
101
- await downloadTemplate(`gh:wxt-dev/wxt/${template.path}`, {
115
+ await downloadTemplate(`gh:${REPO}/${template.path}`, {
102
116
  dir: directory,
103
117
  force: true
104
118
  });
@@ -126,3 +140,4 @@ const TEMPLATE_SORT_WEIGHT = {
126
140
  vue: 1,
127
141
  react: 2
128
142
  };
143
+ const REPO = "wxt-dev/wxt";
@@ -176,7 +176,11 @@ function getMainDeclarationEntry(references) {
176
176
  }
177
177
  async function getTsConfigEntry() {
178
178
  const dir = wxt.config.wxtDir;
179
- const getTsconfigPath = (path2) => normalizePath(relative(dir, path2));
179
+ const getTsconfigPath = (path2) => {
180
+ const res = normalizePath(relative(dir, path2));
181
+ if (res.startsWith(".") || res.startsWith("/")) return res;
182
+ return "./" + res;
183
+ };
180
184
  const paths = Object.entries(wxt.config.alias).flatMap(([alias, absolutePath]) => {
181
185
  const aliasPath = getTsconfigPath(absolutePath);
182
186
  return [
@@ -48,9 +48,6 @@ export async function resolveConfig(inlineConfig, command) {
48
48
  }
49
49
  const filterEntrypoints = !!mergedConfig.filterEntrypoints?.length ? new Set(mergedConfig.filterEntrypoints) : void 0;
50
50
  const publicDir = path.resolve(srcDir, mergedConfig.publicDir ?? "public");
51
- if (await isDirMissing(publicDir)) {
52
- logMissingDir(logger, "Public", publicDir);
53
- }
54
51
  const typesDir = path.resolve(wxtDir, "types");
55
52
  const outBaseDir = path.resolve(root, mergedConfig.outDir ?? ".output");
56
53
  const outDir = path.resolve(outBaseDir, `${browser}-mv${manifestVersion}`);
package/dist/modules.d.ts CHANGED
@@ -107,3 +107,31 @@ export declare function addWxtPlugin(wxt: Wxt, plugin: string): void;
107
107
  * });
108
108
  */
109
109
  export declare function addImportPreset(wxt: Wxt, preset: UnimportOptions['presets'][0]): void;
110
+ /**
111
+ * Adds an import alias to the project's TSConfig paths and bundler. Path can
112
+ * be absolute or relative to the project's root directory.
113
+ *
114
+ * Usually, this is used to provide access to some code generated by your
115
+ * module. In the example below, a `i18n` plugin generates a variable that it
116
+ * wants to provide access to, so it creates the file and adds an import alias
117
+ * to it.
118
+ *
119
+ * @example
120
+ * import path from 'node:path';
121
+ *
122
+ * export default defineWxtModule((wxt) => {
123
+ * const i18nPath = path.resolve(wxt.config.wxtDir, "i18n.ts");
124
+ *
125
+ * // Generate the file
126
+ * wxt.hooks.hook("prepare:types", (_, entries) => {
127
+ * entries.push({
128
+ * path: i18nPath,
129
+ * text: `export const i18n = ...`,
130
+ * });
131
+ * });
132
+ *
133
+ * // Add alias
134
+ * addAlias(wxt, "#i18n", i18nPath);
135
+ * });
136
+ */
137
+ export declare function addAlias(wxt: Wxt, alias: string, path: string): void;
package/dist/modules.mjs CHANGED
@@ -46,3 +46,14 @@ export function addImportPreset(wxt, preset) {
46
46
  wxt2.config.imports.presets.push(preset);
47
47
  });
48
48
  }
49
+ export function addAlias(wxt, alias, path) {
50
+ wxt.hooks.hook("ready", (wxt2) => {
51
+ const target = resolve(wxt2.config.root, path);
52
+ if (wxt2.config.alias[alias] != null) {
53
+ wxt2.logger.warn(
54
+ `Skipped adding alias (${alias} => ${target}) because an alias with the same name already exists: ${alias} => ${wxt2.config.alias[alias]}`
55
+ );
56
+ }
57
+ wxt2.config.alias[alias] = target;
58
+ });
59
+ }
@@ -5,20 +5,20 @@ import {
5
5
  extensionApiMock,
6
6
  resolveAppConfig
7
7
  } from "../core/builders/vite/plugins/index.mjs";
8
- import { resolveConfig } from "../core/utils/building/index.mjs";
9
8
  import { vitePlugin as unimportPlugin } from "../builtin-modules/unimport.mjs";
10
9
  import { createUnimport } from "unimport";
10
+ import { registerWxt, wxt } from "../core/wxt.mjs";
11
11
  export async function WxtVitest(inlineConfig) {
12
- const config = await resolveConfig(inlineConfig ?? {}, "serve");
12
+ await registerWxt("serve", inlineConfig ?? {});
13
13
  const plugins = [
14
- globals(config),
15
- download(config),
16
- tsconfigPaths(config),
17
- resolveAppConfig(config),
18
- extensionApiMock(config)
14
+ globals(wxt.config),
15
+ download(wxt.config),
16
+ tsconfigPaths(wxt.config),
17
+ resolveAppConfig(wxt.config),
18
+ extensionApiMock(wxt.config)
19
19
  ];
20
- if (config.imports !== false) {
21
- const unimport = createUnimport(config.imports);
20
+ if (wxt.config.imports !== false) {
21
+ const unimport = createUnimport(wxt.config.imports);
22
22
  await unimport.init();
23
23
  plugins.push(unimportPlugin(unimport));
24
24
  }
package/dist/version.mjs CHANGED
@@ -1 +1 @@
1
- export const version = "0.19.6";
1
+ export const version = "0.19.7";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wxt",
3
3
  "type": "module",
4
- "version": "0.19.6",
4
+ "version": "0.19.8",
5
5
  "description": "Next gen framework for developing web extensions",
6
6
  "repository": {
7
7
  "type": "git",