1
votes

i am learning webpack rightnow, and i have some trouble with the magic, that is done by webpack.

I have to set the path for the font awesome fonts to the node_modules folder, but after the compile process it is wrong.

Sass File:

$fa-font-path: "~@fortawesome/fontawesome-free/webfonts/";
@import "~@fortawesome/fontawesome-free/scss/fontawesome.scss";
@import "~@fortawesome/fontawesome-free/scss/regular.scss";

I have to put the $fa-font-path var to the current position of the fonts in node_modules/ folder, else webpack cannot find the fonts and throws an error.

My CSS File gets exported to: "App/Resources/Public/Css/main.css" and the webpack magic changes the $fa-font-path to the outputPath i choosed for fonts "Fonts/"

@font-face {
  font-family: 'Font Awesome 5 Free';
  font-style: normal;
  font-weight: 400;
  src: url(Fonts/fa-regular-400.eot);
  src: url(Fonts/fa-regular-400.eot?#iefix) format("embedded-opentype"), url(Fonts/fa-regular-400.woff2) format("woff2"), url(Fonts/fa-regular-400.woff) format("woff"), url(Fonts/fa-regular-400.ttf) format("truetype"), url(Fonts/fa-regular-400.svg#fontawesome) format("svg"); }

But what it should be, is "../Fonts" because the Css and Fonts folder are on the same level.

My webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');

const config = {
  src: './App/Resources/Private/',
  dist: './App/Resources/Public/'
};

module.exports = {
    entry: { main: config.src + 'Javascript/app.js' },
    output: {
        path: path.resolve(__dirname, config.dist),
        filename: 'Javascript/[name].[contenthash].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.scss$/,
                use:  [  'style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
            },
            {
                test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'Fonts/'
                    }
                }]
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(config.dist, {} ),
        new MiniCssExtractPlugin({
            filename: 'Css/[name].[hash].css',
            chunkFilename: 'Css/[id].[hash].css'
        }),
        new HtmlWebpackPlugin({
            inject: true,
            hash: true,
            template: config.src + 'Html/index.html',
            filename: 'index.html'
        })
    ]
};

package.json

{
  "name": "bootstrapwebpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build:prod": "webpack --mode production",
    "build:dev": "webpack --mode development"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.4.1"
  },
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/preset-env": "^7.1.0",
    "autoprefixer": "^9.2.1",
    "babel-loader": "^8.0.4",
    "clean-webpack-plugin": "^0.1.19",
    "css-loader": "^1.0.0",
    "file-loader": "^2.0.0",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.4.4",
    "node-sass": "^4.9.4",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.21.0",
    "webpack-cli": "^3.1.2"
  }
}

The solutions i found here and in the documentations did not work for me. Maybe someone of you can help :) Git Repo for good messure: https://gitlab.com/szmedia/bootstrapwebpack

1

1 Answers

3
votes

Problem solved:

Just add outputPath and publicPath to your file-loader like this:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');

const config = {
  src: './App/Resources/Private/',
  dist: './App/Resources/Public/'
};

module.exports = {
    entry: {
        main: config.src + 'Javascript/app.js' ,
        sass: config.src + 'Scss/app.scss'
    },
    output: {
        path: path.resolve(__dirname, config.dist),
        filename: 'Javascript/[name].[contenthash].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.scss$/,
                use:  [  'style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
            },
            {
                test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'Fonts/',
                        publicPath: '../Fonts/'
                    }
                }]
            }
        ]
    },
    resolve: {
        alias: {
            jquery: "jquery/src/jquery"
        }
    },
    plugins: [
        new CleanWebpackPlugin(config.dist, {} ),
        new MiniCssExtractPlugin({
            filename: 'Css/[name].[hash].css',
            chunkFilename: 'Css/[id].[hash].css'
        }),
        new HtmlWebpackPlugin({
            inject: true,
            hash: true,
            template: config.src + 'Html/index.html',
            filename: 'index.html'
        })
    ]
};