wxt 0.2.4 → 0.2.5

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/index.cjs CHANGED
@@ -263,7 +263,7 @@ function getUnimportOptions(config) {
263
263
  ],
264
264
  presets: [{ package: "wxt/client" }],
265
265
  warn: config.logger.warn,
266
- dirs: ["components", "composables", "hooks", "utils"]
266
+ dirs: ["./components/*", "./composables/*", "./hooks/*", "./utils/*"]
267
267
  };
268
268
  return (0, import_vite.mergeConfig)(
269
269
  defaultOptions,
@@ -337,6 +337,26 @@ function tsconfigPaths(config) {
337
337
  });
338
338
  }
339
339
 
340
+ // src/core/vite-plugins/noopBackground.ts
341
+ function noopBackground() {
342
+ const virtualModuleId = VIRTUAL_NOOP_BACKGROUND_MODULE_ID;
343
+ const resolvedVirtualModuleId = "\0" + virtualModuleId;
344
+ return {
345
+ name: "wxt:noop-background",
346
+ resolveId(id) {
347
+ if (id === virtualModuleId)
348
+ return resolvedVirtualModuleId;
349
+ },
350
+ load(id) {
351
+ if (id === resolvedVirtualModuleId) {
352
+ return `import { defineBackground } from 'wxt/client';
353
+ export default defineBackground(() => void 0)`;
354
+ }
355
+ }
356
+ };
357
+ }
358
+ var VIRTUAL_NOOP_BACKGROUND_MODULE_ID = "virtual:user-background";
359
+
340
360
  // src/core/utils/createFsCache.ts
341
361
  var import_fs_extra3 = __toESM(require("fs-extra"), 1);
342
362
  var import_path4 = require("path");
@@ -510,6 +530,7 @@ async function getInternalConfig(config, command) {
510
530
  );
511
531
  finalConfig.vite.plugins.push(devServerGlobals(finalConfig));
512
532
  finalConfig.vite.plugins.push(tsconfigPaths(finalConfig));
533
+ finalConfig.vite.plugins.push(noopBackground());
513
534
  finalConfig.vite.define ??= {};
514
535
  getGlobals(finalConfig).forEach((global) => {
515
536
  finalConfig.vite.define[global.name] = JSON.stringify(global.value);
@@ -764,6 +785,16 @@ var import_unimport2 = require("unimport");
764
785
  var import_fs_extra6 = __toESM(require("fs-extra"), 1);
765
786
  var import_path7 = require("path");
766
787
  var import_babel = __toESM(require("jiti/dist/babel"), 1);
788
+
789
+ // src/core/utils/strings.ts
790
+ function removeImportStatements(text) {
791
+ return text.replace(
792
+ /(import\s?[{\w][\s\S]*?from\s?["'][\s\S]*?["'];?|import\s?["'][\s\S]*?["'];?)/gm,
793
+ ""
794
+ );
795
+ }
796
+
797
+ // src/core/utils/importTsFile.ts
767
798
  async function importTsFile(path5, config) {
768
799
  config.logger.debug("Loading file metadata:", path5);
769
800
  const unimport2 = (0, import_unimport2.createUnimport)({
@@ -774,7 +805,7 @@ async function importTsFile(path5, config) {
774
805
  });
775
806
  await unimport2.init();
776
807
  const text = await import_fs_extra6.default.readFile(path5, "utf-8");
777
- const textNoImports = text.replace(/import.*[\n;]/gm, "");
808
+ const textNoImports = removeImportStatements(text);
778
809
  const { code } = await unimport2.injectImports(textNoImports);
779
810
  config.logger.debug(
780
811
  ["Text:", text, "No imports:", textNoImports, "Code:", code].join("\n")
@@ -814,6 +845,7 @@ async function findEntrypoints(config) {
814
845
  const pathGlobs = Object.keys(PATH_GLOB_TO_TYPE_MAP);
815
846
  const existingNames = {};
816
847
  const entrypoints = [];
848
+ let hasBackground = false;
817
849
  await Promise.all(
818
850
  relativePaths.map(async (relativePath) => {
819
851
  const path5 = (0, import_path8.resolve)(config.entrypointsDir, relativePath);
@@ -843,6 +875,7 @@ ${JSON.stringify(
843
875
  break;
844
876
  case "background":
845
877
  entrypoint = await getBackgroundEntrypoint(config, path5);
878
+ hasBackground = true;
846
879
  break;
847
880
  case "content-script":
848
881
  entrypoint = await getContentScriptEntrypoint(
@@ -872,6 +905,11 @@ ${JSON.stringify(
872
905
  existingNames[entrypoint.name] = entrypoint;
873
906
  })
874
907
  );
908
+ if (config.command === "serve" && !hasBackground) {
909
+ entrypoints.push(
910
+ await getBackgroundEntrypoint(config, VIRTUAL_NOOP_BACKGROUND_MODULE_ID)
911
+ );
912
+ }
875
913
  return entrypoints;
876
914
  }
877
915
  async function getPopupEntrypoint(config, path5) {
@@ -929,9 +967,17 @@ async function getOptionsEntrypoint(config, path5) {
929
967
  };
930
968
  }
931
969
  async function getBackgroundEntrypoint(config, path5) {
932
- const { main: _, ...options } = await importTsFile(path5, config);
933
- if (options == null) {
934
- throw Error("Background script does not have a default export");
970
+ let options = {};
971
+ if (path5 !== VIRTUAL_NOOP_BACKGROUND_MODULE_ID) {
972
+ const defaultExport = await importTsFile(
973
+ path5,
974
+ config
975
+ );
976
+ if (defaultExport == null) {
977
+ throw Error("Background script does not have a default export");
978
+ }
979
+ const { main: _, ...moduleOptions } = defaultExport;
980
+ options = moduleOptions;
935
981
  }
936
982
  return {
937
983
  type: "background",
@@ -975,6 +1021,7 @@ var PATH_GLOB_TO_TYPE_MAP = {
975
1021
  "devtools.html": "devtools",
976
1022
  "devtools/index.html": "devtools",
977
1023
  "background.ts": "background",
1024
+ [VIRTUAL_NOOP_BACKGROUND_MODULE_ID]: "background",
978
1025
  "content.ts?(x)": "content-script",
979
1026
  "content/index.ts?(x)": "content-script",
980
1027
  "*.content.ts?(x)": "content-script",
@@ -1655,7 +1702,7 @@ async function buildInternal(config) {
1655
1702
  await import_fs_extra12.default.ensureDir(config.outDir);
1656
1703
  const entrypoints = await findEntrypoints(config);
1657
1704
  const groups = groupEntrypoints(entrypoints);
1658
- const { output } = await rebuild(config, groups);
1705
+ const { output } = await rebuild(config, groups, void 0);
1659
1706
  config.logger.success(
1660
1707
  `Built extension in ${formatDuration(Date.now() - startTime)}`
1661
1708
  );
@@ -1667,7 +1714,11 @@ async function rebuild(config, entrypointGroups, existingOutput = {
1667
1714
  publicAssets: []
1668
1715
  }) {
1669
1716
  const allEntrypoints = await findEntrypoints(config);
1670
- await generateTypesDir(allEntrypoints, config);
1717
+ await generateTypesDir(allEntrypoints, config).catch((err) => {
1718
+ config.logger.warn("Failed to update .wxt directory:", err);
1719
+ if (config.command === "build")
1720
+ throw err;
1721
+ });
1671
1722
  const newOutput = await buildEntrypoints(entrypointGroups, config);
1672
1723
  const mergedOutput = {
1673
1724
  steps: [...existingOutput.steps, ...newOutput.steps],
@@ -1843,7 +1894,7 @@ function reloadHtmlPages(groups, server, config) {
1843
1894
  }
1844
1895
 
1845
1896
  // package.json
1846
- var version2 = "0.2.4";
1897
+ var version2 = "0.2.5";
1847
1898
 
1848
1899
  // src/core/utils/defineConfig.ts
1849
1900
  function defineConfig(config) {
@@ -1883,6 +1934,8 @@ async function createServer2(config) {
1883
1934
  changeQueue.push([event, path5]);
1884
1935
  await fileChangedMutex.runExclusive(async () => {
1885
1936
  const fileChanges = changeQueue.splice(0, changeQueue.length);
1937
+ if (fileChanges.length === 0)
1938
+ return;
1886
1939
  const changes = detectDevChanges(fileChanges, server.currentOutput);
1887
1940
  if (changes.type === "no-change")
1888
1941
  return;