I think I am Webpacking wrong. In my Webpack config I have a module rule to load files when they are encountered in the scss files, and I am also copying over a ton of other images using the CopyPlugin.
The problem is that when I run Webpack in development with "watch" set to true, the file-loader will delete everything in the dist folder but then copy over the images that are referenced in the SCSS files. So everything else that was copied via the CopyPlugin gets deleted.
Should I be handling the images referenced in my SCSS/JS a different way. Ideally I would be ignoring all the references to these things in the the SCSS/JS and just copy the files manually via the CopyPlugin
Here is my config:
module.exports = {
mode: 'development',
watch: true,
entry: {
frontend: './_src/entry.js',
},
output: {
filename: 'js/[name].js',
path: path.resolve(__dirname, paths.distRoot),
},
module: {
rules: [
// js
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
},
},
},
// css
{
test: /\.css$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
{ loader: 'css-loader' },
],
},
// scss
{
test: /\.scss$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
// css
{
loader: 'css-loader',
options: { sourceMap: true },
},
// autoprefix things
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
sourceMap: true,
plugins: [
autoprefixer({ browsers: ['last 2 version'] }),
],
},
},
// sass
{
loader: 'sass-loader',
options: {
includePaths: [
paths.npmRoot + '/font-awesome',
paths.srcRoot + '/scss',
paths.npmRoot,
],
},
},
],
},
// fonts
{
test: /\.(woff(2)?|ttf|eot|svg|otf)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts',
publicPath: '../fonts',
},
},
// images
{
test: /\.(jpg|png|svg|gif)$/,
loader: 'url-loader',
options: {
limit: 1,
name: '[name].[ext]',
outputPath: 'img',
publicPath: '../img',
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
new webpack.HotModuleReplacementPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
new CopyPlugin([
{
from: path.resolve(__dirname, paths.srcRoot + '/img'),
to: path.resolve(__dirname, paths.distRoot + '/img'),
copyUnmodified: true,
},
]),
new (webpack.optimize.OccurenceOrderPlugin || webpack.optimize.OccurrenceOrderPlugin)(),
],
};