webpack 4, bundle glyphicons - css

I have issues understanding how to bundle glyphicons using webpack.
Here is my webpack file
module.exports = {
context: path.join(__dirname, "app"),
devtool: 'source-map',
entry: {
main: ["./main.coffee"],
vendor: ["./vendor.coffee"]
},
resolve: {
extensions: [".coffee", ".js", ".json", ".css", ".less", ".html"]
},
output: {
path: path.join(__dirname, 'wwwroot'),
filename: '[name].js',
chunkFilename: '[id].chunk.js',
},
module: {
rules: [
{
test: /\.coffee$/,
use: ['coffee-loader'],
exclude: /node_modules/
},
{
test: /\.less$/i,
use: css.extract({
fallback: "style-loader",
use: ['css-loader', 'less-loader']
}),
exclude: /node_modules/
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000'
}
]
},
plugins: [
css,
new cleanWebpackPlugin(["./wwwroot"])
],
optimization: {
splitChunks: {
minSize: 2,
name: true
}
}
}
here is the output I get from webpack
Child extract-text-webpack-plugin ../node_modules/extract-text-webpack-plugin/dist ../node_modules/css-loader/index.js!../node_modules/less-loader/dist/cjs.js!app.less:
[0] ../Content/fonts/glyphicons-halflings-regular.eot 26.5 KiB {0} [built]
[1] ../Content/images/nice_snow.png 23.9 KiB {0} [built]
[2] ../Content/fonts/glyphicons-halflings-regular.svg 82.2 KiB {0} [built]
[3] ../Content/fonts/glyphicons-halflings-regular.ttf 53.7 KiB {0} [built]
[4] ../Content/fonts/glyphicons-halflings-regular.woff 30.4 KiB {0} [built]
[7] ../node_modules/css-loader!../node_modules/less-loader/dist/cjs.js!./app.less 139 KiB {0} [built]
+ 2 hidden modules
here is how i import glyphicons (it resolves path fine, you can tell by webpack output message)
#font-face {
font-family: 'Glyphicons Halflings';
src: url('../#{icon-font-path}#{icon-font-name}.eot');
src: url('../#{icon-font-path}#{icon-font-name}.eot?#iefix') format('embedded-opentype'),
url('../#{icon-font-path}#{icon-font-name}.woff') format('woff'),
url('../#{icon-font-path}#{icon-font-name}.ttf') format('truetype'),
url('../#{icon-font-path}#{icon-font-name}.svg#glyphicons-halflingsregular') format('svg');
}
Finally here is a folder structure i get for the output
- wwwroot
1. app.css
2. app.css.map
3. main.js
4. main.js.map
5. vendor.js
6. vendor.js.map
Now, everything working as expected. all java script is bundled and css as well. only glyphicons are not showing up.

Related

Why is my webpack config not compiling my css?

i am trying to compile my css via webpack, it gives me a succes on my javascript file but doesnt say anything about my css file
this is my webpack config
`import path from 'path';
import webpack from 'webpack';
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
import loader from 'sass-loader';
const extractSASS = new MiniCssExtractPlugin.default({filename: "app.min.css"});
const jQuery = new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
});
export default {
mode: "production",
entry: {
main: [
"./src/js/index.js"
],
},
output: {
filename: "app.min.js",
path: path.resolve("./../wwwroot/dist")
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
],
test: /\.scss$/,
use: [
{
loader: "postcss-loader",
options: {
config: {
path: path.resolve(import.meta.url, "postcss.config.js")
}
}
},
{
loader: "sass-loader"
}
]
},
{
test: /\.(png|jpe?g|gif)$/,
use: "file-loader?name=images/[name].[contenthash].[ext]"
}
]
},
plugins: [
extractSASS,
jQuery
]
};`
this is the output of npm run build
npm run build
> cms3#1.0.0 build
> webpack --mode=production
asset app.min.js 169 KiB [compared for emit] [minimized] (name: main) 1 related asset
runtime modules 500 bytes 2 modules
cacheable modules 504 KiB
./src/js/index.js 88 bytes [built] [code generated]
./node_modules/bootstrap/dist/js/bootstrap.js 134 KiB [built] [code generated]
./node_modules/jquery/dist/jquery.js 283 KiB [built] [code generated]
./node_modules/popper.js/dist/esm/popper.js 86.4 KiB [built] [code generated]
webpack 5.75.0 compiled successfully in 3469 ms
Most likely webpack thinks that no CSS files are needed in your build.
You need to tell webpack by adding an import line in your index.js file:
import "./path/to/my.scss";

Webpack 5 with SASS loader and MiniCSSExtractPlugin doesn't move fonts

