Bootstrap not applying CSS using Webpack - css

I'm using Bootstrap with webpack, but when I run my app, no Bootstrap and no CSS is applied.
This is my webpack.config.js :
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
// mode: 'production',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
publicPath: '',
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname, './build'),
compress: true,
port: 3000
},
performance: {
maxEntrypointSize: 512000,
maxAssetSize: 512000
},
module: {
rules: [
{
test: /\.s?css$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].css"
}
},
{
loader: "extract-loader"
},
{
loader: "css-loader",
},
{
loader: "sass-loader"
}
]
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ["file-loader"]
},
{
test: /\.(woff(2)?|ttf|otf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}
]
}
]
},
//remove comments from JS files
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
output: {
comments: false,
},
},
}),
new OptimizeCSSAssetsPlugin({
cssProcessorPluginOptions: {
preset: ['default', { discardComments: { removeAll: true } }],
}
})
],
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css"
}),
new ManifestPlugin(),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: path.resolve('./public/index.html'),
}),
]
};
I have this kind of error in brower console :
Uncaught Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js):
CssSyntaxError
(192:1) Unknown word
I don't have an idea where this error comes from.

Below two versions of dev/prod and all the code you can see here webpack-boilerplate
Dev:
{
test: /\.(css|sass|scss)$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
}
],
},
Prod:
{
test: /\.(css|sass|scss)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 2
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},

Related

Webpack Image Path

I coded my site using Webpack. Everything works except the image's path. I'm used to write ../img/test.png for it work. I've researched other questions but none work.
My files are organized inside the dist folder of the wordpress theme.
Here's my webpack.config.js :
module.exports = {
mode: 'development',
entry: [
'./src/index.js'
],
devtool: "source-map", // any "source-map"-like devtool is possible
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
},
module:{
rules:[
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
},
{
test:/\.(s*)css$/,
use: [{
loader: "style-loader", options: {
sourceMap: true
}
}, {
loader: "css-loader", options: {
sourceMap: true
}
}, {
loader: "resolve-url-loader", //resolve-url-loader needs to come *BEFORE* sass-loader
options: {
sourceMap: true
}
},{
loader: "sass-loader", options: {
sourceMap: true
}
}]
}
]
},
plugins: [
new CopyWebpackPlugin([
{from:'assets/images',to:'images'}
])
],
watch: true,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
I've tried other url-loaders but nothing seems to work. Thanks in advance.
I figured it out. The public and output path were the most important things I had to learn. Since this was with Wordpress, the other answers weren't sufficient. The new config looks like:
const webpack = require('webpack');
const path = require('path');
module.exports = {
mode: 'development',
entry: [
'./src/index.js'
],
devtool: "source-map", // any "source-map"-like devtool is possible
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
},
module:{
rules:[
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
},
{
test: /\.(png|jpe?g|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'images',
publicPath: 'wp-content/themes/{{ THEME NAME }}/dist/images',
},
},
],
},
{
test:/\.(s*)css$/,
use: [{
loader: "style-loader", options: {
sourceMap: true
}
}, {
loader: "css-loader", options: {
sourceMap: true
}
}, {
loader: "sass-loader", options: {
sourceMap: true
}
}]
}
]
},
watch: true,
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
Hope this helps for anyone who is working through this.

Webpack don't extract css to general file from vue components from node_modules

When configuring Webpack 4 to work with vue components connected from the node_modules folder, css is not going to the general "app.css" file. But at the same time of the components which I have written is going. How to change the config so that styles from all components are collected in one file?
component connection example
import vSelect from 'vue-select';
Vue.component('v-select', vSelect);
webpack.common.js
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const devMode = process.env.NODE_ENV !== 'production';
module.exports = {
entry: ['./src/js/app.js', './src/sass/app.scss'],
module: {
rules: [{
test: /\.(png|jpeg|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
outputPath: 'resources',
name: '[folder]/[name].[ext]',
}
},
{
test: /\.(eot|ttf|woff|woff2)$/,
loader: 'file-loader',
options: {
outputPath: 'resources/fonts',
name: '[folder]/[name].[ext]',
}
},
{
test: /\.css$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
"postcss-loader",
],
},
{
test: /\.scss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
"postcss-loader",
{
loader: 'sass-loader',
options: {
sourceMap: true
}
},
],
},
{
test: /\.sass$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
"postcss-loader",
{
loader: 'sass-loader',
options: {
indentedSyntax: true,
sourceMap: true
}
},
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
esModule: true,
extractCss: true,
}
},
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/,
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.common.js',
'res': path.join(__dirname, './src/resources'),
'root': path.join(__dirname, './src/js')
},
extensions: ['*', '.js', '.vue', '.json']
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/app.css",
}),
new VueLoaderPlugin()
],
}
webpack.dev.js
const path = require('path');
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
output: {
path: path.resolve(__dirname, './dist'),
filename: 'js/app.js',
publicPath: '/',
chunkFilename: 'js/[name].js'
},
mode: 'development',
devtool: 'inline-source-map',
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
});
webpack.prod.js
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const path = require('path');
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
output: {
path: path.resolve(__dirname, '.././static'),
filename: 'js/app.js',
publicPath: '/static/',
chunkFilename: 'js/[name].js'
},
mode: 'production',
devtool: 'source-map',
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin({}),
new UglifyJsPlugin({
sourceMap: true,
})
]
},
});

