크로스 플랫폼 노드js는 모듈의 패키징 기능을 달성 할 수 있으며, 방법의 흐름을 통해 zip 및 tar 패키지를 재생할 수 있습니다.
설치
npm install archiver -D
또한 크로스 환경이 아직 설치되어 있지 않은 경우 설치해야 합니다.
npm install cross-env -D
dayjs 플러그인 설치
npm install dayjs -D
구성 파일 추가
루트 디렉터리에 다음 내용으로 새 archiver.config.js 구성 파일을 추가합니다:
/**
* @description
* zip을 빠르게 내보내려면 종속성을 설치해야 합니다. npm 설치 아카이버 -D
* 이 라이브러리에 대한 문서는 https에서 확인할 수 있습니다.://.com/archiverjs/node-archiver
* 패키지에 다음 파일을 추가할 수 있습니다..json npm 실행 빌드에서 script 명령을 구성하고 zip 아카이브를 직접 내보내세요.
* @example 대상 배열에 내보낼 디렉터리 추가 명령줄 실행 노드 export-zip.js
*/
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
const dayjs = require("dayjs");
// 프로덕션 환경
const isProduction = process.env.VUE_APP_NODE_ENV === "production";
// 스플라이스 경로
const resolve = (...dirs) => path.resolve(__dirname, ...dirs);
// 새 경로
const createDir = (path) => {
const pathArr = path.split("/");
let i = 0;
while (pathArr[i]) {
const currentPath = resolve(...pathArr.slice(0, i + 1));
const isExits = fs.existsSync(currentPath);
i++;
if (isExits) {
continue;
}
fs.mkdirSync(currentPath);
}
};
//패키징할 경로 목록을 구성하고 일부 디렉터리를 패키징해야 하는 경우 상대 경로 배열에 추가합니다.
const target = isProduction ? ["dist/"] : ["dist/"];
const outputDir =
"zip/" +
process.env.VUE_APP_NODE_ENV +
"/" +
dayjs().format("YYYY-MM-DD") +
"/" +
dayjs().format("HH-mm-ss");
const outputName = "dist_" + dayjs().unix() + ".zip";
// 경로 생성
createDir(outputDir);
// 이 파일은 기본적으로 현재 디렉토리 경로 dist에 생성됩니다..zip
const outputFullName = resolve(outputDir, outputName);
const output = fs.createWriteStream(outputFullName);
const archive = archiver("zip", {
zlib: { level: 9 } // 압축 수준 설정
});
archive.on("error", function (err) {
console.log("error: ", err);
throw err;
});
output.on("close", function () {
console.log(`
--------- ---------압축 완료 --------- ---------
파일 크기 생성${(archive.pointer() / 1024 / 1024).toFixed(1)}MB
현재 프로젝트 경로에서 dist를 찾습니다..zip 파일, 시스템 경로는${outputFullName}
---------생성 경로 또는 파일 이름을 설정해야 하는 경우 출력--------- 을 설정하세요.
`);
});
archive.pipe(output);
for (i of target) {
archive.directory(i, false);
}
archive.finalize();
구성된 패키지의 제품은 루트 디렉터리에 새 zip 폴더가 생성됩니다:
참고: vue.config.js 파일에 구성된 패키지 출력 파일 이름은 archiver.config.js에 구성된 출력 파일 이름과 일치해야 합니다. 즉, vue.config.js의 outputDir 필드와 archiver.config.js의 대상 변수가 일치해야 합니다. 일관성이 없으면 변경해야 합니다.
예: 출력 디렉토리는 백엔드 파일의 이름으로 구성됩니다:
해당 패키지 제품은 다음과 같습니다:
패키징 명령 구성
package.json 파일에서 구성합니다.
"scripts": {
"serve": "vue-cli-service serve",
"dev": "vue-cli-service serve --mode development",
"build:test": "vue-cli-service build --mode test && cross-env VUE_APP_NODE_ENV=development npm run zip",
"build:prod": "vue-cli-service build --mode production && cross-env VUE_APP_NODE_ENV=production npm run zip",
"lint": "vue-cli-service lint",
"zip": "node archiver.config.js"
},
패키징 제품
npm run build:test를 실행하여 테스트 환경 코드를 패키징합니다.
추가된 zip 파일은 아래에 나열되어 있습니다:
테스트 환경:
프로덕션 환경:





