Skip to content

Commit

Permalink
fix: Allow adding multiple hyphens in an entrypoint name (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Aug 30, 2024
1 parent 0bd94fc commit 61d54bd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/wxt/e2e/tests/output-structure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe('Output Directory Structure', () => {
.toMatchInlineSnapshot(`
".output/chrome-mv3/background.js
----------------------------------------
var _background = function() {
var background = function() {
"use strict";
var _a, _b;
function defineBackground(arg) {
Expand All @@ -372,13 +372,13 @@ describe('Output Directory Structure', () => {
function logHello(name) {
console.log(\`Hello \${name}!\`);
}
_background;
background;
const definition = defineBackground({
main() {
logHello("background");
}
});
_background;
background;
function initPlugins() {
}
// @ts-expect-error
Expand Down Expand Up @@ -411,7 +411,7 @@ describe('Output Directory Structure', () => {
const result$1 = result;
return result$1;
}();
_background;
background;
"
`);
});
Expand Down
1 change: 1 addition & 0 deletions packages/wxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"picocolors": "^1.0.1",
"prompts": "^2.4.2",
"publish-browser-extension": "^2.1.3",
"scule": "^1.3.0",
"unimport": "^3.9.1",
"vite": "^5.3.5",
"vite-node": "^2.0.4",
Expand Down
5 changes: 3 additions & 2 deletions packages/wxt/src/core/utils/__tests__/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ describe('String utils', () => {

describe('safeVarName', () => {
it.each([
['Hello world!', '_hello_world'],
['Hello world!', 'helloWorld'],
['123', '_123'],
['abc-123', '_abc_123'],
['abc-123', 'abc123'],
['abc-123-xyz', 'abc123Xyz'],
['', '_'],
[' ', '_'],
['_', '_'],
Expand Down
15 changes: 13 additions & 2 deletions packages/wxt/src/core/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { camelCase } from 'scule';

export function kebabCaseAlphanumeric(str: string): string {
return str
.toLowerCase()
Expand All @@ -9,8 +11,17 @@ export function kebabCaseAlphanumeric(str: string): string {
* Return a safe variable name for a given string.
*/
export function safeVarName(str: string): string {
// _ prefix to ensure it doesn't start with a number
return '_' + kebabCaseAlphanumeric(str.trim()).replace('-', '_');
const name = camelCase(kebabCaseAlphanumeric(str));
if (name.match(/^[a-z]/)) return name;
// _ prefix to ensure it doesn't start with a number or other invalid symbol
return '_' + name;
}

/**
* Converts a string to a valid filename (NOT path), stripping out invalid characters.
*/
export function safeFilename(str: string): string {
return kebabCaseAlphanumeric(str);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions packages/wxt/src/core/zip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InlineConfig } from '../types';
import path from 'node:path';
import fs from 'fs-extra';
import { kebabCaseAlphanumeric } from './utils/strings';
import { safeFilename } from './utils/strings';
import { getPackageJson } from './utils/package';
import { minimatch } from 'minimatch';
import { formatDuration } from './utils/time';
Expand All @@ -27,9 +27,7 @@ export async function zip(config?: InlineConfig): Promise<string[]> {

const projectName =
wxt.config.zip.name ??
kebabCaseAlphanumeric(
(await getPackageJson())?.name || path.dirname(process.cwd()),
);
safeFilename((await getPackageJson())?.name || path.dirname(process.cwd()));
const applyTemplate = (template: string): string =>
template
.replaceAll('{{name}}', projectName)
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61d54bd

Please sign in to comment.