Skip to content

Commit

Permalink
feature: add webpack config and util files
Browse files Browse the repository at this point in the history
  • Loading branch information
abuna1985 committed Jan 5, 2023
1 parent 0b4c226 commit 309ad98
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
20 changes: 20 additions & 0 deletions webpack/utils/file-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const path = require('path');
const process = require('process');

function getFilenamesFromPath(workingFolder, extension) {
console.log(process.cwd());
const files = [];
const folderPath = path.join(__dirname, workingFolder);
fs.readdirSync(folderPath)
.forEach(file => {
if( file.match(new RegExp(`.*\.(${extension})`, 'ig'))) {
files.push(path.parse(file).name)
}
})
return files;
}

module.exports = {
getFilenamesFromPath
}
46 changes: 46 additions & 0 deletions webpack/webpack.common.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const { getFilenamesFromPath } = require('./utils/file-utils.js');
const listOfHtmlFiles = getFilenamesFromPath('../../src', 'html');
const entry = listOfHtmlFiles.reduce((entries, fileName) => {
entries[fileName] = path.join(__dirname, `../src/js/${fileName}.js`);
return entries;
}, {});
const htmlGenerators = listOfHtmlFiles.reduce((entries, fileName) => {
entries.push(new HtmlWebpackPlugin({
inject: true,
template: `./src/${fileName}.html`,
filename: `${fileName}.html`,
chunks: [fileName],
}));
return entries;
}, []);

const config ={
entry,
output: {
filename: "[name].js",
path: path.resolve(__dirname, "../dist/"),
},
module: {
rules: [
{
test: /\.html$/,
use: [
{
loader: 'html-loader'
}
]
},
]
},
plugins: [
new CleanWebpackPlugin(),
...htmlGenerators,
],

}

module.exports = config
48 changes: 48 additions & 0 deletions webpack/webpack.dev.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const common = require('./webpack.common.config');
const { merge } = require('webpack-merge');
const path = require('path');

module.exports = merge(common, {
output: {
filename: 'js/[name].js'
},
mode: 'development',
devServer: {
port: 9000,
static: {
directory: path.resolve(__dirname, '../dist')
},
devMiddleware: {
index: 'index.html',
writeToDisk: true
},
client: {
overlay: true
},
liveReload: false
},
module: {
rules:[
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.scss$/,
use: [ 'style-loader', 'css-loader', 'sass-loader' ],
},
{
test: /\.(png|jpg|svg)$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10 * 1024,
}
},
generator: {
filename: './images/[name][ext]'
}
}
]
}
})
63 changes: 63 additions & 0 deletions webpack/webpack.prod.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const common = require('./webpack.common.config');
const { merge } = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const glob = require('glob');
const { PurgeCSSPlugin } = require('purgecss-webpack-plugin')


module.exports = merge(common, {
mode: 'production',
output: {
filename: 'js/[name].[contenthash:10].js'
},
module: {
rules:[
{
test: /\.css$/,
use: [ MiniCssExtractPlugin.loader, 'css-loader' ]
},
{
test: /\.scss$/,
use: [ MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader' ],
},
{
test: /\.(png|jpg|svg)$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10 * 1024,
}
},
generator: {
filename: './images/[name].[contenthash:10][ext]'
},
use: [
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
quality: 40
},
pngquant: {
quality: [0.65, 0.90],
speed: 4
}
}
}
]
}
]
},
plugins:[
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:10].css'
}),
new PurgeCSSPlugin({
paths: glob.sync(
`${path.join(__dirname, '../src')}/**/*`,
{ nodir: true }
)
})
]
})

0 comments on commit 309ad98

Please sign in to comment.