Skip to content
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"eslint-linter-browserify": "9.26.0",
"eventemitter3": "^5.0.1",
"fast-xml-parser": "^5.5.8",
"fflate": "^0.8.3",
"i18next": "^23.16.4",
"monaco-editor": "^0.52.2",
"react": "^18.3.1",
Expand Down Expand Up @@ -86,7 +87,6 @@
"husky": "^9.1.7",
"iconv-lite": "^0.7.2",
"jsdom": "^26.1.0",
"jszip": "^3.10.1",
"mock-xmlhttprequest": "^8.4.1",
"postcss": "^8.5.6",
"postcss-loader": "^8.2.0",
Expand Down
13 changes: 6 additions & 7 deletions packages/filesystem/zip/rw.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { JSZipObject } from "jszip";
import type { JSZipFileOptions, JSZipFile } from "@App/pkg/utils/jszip-x";
import type { JSZipFileOptions, JSZipFile, JSZipObject } from "@App/pkg/utils/jszip-x";
import type { FileCreateOptions, FileReader, FileWriter } from "../filesystem";

export class ZipFileReader implements FileReader {
Expand Down Expand Up @@ -29,13 +28,13 @@ export class ZipFileWriter implements FileWriter {
}
}

async write(content: string): Promise<void> {
async write(content: string | Blob): Promise<void> {
const opts = {} as JSZipFileOptions;
if (this.modifiedDate) {
const date = new Date(this.modifiedDate);
const dateWithOffset = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
opts.date = dateWithOffset;
opts.date = new Date(this.modifiedDate);
// fflate does not require timezone adjustment to UTC Date
}
this.zip.file(this.path, content, opts);
const fileData = typeof content === "string" ? content : new Uint8Array(await content.arrayBuffer());
this.zip.file(this.path, fileData, opts);
}
}
3 changes: 1 addition & 2 deletions packages/filesystem/zip/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export default class ZipFileSystem implements FileSystem {
const files: FileInfo[] = [];
for (const [filename, jsZipObject] of Object.entries(this.zip.files)) {
const date = jsZipObject.date; // the last modification date
const dateWithOffset = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
const lastModificationDate = dateWithOffset.getTime();
const lastModificationDate = date.getTime();
files.push({
name: filename,
path: filename,
Expand Down
43 changes: 8 additions & 35 deletions pnpm-lock.yaml

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

39 changes: 12 additions & 27 deletions scripts/pack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* global process */
import { promises as fs } from "fs";
import { createWriteStream } from "fs";
import JSZip from "jszip";
import { strToU8, zipSync } from "fflate";
import ChromeExtension from "crx";
import { execSync } from "child_process";
import manifest from "../src/manifest.json" with { type: "json" };
Expand All @@ -16,12 +15,10 @@ const PACK_FIREFOX = false;

// ============================================================================

const createJSZip = () => {
const currDate = new Date();
const dateWithOffset = new Date(currDate.getTime() - currDate.getTimezoneOffset() * 60000);
// replace the default date with dateWithOffset
JSZip.defaults.date = dateWithOffset;
return new JSZip();
const zipMtime = new Date();

const addZipFile = (zip, path, content) => {
zip[path] = [typeof content === "string" ? strToU8(content) : content, { mtime: zipMtime }];
};

// 判断是否为beta版本
Expand Down Expand Up @@ -113,8 +110,8 @@ firefoxManifest.optional_permissions = firefoxManifest.optional_permissions?.fil
(permission) => permission !== "background"
);

const chrome = createJSZip();
const firefox = createJSZip();
const chrome = {};
const firefox = {};

async function addDir(zip, localDir, toDir, filters) {
const sub = async (localDir, toDir) => {
Expand All @@ -129,38 +126,26 @@ async function addDir(zip, localDir, toDir, filters) {
if (stats.isDirectory()) {
await sub(localPath, `${toPath}/`);
} else {
zip.file(toPath, await fs.readFile(localPath));
addZipFile(zip, toPath, await fs.readFile(localPath));
}
}
};
await sub(localDir, toDir);
}

chrome.file("manifest.json", JSON.stringify(chromeManifest));
firefox.file("manifest.json", JSON.stringify(firefoxManifest));
addZipFile(chrome, "manifest.json", JSON.stringify(chromeManifest));
addZipFile(firefox, "manifest.json", JSON.stringify(firefoxManifest));

await Promise.all([
addDir(chrome, "./dist/ext", "", ["manifest.json"]),
addDir(firefox, "./dist/ext", "", ["manifest.json"]),
]);

// 导出zip包
chrome
.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
compression: "DEFLATE",
})
.pipe(createWriteStream(`./dist/${packageInfo.name}-v${packageInfo.version}-chrome.zip`));
await fs.writeFile(`./dist/${packageInfo.name}-v${packageInfo.version}-chrome.zip`, zipSync(chrome));

PACK_FIREFOX &&
firefox
.generateNodeStream({
type: "nodebuffer",
streamFiles: true,
compression: "DEFLATE",
})
.pipe(createWriteStream(`./dist/${packageInfo.name}-v${packageInfo.version}-firefox.zip`));
(await fs.writeFile(`./dist/${packageInfo.name}-v${packageInfo.version}-firefox.zip`, zipSync(firefox)));

// 处理crx
const crx = new ChromeExtension({
Expand Down
Loading
Loading