Styling differences in Webpack Dev versus Prod

Short Summary:
I've noticed that there are small styling discrepancies between my dev/prod configurations of webpack when testing locally. It seems to have to do with the sass-loader, but it might expand outside of that? I can't be sure because it's very difficult to track down the bug.
(Code at bottom, images just to highlight certain elements)
The Clue
The majority of styles that are missing on dev or overwritten come from my Sass file. Therefore I imagine the culprit is buried somewhere in how I process Sass files on prod.config.js versus dev.config.js. Specifically, I've noticed that variables in Sass like $main-font are not interpreted on dev, but do seem ok on prod config. I've also noticed some weird overrides happening where dev might have a border applied from a set of classes, but production doesn't. Perhaps Minicssextract is doing something differently for prod?
The Problem
In the image below, the left is my production configuration. As you can see it's extracting text into a CSS file which is called in index.html. It's loading my SCSS variables just fine. On the right, I don't use minicssextractloader and all my styles get put into tags and injected. It's switching the font-family over to something that I haven't defined anywhere in my code?
The Loader
On the left is dev, on the right is prod for Sass.
Here's how this font is defined in Sass:
Ideas and tests:
Style-loader: I tried removing postcss, sass-loader, and style-loader on dev form the scss test. Didn't seem to fix the problem.
Is there a chance webpack is doing something funky between dev/production modes?
Perhaps my css compression on prod is making things strange?
Dev.config
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
require('babel-polyfill').default;
const PATHS = {
app: path.join(__dirname, '../src'),
build: path.join(__dirname, '../dist'),
};
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: ['webpack-hot-middleware/client', './src/index'],
mode: 'development',
output: {
publicPath: '/dist/',
filename: 'bundle.js',
},
resolve: {
extensions: ['.jsx', '.js', '.json', '.scss', '.less'],
modules: ['node_modules', PATHS.app],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
'postcss-loader',
],
},
{
test: /\.(sa|sc|c)ss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2,
},
},
'postcss-loader',
'sass-loader',
],
},
{
test: /\.less$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2,
},
},
'postcss-loader',
'less-loader',
},
],
},
{
test: /bootstrap-sass\/assets\/javascripts\//,
use: [
{
loader: 'imports-loader',
options: {
jQuery: 'jquery',
},
},
],
},
{
test: require.resolve('jquery'),
use: [
{
loader: 'expose-loader',
options: '$',
},
{
loader: 'expose-loader',
options: 'jQuery',
},
],
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 50000,
mimetype: 'application/font-woff',
},
},
],
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/octet-stream',
},
},
],
},
{
test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/font-otf',
},
},
],
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
},
],
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'image/svg+xml',
},
},
],
},
{
test: /\.png$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
{
test: /\.jpg$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
{
test: /\.ico$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"development"',
},
__DEVELOPMENT__: true,
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.ProvidePlugin({
jQuery: 'jquery',
}),
],
};
Prod.config
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
require('babel-polyfill').default;
// const PurifyCSSPlugin = require('purifycss-webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const StatsPlugin = require('stats-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const cssnano = require('cssnano');
const PATHS = {
app: path.join(__dirname, '../src'),
build: path.join(__dirname, '../dist'),
};
const pathsToClean = [
'../dist/*.{js,css,eot,woff,woff2,svg,ttf,jpg,map}',
'../dist/index.html',
];
const cleanOptions = {
root: PATHS.build,
exclude: [
'sitemap.xml',
],
dry: false,
};
module.exports = {
// devtool: 'source-map',
entry: ['./src/index'],
mode: 'production',
output: {
publicPath: '/dist/',
chunkFilename: '[name].[chunkhash:4].js',
filename: '[name].[chunkhash:4].js',
},
resolve: {
extensions: ['.jsx', '.js', '.json', '.scss', '.less'],
modules: ['node_modules', PATHS.app],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
'postcss-loader',
],
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
importLoaders: 2,
},
},
'postcss-loader',
'sass-loader',
],
},
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
importLoaders: 2,
},
},
'postcss-loader',
'less-loader',
},
],
},
{
test: /bootstrap-sass\/assets\/javascripts\//,
use: [
{
loader: 'imports-loader',
options: {
jQuery: 'jquery',
},
},
],
},
{
test: require.resolve('jquery'),
use: [
{
loader: 'expose-loader',
options: '$',
},
{
loader: 'expose-loader',
options: 'jQuery',
},
],
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
// test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 50000,
mimetype: 'application/font-woff',
},
},
],
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/octet-stream',
},
},
],
},
{
test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/font-otf',
},
},
],
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
},
],
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'image/svg+xml',
},
},
],
},
{
test: /\.png$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
{
test: /\.jpg$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
{
test: /\.ico$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
],
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
},
__DEVELOPMENT__: false,
}),
new CleanWebpackPlugin(pathsToClean, cleanOptions),
new MiniCssExtractPlugin({
filename: '[name].[contenthash:4].css',
}),
new OptimizeCSSAssetsPlugin({
cssProcessor: cssnano,
cssProcessorOptions: {
options: {
discardComments: {
removeAll: true,
},
// Run cssnano in safe mode to avoid
// potentially unsafe transformations.
safe: true,
},
},
canPrint: false,
}),
new StatsPlugin('stats.json', {
chunkModules: true,
exclude: [/node_modules[\\/]react/],
}),
// new PurifyCSSPlugin({
// paths: glob.sync(PATHS.app),
// }),
],
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
}),
],
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'initial',
},
},
},
runtimeChunk: {
name: 'manifest',
},
},
};
Some of the Sass that seems to be having trouble?
/** 03. Typography **/
$font-source: 'Source Sans Pro', 'Helvetica', 'Arial', sans-serif;
body {
font-size: 1em;
line-height: 1.85em;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-family: $font-source;
color: $color-secondary;
font-weight: 400;
}
Found this old q while looking into why I had a similar problem.
In my case, there was iffy css in a sass file (an opacity value set as a percentage instead of a decimal)
Dev coped with that but prod didn't. I'm still not sure why beyond "prod is more strict" - but correcting the css syntax made it work on prod (of course)

