wxt 0.5.2 → 0.5.3

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
@@ -40,9 +40,45 @@ __export(src_exports, {
40
40
  module.exports = __toCommonJS(src_exports);
41
41
 
42
42
  // src/core/utils/getInternalConfig.ts
43
+ var import_c12 = require("c12");
43
44
  var import_node_path4 = __toESM(require("path"), 1);
44
45
  var vite2 = __toESM(require("vite"), 1);
45
- var import_consola = require("consola");
46
+
47
+ // src/core/utils/createFsCache.ts
48
+ var import_fs_extra2 = __toESM(require("fs-extra"), 1);
49
+ var import_path = require("path");
50
+
51
+ // src/core/utils/fs.ts
52
+ var import_fs_extra = __toESM(require("fs-extra"), 1);
53
+ async function writeFileIfDifferent(file, newContents) {
54
+ const existingContents = await import_fs_extra.default.readFile(file, "utf-8").catch(() => void 0);
55
+ if (existingContents !== newContents) {
56
+ await import_fs_extra.default.writeFile(file, newContents);
57
+ }
58
+ }
59
+
60
+ // src/core/utils/createFsCache.ts
61
+ function createFsCache(wxtDir) {
62
+ const getPath = (key) => (0, import_path.resolve)(wxtDir, "cache", encodeURIComponent(key));
63
+ return {
64
+ async set(key, value) {
65
+ const path7 = getPath(key);
66
+ await (0, import_fs_extra2.ensureDir)((0, import_path.dirname)(path7));
67
+ await writeFileIfDifferent(path7, value);
68
+ },
69
+ async get(key) {
70
+ const path7 = getPath(key);
71
+ try {
72
+ return await import_fs_extra2.default.readFile(path7, "utf-8");
73
+ } catch {
74
+ return void 0;
75
+ }
76
+ }
77
+ };
78
+ }
79
+
80
+ // src/core/utils/getInternalConfig.ts
81
+ var import_consola = __toESM(require("consola"), 1);
46
82
 
47
83
  // src/core/utils/entrypoints.ts
48
84
  var import_node_path2 = __toESM(require("path"), 1);
@@ -76,7 +112,7 @@ function getEntrypointBundlePath(entrypoint, outDir, ext) {
76
112
 
77
113
  // src/core/vite-plugins/devHtmlPrerender.ts
78
114
  var import_linkedom = require("linkedom");
79
- var import_path = require("path");
115
+ var import_path2 = require("path");
80
116
  function devHtmlPrerender(config) {
81
117
  return {
82
118
  apply: "build",
@@ -85,7 +121,7 @@ function devHtmlPrerender(config) {
85
121
  return {
86
122
  resolve: {
87
123
  alias: {
88
- "@wxt/reload-html": (0, import_path.resolve)(
124
+ "@wxt/reload-html": (0, import_path2.resolve)(
89
125
  config.root,
90
126
  "node_modules/wxt/dist/virtual-modules/reload-html.js"
91
127
  )
@@ -111,11 +147,11 @@ function devHtmlPrerender(config) {
111
147
  const src = element.getAttribute(attr);
112
148
  if (!src)
113
149
  return;
114
- if ((0, import_path.isAbsolute)(src)) {
150
+ if ((0, import_path2.isAbsolute)(src)) {
115
151
  element.setAttribute(attr, server.origin + src);
116
152
  } else if (src.startsWith(".")) {
117
- const abs = (0, import_path.resolve)((0, import_path.dirname)(id), src);
118
- const pathname = (0, import_path.relative)(config.root, abs);
153
+ const abs = (0, import_path2.resolve)((0, import_path2.dirname)(id), src);
154
+ const pathname = (0, import_path2.relative)(config.root, abs);
119
155
  element.setAttribute(attr, `${server.origin}/${pathname}`);
120
156
  }
121
157
  });
@@ -226,7 +262,7 @@ function download(config) {
226
262
 
227
263
  // src/core/vite-plugins/multipageMove.ts
228
264
  var import_node_path3 = require("path");
229
- var import_fs_extra = __toESM(require("fs-extra"), 1);
265
+ var import_fs_extra3 = __toESM(require("fs-extra"), 1);
230
266
  function multipageMove(entrypoints, config) {
231
267
  return {
232
268
  name: "wxt:multipage-move",
@@ -255,8 +291,8 @@ function multipageMove(entrypoints, config) {
255
291
  }
256
292
  const oldAbsPath = (0, import_node_path3.resolve)(config.outDir, oldBundlePath);
257
293
  const newAbsPath = (0, import_node_path3.resolve)(config.outDir, newBundlePath);
258
- await (0, import_fs_extra.ensureDir)((0, import_node_path3.dirname)(newAbsPath));
259
- await import_fs_extra.default.move(oldAbsPath, newAbsPath, { overwrite: true });
294
+ await (0, import_fs_extra3.ensureDir)((0, import_node_path3.dirname)(newAbsPath));
295
+ await import_fs_extra3.default.move(oldAbsPath, newAbsPath, { overwrite: true });
260
296
  const renamedChunk = {
261
297
  ...bundle[oldBundlePath],
262
298
  fileName: newBundlePath
@@ -290,7 +326,7 @@ function getUnimportOptions(config) {
290
326
  }
291
327
 
292
328
  // src/core/vite-plugins/unimport.ts
293
- var import_path2 = require("path");
329
+ var import_path3 = require("path");
294
330
  var ENABLED_EXTENSIONS = {
295
331
  ".js": true,
296
332
  ".jsx": true,
@@ -310,7 +346,7 @@ function unimport(config) {
310
346
  await unimport2.scanImportsFromDir(void 0, { cwd: config.srcDir });
311
347
  },
312
348
  async transform(code, id) {
313
- const ext = (0, import_path2.extname)(id);
349
+ const ext = (0, import_path3.extname)(id);
314
350
  if (ENABLED_EXTENSIONS[ext])
315
351
  return unimport2.injectImports(code, id);
316
352
  }
@@ -318,8 +354,8 @@ function unimport(config) {
318
354
  }
319
355
 
320
356
  // src/core/vite-plugins/virtualEntrypoint.ts
321
- var import_fs_extra2 = __toESM(require("fs-extra"), 1);
322
- var import_path3 = require("path");
357
+ var import_fs_extra4 = __toESM(require("fs-extra"), 1);
358
+ var import_path4 = require("path");
323
359
  function virtualEntrypoin(type, config) {
324
360
  const virtualId = `virtual:wxt-${type}?`;
325
361
  const resolvedVirtualId = `\0${virtualId}`;
@@ -336,8 +372,8 @@ function virtualEntrypoin(type, config) {
336
372
  if (!id.startsWith(resolvedVirtualId))
337
373
  return;
338
374
  const inputPath = id.replace(resolvedVirtualId, "");
339
- const template = await import_fs_extra2.default.readFile(
340
- (0, import_path3.resolve)(
375
+ const template = await import_fs_extra4.default.readFile(
376
+ (0, import_path4.resolve)(
341
377
  config.root,
342
378
  `node_modules/wxt/dist/virtual-modules/${type}-entrypoint.js`
343
379
  ),
@@ -411,39 +447,6 @@ function cssEntrypoints(entrypoint, config) {
411
447
  };
412
448
  }
413
449
 
414
- // src/core/utils/createFsCache.ts
415
- var import_fs_extra4 = __toESM(require("fs-extra"), 1);
416
- var import_path4 = require("path");
417
-
418
- // src/core/utils/fs.ts
419
- var import_fs_extra3 = __toESM(require("fs-extra"), 1);
420
- async function writeFileIfDifferent(file, newContents) {
421
- const existingContents = await import_fs_extra3.default.readFile(file, "utf-8").catch(() => void 0);
422
- if (existingContents !== newContents) {
423
- await import_fs_extra3.default.writeFile(file, newContents);
424
- }
425
- }
426
-
427
- // src/core/utils/createFsCache.ts
428
- function createFsCache(wxtDir) {
429
- const getPath = (key) => (0, import_path4.resolve)(wxtDir, "cache", encodeURIComponent(key));
430
- return {
431
- async set(key, value) {
432
- const path7 = getPath(key);
433
- await (0, import_fs_extra4.ensureDir)((0, import_path4.dirname)(path7));
434
- await writeFileIfDifferent(path7, value);
435
- },
436
- async get(key) {
437
- const path7 = getPath(key);
438
- try {
439
- return await import_fs_extra4.default.readFile(path7, "utf-8");
440
- } catch {
441
- return void 0;
442
- }
443
- }
444
- };
445
- }
446
-
447
450
  // src/core/utils/globals.ts
448
451
  function getGlobals(config) {
449
452
  return [
@@ -491,128 +494,182 @@ function getGlobals(config) {
491
494
  }
492
495
 
493
496
  // src/core/utils/getInternalConfig.ts
494
- var import_c12 = require("c12");
495
- async function getInternalConfig(config, command) {
496
- const root = config.root ? import_node_path4.default.resolve(config.root) : process.cwd();
497
- const mode = config.mode ?? (command === "build" ? "production" : "development");
498
- const browser = config.browser ?? "chrome";
499
- const manifestVersion = config.manifestVersion ?? (browser == "firefox" ? 2 : 3);
500
- const outBaseDir = import_node_path4.default.resolve(root, ".output");
501
- const outDir = import_node_path4.default.resolve(outBaseDir, `${browser}-mv${manifestVersion}`);
502
- const logger = config.logger ?? import_consola.consola;
503
- const debug = !!config.debug;
504
- if (debug)
505
- logger.level = import_consola.LogLevels.debug;
506
- const baseConfig = {
507
- root,
508
- outDir,
509
- outBaseDir,
510
- storeIds: config.storeIds ?? {},
511
- browser,
512
- manifestVersion,
513
- mode,
514
- command,
515
- debug,
516
- logger,
517
- vite: config.vite ?? {},
518
- imports: config.imports ?? {},
519
- runnerConfig: await (0, import_c12.loadConfig)({
520
- name: "web-ext",
521
- cwd: root,
522
- globalRc: true,
523
- rcFile: ".webextrc",
524
- overrides: config.runner
525
- })
526
- };
527
- let userConfig = {
528
- mode
529
- };
530
- if (config.configFile !== false) {
497
+ async function getInternalConfig(inlineConfig, command) {
498
+ let userConfig = {};
499
+ if (inlineConfig.configFile !== false) {
531
500
  const loaded = await (0, import_c12.loadConfig)({
532
501
  name: "wxt",
533
- cwd: root,
502
+ cwd: inlineConfig.root ?? process.cwd(),
534
503
  rcFile: false
535
504
  });
536
505
  userConfig = loaded.config ?? {};
537
506
  }
538
- const merged = vite2.mergeConfig(
539
- baseConfig,
540
- userConfig
507
+ const mergedConfig = mergeInlineConfig(inlineConfig, userConfig);
508
+ const debug = mergedConfig.debug ?? false;
509
+ const logger = mergedConfig.logger ?? import_consola.default;
510
+ if (debug)
511
+ logger.level = import_consola.LogLevels.debug;
512
+ const browser = mergedConfig.browser ?? "chrome";
513
+ const manifestVersion = mergedConfig.manifestVersion ?? (browser == "firefox" ? 2 : 3);
514
+ const mode = mergedConfig.mode ?? (command === "build" ? "production" : "development");
515
+ const env = { browser, command, manifestVersion, mode };
516
+ const root = import_node_path4.default.resolve(
517
+ inlineConfig.root ?? userConfig.root ?? process.cwd()
541
518
  );
542
- const srcDir = userConfig.srcDir ? (0, import_node_path4.resolve)(root, userConfig.srcDir) : root;
543
- const entrypointsDir = (0, import_node_path4.resolve)(
519
+ const wxtDir = import_node_path4.default.resolve(root, ".wxt");
520
+ const srcDir = import_node_path4.default.resolve(root, mergedConfig.srcDir ?? root);
521
+ const entrypointsDir = import_node_path4.default.resolve(
544
522
  srcDir,
545
- userConfig.entrypointsDir ?? "entrypoints"
523
+ mergedConfig.entrypointsDir ?? "entrypoints"
546
524
  );
547
- const publicDir = (0, import_node_path4.resolve)(srcDir, userConfig.publicDir ?? "public");
548
- const wxtDir = (0, import_node_path4.resolve)(root, ".wxt");
549
- const typesDir = (0, import_node_path4.resolve)(wxtDir, "types");
550
- const env = { mode, browser, manifestVersion, command };
551
- const userManifest = await resolveManifestConfig(env, userConfig.manifest);
552
- const inlineManifest = await resolveManifestConfig(env, config.manifest);
553
- const manifest = vite2.mergeConfig(userManifest, inlineManifest);
525
+ const publicDir = import_node_path4.default.resolve(srcDir, mergedConfig.publicDir ?? "public");
526
+ const typesDir = import_node_path4.default.resolve(wxtDir, "types");
527
+ const outBaseDir = import_node_path4.default.resolve(root, ".output");
528
+ const outDir = import_node_path4.default.resolve(outBaseDir, `${browser}-mv${manifestVersion}`);
529
+ const runnerConfig = await (0, import_c12.loadConfig)({
530
+ name: "web-ext",
531
+ cwd: root,
532
+ globalRc: true,
533
+ rcFile: ".webextrc",
534
+ overrides: mergedConfig.runner
535
+ });
554
536
  const finalConfig = {
555
- ...merged,
556
- srcDir,
537
+ browser,
538
+ command,
539
+ debug,
557
540
  entrypointsDir,
541
+ fsCache: createFsCache(wxtDir),
542
+ imports: mergedConfig.imports ?? {},
543
+ logger,
544
+ manifest: await resolveManifestConfig(env, mergedConfig.manifest),
545
+ manifestVersion,
546
+ mode,
547
+ outBaseDir,
548
+ outDir,
558
549
  publicDir,
559
- wxtDir,
550
+ root,
551
+ runnerConfig,
552
+ srcDir,
560
553
  typesDir,
561
- fsCache: createFsCache(wxtDir),
562
- manifest,
563
- zip: {
564
- sourcesTemplate: "{{name}}-{{version}}-sources.zip",
565
- artifactTemplate: "{{name}}-{{version}}-{{browser}}.zip",
566
- sourcesRoot: root,
567
- ...userConfig.zip,
568
- ...config.zip,
569
- ignoredSources: [
570
- "**/node_modules",
571
- // WXT files
572
- "**/web-ext.config.ts",
573
- // Hidden files
574
- "**/.*",
575
- // Tests
576
- "**/__tests__/**",
577
- "**/*.+(test|spec).?(c|m)+(j|t)s?(x)",
578
- // User config
579
- ...userConfig.zip?.ignoredSources ?? [],
580
- ...config.zip?.ignoredSources ?? []
581
- ]
582
- }
554
+ vite: {},
555
+ // Real value added after this object is initialized.
556
+ wxtDir,
557
+ zip: resolveInternalZipConfig(root, mergedConfig)
583
558
  };
584
- finalConfig.vite.root = root;
585
- finalConfig.vite.configFile = false;
586
- finalConfig.vite.logLevel = "warn";
587
- finalConfig.vite.build ??= {};
588
- finalConfig.vite.build.outDir = outDir;
589
- finalConfig.vite.build.emptyOutDir = false;
590
- finalConfig.vite.plugins ??= [];
591
- finalConfig.vite.plugins.push(download(finalConfig));
592
- finalConfig.vite.plugins.push(devHtmlPrerender(finalConfig));
593
- finalConfig.vite.plugins.push(unimport(finalConfig));
594
- finalConfig.vite.plugins.push(
595
- virtualEntrypoin("background", finalConfig)
559
+ finalConfig.vite = await resolveInternalViteConfig(
560
+ env,
561
+ mergedConfig,
562
+ finalConfig
596
563
  );
597
- finalConfig.vite.plugins.push(
598
- virtualEntrypoin("content-script", finalConfig)
599
- );
600
- finalConfig.vite.plugins.push(devServerGlobals(finalConfig));
601
- finalConfig.vite.plugins.push(tsconfigPaths(finalConfig));
602
- finalConfig.vite.plugins.push(noopBackground());
603
- finalConfig.vite.define ??= {};
604
- getGlobals(finalConfig).forEach((global) => {
605
- finalConfig.vite.define[global.name] = JSON.stringify(global.value);
606
- });
607
564
  return finalConfig;
608
565
  }
609
566
  async function resolveManifestConfig(env, manifest) {
610
567
  return await (typeof manifest === "function" ? manifest(env) : manifest ?? {});
611
568
  }
569
+ function mergeInlineConfig(inlineConfig, userConfig) {
570
+ let imports;
571
+ if (inlineConfig.imports === false || userConfig.imports === false) {
572
+ imports = false;
573
+ } else if (userConfig.imports == null && inlineConfig.imports == null) {
574
+ imports = void 0;
575
+ } else {
576
+ imports = vite2.mergeConfig(
577
+ userConfig.imports ?? {},
578
+ inlineConfig.imports ?? {}
579
+ );
580
+ }
581
+ const manifest = async (env) => {
582
+ const user = await resolveManifestConfig(env, userConfig.manifest);
583
+ const inline = await resolveManifestConfig(env, inlineConfig.manifest);
584
+ return vite2.mergeConfig(user, inline);
585
+ };
586
+ const viteConfig = async (env) => {
587
+ const user = await resolveViteConfig(env, userConfig.vite);
588
+ const inline = await resolveViteConfig(env, inlineConfig.vite);
589
+ return vite2.mergeConfig(user, inline);
590
+ };
591
+ const runner = vite2.mergeConfig(
592
+ userConfig.runner ?? {},
593
+ inlineConfig.runner ?? {}
594
+ );
595
+ const zip = vite2.mergeConfig(
596
+ userConfig.zip ?? {},
597
+ inlineConfig.zip ?? {}
598
+ );
599
+ return {
600
+ root: inlineConfig.root ?? userConfig.root,
601
+ browser: inlineConfig.browser ?? userConfig.browser,
602
+ manifestVersion: inlineConfig.manifestVersion ?? userConfig.manifestVersion,
603
+ configFile: inlineConfig.configFile,
604
+ debug: inlineConfig.debug ?? userConfig.debug,
605
+ entrypointsDir: inlineConfig.entrypointsDir ?? userConfig.entrypointsDir,
606
+ imports,
607
+ logger: inlineConfig.logger ?? userConfig.logger,
608
+ manifest,
609
+ mode: inlineConfig.mode ?? userConfig.mode,
610
+ publicDir: inlineConfig.publicDir ?? userConfig.publicDir,
611
+ runner,
612
+ srcDir: inlineConfig.srcDir ?? userConfig.srcDir,
613
+ vite: viteConfig,
614
+ zip
615
+ };
616
+ }
617
+ async function resolveViteConfig(env, vite6) {
618
+ return await (typeof vite6 === "function" ? vite6(env) : vite6 ?? {});
619
+ }
620
+ function resolveInternalZipConfig(root, mergedConfig) {
621
+ return {
622
+ sourcesTemplate: "{{name}}-{{version}}-sources.zip",
623
+ artifactTemplate: "{{name}}-{{version}}-{{browser}}.zip",
624
+ sourcesRoot: root,
625
+ ...mergedConfig.zip,
626
+ ignoredSources: [
627
+ "**/node_modules",
628
+ // WXT files
629
+ "**/web-ext.config.ts",
630
+ // Hidden files
631
+ "**/.*",
632
+ // Tests
633
+ "**/__tests__/**",
634
+ "**/*.+(test|spec).?(c|m)+(j|t)s?(x)",
635
+ // From user
636
+ ...mergedConfig.zip?.ignoredSources ?? []
637
+ ]
638
+ };
639
+ }
640
+ async function resolveInternalViteConfig(env, mergedConfig, finalConfig) {
641
+ const internalVite = await resolveViteConfig(
642
+ env,
643
+ mergedConfig.vite
644
+ );
645
+ internalVite.root = finalConfig.root;
646
+ internalVite.configFile = false;
647
+ internalVite.logLevel = "warn";
648
+ internalVite.build ??= {};
649
+ internalVite.build.outDir = finalConfig.outDir;
650
+ internalVite.build.emptyOutDir = false;
651
+ internalVite.plugins ??= [];
652
+ internalVite.plugins.push(download(finalConfig));
653
+ internalVite.plugins.push(devHtmlPrerender(finalConfig));
654
+ internalVite.plugins.push(unimport(finalConfig));
655
+ internalVite.plugins.push(
656
+ virtualEntrypoin("background", finalConfig)
657
+ );
658
+ internalVite.plugins.push(
659
+ virtualEntrypoin("content-script", finalConfig)
660
+ );
661
+ internalVite.plugins.push(devServerGlobals(finalConfig));
662
+ internalVite.plugins.push(tsconfigPaths(finalConfig));
663
+ internalVite.plugins.push(noopBackground());
664
+ internalVite.define ??= {};
665
+ for (const global of getGlobals(finalConfig)) {
666
+ internalVite.define[global.name] = JSON.stringify(global.value);
667
+ }
668
+ return internalVite;
669
+ }
612
670
 
613
671
  // src/index.ts
614
672
  var import_picocolors4 = __toESM(require("picocolors"), 1);
615
- var vite6 = __toESM(require("vite"), 1);
616
673
 
617
674
  // src/core/utils/arrays.ts
618
675
  function every(array, predicate) {
@@ -2225,7 +2282,7 @@ async function clean(root = process.cwd()) {
2225
2282
  }
2226
2283
 
2227
2284
  // package.json
2228
- var version2 = "0.5.2";
2285
+ var version2 = "0.5.3";
2229
2286
 
2230
2287
  // src/core/utils/defineConfig.ts
2231
2288
  function defineConfig(config) {
@@ -2244,12 +2301,14 @@ async function build2(config) {
2244
2301
  }
2245
2302
  async function createServer2(config) {
2246
2303
  const serverInfo = await getServerInfo();
2247
- const getLatestInternalConfig = () => {
2248
- const viteConfig = vite6.mergeConfig(
2249
- serverInfo.viteServerConfig,
2250
- config?.vite ?? {}
2304
+ const getLatestInternalConfig = async () => {
2305
+ return getInternalConfig(
2306
+ {
2307
+ ...config,
2308
+ vite: serverInfo.viteServerConfig
2309
+ },
2310
+ "serve"
2251
2311
  );
2252
- return getInternalConfig({ ...config, vite: viteConfig }, "serve");
2253
2312
  };
2254
2313
  let internalConfig = await getLatestInternalConfig();
2255
2314
  const server = await setupServer(serverInfo, internalConfig);