wxt 0.17.8 → 0.17.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/testing.cjs CHANGED
@@ -50,184 +50,9 @@ function normalizePath(path8) {
50
50
  var CSS_EXTENSIONS = ["css", "scss", "sass", "less", "styl", "stylus"];
51
51
  var CSS_EXTENSIONS_PATTERN = `+(${CSS_EXTENSIONS.join("|")})`;
52
52
 
53
- // src/core/utils/entrypoints.ts
54
- function getEntrypointName(entrypointsDir, inputPath) {
55
- const relativePath = import_node_path2.default.relative(entrypointsDir, inputPath);
56
- const name = relativePath.split(/[\.\/\\]/, 2)[0];
57
- return name;
58
- }
59
- function getEntrypointOutputFile(entrypoint, ext) {
60
- return (0, import_node_path2.resolve)(entrypoint.outputDir, `${entrypoint.name}${ext}`);
61
- }
62
- function getEntrypointBundlePath(entrypoint, outDir, ext) {
63
- return normalizePath(
64
- (0, import_node_path2.relative)(outDir, getEntrypointOutputFile(entrypoint, ext))
65
- );
66
- }
67
- function isHtmlEntrypoint(entrypoint) {
68
- return entrypoint.inputPath.endsWith(".html");
69
- }
70
-
71
53
  // src/core/builders/vite/plugins/devHtmlPrerender.ts
72
54
  var import_linkedom = require("linkedom");
73
55
  var import_node_path3 = require("path");
74
- var reactRefreshPreamble = "";
75
- function devHtmlPrerender(config) {
76
- const htmlReloadId = "@wxt/reload-html";
77
- const resolvedHtmlReloadId = (0, import_node_path3.resolve)(
78
- config.wxtModuleDir,
79
- "dist/virtual/reload-html.js"
80
- );
81
- const virtualReactRefreshId = "@wxt/virtual-react-refresh";
82
- const resolvedVirtualReactRefreshId = "\0" + virtualReactRefreshId;
83
- return [
84
- {
85
- apply: "build",
86
- name: "wxt:dev-html-prerender",
87
- config() {
88
- return {
89
- resolve: {
90
- alias: {
91
- [htmlReloadId]: resolvedHtmlReloadId
92
- }
93
- }
94
- };
95
- },
96
- // Convert scripts like src="./main.tsx" -> src="http://localhost:3000/entrypoints/popup/main.tsx"
97
- // before the paths are replaced with their bundled path
98
- transform(code, id) {
99
- const server = config.server;
100
- if (config.command !== "serve" || server == null || !id.endsWith(".html"))
101
- return;
102
- const { document } = (0, import_linkedom.parseHTML)(code);
103
- const _pointToDevServer = (querySelector, attr) => pointToDevServer(config, server, id, document, querySelector, attr);
104
- _pointToDevServer("script[type=module]", "src");
105
- _pointToDevServer("link[rel=stylesheet]", "href");
106
- const reloader = document.createElement("script");
107
- reloader.src = htmlReloadId;
108
- reloader.type = "module";
109
- document.head.appendChild(reloader);
110
- const newHtml = document.toString();
111
- config.logger.debug("transform " + id);
112
- config.logger.debug("Old HTML:\n" + code);
113
- config.logger.debug("New HTML:\n" + newHtml);
114
- return newHtml;
115
- },
116
- // Pass the HTML through the dev server to add dev-mode specific code
117
- async transformIndexHtml(html, ctx) {
118
- const server = config.server;
119
- if (config.command !== "serve" || server == null)
120
- return;
121
- const originalUrl = `${server.origin}${ctx.path}`;
122
- const name = getEntrypointName(config.entrypointsDir, ctx.filename);
123
- const url = `${server.origin}/${name}.html`;
124
- const serverHtml = await server.transformHtml(url, html, originalUrl);
125
- const { document } = (0, import_linkedom.parseHTML)(serverHtml);
126
- const reactRefreshScript = Array.from(
127
- document.querySelectorAll("script[type=module]")
128
- ).find((script) => script.innerHTML.includes("@react-refresh"));
129
- if (reactRefreshScript) {
130
- reactRefreshPreamble = reactRefreshScript.innerHTML;
131
- const virtualScript = document.createElement("script");
132
- virtualScript.type = "module";
133
- virtualScript.src = `${server.origin}/${virtualReactRefreshId}`;
134
- reactRefreshScript.replaceWith(virtualScript);
135
- }
136
- const viteClientScript = document.querySelector(
137
- "script[src='/@vite/client']"
138
- );
139
- if (viteClientScript) {
140
- viteClientScript.src = `${server.origin}${viteClientScript.src}`;
141
- }
142
- const newHtml = document.toString();
143
- config.logger.debug("transformIndexHtml " + ctx.filename);
144
- config.logger.debug("Old HTML:\n" + html);
145
- config.logger.debug("New HTML:\n" + newHtml);
146
- return newHtml;
147
- }
148
- },
149
- {
150
- name: "wxt:virtualize-react-refresh",
151
- apply: "serve",
152
- resolveId(id) {
153
- if (id === `/${virtualReactRefreshId}`) {
154
- return resolvedVirtualReactRefreshId;
155
- }
156
- if (id.startsWith("/chunks/")) {
157
- return "\0noop";
158
- }
159
- },
160
- load(id) {
161
- if (id === resolvedVirtualReactRefreshId) {
162
- return reactRefreshPreamble;
163
- }
164
- if (id === "\0noop") {
165
- return "";
166
- }
167
- }
168
- }
169
- ];
170
- }
171
- function pointToDevServer(config, server, id, document, querySelector, attr) {
172
- document.querySelectorAll(querySelector).forEach((element) => {
173
- const src = element.getAttribute(attr);
174
- if (!src || isUrl(src))
175
- return;
176
- let resolvedAbsolutePath;
177
- const matchingAlias = Object.entries(config.alias).find(
178
- ([key]) => src.startsWith(key)
179
- );
180
- if (matchingAlias) {
181
- const [alias, replacement] = matchingAlias;
182
- resolvedAbsolutePath = (0, import_node_path3.resolve)(
183
- config.root,
184
- src.replace(alias, replacement)
185
- );
186
- } else {
187
- resolvedAbsolutePath = (0, import_node_path3.resolve)((0, import_node_path3.dirname)(id), src);
188
- }
189
- if (resolvedAbsolutePath) {
190
- const relativePath = normalizePath(
191
- (0, import_node_path3.relative)(config.root, resolvedAbsolutePath)
192
- );
193
- if (relativePath.startsWith(".")) {
194
- let path8 = normalizePath(resolvedAbsolutePath);
195
- if (!path8.startsWith("/"))
196
- path8 = "/" + path8;
197
- element.setAttribute(attr, `${server.origin}/@fs${path8}`);
198
- } else {
199
- const url = new URL(relativePath, server.origin);
200
- element.setAttribute(attr, url.href);
201
- }
202
- }
203
- });
204
- }
205
- function isUrl(str) {
206
- try {
207
- new URL(str);
208
- return true;
209
- } catch {
210
- return false;
211
- }
212
- }
213
-
214
- // src/core/builders/vite/plugins/devServerGlobals.ts
215
- function devServerGlobals(config) {
216
- return {
217
- name: "wxt:dev-server-globals",
218
- config() {
219
- if (config.server == null || config.command == "build")
220
- return;
221
- return {
222
- define: {
223
- __DEV_SERVER_PROTOCOL__: JSON.stringify("ws:"),
224
- __DEV_SERVER_HOSTNAME__: JSON.stringify(config.server.hostname),
225
- __DEV_SERVER_PORT__: JSON.stringify(config.server.port)
226
- }
227
- };
228
- }
229
- };
230
- }
231
56
 
232
57
  // src/core/utils/network.ts
233
58
  var import_node_dns = __toESM(require("dns"), 1);
@@ -301,61 +126,6 @@ function download(config) {
301
126
  // src/core/builders/vite/plugins/multipageMove.ts
302
127
  var import_node_path4 = require("path");
303
128
  var import_fs_extra = __toESM(require("fs-extra"), 1);
304
- function multipageMove(entrypoints, config) {
305
- return {
306
- name: "wxt:multipage-move",
307
- async writeBundle(_, bundle) {
308
- for (const oldBundlePath in bundle) {
309
- const entrypoint = entrypoints.find(
310
- (entry) => !!normalizePath(entry.inputPath).endsWith(oldBundlePath)
311
- );
312
- if (entrypoint == null) {
313
- config.logger.debug(
314
- `No entrypoint found for ${oldBundlePath}, leaving in chunks directory`
315
- );
316
- continue;
317
- }
318
- const newBundlePath = getEntrypointBundlePath(
319
- entrypoint,
320
- config.outDir,
321
- (0, import_node_path4.extname)(oldBundlePath)
322
- );
323
- if (newBundlePath === oldBundlePath) {
324
- config.logger.debug(
325
- "HTML file is already in the correct location",
326
- oldBundlePath
327
- );
328
- continue;
329
- }
330
- const oldAbsPath = (0, import_node_path4.resolve)(config.outDir, oldBundlePath);
331
- const newAbsPath = (0, import_node_path4.resolve)(config.outDir, newBundlePath);
332
- await (0, import_fs_extra.ensureDir)((0, import_node_path4.dirname)(newAbsPath));
333
- await import_fs_extra.default.move(oldAbsPath, newAbsPath, { overwrite: true });
334
- const renamedChunk = {
335
- ...bundle[oldBundlePath],
336
- fileName: newBundlePath
337
- };
338
- delete bundle[oldBundlePath];
339
- bundle[newBundlePath] = renamedChunk;
340
- }
341
- removeEmptyDirs(config.outDir);
342
- }
343
- };
344
- }
345
- async function removeEmptyDirs(dir) {
346
- const files = await import_fs_extra.default.readdir(dir);
347
- for (const file of files) {
348
- const filePath = (0, import_node_path4.join)(dir, file);
349
- const stats = await import_fs_extra.default.stat(filePath);
350
- if (stats.isDirectory()) {
351
- await removeEmptyDirs(filePath);
352
- }
353
- }
354
- try {
355
- await import_fs_extra.default.rmdir(dir);
356
- } catch {
357
- }
358
- }
359
129
 
360
130
  // src/core/builders/vite/plugins/unimport.ts
361
131
  var import_unimport = require("unimport");
@@ -394,31 +164,6 @@ function unimport(config) {
394
164
 
395
165
  // src/core/builders/vite/plugins/virtualEntrypoint.ts
396
166
  var import_fs_extra2 = __toESM(require("fs-extra"), 1);
397
- var import_path2 = require("path");
398
- function virtualEntrypoint(type, config) {
399
- const virtualId = `virtual:wxt-${type}?`;
400
- const resolvedVirtualId = `\0${virtualId}`;
401
- return {
402
- name: `wxt:virtual-entrypoint`,
403
- resolveId(id) {
404
- const index = id.indexOf(virtualId);
405
- if (index === -1)
406
- return;
407
- const inputPath = normalizePath(id.substring(index + virtualId.length));
408
- return resolvedVirtualId + inputPath;
409
- },
410
- async load(id) {
411
- if (!id.startsWith(resolvedVirtualId))
412
- return;
413
- const inputPath = id.replace(resolvedVirtualId, "");
414
- const template = await import_fs_extra2.default.readFile(
415
- (0, import_path2.resolve)(config.wxtModuleDir, `dist/virtual/${type}-entrypoint.js`),
416
- "utf-8"
417
- );
418
- return template.replace(`virtual:user-${type}`, inputPath);
419
- }
420
- };
421
- }
422
167
 
423
168
  // src/core/builders/vite/plugins/tsconfigPaths.ts
424
169
  function tsconfigPaths(config) {
@@ -437,62 +182,9 @@ function tsconfigPaths(config) {
437
182
  // src/core/utils/constants.ts
438
183
  var VIRTUAL_NOOP_BACKGROUND_MODULE_ID = "virtual:user-background";
439
184
 
440
- // src/core/builders/vite/plugins/noopBackground.ts
441
- function noopBackground() {
442
- const virtualModuleId = VIRTUAL_NOOP_BACKGROUND_MODULE_ID;
443
- const resolvedVirtualModuleId = "\0" + virtualModuleId;
444
- return {
445
- name: "wxt:noop-background",
446
- resolveId(id) {
447
- if (id === virtualModuleId)
448
- return resolvedVirtualModuleId;
449
- },
450
- load(id) {
451
- if (id === resolvedVirtualModuleId) {
452
- return `import { defineBackground } from 'wxt/sandbox';
453
- export default defineBackground(() => void 0)`;
454
- }
455
- }
456
- };
457
- }
458
-
459
- // src/core/builders/vite/plugins/cssEntrypoints.ts
460
- function cssEntrypoints(entrypoint, config) {
461
- return {
462
- name: "wxt:css-entrypoint",
463
- config() {
464
- return {
465
- build: {
466
- rollupOptions: {
467
- output: {
468
- assetFileNames: () => getEntrypointBundlePath(entrypoint, config.outDir, ".css")
469
- }
470
- }
471
- }
472
- };
473
- },
474
- generateBundle(_, bundle) {
475
- Object.keys(bundle).forEach((file) => {
476
- if (file.endsWith(".js"))
477
- delete bundle[file];
478
- });
479
- }
480
- };
481
- }
482
-
483
185
  // src/core/builders/vite/plugins/bundleAnalysis.ts
484
186
  var import_rollup_plugin_visualizer = require("@aklinker1/rollup-plugin-visualizer");
485
187
  var import_node_path5 = __toESM(require("path"), 1);
486
- var increment = 0;
487
- function bundleAnalysis(config) {
488
- return (0, import_rollup_plugin_visualizer.visualizer)({
489
- template: "raw-data",
490
- filename: import_node_path5.default.resolve(
491
- config.analysis.outputDir,
492
- `${config.analysis.outputName}-${increment++}.json`
493
- )
494
- });
495
- }
496
188
 
497
189
  // src/core/utils/globals.ts
498
190
  function getGlobals(config) {
@@ -539,15 +231,6 @@ function getGlobals(config) {
539
231
  }
540
232
  ];
541
233
  }
542
- function getEntrypointGlobals(entrypointName) {
543
- return [
544
- {
545
- name: "ENTRYPOINT",
546
- value: entrypointName,
547
- type: `string`
548
- }
549
- ];
550
- }
551
234
 
552
235
  // src/core/builders/vite/plugins/globals.ts
553
236
  function globals(config) {
@@ -590,62 +273,6 @@ function webextensionPolyfillMock(config) {
590
273
  };
591
274
  }
592
275
 
593
- // src/core/builders/vite/plugins/excludeBrowserPolyfill.ts
594
- function excludeBrowserPolyfill(config) {
595
- const virtualId = "virtual:wxt-webextension-polyfill-disabled";
596
- return {
597
- name: "wxt:exclude-browser-polyfill",
598
- config() {
599
- if (config.experimental.includeBrowserPolyfill)
600
- return;
601
- return {
602
- resolve: {
603
- alias: {
604
- "webextension-polyfill": virtualId
605
- }
606
- }
607
- };
608
- },
609
- load(id) {
610
- if (id === virtualId) {
611
- return "export default chrome";
612
- }
613
- }
614
- };
615
- }
616
-
617
- // src/core/builders/vite/plugins/entrypointGroupGlobals.ts
618
- function entrypointGroupGlobals(entrypointGroup) {
619
- return {
620
- name: "wxt:entrypoint-group-globals",
621
- config() {
622
- const define = {};
623
- let name = Array.isArray(entrypointGroup) ? "html" : entrypointGroup.name;
624
- for (const global of getEntrypointGlobals(name)) {
625
- define[`import.meta.env.${global.name}`] = JSON.stringify(global.value);
626
- }
627
- return {
628
- define
629
- };
630
- }
631
- };
632
- }
633
-
634
- // src/core/builders/vite/plugins/defineImportMeta.ts
635
- function defineImportMeta() {
636
- return {
637
- name: "wxt:define",
638
- config() {
639
- return {
640
- define: {
641
- // This works for all extension contexts, including background service worker
642
- "import.meta.url": "self.location.href"
643
- }
644
- };
645
- }
646
- };
647
- }
648
-
649
276
  // src/core/utils/fs.ts
650
277
  var import_fs_extra4 = __toESM(require("fs-extra"), 1);
651
278
  var import_fast_glob = __toESM(require("fast-glob"), 1);
@@ -730,13 +357,13 @@ var import_node_path10 = __toESM(require("path"), 1);
730
357
 
731
358
  // src/core/utils/cache.ts
732
359
  var import_fs_extra8 = __toESM(require("fs-extra"), 1);
733
- var import_path3 = require("path");
360
+ var import_path2 = require("path");
734
361
  function createFsCache(wxtDir) {
735
- const getPath = (key) => (0, import_path3.resolve)(wxtDir, "cache", encodeURIComponent(key));
362
+ const getPath = (key) => (0, import_path2.resolve)(wxtDir, "cache", encodeURIComponent(key));
736
363
  return {
737
364
  async set(key, value) {
738
365
  const path8 = getPath(key);
739
- await (0, import_fs_extra8.ensureDir)((0, import_path3.dirname)(path8));
366
+ await (0, import_fs_extra8.ensureDir)((0, import_path2.dirname)(path8));
740
367
  await writeFileIfDifferent(path8, value);
741
368
  },
742
369
  async get(key) {
@@ -752,227 +379,6 @@ function createFsCache(wxtDir) {
752
379
 
753
380
  // src/core/utils/building/resolve-config.ts
754
381
  var import_consola = __toESM(require("consola"), 1);
755
-
756
- // src/core/builders/vite/index.ts
757
- async function createViteBuilder(inlineConfig, userConfig, wxtConfig) {
758
- const vite = await import("vite");
759
- const getBaseConfig = async () => {
760
- const resolvedInlineConfig = await inlineConfig.vite?.(wxtConfig.env) ?? {};
761
- const resolvedUserConfig = await userConfig.vite?.(wxtConfig.env) ?? {};
762
- const config = vite.mergeConfig(
763
- resolvedUserConfig,
764
- resolvedInlineConfig
765
- );
766
- config.root = wxtConfig.root;
767
- config.configFile = false;
768
- config.logLevel = "warn";
769
- config.mode = wxtConfig.mode;
770
- config.build ??= {};
771
- config.build.outDir = wxtConfig.outDir;
772
- config.build.emptyOutDir = false;
773
- if (config.build.minify == null && wxtConfig.command === "serve") {
774
- config.build.minify = false;
775
- }
776
- if (config.build.sourcemap == null && wxtConfig.command === "serve") {
777
- config.build.sourcemap = "inline";
778
- }
779
- config.plugins ??= [];
780
- config.plugins.push(
781
- download(wxtConfig),
782
- devHtmlPrerender(wxtConfig),
783
- unimport(wxtConfig),
784
- virtualEntrypoint("background", wxtConfig),
785
- virtualEntrypoint("content-script-isolated-world", wxtConfig),
786
- virtualEntrypoint("content-script-main-world", wxtConfig),
787
- virtualEntrypoint("unlisted-script", wxtConfig),
788
- devServerGlobals(wxtConfig),
789
- tsconfigPaths(wxtConfig),
790
- noopBackground(),
791
- globals(wxtConfig),
792
- excludeBrowserPolyfill(wxtConfig),
793
- defineImportMeta()
794
- );
795
- if (wxtConfig.analysis.enabled) {
796
- config.plugins.push(bundleAnalysis(wxtConfig));
797
- }
798
- return config;
799
- };
800
- const getLibModeConfig = (entrypoint) => {
801
- const entry = getRollupEntry(entrypoint);
802
- const plugins = [
803
- entrypointGroupGlobals(entrypoint)
804
- ];
805
- if (entrypoint.type === "content-script-style" || entrypoint.type === "unlisted-style") {
806
- plugins.push(cssEntrypoints(entrypoint, wxtConfig));
807
- }
808
- const libMode = {
809
- mode: wxtConfig.mode,
810
- plugins,
811
- build: {
812
- lib: {
813
- entry,
814
- formats: ["iife"],
815
- name: "_",
816
- fileName: entrypoint.name
817
- },
818
- rollupOptions: {
819
- output: {
820
- // There's only a single output for this build, so we use the desired bundle path for the
821
- // entry output (like "content-scripts/overlay.js")
822
- entryFileNames: getEntrypointBundlePath(
823
- entrypoint,
824
- wxtConfig.outDir,
825
- ".js"
826
- ),
827
- // Output content script CSS to `content-scripts/`, but all other scripts are written to
828
- // `assets/`.
829
- assetFileNames: ({ name }) => {
830
- if (entrypoint.type === "content-script" && name?.endsWith("css")) {
831
- return `content-scripts/${entrypoint.name}.[ext]`;
832
- } else {
833
- return `assets/${entrypoint.name}.[ext]`;
834
- }
835
- }
836
- }
837
- }
838
- },
839
- define: {
840
- // See https://github.com/aklinker1/vite-plugin-web-extension/issues/96
841
- "process.env.NODE_ENV": JSON.stringify(wxtConfig.mode)
842
- }
843
- };
844
- return libMode;
845
- };
846
- const getMultiPageConfig = (entrypoints) => {
847
- const htmlEntrypoints = new Set(
848
- entrypoints.filter(isHtmlEntrypoint).map((e) => e.name)
849
- );
850
- return {
851
- mode: wxtConfig.mode,
852
- plugins: [
853
- multipageMove(entrypoints, wxtConfig),
854
- entrypointGroupGlobals(entrypoints)
855
- ],
856
- build: {
857
- rollupOptions: {
858
- input: entrypoints.reduce((input, entry) => {
859
- input[entry.name] = getRollupEntry(entry);
860
- return input;
861
- }, {}),
862
- output: {
863
- // Include a hash to prevent conflicts
864
- chunkFileNames: "chunks/[name]-[hash].js",
865
- entryFileNames: ({ name }) => {
866
- if (htmlEntrypoints.has(name))
867
- return "chunks/[name]-[hash].js";
868
- return "[name].js";
869
- },
870
- // We can't control the "name", so we need a hash to prevent conflicts
871
- assetFileNames: "assets/[name]-[hash].[ext]"
872
- }
873
- }
874
- }
875
- };
876
- };
877
- const getCssConfig = (entrypoint) => {
878
- return {
879
- mode: wxtConfig.mode,
880
- plugins: [entrypointGroupGlobals(entrypoint)],
881
- build: {
882
- rollupOptions: {
883
- input: {
884
- [entrypoint.name]: entrypoint.inputPath
885
- },
886
- output: {
887
- assetFileNames: () => {
888
- if (entrypoint.type === "content-script-style") {
889
- return `content-scripts/${entrypoint.name}.[ext]`;
890
- } else {
891
- return `assets/${entrypoint.name}.[ext]`;
892
- }
893
- }
894
- }
895
- }
896
- }
897
- };
898
- };
899
- return {
900
- name: "Vite",
901
- version: vite.version,
902
- async build(group) {
903
- let entryConfig;
904
- if (Array.isArray(group))
905
- entryConfig = getMultiPageConfig(group);
906
- else if (group.inputPath.endsWith(".css"))
907
- entryConfig = getCssConfig(group);
908
- else
909
- entryConfig = getLibModeConfig(group);
910
- const buildConfig = vite.mergeConfig(await getBaseConfig(), entryConfig);
911
- const result = await vite.build(buildConfig);
912
- return {
913
- entrypoints: group,
914
- chunks: getBuildOutputChunks(result)
915
- };
916
- },
917
- async createServer(info) {
918
- const serverConfig = {
919
- server: {
920
- port: info.port,
921
- strictPort: true,
922
- host: info.hostname,
923
- origin: info.origin
924
- }
925
- };
926
- const baseConfig = await getBaseConfig();
927
- const viteServer = await vite.createServer(
928
- vite.mergeConfig(baseConfig, serverConfig)
929
- );
930
- const server = {
931
- async listen() {
932
- await viteServer.listen(info.port);
933
- },
934
- async close() {
935
- await viteServer.close();
936
- },
937
- transformHtml(...args) {
938
- return viteServer.transformIndexHtml(...args);
939
- },
940
- ws: {
941
- send(message, payload) {
942
- return viteServer.ws.send(message, payload);
943
- },
944
- on(message, cb) {
945
- viteServer.ws.on(message, cb);
946
- }
947
- },
948
- watcher: viteServer.watcher
949
- };
950
- return server;
951
- }
952
- };
953
- }
954
- function getBuildOutputChunks(result) {
955
- if ("on" in result)
956
- throw Error("wxt does not support vite watch mode.");
957
- if (Array.isArray(result))
958
- return result.flatMap(({ output }) => output);
959
- return result.output;
960
- }
961
- function getRollupEntry(entrypoint) {
962
- let virtualEntrypointType;
963
- switch (entrypoint.type) {
964
- case "background":
965
- case "unlisted-script":
966
- virtualEntrypointType = entrypoint.type;
967
- break;
968
- case "content-script":
969
- virtualEntrypointType = entrypoint.options.world === "MAIN" ? "content-script-main-world" : "content-script-isolated-world";
970
- break;
971
- }
972
- return virtualEntrypointType ? `virtual:wxt-${virtualEntrypointType}?${entrypoint.inputPath}` : entrypoint.inputPath;
973
- }
974
-
975
- // src/core/utils/building/resolve-config.ts
976
382
  var import_defu = __toESM(require("defu"), 1);
977
383
 
978
384
  // src/core/utils/package.ts
@@ -985,7 +391,7 @@ function isModuleInstalled(name) {
985
391
  // src/core/utils/building/resolve-config.ts
986
392
  var import_fs_extra10 = __toESM(require("fs-extra"), 1);
987
393
  var import_meta = {};
988
- async function resolveConfig(inlineConfig, command, server) {
394
+ async function resolveConfig(inlineConfig, command) {
989
395
  let userConfig = {};
990
396
  let userConfigMetadata;
991
397
  if (inlineConfig.configFile !== false) {
@@ -1001,14 +407,14 @@ async function resolveConfig(inlineConfig, command, server) {
1001
407
  userConfig = loadedConfig ?? {};
1002
408
  userConfigMetadata = metadata;
1003
409
  }
1004
- const mergedConfig = mergeInlineConfig(inlineConfig, userConfig);
410
+ const mergedConfig = await mergeInlineConfig(inlineConfig, userConfig);
1005
411
  const debug = mergedConfig.debug ?? false;
1006
412
  const logger = mergedConfig.logger ?? import_consola.default;
1007
413
  if (debug)
1008
414
  logger.level = import_consola.LogLevels.debug;
1009
415
  const browser = mergedConfig.browser ?? "chrome";
1010
416
  const manifestVersion = mergedConfig.manifestVersion ?? (browser === "firefox" || browser === "safari" ? 2 : 3);
1011
- const mode = mergedConfig.mode ?? (command === "build" ? "production" : "development");
417
+ const mode = mergedConfig.mode ?? COMMAND_MODES[command];
1012
418
  const env = { browser, command, manifestVersion, mode };
1013
419
  const root = import_node_path10.default.resolve(
1014
420
  inlineConfig.root ?? userConfig.root ?? process.cwd()
@@ -1049,13 +455,19 @@ async function resolveConfig(inlineConfig, command, server) {
1049
455
  "~~": root
1050
456
  }).map(([key, value]) => [key, import_node_path10.default.resolve(root, value)])
1051
457
  );
1052
- const analysisOutputFile = import_node_path10.default.resolve(
1053
- root,
1054
- mergedConfig.analysis?.outputFile ?? "stats.html"
1055
- );
1056
- const analysisOutputDir = import_node_path10.default.dirname(analysisOutputFile);
1057
- const analysisOutputName = import_node_path10.default.parse(analysisOutputFile).name;
1058
- const finalConfig = {
458
+ let devServerConfig;
459
+ if (command === "serve") {
460
+ let port = mergedConfig.dev?.server?.port;
461
+ if (port == null || !isFinite(port)) {
462
+ const { default: getPort, portNumbers } = await import("get-port");
463
+ port = await getPort({ port: portNumbers(3e3, 3010) });
464
+ }
465
+ devServerConfig = {
466
+ port,
467
+ hostname: "localhost"
468
+ };
469
+ }
470
+ return {
1059
471
  browser,
1060
472
  command,
1061
473
  debug,
@@ -1077,109 +489,47 @@ async function resolveConfig(inlineConfig, command, server) {
1077
489
  srcDir,
1078
490
  typesDir,
1079
491
  wxtDir,
1080
- zip: resolveInternalZipConfig(root, mergedConfig),
1081
- transformManifest(manifest) {
1082
- userConfig.transformManifest?.(manifest);
1083
- inlineConfig.transformManifest?.(manifest);
1084
- },
1085
- analysis: {
1086
- enabled: mergedConfig.analysis?.enabled ?? false,
1087
- open: mergedConfig.analysis?.open ?? false,
1088
- template: mergedConfig.analysis?.template ?? "treemap",
1089
- outputFile: analysisOutputFile,
1090
- outputDir: analysisOutputDir,
1091
- outputName: analysisOutputName,
1092
- keepArtifacts: mergedConfig.analysis?.keepArtifacts ?? false
1093
- },
492
+ zip: resolveZipConfig(root, mergedConfig),
493
+ transformManifest: mergedConfig.transformManifest,
494
+ analysis: resolveAnalysisConfig(root, mergedConfig),
1094
495
  userConfigMetadata: userConfigMetadata ?? {},
1095
496
  alias,
1096
- experimental: {
1097
- includeBrowserPolyfill: mergedConfig.experimental?.includeBrowserPolyfill ?? true
1098
- },
1099
- server,
497
+ experimental: (0, import_defu.default)(mergedConfig.experimental, {
498
+ includeBrowserPolyfill: true
499
+ }),
1100
500
  dev: {
501
+ server: devServerConfig,
1101
502
  reloadCommand
1102
503
  },
1103
- hooks: mergedConfig.hooks ?? {}
1104
- };
1105
- const builder = await createViteBuilder(
1106
- inlineConfig,
1107
- userConfig,
1108
- finalConfig
1109
- );
1110
- return {
1111
- ...finalConfig,
1112
- builder
504
+ hooks: mergedConfig.hooks ?? {},
505
+ vite: mergedConfig.vite ?? (() => ({}))
1113
506
  };
1114
507
  }
1115
508
  async function resolveManifestConfig(env, manifest) {
1116
509
  return await (typeof manifest === "function" ? manifest(env) : manifest ?? {});
1117
510
  }
1118
- function mergeInlineConfig(inlineConfig, userConfig) {
1119
- let imports;
1120
- if (inlineConfig.imports === false || userConfig.imports === false) {
1121
- imports = false;
1122
- } else if (userConfig.imports == null && inlineConfig.imports == null) {
1123
- imports = void 0;
1124
- } else {
1125
- imports = (0, import_defu.default)(inlineConfig.imports ?? {}, userConfig.imports ?? {});
1126
- }
511
+ async function mergeInlineConfig(inlineConfig, userConfig) {
512
+ const imports = inlineConfig.imports === false || userConfig.imports === false ? false : userConfig.imports == null && inlineConfig.imports == null ? void 0 : (0, import_defu.default)(inlineConfig.imports ?? {}, userConfig.imports ?? {});
1127
513
  const manifest = async (env) => {
1128
514
  const user = await resolveManifestConfig(env, userConfig.manifest);
1129
515
  const inline = await resolveManifestConfig(env, inlineConfig.manifest);
1130
516
  return (0, import_defu.default)(inline, user);
1131
517
  };
1132
- const runner = (0, import_defu.default)(
1133
- inlineConfig.runner ?? {},
1134
- userConfig.runner ?? {}
1135
- );
1136
- const zip = (0, import_defu.default)(
1137
- inlineConfig.zip ?? {},
1138
- userConfig.zip ?? {}
1139
- );
1140
- const hooks = (0, import_defu.default)(
1141
- inlineConfig.hooks ?? {},
1142
- userConfig.hooks ?? {}
1143
- );
518
+ const transformManifest = (manifest2) => {
519
+ userConfig.transformManifest?.(manifest2);
520
+ inlineConfig.transformManifest?.(manifest2);
521
+ };
522
+ const builderConfig = await mergeBuilderConfig(inlineConfig, userConfig);
1144
523
  return {
1145
- root: inlineConfig.root ?? userConfig.root,
1146
- browser: inlineConfig.browser ?? userConfig.browser,
1147
- manifestVersion: inlineConfig.manifestVersion ?? userConfig.manifestVersion,
1148
- configFile: inlineConfig.configFile,
1149
- debug: inlineConfig.debug ?? userConfig.debug,
1150
- entrypointsDir: inlineConfig.entrypointsDir ?? userConfig.entrypointsDir,
1151
- filterEntrypoints: inlineConfig.filterEntrypoints ?? userConfig.filterEntrypoints,
524
+ ...(0, import_defu.default)(inlineConfig, userConfig),
525
+ // Custom merge values
526
+ transformManifest,
1152
527
  imports,
1153
- logger: inlineConfig.logger ?? userConfig.logger,
1154
528
  manifest,
1155
- mode: inlineConfig.mode ?? userConfig.mode,
1156
- publicDir: inlineConfig.publicDir ?? userConfig.publicDir,
1157
- runner,
1158
- srcDir: inlineConfig.srcDir ?? userConfig.srcDir,
1159
- outDir: inlineConfig.outDir ?? userConfig.outDir,
1160
- zip,
1161
- analysis: {
1162
- ...userConfig.analysis,
1163
- ...inlineConfig.analysis
1164
- },
1165
- alias: {
1166
- ...userConfig.alias,
1167
- ...inlineConfig.alias
1168
- },
1169
- experimental: {
1170
- ...userConfig.experimental,
1171
- ...inlineConfig.experimental
1172
- },
1173
- vite: void 0,
1174
- transformManifest: void 0,
1175
- dev: {
1176
- ...userConfig.dev,
1177
- ...inlineConfig.dev
1178
- },
1179
- hooks
529
+ ...builderConfig
1180
530
  };
1181
531
  }
1182
- function resolveInternalZipConfig(root, mergedConfig) {
532
+ function resolveZipConfig(root, mergedConfig) {
1183
533
  const downloadedPackagesDir = import_node_path10.default.resolve(root, ".wxt/local_modules");
1184
534
  return {
1185
535
  name: void 0,
@@ -1204,6 +554,23 @@ function resolveInternalZipConfig(root, mergedConfig) {
1204
554
  downloadedPackagesDir
1205
555
  };
1206
556
  }
557
+ function resolveAnalysisConfig(root, mergedConfig) {
558
+ const analysisOutputFile = import_node_path10.default.resolve(
559
+ root,
560
+ mergedConfig.analysis?.outputFile ?? "stats.html"
561
+ );
562
+ const analysisOutputDir = import_node_path10.default.dirname(analysisOutputFile);
563
+ const analysisOutputName = import_node_path10.default.parse(analysisOutputFile).name;
564
+ return {
565
+ enabled: mergedConfig.analysis?.enabled ?? false,
566
+ open: mergedConfig.analysis?.open ?? false,
567
+ template: mergedConfig.analysis?.template ?? "treemap",
568
+ outputFile: analysisOutputFile,
569
+ outputDir: analysisOutputDir,
570
+ outputName: analysisOutputName,
571
+ keepArtifacts: mergedConfig.analysis?.keepArtifacts ?? false
572
+ };
573
+ }
1207
574
  async function getUnimportOptions(wxtDir, logger, config) {
1208
575
  if (config.imports === false)
1209
576
  return false;
@@ -1256,6 +623,23 @@ function logMissingDir(logger, name, expected) {
1256
623
  )}`
1257
624
  );
1258
625
  }
626
+ var COMMAND_MODES = {
627
+ build: "production",
628
+ serve: "development"
629
+ };
630
+ async function mergeBuilderConfig(inlineConfig, userConfig) {
631
+ const vite = await import("vite").catch(() => void 0);
632
+ if (vite) {
633
+ return {
634
+ vite: async (env) => {
635
+ const resolvedInlineConfig = await inlineConfig.vite?.(env) ?? {};
636
+ const resolvedUserConfig = await userConfig.vite?.(env) ?? {};
637
+ return vite.mergeConfig(resolvedUserConfig, resolvedInlineConfig);
638
+ }
639
+ };
640
+ }
641
+ throw Error("Builder not found. Make sure vite is installed.");
642
+ }
1259
643
 
1260
644
  // src/core/utils/building/import-entrypoint.ts
1261
645
  var import_jiti = __toESM(require("jiti"), 1);