Vue 2 + Webpack: Accessing Assets Folder

I have my assets in the /src/assets/ folder, specifically /src/assets/stylesheets/file.css but I cannot seem to bundle them. In my main.js file I've included the following
require('./assets/stylesheets/file.css')
but it does not work. I see:
Module not found: Error: Can't resolve './assets/stylesheets/codrops.css' in '/Users/USERNAME/Documents/Projects/vue/APPNAME/src'
# ./src/main.js 9:0-43
# multi ./build/dev-client ./src/main.js
I've tried using ../assets and that didn't work either.
Here's my build/webpack.base.conf.js file:
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',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
}
}
EDIT: I've updated this file to look like:
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)
}
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const extractCSS = new ExtractTextPlugin({ filename: 'css.bundle.css' })
const extractSASS = new ExtractTextPlugin({ filename: 'sass.bundle.css' })
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: {
// loaders: [
// { test: /\.css$/, loader: "style-loader!css-loader" }
// ],
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test')]
},
{
test: /\.css$/,
use: extractCSS.extract({ // Instance 1
fallback: 'style-loader!css-loader',
use: [ 'css-loader' ]
})
},
{
test: /\.scss$/,
use: extractSASS.extract({ // Instance 2
fallback: 'style-loader',
use: [ 'css-loader', 'sass-loader' ]
})
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
plugins: [
extractCSS,
extractSASS
]
}
but when I require the .css: require("./assets/now-ui-kit.css"); I see the following:
error in ./src/assets/now-ui-kit.css
Module build failed: ModuleBuildError: Module build failed: Unknown word (5:1)
3 | // load the styles
4 | var content = require("!!../../node_modules/css-loader/index.js?{\"minimize\":false,\"sourceMap\":false}!./now-ui-kit.css");
> 5 | if(typeof content === 'string') content = [[module.id, content, '']];
| ^
6 | if(content.locals) module.exports = content.locals;
7 | // add the styles to the DOM
8 | var update = require("!../../node_modules/vue-style-loader/lib/addStylesClient.js")("94a1c16e", content, false);
It doesn't look like you have any css loader in your webpack config file. Add the following imports, rules and plugins to your file to accomplish the css bundling:
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const extractCSS = new ExtractTextPlugin({ filename: 'css.bundle.css' })
const extractSASS = new ExtractTextPlugin({ filename: 'sass.bundle.css' })
const config = {
module: {
rules: [
{
test: /\.css$/,
use: extractCSS.extract({ // Instance 1
fallback: 'style-loader',
use: [ 'css-loader' ]
})
},
{
test: /\.scss$/,
use: extractSASS.extract({ // Instance 2
fallback: 'style-loader',
use: [ 'css-loader', 'sass-loader' ]
})
}
]
},
plugins: [
extractCSS // Instance 1
extractSASS // Instance 2
]
}
Here is where I took the code snippet from and here is the webpack css documentation.

