// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// combineJS 모듈 정의
function combineJS(){
console.log('combineJS 모듈');
}
위와 같이 코드를 입력하고 저장합니다.
// mergeScript.js 파일 소스 내용
// 사용자 정의 모듈 'modules/combineJS.js' 호출
var combineJS = require('./modules/combineJS.js');
// combineJS 모듈 사용
combineJS();
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// combineJS 모듈 정의
function combineJS(){
console.log('combineJS 모듈');
}
// combineJS 모듈 외부로 출력 (Exports)
module.exports = combineJS;
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// combineJS 모듈 외부로 출력 (Exports)
module.exports = function (){
console.log('combineJS 모듈');
};
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// File System 내장 모듈 호출
var fs = require('fs');
/*
* combineJS 모듈 정의 및 외부로 출력
* 모듈 내부에 전달받을 인자(배열, 문자열) 설정
*/
module.exports = function(jsSrc, exportJs){
console.log(jsSrc, exportJs);
}
// mergeScript.js 파일 소스 내용
// 사용자 정의 모듈 'modules/combineJS.js' 호출
var combineJS = require('./modules/combineJS.js');
/*
* combineJS 모듈 사용
* 전달인자 1 : 병합하고자 하는 JS 파일 리스트(배열)
* 전달인자 2 : 병합되어 생성된 파일 경로(문자열)
* */
combineJS(['./readFile.js', './writeFile.js'], './jsCombine.js');
node mergeScript.js
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// File System 내장 모듈 호출
var fs = require('fs');
/*
* combineJS 모듈 정의 및 외부로 출력
* 모듈 내부에 전달받을 인자(배열, 문자열) 설정
*/
module.exports = function(jsSrc, exportJs){
// 배열 jsSrc를 개별적으로 접근 조작하기 위해
// forEach 배열 메소드 사용
jsSrc.forEach(function(file, index){
console.log(file, index);
});
}
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// File System 내장 모듈 호출
var fs = require('fs');
/*
* combineJS 모듈 정의 및 외부로 출력
* 모듈 내부에 전달받을 인자(배열, 문자열) 설정
*/
module.exports = function(jsSrc, exportJs){
// 배열 jsSrc를 개별적으로 접근 조작하기 위해
// forEach 배열 메소드 사용
jsSrc.forEach(function(file, index){
var content = fs.readFileSync(file);
console.log('콘텐츠: \n' + content);
});
}
각 file의 경로로 문서 내용을 읽어오는 코드를 추가로 입력합니다.
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// File System 내장 모듈 호출
var fs = require('fs');
/*
* combineJS 모듈 정의 및 외부로 출력
* 모듈 내부에 전달받을 인자(배열, 문자열) 설정
*/
module.exports = function(jsSrc, exportJs){
var mergeCode = '';
// 배열 jsSrc를 개별적으로 접근 조작하기 위해
// forEach 배열 메소드 사용
jsSrc.forEach(function(file, index){
// fs.readFileSync() 사용하여 파일 내용 읽기
mergeCode += fs.readFileSync(file);
});
console.log(mergeCode);
}
// combineJS.js 파일 소스 내용
/* ! combineJS module @ hyungju-lee, 2020 */
// File System 내장 모듈 호출
var fs = require('fs');
/*
* combineJS 모듈 정의 및 외부로 출력
* 모듈 내부에 전달받을 인자(배열, 문자열) 설정
*/
module.exports = function(jsSrc, exportJs){
var mergeCode = '';
// 배열 jsSrc를 개별적으로 접근 조작하기 위해
// forEach 배열 메소드 사용
jsSrc.forEach(function(file, index){
// fs.readFileSync() 사용하여 파일 내용 읽기
mergeCode += fs.readFileSync(file);
});
fs.writeFileSync(exportJs, mergeCode);
}