Skip to content

Commit

Permalink
chore: Register global wxt instance (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Feb 4, 2024
1 parent 37ad2c7 commit bb02264
Show file tree
Hide file tree
Showing 55 changed files with 1,691 additions and 1,631 deletions.
7 changes: 4 additions & 3 deletions src/cli/cli-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CAC, Command } from 'cac';
import consola, { LogLevels } from 'consola';
import { getInternalConfig } from '~/core/utils/building';
import { exec } from '~/core/utils/exec';
import { printHeader } from '~/core/utils/log';
import { formatDuration } from '~/core/utils/time';
import { ValidationError } from '~/core/utils/validation';
import { registerWxt } from '~/core/wxt';

/**
* Wrap an action handler to add a timer, error handling, and maybe enable debug mode.
Expand Down Expand Up @@ -74,11 +74,12 @@ export function createAliasedCommand(
.allowUnknownOptions()
.action(async () => {
try {
const config = await getInternalConfig({}, 'build');
await registerWxt('build');

const args = process.argv.slice(
process.argv.indexOf(aliasedCommand.name) + 1,
);
await exec(config, alias, args, {
await exec(alias, args, {
stdio: 'inherit',
});
} catch {
Expand Down
8 changes: 5 additions & 3 deletions src/core/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BuildOutput, InlineConfig } from '~/types';
import { getInternalConfig, internalBuild } from './utils/building';
import { internalBuild } from './utils/building';
import { registerWxt } from './wxt';

/**
* Bundles the extension for production. Returns a promise of the build result. Discovers the `wxt.config.ts` file in
Expand All @@ -15,6 +16,7 @@ import { getInternalConfig, internalBuild } from './utils/building';
* })
*/
export async function build(config?: InlineConfig): Promise<BuildOutput> {
const internalConfig = await getInternalConfig(config ?? {}, 'build');
return await internalBuild(internalConfig);
await registerWxt('build', config);

return await internalBuild();
}
4 changes: 2 additions & 2 deletions src/core/builders/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BuildStepOutput,
Entrypoint,
InlineConfig,
InternalConfig,
ResolvedConfig,
UserConfig,
VirtualEntrypointType,
WxtBuilder,
Expand All @@ -15,7 +15,7 @@ import { getEntrypointBundlePath } from '~/core/utils/entrypoints';
export async function createViteBuilder(
inlineConfig: InlineConfig,
userConfig: UserConfig,
wxtConfig: Omit<InternalConfig, 'builder'>,
wxtConfig: Omit<ResolvedConfig, 'builder'>,
): Promise<WxtBuilder> {
const vite = await import('vite');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Window } from 'happy-dom';
import { pointToDevServer } from '../devHtmlPrerender';
import {
fakeDevServer,
fakeInternalConfig,
fakeResolvedConfig,
} from '~/core/utils/testing/fake-objects';
import { normalizePath } from '~/core/utils/paths';
import { resolve } from 'node:path';
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('Dev HTML Prerender Plugin', () => {
url: 'http://localhost',
});
const root = '/some/root';
const config = fakeInternalConfig({
const config = fakeResolvedConfig({
root,
alias: {
'~local': '.',
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/cssEntrypoints.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as vite from 'vite';
import { Entrypoint, InternalConfig } from '~/types';
import { Entrypoint, ResolvedConfig } from '~/types';
import { getEntrypointBundlePath } from '~/core/utils/entrypoints';

/**
Expand All @@ -14,7 +14,7 @@ import { getEntrypointBundlePath } from '~/core/utils/entrypoints';
*/
export function cssEntrypoints(
entrypoint: Entrypoint,
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.Plugin {
return {
name: 'wxt:css-entrypoint',
Expand Down
6 changes: 3 additions & 3 deletions src/core/builders/vite/plugins/devHtmlPrerender.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as vite from 'vite';
import { InternalConfig, WxtDevServer } from '~/types';
import { ResolvedConfig, WxtDevServer } from '~/types';
import { getEntrypointName } from '~/core/utils/entrypoints';
import { parseHTML } from 'linkedom';
import { dirname, relative, resolve } from 'node:path';
Expand All @@ -12,7 +12,7 @@ let reactRefreshPreamble = '';
* Pre-renders the HTML entrypoints when building the extension to connect to the dev server.
*/
export function devHtmlPrerender(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.PluginOption {
const htmlReloadId = '@wxt/reload-html';
const resolvedHtmlReloadId = resolve(
Expand Down Expand Up @@ -134,7 +134,7 @@ export function devHtmlPrerender(
}

export function pointToDevServer(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
server: WxtDevServer,
id: string,
document: Document,
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/devServerGlobals.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Plugin } from 'vite';
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';

/**
* Defines global constants about the dev server. Helps scripts connect to the server's web socket.
*/
export function devServerGlobals(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): Plugin {
return {
name: 'wxt:dev-server-globals',
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Plugin } from 'vite';
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';
import { fetchCached } from '~/core/utils/network';

/**
Expand All @@ -9,7 +9,7 @@ import { fetchCached } from '~/core/utils/network';
* @example
* import "url:https://google-tagmanager.com/gtag?id=XYZ";
*/
export function download(config: Omit<InternalConfig, 'builder'>): Plugin {
export function download(config: Omit<ResolvedConfig, 'builder'>): Plugin {
return {
name: 'wxt:download',
resolveId(id) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/excludeBrowserPolyfill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';
import type * as vite from 'vite';

/**
Expand All @@ -7,7 +7,7 @@ import type * as vite from 'vite';
* virtual module.
*/
export function excludeBrowserPolyfill(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.Plugin {
const virtualId = 'virtual:wxt-webextension-polyfill-disabled';

Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/globals.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type * as vite from 'vite';
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';
import { getGlobals } from '~/core/utils/globals';

export function globals(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.PluginOption {
return {
name: 'wxt:globals',
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/multipageMove.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as vite from 'vite';
import { Entrypoint, InternalConfig } from '~/types';
import { Entrypoint, ResolvedConfig } from '~/types';
import { dirname, extname, resolve, join } from 'node:path';
import { getEntrypointBundlePath } from '~/core/utils/entrypoints';
import fs, { ensureDir } from 'fs-extra';
Expand All @@ -20,7 +20,7 @@ import { normalizePath } from '~/core/utils/paths';
*/
export function multipageMove(
entrypoints: Entrypoint[],
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.Plugin {
return {
name: 'wxt:multipage-move',
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/tsconfigPaths.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';
import type * as vite from 'vite';

export function tsconfigPaths(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.Plugin {
return {
name: 'wxt:aliases',
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/unimport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUnimport } from 'unimport';
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';
import { getUnimportOptions } from '~/core/utils/unimport';
import type * as vite from 'vite';
import { extname } from 'path';
Expand All @@ -17,7 +17,7 @@ const ENABLED_EXTENSIONS = new Set([
* Inject any global imports defined by unimport
*/
export function unimport(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.PluginOption {
const options = getUnimportOptions(config);
if (options === false) return [];
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/virtualEntrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Plugin } from 'vite';
import { InternalConfig, VirtualEntrypointType } from '~/types';
import { ResolvedConfig, VirtualEntrypointType } from '~/types';
import fs from 'fs-extra';
import { resolve } from 'path';
import { normalizePath } from '~/core/utils/paths';
Expand All @@ -9,7 +9,7 @@ import { normalizePath } from '~/core/utils/paths';
*/
export function virtualEntrypoint(
type: VirtualEntrypointType,
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): Plugin {
const virtualId = `virtual:wxt-${type}?`;
const resolvedVirtualId = `\0${virtualId}`;
Expand Down
4 changes: 2 additions & 2 deletions src/core/builders/vite/plugins/webextensionPolyfillMock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import type * as vite from 'vite';
import { InternalConfig } from '~/types';
import { ResolvedConfig } from '~/types';

/**
* Mock `webextension-polyfill` by inlining all dependencies that import it and adding a custom
Expand All @@ -16,7 +16,7 @@ import { InternalConfig } from '~/types';
* `npm list` and inline them automatically.
*/
export function webextensionPolyfillMock(
config: Omit<InternalConfig, 'builder'>,
config: Omit<ResolvedConfig, 'builder'>,
): vite.PluginOption {
return {
name: 'wxt:testing-inline-deps',
Expand Down
Loading

0 comments on commit bb02264

Please sign in to comment.