I'm fairly new to Webpack 5, while my build is almost working, my fonts are not being transfered to my /dist folder. I include the fonts in my main.scss file (see below)
Am I missing something with my current config?
folder structure
- src
- assets
- sass
- fonts
src/assets/sass/main.scss
#font-face {
font-family: 'P22MackinacMedium';
src: url('../fonts/p22-mackinac-medium.eot');
src: url('../fonts/p22-mackinac-medium.eot?#iefix') format('embedded-opentype'),
url('../fonts/p22-mackinac-medium.woff2') format('woff2'),
url('../fonts/p22-mackinac-medium.woff') format('woff'),
url('../fonts/p22-mackinac-medium.ttf') format('truetype'),
url('../fonts/p22-mackinac-medium.svg#youworkforthem') format('svg');
font-weight: normal;
font-style: normal;
}
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
mode: 'development',
entry: './src/index.js',
plugins: [new MiniCssExtractPlugin(), new HtmlWebpackPlugin({
template: 'src/index.html'
})],
output: {
filename: '[name]-[contenthash].js',
chunkFilename: '[name]-[chunkhash].js',
path: path.resolve(__dirname, 'dist'),
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
}
},
module: {
rules: [
{
test: /\.css$/,
use: [
// Creates `style` nodes from JS strings
"style-loader",
// Translates CSS into CommonJS
"css-loader",
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader, {
loader: 'css-loader',
options: { url: false }
},
'sass-loader'
],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},
devServer: {
static: './dist',
},
};
As far as I can see this should be working out of the box, like you wrote it, since webpack 5 is using new Assets management. But, since it is not working for you, you can still try to use url-loader. Like this:
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
use: {
loader: 'url-loader',
},
},
Or you could try 'asset/inline' before installing url loader.

Webpack + postcss not purging Tailwind

I'm trying to use webpack 5 and tailwind 2. The logs suggest that most things are set up correctly, but no purging is taking place.
I run NODE_ENV=production webpack --config webpack.prod.js
I have the following webpack configuration
mode: 'production',
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/i,
use: ['style-loader', 'css-loader', {
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
require("tailwindcss")("./tailwind.config.js"),
require("autoprefixer"),
],
},
}
}, "sass-loader"],
}
tailwind.config.js
module.exports = {
purge: {
mode: 'layers',
layers: [],
content: ['./src/*.elm', './src/**/*.elm'],
},
theme: {
extend: {}
},
variants: {},
plugins: []
};
My production builds are still yielding 4mb bundles which tells me that no purging has taken place. I think I struggled before and switched to putting the options in a separate postcss.config.js but that shouldn't always be necessary?
In the command line logging I see
cacheable modules 4.14 MiB (javascript) 7.64 KiB (asset)
modules by path ./src/ 4.13 MiB (javascript) 7.64 KiB (asset)
modules by path ./src/*.scss 1.34 KiB
./src/styles.scss 439 bytes [built] [code generated]
./node_modules/css-loader/dist/cjs.js!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[0].use[2]!./node_modules/sass-loader/dist/cjs.js!./src/styles.scss 930 bytes [built] [code generated]
./src/index.js + 1 modules 754 bytes [built] [code generated]
./src/Main.elm 159 KiB [built] [code generated]
./node_modules/css-loader/dist/cjs.js!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[0].use[2]!./node_modules/sass-loader/dist/cjs.js!./src/tailwind.css 3.98 MiB [built] [code generated]
Try this:
module.exports = {
purge: {
mode: 'layers',
enabled: true, // This fixed it for me
layers: [],
content: ['./src/*.elm', './src/**/*.elm'],
},
theme: {
extend: {}
},
variants: {},
plugins: []
};

Webpack: Failed to decode downloaded font

Using FontFaceObserver and getting a webpack error.
Failed to decode downloaded font
OTS parsing error: invalid version tag
Webpack config:
var path = require('path')
var utils = require('./utils')
var config = require('../config')
var vueLoaderConfig = require('./vue-loader.conf')
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'#': resolve('src'),
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
query: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
query: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
},
{
test: /\.mp4$/,
loader: 'url-loader',
query: {
limit: 10000,
name: utils.assetsPath('videos/[name].[hash:7].[ext]')
}
}
]
}
}
At the top of App.vue stylesheet
#font-face {
font-family: Pragmatica;
font-weight: bold;
src: url('/static/fonts/PragmaticaCond-Extrabold.woff') format('woff');
}
#font-face {
font-family: Pragmatica;
src: url('/static/fonts/Pragmatica-Book.woff') format('woff');
}
My fonts sit within static/fonts/.
Any ideas?
It was a combination of a couple things. first was the path to the font file, I was setting the path as if there was no “build” happening, Webpack was putting them under /assetsafter release build so I had to update my path from /static/fonts to /fonts as after the build static files are automagically looked under assets folder + adding regex to support versioning in my webpack loader config + adding mimetype for woff files

