Skip to content

feat: prevent building public files #117

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions src/devBuilder/devBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default abstract class DevBuilder<
protected inlineScriptHashes = new Set<string>();
protected outDir: string;
protected hmrViteClientUrl = "";
protected publicDir: string;

constructor(
protected viteConfig: ResolvedConfig,
Expand All @@ -29,6 +30,11 @@ export default abstract class DevBuilder<
this.viteConfig.root,
this.viteConfig.build.outDir
);
this.publicDir = path.resolve(
process.cwd(),
this.viteConfig.root,
this.viteConfig.publicDir
);
}

protected abstract updateContentSecurityPolicyForHmr(): void;
Expand Down Expand Up @@ -58,12 +64,7 @@ export default abstract class DevBuilder<
this.hmrViteClientUrl = `${this.hmrServerOrigin}/@vite/client`;

await emptyDir(this.outDir);
const publicDir = path.resolve(
process.cwd(),
this.viteConfig.root,
this.viteConfig.publicDir
);
await copy(publicDir, this.outDir);
await copy(this.publicDir, this.outDir);

await this.writeManifestHtmlFiles(manifestHtmlFiles);
await this.writeManifestContentScriptFiles();
Expand Down Expand Up @@ -177,6 +178,12 @@ export default abstract class DevBuilder<
}

protected async writeManifestScriptFile(fileName: string): Promise<string> {
const publicDirName = path.basename(this.publicDir);
if (fileName.split(path.sep)[0] === publicDirName) {
await copy(fileName, `${this.outDir}/${fileName}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this copy need to happen? Wouldn't all public files have already been copied by line 67?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is necessary because their behaviors are different.
line 67: public/foo.js -> dist/foo.js
this line: public/foo.js -> dist/public/foo.js

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. Line 67 is correct. The contents of public are supposed to be copied to the root of the dist directory not a subdirectory [1] so we wouldn't want this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we include public/foo.js in the content scripts section of the manifest, it will not be found.

return fileName;
}

const outputFileName = getOutputFileName(fileName);

const scriptLoaderFile = getScriptLoaderFile(outputFileName, [
Expand Down