Skip to content

Instantly share code, notes, and snippets.

@olygood
Last active April 1, 2022 11:06
Show Gist options
  • Save olygood/47fea439997932115af19d2f3e6d9553 to your computer and use it in GitHub Desktop.
Save olygood/47fea439997932115af19d2f3e6d9553 to your computer and use it in GitHub Desktop.
webpack css/ image/
// terminal : install webpack : npm i webpack-cli webpack-dev-server babel-loader
// puis npm i html-webpack-plugin
Upgrade your account for access to GitLens+ features on both public and private repos.
//ne gère que le html css scss images webpack-dev-server
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
module.exports = {
mode: "production",
entry: path.resolve(__dirname, "./src/index.js"),
output: {
path: path.resolve(__dirname, "./dist"),
filename: "main.js",
},
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
minimize: true,
},
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
{
test: /\.(csv|tsv)$/i,
use: ['csv-loader'],
},
{
test: /\.xml$/i,
use: ['xml-loader'],
},
{
test: /\.css/i,
use: [MiniCssExtractPlugin.loader,"css-loader"],
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
MiniCssExtractPlugin.loader,
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader",
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "./src/index.html"),
filename: "index.html",
}),
new MiniCssExtractPlugin({
filename: "style.css",
})
],
};
// In HtmlWebpackPlugin insert : chunks: ["main", "topbar"]
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
entry: {
main: path.join(__dirname, "src/index.js"),
form: path.join(__dirname, "src/form/form.js"),
topbar: path.join(__dirname, "src/assets/javascript/topbar.js"),
},
output: {
path: path.join(__dirname, "dist"),
filename: "[name].bundle.js"
},
module: {
rules: [
{
test: /\.js/,
exclude: /(node_modules)/,
use: ["babel-loader"]
},
{
test: /\.scss$/i,
use: ["style-loader", "css-loader", "sass-loader"]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: './src/assets/images/*',
to: 'assets/images/[name][ext]',
},
],
}),
new HtmlWebpackPlugin({
filename: "index.html",
template: path.join(__dirname, "./src/index.html"),
chunks: ["main", "topbar"]
}),
new HtmlWebpackPlugin({
filename: "form.html",
template: path.join(__dirname, "./src/form/form.html"),
chunks: ["form", "topbar"]
})
],
stats: "minimal",
devtool: "source-map",
mode: "development",
devServer: {
open: false,
static: path.resolve(__dirname, './dist'),
port: 4000
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment