wxt 0.2.3 → 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");
@@ -470,7 +490,27 @@ async function getInternalConfig(config, command) {
470
490
  wxtDir,
471
491
  typesDir,
472
492
  fsCache: createFsCache(wxtDir),
473
- manifest
493
+ manifest,
494
+ zip: {
495
+ sourcesTemplate: "{{name}}-{{version}}-sources.zip",
496
+ artifactTemplate: "{{name}}-{{version}}-{{browser}}.zip",
497
+ sourcesRoot: root,
498
+ ...userConfig.zip,
499
+ ...config.zip,
500
+ ignoredSources: [
501
+ "**/node_modules",
502
+ // WXT files
503
+ "**/web-ext.config.ts",
504
+ // Hidden files
505
+ "**/.*",
506
+ // Tests
507
+ "**/__tests__/**",
508
+ "**/*.+(test|spec).?(c|m)+(j|t)s?(x)",
509
+ // User config
510
+ ...userConfig.zip?.ignoredSources ?? [],
511
+ ...config.zip?.ignoredSources ?? []
512
+ ]
513
+ }
474
514
  };
475
515
  finalConfig.vite.root = root;
476
516
  finalConfig.vite.configFile = false;
@@ -490,6 +530,7 @@ async function getInternalConfig(config, command) {
490
530
  );
491
531
  finalConfig.vite.plugins.push(devServerGlobals(finalConfig));
492
532
  finalConfig.vite.plugins.push(tsconfigPaths(finalConfig));
533
+ finalConfig.vite.plugins.push(noopBackground());
493
534
  finalConfig.vite.define ??= {};