Using local web fonts with webpack

I'm trying to use some local web fonts in my React project. I'm including them in my main.scss file, and loading them via webpack. The bundle builds correctly, including main.scss styles. I see webpack load the font files, and copy them to public/fonts/, but my bundle file can't find the fonts.
As I understand it, your #font-face src should be in relation to where bundle will be. I'm setting this to the same path that I load the fonts with in webpack, './fonts/'. The exact error I'm seeing is:
file:///Users/myusername/Documents/folder1/folder2/folder3/APP/fonts/FoundersGroteskWeb-Regular.woff net::ERR_FILE_NOT_FOUND
I've been trying a lot of different path configurations, and using the publicPath option in webpack, but I'm going in circles at this point over what seems like a really simple reference error.
File Structure:
APP
├──webpack.config.js
├──src
├──app
├──App.jsx
├──styles
├──main.scss
├──fonts
├──allthefonts.woff
├──public
├──bundle.js
├──fonts
├──allthefonts.woff
App.jsx:
require('./styles/main.scss');
main.scss:
#font-face {
font-family: FoundersGrotesk;
src: url('./fonts/FoundersGroteskWeb-Bold.eot') format('eot'),
url('./fonts/FoundersGroteskWeb-Bold.woff') format('woff'),
url('./fonts/FoundersGroteskWeb-Bold.woff2') format('woff2');
font-weight: bold;
}
#font-face {
font-family: FoundersGrotesk_Cnd;
src: url('./fonts/FoundersGrotXCondWeb-Bold.eot') format('eot'),
url('./fonts/FoundersGrotXCondWeb-Bold.woff') format('woff'),
url('./fonts/FoundersGrotXCondWeb-Bold.woff2') format('woff2');
font-weight: bold;
}
#font-face {
font-family: FoundersGrotesk;
src: url('./fonts/FoundersGroteskWeb-Regular.eot') format('eot'),
url('./fonts/FoundersGroteskWeb-Regular.woff') format('woff'),
url('./fonts/FoundersGroteskWeb-Regular.woff2') format('woff2');
font-weight: normal;
}
webpack.config.js:
'use strict';
const webpack = require('webpack');
const PROD = JSON.parse(process.env.PROD_ENV || '0');
module.exports = {
entry: './src/app/App.jsx',
output: {
path: './src/public/',
filename: PROD ? 'bundle.min.js' : 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: '/node_modules/',
query: {
presets: ['es2015', 'react', 'stage-1']
}
},
{
test: /\.s?css$/,
loaders: ['style', 'css', 'sass']
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=./fonts/[name].[ext]'
}
]
}
};
Got a working solution thanks to #omerts in this thread. Solution involved using publicPath. I had been trying to use it as an option in module.exports with the fonts file-loader, not the output.
Updated webpack.config.js:
const webpack = require('webpack');
const PROD = JSON.parse(process.env.PROD_ENV || '0');
const path = require('path');
const PATHS = {
build: path.join(__dirname, './src/public')
};
module.exports = {
entry: './src/app/App.jsx',
output: {
path: PATHS.build,
filename: PROD ? 'bundle.min.js' : 'bundle.js',
publicPath: PATHS.build
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: '/node_modules/',
query: {
presets: ['es2015', 'react', 'stage-1']
}
},
{
test: /\.s?css$/,
loaders: ['style', 'css', 'sass']
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
},
{
test: /\.(jpg|png)$/,
loader: 'file-loader?name=/fonts/[name].[ext]'
}
]
},
plugins: PROD ? [
new webpack.optimize.UglifyJsPlugin({
beautify: false,
comments: false,
compress: {
warnings: false,
drop_console: true
},
mangle: {
except: ['$'],
screw_ie8: true,
keep_fnames: false
}
})
] : []
};
a better approach would be to use 'url-loader' and add the following line in loaders.
{
test: /\.(jpe?g|png|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'url-loader?limit=100000'
}
Is it possible to just always reference the original fonts? They don't appear to get changed by file-loader anyways.
File structure
APP
├───build
│ │ build.js
│ │ build.min.js
│ │
│ └───fonts
│ allthefonts.woff
│
├───css
│ main.css
│
├───fonts
│ allthefonts.woff
│
└───js
main.js
main.css
#font-face {
font-family: All-The-Fonts;
src: url('../fonts/allthefonts.woff') format('woff');
}
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
...
output: {
path: path.resolve(__dirname, 'build'),
filename: "[name].js",
globalObject: 'this'
},
module: {
rules: [
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './fonts/' //dont actually use these fonts but still need to process them
}
}]
}
]
},
...
};
I got it working thanks to this article: https://www.robinwieruch.de/webpack-font

Resources