我對webpack並不完全滿意,所以我寫了自己的小腳本。隨意使用它。
將做到以下幾點:
- 觀看源路徑定義的文件夾。
- 如果它檢測到任何在源文件夾中的文件的變化,將讀取的所有文件源文件夾 ,並將其寫入到文件destination.js
要執行,只要執行節點腳本名。 js
/* jshint esversion: 6 */
'use strict';
const watch = require('node-watch');
let allFilesPath = [];
const pathDestination = './path/to/destination.js';
const source = 'source/folder/';
const fs = require('fs');
init();
function init() {
fs.truncate(pathDestination, 0, function() {
console.log('delete everything in', pathDestination);
});
const allFiles = fs.readdirSync(source);
allFiles.forEach(function(element) {
allFilesPath.push(source + element);
});
allFiles.forEach(function(element) {
getFileContent(element);
});
}
function getFileContent(file) {
fs.readFile(source + file, 'utf8', function(err, data) {
if (err) {
return console.log(err);
} else {
console.log(source + file + ' was added to ' + pathDestination);
addToBundle(data);
}
});
}
function addToBundle(code) {
fs.appendFile(pathDestination, code, function(err) {
if (err) {
console.log(err);
}
});
}
watch(allFilesPath, function(evt, filename) {
console.log(filename, 'changed');
init();
console.log();
});
這看起來很有趣。讓我深入瞭解這一點 – User12547645