494
535
  getGlobals(finalConfig).forEach((global) => {
495
536
  finalConfig.vite.define[global.name] = JSON.stringify(global.value);
@@ -598,7 +639,7 @@ function findEffectedSteps(changedFile, currentOutput) {
598
639
  // src/index.ts
599
640
  var import_async_mutex = require("async-mutex");
600
641
  var import_consola2 = require("consola");
601
- var import_node_path4 = require("path");
642
+ var import_node_path6 = require("path");
602
643
 
603
644
  // src/core/build/buildEntrypoints.ts
604
645
  var vite2 = __toESM(require("vite"), 1);
@@ -744,6 +785,16 @@ var import_unimport2 = require("unimport");
744
785
  var import_fs_extra6 = __toESM(require("fs-extra"), 1);
745
786
  var import_path7 = require("path");
746
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
747
798
  async function importTsFile(path5, config) {
748
799
  config.logger.debug("Loading file metadata:", path5);
749
800
  const unimport2 = (0, import_unimport2.createUnimport)({
@@ -754,7 +805,7 @@ async function importTsFile(path5, config) {
754
805
  });
755
806
  await unimport2.init();
756
807
  const text = await import_fs_extra6.default.readFile(path5, "utf-8");
757
- const textNoImports = text.replace(/import.*[\n;]/gm, "");
808
+ const textNoImports = removeImportStatements(text);
758
809
  const { code } = await unimport2.injectImports(textNoImports);
759
810
  config.logger.debug(
760
811
  ["Text:", text, "No imports:", textNoImports, "Code:", code].join("\n")
@@ -794,6 +845,7 @@ async function findEntrypoints(config) {
794
845
  const pathGlobs = Object.keys(PATH_GLOB_TO_TYPE_MAP);
795
846
  const existingNames = {};
796
847
  const entrypoints = [];
848
+ let hasBackground = false;
797
849
  await Promise.all(
798
850
  relativePaths.map(async (relativePath) => {
799
851
  const path5 = (0, import_path8.resolve)(config.entrypointsDir, relativePath);
@@ -823,6 +875,7 @@ ${JSON.stringify(
823
875
  break;
824
876
  case "background":
825
877
  entrypoint = await getBackgroundEntrypoint(config, path5);
878
+ hasBackground = true;
826
879
  break;
827
880
  case "content-script":
828
881
  entrypoint = await getContentScriptEntrypoint(
@@ -852,6 +905,11 @@ ${JSON.stringify(
852
905
  existingNames[entrypoint.name] = entrypoint;
853
906
  })
854
907
  );
908
+ if (config.command === "serve" && !hasBackground) {
909
+ entrypoints.push(
910
+ await getBackgroundEntrypoint(config, VIRTUAL_NOOP_BACKGROUND_MODULE_ID)
911
+ );
912
+ }
855
913
  return entrypoints;
856
914
  }
857
915
  async function getPopupEntrypoint(config, path5) {
@@ -909,9 +967,17 @@ async function getOptionsEntrypoint(config, path5) {
909
967
  };
910
968
  }
911
969
  async function getBackgroundEntrypoint(config, path5) {
912
- const { main: _, ...options } = await importTsFile(path5, config);
913
- if (options == null) {
914
- 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;
915
981
  }
916
982
  return {
917
983
  type: "background",
@@ -955,6 +1021,7 @@ var PATH_GLOB_TO_TYPE_MAP = {
955
1021
  "devtools.html": "devtools",
956
1022
  "devtools/index.html": "devtools",
957
1023
  "background.ts": "background",
1024
+ [VIRTUAL_NOOP_BACKGROUND_MODULE_ID]: "background",
958
1025
  "content.ts?(x)": "content-script",
959
1026
  "content/index.ts?(x)": "content-script",
960
1027
  "*.content.ts?(x)": "content-script",
@@ -1089,7 +1156,7 @@ async function writeTsConfigFile(mainReference, config) {
1089
1156
  }
1090
1157
 
1091
1158
  // src/core/utils/manifest.ts
1092
- var import_fs_extra9 = __toESM(require("fs-extra"), 1);
1159
+ var import_fs_extra10 = __toESM(require("fs-extra"), 1);
1093
1160
  var import_path10 = require("path");
1094
1161
 
1095
1162
  // src/core/utils/ContentSecurityPolicy.ts
@@ -1173,11 +1240,26 @@ function mapWxtOptionsToContentScript(options) {
1173
1240
  };
1174
1241
  }
1175
1242
 
1243
+ // src/core/utils/package.ts
1244
+ var import_node_path4 = require("path");
1245
+ var import_fs_extra9 = __toESM(require("fs-extra"), 1);
1246
+ async function getPackageJson(config) {
1247
+ const file = (0, import_node_path4.resolve)(config.root, "package.json");
1248
+ try {
1249
+ return await import_fs_extra9.default.readJson(file);
1250
+ } catch (err) {
1251
+ config.logger.debug(
1252
+ `Failed to read package.json at: ${file}. Returning undefined.`
1253
+ );
1254
+ return {};
1255
+ }
1256
+ }
1257
+
1176
1258
  // src/core/utils/manifest.ts
1177
1259
  async function writeManifest(manifest, output, config) {
1178
1260
  const str = config.mode === "production" ? JSON.stringify(manifest) : JSON.stringify(manifest, null, 2);
1179
- await import_fs_extra9.default.ensureDir(config.outDir);
1180
- await import_fs_extra9.default.writeFile((0, import_path10.resolve)(config.outDir, "manifest.json"), str, "utf-8");
1261
+ await import_fs_extra10.default.ensureDir(config.outDir);
1262
+ await import_fs_extra10.default.writeFile((0, import_path10.resolve)(config.outDir, "manifest.json"), str, "utf-8");
1181
1263
  output.publicAssets.unshift({
1182
1264
  type: "asset",
1183
1265
  fileName: "manifest.json",
@@ -1216,17 +1298,6 @@ async function generateMainfest(entrypoints, buildOutput, config) {
1216
1298
  }
1217
1299
  return manifest;
1218
1300
  }
1219
- async function getPackageJson(config) {
1220
- const file = (0, import_path10.resolve)(config.root, "package.json");
1221
- try {
1222
- return await import_fs_extra9.default.readJson(file);
1223
- } catch (err) {
1224
- config.logger.debug(
1225
- `Failed to read package.json at: ${file}. Returning undefined.`
1226
- );
1227
- return {};
1228
- }
1229
- }
1230
1301
  function simplifyVersion(versionName) {
1231
1302
  const version3 = /^((0|[1-9][0-9]{0,8})([.](0|[1-9][0-9]{0,8})){0,3}).*$/.exec(
1232
1303
  versionName
@@ -1470,7 +1541,7 @@ function addHostPermission(manifest, hostPermission) {
1470
1541
  // src/core/build.ts
1471
1542
  var import_picocolors2 = __toESM(require("picocolors"), 1);
1472
1543
  var vite3 = __toESM(require("vite"), 1);
1473
- var import_fs_extra11 = __toESM(require("fs-extra"), 1);
1544
+ var import_fs_extra12 = __toESM(require("fs-extra"), 1);
1474
1545
 
1475
1546
  // src/core/utils/groupEntrypoints.ts
1476
1547
  function groupEntrypoints(entrypoints) {
@@ -1518,7 +1589,13 @@ function formatDuration(duration) {
1518
1589
  }
1519
1590
 
1520
1591
  // src/core/log/printBuildSummary.ts
1521
- var import_path11 = __toESM(require("path"), 1);
1592
+ var import_path11 = require("path");
1593
+
1594
+ // src/core/log/printFileList.ts
1595
+ var import_node_path5 = __toESM(require("path"), 1);
1596
+ var import_picocolors = __toESM(require("picocolors"), 1);
1597
+ var import_fs_extra11 = __toESM(require("fs-extra"), 1);
1598
+ var import_filesize = require("filesize");
1522
1599
 
1523
1600
  // src/core/log/printTable.ts
1524
1601
  function printTable(log, rows, gap = 2) {
@@ -1546,10 +1623,42 @@ function printTable(log, rows, gap = 2) {
1546
1623
  log(str);
1547
1624
  }
1548
1625
 
1626
+ // src/core/log/printFileList.ts
1627
+ async function printFileList(log, baseDir, files) {
1628
+ let totalSize = 0;
1629
+ const fileRows = await Promise.all(
1630
+ files.map(async (file, i) => {
1631
+ const parts = [
1632
+ import_node_path5.default.relative(process.cwd(), baseDir) + import_node_path5.default.sep,
1633
+ import_node_path5.default.relative(baseDir, file)
1634
+ ];
1635
+ const prefix = i === files.length - 1 ? " \u2514\u2500" : " \u251C\u2500";
1636
+ const color = getChunkColor(file);
1637
+ const stats = await import_fs_extra11.default.lstat(file);
1638
+ totalSize += stats.size;
1639
+ const size = String((0, import_filesize.filesize)(stats.size));
1640
+ return [
1641
+ `${import_picocolors.default.gray(prefix)} ${import_picocolors.default.dim(parts[0])}${color(parts[1])}`,
1642
+ import_picocolors.default.dim(size)
1643
+ ];
1644
+ })
1645
+ );
1646
+ printTable(log, fileRows);
1647
+ log(`${import_picocolors.default.cyan("\u03A3 Total size:")} ${String((0, import_filesize.filesize)(totalSize))}`);
1648
+ }
1649
+ var DEFAULT_COLOR = import_picocolors.default.blue;
1650
+ var CHUNK_COLORS = {
1651
+ ".js.map": import_picocolors.default.gray,
1652
+ ".html": import_picocolors.default.green,
1653
+ ".css": import_picocolors.default.magenta,
1654
+ ".js": import_picocolors.default.cyan,
1655
+ ".zip": import_picocolors.default.yellow
1656
+ };
1657
+ function getChunkColor(filename) {
1658
+ return Object.entries(CHUNK_COLORS).find(([key]) => filename.endsWith(key))?.[1] ?? DEFAULT_COLOR;
1659
+ }
1660
+
1549
1661
  // src/core/log/printBuildSummary.ts
1550
- var import_picocolors = __toESM(require("picocolors"), 1);
1551
- var import_fs_extra10 = __toESM(require("fs-extra"), 1);
1552
- var import_filesize = require("filesize");
1553
1662
  async function printBuildSummary(output, config) {
1554
1663
  const chunks = [
1555
1664
  ...output.steps.flatMap((step) => step.chunks),
@@ -1562,28 +1671,8 @@ async function printBuildSummary(output, config) {
1562
1671
  return diff;
1563
1672
  return l.fileName.localeCompare(r.fileName);
1564
1673
  });
1565
- let totalSize = 0;
1566
- const chunkRows = await Promise.all(
1567
- chunks.map(async (chunk, i) => {
1568
- const file = [
1569
- (0, import_path11.relative)(process.cwd(), config.outDir) + import_path11.default.sep,
1570
- chunk.fileName
1571
- ];
1572
- const prefix = i === chunks.length - 1 ? " \u2514\u2500" : " \u251C\u2500";
1573
- const color = getChunkColor(chunk.fileName);
1574
- const stats = await import_fs_extra10.default.lstat((0, import_path11.resolve)(config.outDir, chunk.fileName));
1575
- totalSize += stats.size;
1576
- const size = String((0, import_filesize.filesize)(stats.size));
1577
- return [
1578
- `${import_picocolors.default.gray(prefix)} ${import_picocolors.default.dim(file[0])}${color(file[1])}`,
1579
- import_picocolors.default.dim(size)
1580
- ];
1581
- })
1582
- );
1583
- printTable(config.logger.log, chunkRows);
1584
- config.logger.log(
1585
- `${import_picocolors.default.cyan("\u03A3 Total size:")} ${String((0, import_filesize.filesize)(totalSize))}`
1586
- );
1674
+ const files = chunks.map((chunk) => (0, import_path11.resolve)(config.outDir, chunk.fileName));
1675
+ await printFileList(config.logger.log, config.outDir, files);
1587
1676
  }
1588
1677
  var DEFAULT_SORT_WEIGHT = 100;
1589
1678
  var CHUNK_SORT_WEIGHTS = {
@@ -1598,16 +1687,6 @@ function getChunkSortWeight(filename) {
1598
1687
  ([key]) => filename.endsWith(key)
1599
1688
  )?.[1] ?? DEFAULT_SORT_WEIGHT;
1600
1689
  }
1601
- var DEFAULT_COLOR = import_picocolors.default.blue;
1602
- var CHUNK_COLORS = {
1603
- ".js.map": import_picocolors.default.gray,
1604
- ".html": import_picocolors.default.green,
1605
- ".css": import_picocolors.default.magenta,
1606
- ".js": import_picocolors.default.cyan
1607
- };
1608
- function getChunkColor(filename) {
1609
- return Object.entries(CHUNK_COLORS).find(([key]) => filename.endsWith(key))?.[1] ?? DEFAULT_COLOR;
1610
- }
1611
1690
 
1612
1691
  // src/core/build.ts
1613
1692
  async function buildInternal(config) {
@@ -1619,11 +1698,11 @@ async function buildInternal(config) {
1619
1698
  )}`
1620
1699
  );
1621
1700
  const startTime = Date.now();
1622
- await import_fs_extra11.default.rm(config.outDir, { recursive: true, force: true });
1623
- await import_fs_extra11.default.ensureDir(config.outDir);
1701
+ await import_fs_extra12.default.rm(config.outDir, { recursive: true, force: true });
1702
+ await import_fs_extra12.default.ensureDir(config.outDir);
1624
1703
  const entrypoints = await findEntrypoints(config);
1625
1704
  const groups = groupEntrypoints(entrypoints);
1626
- const { output } = await rebuild(config, groups);
1705
+ const { output } = await rebuild(config, groups, void 0);
1627
1706
  config.logger.success(
1628
1707
  `Built extension in ${formatDuration(Date.now() - startTime)}`
1629
1708
  );
@@ -1635,7 +1714,11 @@ async function rebuild(config, entrypointGroups, existingOutput = {
1635
1714
  publicAssets: []
1636
1715
  }) {
1637
1716
  const allEntrypoints = await findEntrypoints(config);
1638
- 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
+ });
1639
1722
  const newOutput = await buildEntrypoints(entrypointGroups, config);
1640
1723
  const mergedOutput = {
1641
1724
  steps: [...existingOutput.steps, ...newOutput.steps],
@@ -1811,7 +1894,7 @@ function reloadHtmlPages(groups, server, config) {
1811
1894
  }
1812
1895
 
1813
1896
  // package.json
1814
- var version2 = "0.2.3";
1897
+ var version2 = "0.2.5";
1815
1898
 
1816
1899
  // src/core/utils/defineConfig.ts
1817
1900
  function defineConfig(config) {
@@ -1851,15 +1934,17 @@ async function createServer2(config) {
1851
1934
  changeQueue.push([event, path5]);
1852
1935
  await fileChangedMutex.runExclusive(async () => {
1853
1936
  const fileChanges = changeQueue.splice(0, changeQueue.length);
1937
+ if (fileChanges.length === 0)
1938
+ return;
1854
1939
  const changes = detectDevChanges(fileChanges, server.currentOutput);
1855
1940
  if (changes.type === "no-change")
1856
1941
  return;
1857
1942
  internalConfig.logger.info(
1858
- `Changed: ${Array.from(new Set(fileChanges.map((change) => change[1]))).map((file) => import_picocolors3.default.dim((0, import_node_path4.relative)(internalConfig.root, file))).join(", ")}`
1943
+ `Changed: ${Array.from(new Set(fileChanges.map((change) => change[1]))).map((file) => import_picocolors3.default.dim((0, import_node_path6.relative)(internalConfig.root, file))).join(", ")}`
1859
1944
  );
1860
1945
  const rebuiltNames = changes.rebuildGroups.flat().map((entry) => {
1861
1946
  return import_picocolors3.default.cyan(
1862
- (0, import_node_path4.relative)(internalConfig.outDir, getEntrypointOutputFile(entry, ""))
1947
+ (0, import_node_path6.relative)(internalConfig.outDir, getEntrypointOutputFile(entry, ""))
1863
1948
  );
1864
1949
  }).join(import_picocolors3.default.dim(", "));
1865
1950
  internalConfig = await getLatestInternalConfig();