Skip to content

Commit

Permalink
feat(modules): Allow adding generated public files (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 authored Jun 24, 2024
1 parent b15a933 commit c0a0edb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
11 changes: 10 additions & 1 deletion packages/wxt/e2e/tests/modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ describe('Module Helpers', () => {
export default defineWxtModule((wxt) => {
addPublicAssets(wxt, "${normalizePath(dir)}")
})
wxt.hooks.hook("build:publicAssets", (_, assets) => {
assets.push({
relativeDest: "example/generated.txt",
contents: "",
});
});
});
`,
);

Expand All @@ -114,6 +120,9 @@ describe('Module Helpers', () => {
await expect(
project.fileExists('.output/chrome-mv3/module.txt'),
).resolves.toBe(true);
await expect(
project.fileExists('.output/chrome-mv3/example/generated.txt'),
).resolves.toBe(true);
});

it("should not overwrite the user's public files", async () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/wxt/src/core/utils/building/build-entrypoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ async function copyPublicDirectory(): Promise<BuildOutput['publicAssets']> {
if (files.length === 0) return [];

const publicAssets: BuildOutput['publicAssets'] = [];
for (const { absoluteSrc, relativeDest } of files) {
const absoluteDest = resolve(wxt.config.outDir, relativeDest);
for (const file of files) {
const absoluteDest = resolve(wxt.config.outDir, file.relativeDest);

await fs.ensureDir(dirname(absoluteDest));
await fs.copyFile(absoluteSrc, absoluteDest);
if ('absoluteSrc' in file) {
await fs.copyFile(file.absoluteSrc, absoluteDest);
} else {
await fs.writeFile(absoluteDest, file.contents, 'utf8');
}
publicAssets.push({
type: 'asset',
fileName: relativeDest,
fileName: file.relativeDest,
});
}

Expand Down
22 changes: 17 additions & 5 deletions packages/wxt/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1351,19 +1351,31 @@ export interface WxtModuleWithMetadata<TOptions extends WxtModuleOptions>
id: string;
}

export interface ResolvedPublicFile {
export type ResolvedPublicFile = CopiedPublicFile | GeneratedPublicFile;

export interface ResolvedBasePublicFile {
/**
* The relative path in the output directory to copy the file to.
* @example
* "content-scripts/base-styles.css"
*/
relativeDest: string;
}

export interface CopiedPublicFile extends ResolvedBasePublicFile {
/**
* The absolute path to the file that will be copied to the output directory.
* @example
* "/path/to/any/file.css"
*/
absoluteSrc: string;
}

export interface GeneratedPublicFile extends ResolvedBasePublicFile {
/**
* The relative path in the output directory to copy the file to.
* @example
* "content-scripts/base-styles.css"
* Text to write to the file.
*/
relativeDest: string;
contents: string;
}

export type WxtPlugin = () => void;
Expand Down

0 comments on commit c0a0edb

Please sign in to comment.