11/* global process */
22import { promises as fs } from "fs" ;
3- import { createWriteStream } from "fs" ;
4- import JSZip from "jszip" ;
3+ import { ZipWriter } from "web-jszipp" ;
54import ChromeExtension from "crx" ;
65import { execSync } from "child_process" ;
76import manifest from "../src/manifest.json" with { type : "json" } ;
@@ -16,12 +15,14 @@ const PACK_FIREFOX = false;
1615
1716// ============================================================================
1817
19- const createJSZip = ( ) => {
20- const currDate = new Date ( ) ;
21- const dateWithOffset = new Date ( currDate . getTime ( ) - currDate . getTimezoneOffset ( ) * 60000 ) ;
22- // replace the default date with dateWithOffset
23- JSZip . defaults . date = dateWithOffset ;
24- return new JSZip ( ) ;
18+ const zipMtime = new Date ( ) ;
19+
20+ const addZipFile = async ( zip , path , content ) => {
21+ await zip . add ( {
22+ path,
23+ data : content ,
24+ meta : { modifiedAt : zipMtime } ,
25+ } ) ;
2526} ;
2627
2728// 判断是否为beta版本
@@ -113,8 +114,8 @@ firefoxManifest.optional_permissions = firefoxManifest.optional_permissions?.fil
113114 ( permission ) => permission !== "background"
114115) ;
115116
116- const chrome = createJSZip ( ) ;
117- const firefox = createJSZip ( ) ;
117+ const chrome = new ZipWriter ( { outputAs : "uint8array" } ) ;
118+ const firefox = new ZipWriter ( { outputAs : "uint8array" } ) ;
118119
119120async function addDir ( zip , localDir , toDir , filters ) {
120121 const sub = async ( localDir , toDir ) => {
@@ -129,38 +130,26 @@ async function addDir(zip, localDir, toDir, filters) {
129130 if ( stats . isDirectory ( ) ) {
130131 await sub ( localPath , `${ toPath } /` ) ;
131132 } else {
132- zip . file ( toPath , await fs . readFile ( localPath ) ) ;
133+ await addZipFile ( zip , toPath , await fs . readFile ( localPath ) ) ;
133134 }
134135 }
135136 } ;
136137 await sub ( localDir , toDir ) ;
137138}
138139
139- chrome . file ( "manifest.json" , JSON . stringify ( chromeManifest ) ) ;
140- firefox . file ( "manifest.json" , JSON . stringify ( firefoxManifest ) ) ;
140+ await addZipFile ( chrome , "manifest.json" , JSON . stringify ( chromeManifest ) ) ;
141+ await addZipFile ( firefox , "manifest.json" , JSON . stringify ( firefoxManifest ) ) ;
141142
142143await Promise . all ( [
143144 addDir ( chrome , "./dist/ext" , "" , [ "manifest.json" ] ) ,
144145 addDir ( firefox , "./dist/ext" , "" , [ "manifest.json" ] ) ,
145146] ) ;
146147
147148// 导出zip包
148- chrome
149- . generateNodeStream ( {
150- type : "nodebuffer" ,
151- streamFiles : true ,
152- compression : "DEFLATE" ,
153- } )
154- . pipe ( createWriteStream ( `./dist/${ packageInfo . name } -v${ packageInfo . version } -chrome.zip` ) ) ;
149+ await fs . writeFile ( `./dist/${ packageInfo . name } -v${ packageInfo . version } -chrome.zip` , await chrome . close ( ) ) ;
155150
156151PACK_FIREFOX &&
157- firefox
158- . generateNodeStream ( {
159- type : "nodebuffer" ,
160- streamFiles : true ,
161- compression : "DEFLATE" ,
162- } )
163- . pipe ( createWriteStream ( `./dist/${ packageInfo . name } -v${ packageInfo . version } -firefox.zip` ) ) ;
152+ ( await fs . writeFile ( `./dist/${ packageInfo . name } -v${ packageInfo . version } -firefox.zip` , await firefox . close ( ) ) ) ;
164153
165154// 处理crx
166155const crx = new ChromeExtension ( {
0 commit comments