Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌟 Implemented a way for users to define the output entrypoint name #1304

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/wxt/src/core/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export async function resolveConfig(
userModules,
plugins: [],
...moduleOptions,
entrypointNames: mergedConfig.entrypointNames ?? {},
};
}

Expand Down
18 changes: 17 additions & 1 deletion packages/wxt/src/core/utils/entrypoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../types';
import path, { relative, resolve, extname } from 'node:path';
import { normalizePath } from './paths';
import { wxt } from '../wxt';

export function getEntrypointName(
entrypointsDir: string,
Expand All @@ -23,7 +24,22 @@ export function getEntrypointOutputFile(
entrypoint: Entrypoint,
ext: string,
): string {
return resolve(entrypoint.outputDir, `${entrypoint.name}${ext}`);
const pattern = wxt.config.entrypointNames[entrypoint.name];
if (!pattern)
return resolve(entrypoint.outputDir, `${entrypoint.name}${ext}`);

return resolve(
entrypoint.outputDir,
pattern
.replace(/\[name]/g, entrypoint.name)
.replace(/\[hash:(\d+)]/g, (_, length) =>
Array(Number(length))
.fill(0)
.map(() => Math.floor(Math.random() * 16).toString(16))
.join(''),
)
.replace(/\[ext]/g, ext),
);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions packages/wxt/src/core/utils/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ export async function generateManifest(
);
}
const version = wxt.config.manifest.version ?? simplifyVersion(versionName);
const { author, name, description, shortName } = pkg;

const baseManifest: Manifest.WebExtensionManifest = {
manifest_version: wxt.config.manifestVersion,
name: pkg?.name,
description: pkg?.description,
version,
short_name: pkg?.shortName,
author,
name,
description,
short_name: shortName,
icons: discoverIcons(buildOutput),
};
const userManifest = wxt.config.manifest;
Expand Down
62 changes: 47 additions & 15 deletions packages/wxt/src/core/utils/package.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
import { resolve } from 'node:path';
import { resolve, dirname } from 'node:path';
import fs from 'fs-extra';
import { wxt } from '../wxt';

type PackageJson = {
name: string;
version?: string;
description: string;
author?: string;
main: string;
scripts?: Record<string, string>;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
shortName?: string;
[key: string]: unknown;
};

/**
* Recursively searches for package.json up directory tree
*/
const findPackageJson = async (dir: string): Promise<string | undefined> => {
const path = resolve(dir, 'package.json');
try {
await fs.access(path);
return path;
} catch {
const parent = dirname(dir);
return parent === dir ? undefined : findPackageJson(parent);
}
};

/**
* Read the project's package.json.
*
* TODO: look in root and up directories until it's found
* Reads and returns package.json contents, searching up directories if needed
*/
export async function getPackageJson(): Promise<
Partial<Record<string, any>> | undefined
> {
const file = resolve(wxt.config.root, 'package.json');
export const getPackageJson = async (): Promise<PackageJson> => {
try {
return await fs.readJson(file);
const path = await findPackageJson(wxt.config.root);
return path ? await fs.readJson(path) : {};
} catch (err) {
wxt.logger.debug(
`Failed to read package.json at: ${file}. Returning undefined.`,
err,
);
return {};
wxt.logger.debug('Failed to read package.json', err);
return {
name: '',
version: '',
description: '',
author: '',
main: '',
types: '',
scripts: {},
dependencies: {},
devDependencies: {},
peerDependencies: {},
};
}
}
};
1 change: 1 addition & 0 deletions packages/wxt/src/core/utils/testing/fake-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export const fakeResolvedConfig = fakeObjectCreator<ResolvedConfig>(() => {
hooks: {},
vite: () => ({}),
plugins: [],
entrypointNames: {},
};
});

Expand Down
17 changes: 17 additions & 0 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ export interface InlineConfig {
* "wxt-module-analytics").
*/
modules?: string[];
/**
* User defined names for entrypoints output files.
*
* The keys are the entrypoint names, and the values are the output file names.
*
* The following tokens are supported:
* - `[name]` - The entrypoint name
* - `[hash:length]` - A random hash of the specified length
* - `[ext]` - The original extension of the entrypoint
*
* @example
* {
* "background": "[name]-[hash:8][ext] // -> background-f7e9d120.js,
* }
*/
entrypointNames?: Record<string, string>;
}

// TODO: Extract to @wxt/vite-builder and use module augmentation to include the vite field
Expand Down Expand Up @@ -1418,6 +1434,7 @@ export interface ResolvedConfig {
* ["@wxt-dev/module-vue/plugin", "wxt-module-google-analytics/plugin"]
*/
plugins: string[];
entrypointNames: Record<string, string>;
}

export interface FsCache {
Expand Down