Skip to content

Commit c603e7d

Browse files
committed
refactor: use static imports now that wxt is esm-only
Since #848, there's no need to do all this async work. At least, as far as I could tell...
1 parent ce74463 commit c603e7d

File tree

13 files changed

+35
-48
lines changed

13 files changed

+35
-48
lines changed

packages/wxt/src/@types/modules.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ declare module 'web-ext-run' {
2323
exit(): Promise<void>;
2424
}
2525

26-
const webExt: {
27-
cmd: {
28-
run(config: any, executeOptions: any): Promise<WebExtRunInstance>;
29-
};
26+
export declare const cmd: {
27+
run(config: any, executeOptions: any): Promise<WebExtRunInstance>;
3028
};
29+
30+
declare const webExt: { cmd: typeof cmd };
3131
export default webExt;
3232
}
3333

packages/wxt/src/core/builders/vite/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type * as vite from 'vite';
1+
import * as vite from 'vite';
22
import {
33
BuildStepOutput,
44
Entrypoint,
@@ -29,13 +29,11 @@ import { dirname, extname, join, relative } from 'node:path';
2929
import fs from 'fs-extra';
3030
import { normalizePath } from '../../utils/paths';
3131

32-
export async function createViteBuilder(
32+
export function createViteBuilder(
3333
wxtConfig: ResolvedConfig,
3434
hooks: Hookable<WxtHooks>,
3535
getWxtDevServer?: () => WxtDevServer | undefined,
36-
): Promise<WxtBuilder> {
37-
const vite = await import('vite');
38-
36+
): WxtBuilder {
3937
/**
4038
* Returns the base vite config shared by all builds based on the inline and user config.
4139
*/

packages/wxt/src/core/builders/vite/plugins/devServerGlobals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'vite';
1+
import type { Plugin } from 'vite';
22
import { ResolvedConfig, WxtDevServer } from '../../../../types';
33

44
/**

packages/wxt/src/core/builders/vite/plugins/download.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'vite';
1+
import type { Plugin } from 'vite';
22
import { ResolvedConfig } from '../../../../types';
33
import { fetchCached } from '../../../utils/network';
44

packages/wxt/src/core/builders/vite/plugins/noopBackground.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'vite';
1+
import type { Plugin } from 'vite';
22
import { VIRTUAL_NOOP_BACKGROUND_MODULE_ID } from '../../../utils/constants';
33

44
/**

packages/wxt/src/core/builders/vite/plugins/removeEntrypointMainFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ResolvedConfig } from '../../../../types';
2-
import * as vite from 'vite';
2+
import type * as vite from 'vite';
33
import { normalizePath } from '../../../utils/paths';
44
import { removeMainFunctionCode } from '../../../utils/transform';
55
import { resolve } from 'node:path';

packages/wxt/src/core/builders/vite/plugins/resolveVirtualModules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Plugin } from 'vite';
2-
import { ResolvedConfig } from '../../../../types';
1+
import type { Plugin } from 'vite';
2+
import type { ResolvedConfig } from '../../../../types';
33
import { normalizePath } from '../../../utils/paths';
44
import {
55
VirtualModuleId,

packages/wxt/src/core/initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs-extra';
55
import path from 'node:path';
66
import pc from 'picocolors';
77
import { Formatter } from 'picocolors/types';
8+
import ora from 'ora';
89

910
export async function initialize(options: {
1011
directory: string;
@@ -161,7 +162,6 @@ async function cloneProject({
161162
directory: string;
162163
template: Template;
163164
}) {
164-
const { default: ora } = await import('ora');
165165
const spinner = ora('Downloading template').start();
166166
try {
167167
// 1. Clone repo

packages/wxt/src/core/resolve-config.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { safeStringToNumber } from './utils/number';
2929
import { loadEnv } from './utils/env';
3030
import { getPort } from 'get-port-please';
3131
import { fileURLToPath, pathToFileURL } from 'node:url';
32+
import * as vite from 'vite';
3233

3334
/**
3435
* Given an inline config, discover the config file if necessary, merge the results, resolve any
@@ -61,7 +62,7 @@ export async function resolveConfig(
6162

6263
// Merge it into the inline config
6364

64-
const mergedConfig = await mergeInlineConfig(inlineConfig, userConfig);
65+
const mergedConfig = mergeInlineConfig(inlineConfig, userConfig);
6566

6667
// Apply defaults to make internal config.
6768

@@ -255,10 +256,10 @@ async function resolveManifestConfig(
255256
/**
256257
* Merge the inline config and user config. Inline config is given priority. Defaults are not applied here.
257258
*/
258-
async function mergeInlineConfig(
259+
function mergeInlineConfig(
259260
inlineConfig: InlineConfig,
260261
userConfig: UserConfig,
261-
): Promise<InlineConfig> {
262+
): InlineConfig {
262263
// Merge imports option
263264
const imports: InlineConfig['imports'] =
264265
inlineConfig.imports === false || userConfig.imports === false
@@ -277,11 +278,7 @@ async function mergeInlineConfig(
277278
const merged = defu(inlineConfig, userConfig);
278279

279280
// Builders
280-
const builderConfig = await mergeBuilderConfig(
281-
merged.logger ?? consola,
282-
inlineConfig,
283-
userConfig,
284-
);
281+
const builderConfig = mergeBuilderConfig(inlineConfig, userConfig);
285282

286283
return {
287284
...merged,
@@ -566,25 +563,17 @@ const COMMAND_MODES: Record<WxtCommand, string> = {
566563
serve: 'development',
567564
};
568565

569-
export async function mergeBuilderConfig(
570-
logger: Logger,
566+
function mergeBuilderConfig(
571567
inlineConfig: InlineConfig,
572568
userConfig: UserConfig,
573-
): Promise<Pick<InlineConfig, 'vite'>> {
574-
const vite = await import('vite').catch((err) => {
575-
logger.debug('Failed to import vite:', err);
576-
});
577-
if (vite) {
578-
return {
579-
vite: async (env) => {
580-
const resolvedInlineConfig = (await inlineConfig.vite?.(env)) ?? {};
581-
const resolvedUserConfig = (await userConfig.vite?.(env)) ?? {};
582-
return vite.mergeConfig(resolvedUserConfig, resolvedInlineConfig);
583-
},
584-
};
585-
}
586-
587-
throw Error('Builder not found. Make sure vite is installed.');
569+
): Pick<InlineConfig, 'vite'> {
570+
return {
571+
vite: async (env) => {
572+
const resolvedInlineConfig = (await inlineConfig.vite?.(env)) ?? {};
573+
const resolvedUserConfig = (await userConfig.vite?.(env)) ?? {};
574+
return vite.mergeConfig(resolvedUserConfig, resolvedInlineConfig);
575+
},
576+
};
588577
}
589578

590579
export async function resolveWxtUserModules(

packages/wxt/src/core/runners/web-ext.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { WebExtRunInstance } from 'web-ext-run';
1+
import webExt, { type WebExtRunInstance } from 'web-ext-run';
2+
import * as webExtLogger from 'web-ext-run/util/logger';
3+
24
import { ExtensionRunner } from '../../types';
35
import { formatDuration } from '../utils/time';
46
import defu from 'defu';
@@ -27,7 +29,6 @@ export function createWebExtRunner(): ExtensionRunner {
2729
}
2830

2931
// Use WXT's logger instead of web-ext's built-in one.
30-
const webExtLogger = await import('web-ext-run/util/logger');
3132
webExtLogger.consoleStream.write = ({ level, msg, name }) => {
3233
if (level >= ERROR_LOG_LEVEL) wxt.logger.error(name, msg);
3334
if (level >= WARN_LOG_LEVEL) wxt.logger.warn(msg);
@@ -80,8 +81,7 @@ export function createWebExtRunner(): ExtensionRunner {
8081
wxt.logger.debug('web-ext config:', finalConfig);
8182
wxt.logger.debug('web-ext options:', options);
8283

83-
const webExt = await import('web-ext-run');
84-
runner = await webExt.default.cmd.run(finalConfig, options);
84+
runner = await webExt.cmd.run(finalConfig, options);
8585

8686
const duration = Date.now() - startTime;
8787
wxt.logger.success(`Opened browser in ${formatDuration(duration)}`);

0 commit comments

Comments
 (0)