Webpack erroring on bundling font awesome

I'm having issues with my webpack build (v3), it's a migration from v1 however, it breaks on the importing of the font-awesome file (css) I have on my app.js page. This is the response from webpack:
ERROR in ./node_modules/font-awesome/css/font-awesome.css
Module parse failed: /../node_modules/font-awesome/css/font-awesome.css Unexpected character '#' (7:0)
You may need an appropriate loader to handle this file type.
| /* FONT PATH
| * -------------------------- */
| #font-face {
| font-family: 'FontAwesome';
| src: url('../fonts/fontawesome-webfont.eot?v=4.7.0');
Below is my config file:
const NODE_ENV = process.env.NODE_ENV;
const path = require('path');
const join = path.join;
const resolve = path.resolve;
const root = resolve(__dirname);
const src = join(root, 'src');
const chalk = require('chalk');
const autoprefixer = require('autoprefixer');
const precss = require('precss');
const cssnano = require('cssnano');
const dotenv = require('dotenv');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const _ = require('lodash');
const env = process.env.NODE_ENV || 'development';
const dotEnvVars = dotenv.config();
const environmentEnv = dotenv.config({
path: path.join(root, 'config', `${NODE_ENV}.config.js`),
silent: true,
});
const envVariables = _.merge({}, dotEnvVars, environmentEnv);
const defines = Object.keys(envVariables).reduce(
function (memo, key) {
const val = JSON.stringify(envVariables[key]);
memo[`__${key.toUpperCase()}__`] = val;
return memo;
},
{
__NODE_ENV__: JSON.stringify(NODE_ENV),
__DEBUG__: NODE_ENV === 'development',
}
);
const webpackConfig = {
target: 'web',
devtool: 'cheap-eval-source-map',
entry: {
app: path.resolve('src/app.js'),
},
output: {
path: path.resolve('dist/'),
publicPath: '/',
filename: 'app.js',
},
cache: true,
devServer: {
hot: true,
overlay: true,
compress: true,
lazy: true,
stats: {
assets: true,
chunks: true,
colors: true,
modules: true,
modulesSort: 'field',
publicPath: true,
version: true,
reasons: true,
},
watchOptions: {
ignored: /node_modules/,
},
historyApiFallback: {
disableDotRule: true,
},
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ['es2015', 'stage-0', 'react', 'react-hmre'],
},
},
{
test: /\.css$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{ loader: 'css-loader', query: { sourceMap: true } }, 'postcss-loader'],
}),
},
{
test: /\.(svg|woff|woff2|ttf|eot|otf)(\?v=[a-z0-9]\.[a-z0-9]\.[a-z0-9])?$/,
use: [{ loader: 'file-loader', options: { name: '[name].[ext]' } }],
},
{
test: /\.otf(\?\S*)?$/,
exclude: /node_modules/,
use: [{ loader: 'url-loader', options: { limit: 10000 } }],
},
{
test: /\.eot(\?\S*)?$/,
exclude: /node_modules/,
use: [{ loader: 'url-loader', options: { limit: 10000 } }],
},
{
test: /\.svg(\?\S*)?$/,
exclude: /node_modules/,
use: [{ loader: 'url-loader', options: { mimetype: 'image/svg+xml', limit: 10000 } }],
},
{
test: /\.ttf(\?\S*)?$/,
exclude: /node_modules/,
use: [
{ loader: 'url-loader', options: { mimetype: 'application/octet-stream', limit: 10000 } },
],
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: /node_modules/,
use: [
{
loader: 'url-loader',
options: { mimetype: 'application/font-woff', limit: 10000, name: './[hash].[ext]' },
},
],
},
{
test: /\.(jpe?g|png|gif)$/,
exclude: /node_modules/,
use: [{ loader: 'url-loader', options: { limit: 10000 } }],
},
],
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
}),
new webpack.DefinePlugin(defines),
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
title: 'Privacy Dashboard',
template: 'default.ejs',
inject: true,
hash: true,
}),
new webpack.LoaderOptionsPlugin({
options: {
context: src,
postcss: [precss(), autoprefixer(), cssnano()],
},
}),
new ExtractTextPlugin({ filename: 'app.css', disable: false, allChunks: true }),
],
resolve: {
modules: [path.resolve('src'), 'node_modules'],
extensions: ['.css', '.js', '.jsx'],
alias: {
css: join(src, 'styles'),
containers: join(src, 'containers'),
components: join(src, 'components'),
utils: join(src, 'utils'),
styles: join(src, 'styles'),
demo: join(src, 'demo'),
stores: join(src, 'stores'),
config: join(src, 'config', env),
},
},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
},
performance: {
hints: false,
},
};
module.exports = webpackConfig;

Resources