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

feat: support reproducible builds using SOURCE_DATE_EPOCH #2646

Open
wants to merge 1 commit into
base: v2
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
5 changes: 4 additions & 1 deletion src/core/build/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export async function buildProduction(
// Write .output/nitro.json
const buildInfoPath = resolve(nitro.options.output.dir, "nitro.json");
const buildInfo: NitroBuildInfo = {
date: new Date().toJSON(),
date: (process.env.SOURCE_DATE_EPOCH
? new Date(Number(process.env.SOURCE_DATE_EPOCH) * 1000)
: new Date()
).toJSON(),
preset: nitro.options.preset,
framework: nitro.options.framework,
versions: {
Expand Down
7 changes: 6 additions & 1 deletion src/rollup/plugins/public-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export function publicAssets(nitro: Nitro): Plugin {
const assetData = await fsp.readFile(fullPath);
const etag = createEtag(assetData);
const stat = await fsp.stat(fullPath);
const mtime = (
process.env.SOURCE_DATE_EPOCH
? new Date(Number(process.env.SOURCE_DATE_EPOCH) * 1000)
: stat.mtime
).toJSON();

const assetId = "/" + decodeURIComponent(id);

Expand All @@ -55,7 +60,7 @@ export function publicAssets(nitro: Nitro): Plugin {
type: nitro._prerenderMeta?.[assetId]?.contentType || mimeType,
encoding,
etag,
mtime: stat.mtime.toJSON(),
mtime,
size: stat.size,
path: relative(nitro.options.output.serverDir, fullPath),
data:
Expand Down
4 changes: 3 additions & 1 deletion src/rollup/plugins/server-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export function serverAssets(nitro: Nitro): Plugin {
type += "; charset=utf-8";
}
const etag = createEtag(await fsp.readFile(fsPath));
const mtime = await fsp.stat(fsPath).then((s) => s.mtime.toJSON());
const mtime = process.env.SOURCE_DATE_EPOCH
? new Date(Number(process.env.SOURCE_DATE_EPOCH) * 1000).toJSON()
: await fsp.stat(fsPath).then((s) => s.mtime.toJSON());
assets[id].meta = { type, etag, mtime };
}
}
Expand Down