const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');
const webpackBaseConfig = require('./webpack.base.config');
const getEntry = require('./getEntry');
const writeFile = require('./writeFile');
const config = require('../new-config.json');
let entries = getEntry();
let libEntries = getEntry('lib');
let targetPath = webpackBaseConfig.output.path;
createHtml();
let pageCompiler = webpack({
...webpackBaseConfig,
entry: entries
});
let libCompiler = webpack({
...webpackBaseConfig,
entry: libEntries
});
libCompiler.run(function(err, stat) {
if(err) {
console.log(err);
}
let server = new webpackDevServer(pageCompiler, {
contentBase: path.join(__dirname, '../dist'),
proxy: config.proxy,
stats: 'errors-only'
});
server.listen(3006, '', () => {
console.log('open localhost:3006');
});
});
function createHtml() {
try {
let template = fs.readFileSync(path.join(__dirname, '../index.html'), {
encoding: 'utf8'
});
let publicPath = webpackBaseConfig.output.publicPath;
Object.keys(entries).map((filePath) => {
let htmlPath = filePath + '.html';
let cssPath = path.join(publicPath, filePath) + '.css';
let jsPath = path.join(publicPath, filePath) + '.js';
let content = template;
let $ = cheerio.load(content);
$('head').append(``);
$('body').append(``)
writeFile(targetPath, htmlPath, $.html());
});
}
catch(e) {
console.log(e);
}
}