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";
Related
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: []
};
I am and have always been bad with webpack, but this time I am beaten...
Thats sounds easy : I just want to import my apply my CSS.
He is my project structure :
/src
|_assets/
| |_index.html
|_components/
| |_App.js
| |_WelcomeLogo.js
|_styles/
| |_welcomeLogo.css
|_index.js
So, index.js imports App.js, which import WelcomeLogo.js, and js is rendered properly.
WelcomeLogo.js import welcomeLogo.css : import './../styles/welcomeLogo.css';
And here is my webpackConfig :
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');
const autoprefixer = require('autoprefixer');
module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.js|jsx$/,
exclude: [
/node_modules/,
/tests/
],
use: { loader: 'babel-loader' }
},
{
test: /\.css$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer()]
}
}
]
}
]
},
output: {
path: path.resolve(__dirname, './dist'),
filename: process.env.production ? `index.[chunkHash].js` : `index.[hash].js`
},
mode: 'development',
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 3030,
open: true
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'BananaHammock.io',
template: 'src/assets/index.html',
meta: {
'http-equiv': 'cache-control',
'content': 'no-cache'
}
}),
]
};
So the issue is, no CSS is applied to my output App...
It is the first time to not manage to do that, and feel kind of lost.
Thanks for your help.
Webpack version : 4.41.2
npm run build shows that the css is found, and parsed... But its content is not a part of the bundle :
-> % npm run build
> xxxxxx#1.0.0 build /Users/xxxx/Documents/workspace/javascript/xxxx.io
> webpack --mode production
Hash: 480520f961b41a464f93
Version: webpack 4.41.2
Time: 2137ms
Built at: 03/17/2020 9:19:56 PM
Asset Size Chunks Chunk Names
index.480520f961b41a464f93.js 134 KiB 0 [emitted] [immutable] main
index.html 822 bytes [emitted]
Entrypoint main = index.480520f961b41a464f93.js
[7] ./src/index.js 184 bytes {0} [built]
[12] (webpack)/buildin/harmony-module.js 573 bytes {0} [built]
[15] ./src/styles/welcomeLogo.css 573 bytes {0} [built]
[17] ./node_modules/css-loader/dist/cjs.js!./src/styles/welcomeLogo.css 235 bytes {0} [built]
+ 16 hidden modules
Child html-webpack-plugin for "index.html":
1 asset
Entrypoint undefined = index.html
[0] ./node_modules/html-webpack-plugin/lib/loader.js!./src/assets/index.html 927 bytes {0} [built]
[2] (webpack)/buildin/global.js 472 bytes {0} [built]
[3] (webpack)/buildin/module.js 497 bytes {0} [built]
+ 1 hidden module
If I use MiniCssExtractPlugin, it generates a css file... But empty.
And my welcomeLogo.css file isn't empty, and is correct according to W3C validator...
I think the css importation path is wrong. it should be '../styles/welcomeLogo.css'.
TL;DR: Is there any way to automatically scan or automatically import all stylesheets used by our web packages, from our node_modules folder?
I am working on a project that uses angularJS as its MVC framework, and we are currently in the process of migrating to use webpack. I currently wrote the following configuration:
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const bbProperties = require('./webpack.properties');
const appRoot = __dirname + "/../../..";
console.log('__dirname:', __dirname);
module.exports = {
entry: {
app: appRoot + bbProperties.app.entryScript,
login: appRoot + bbProperties.login.entryScript
},
output: {
path: appRoot + bbProperties.distFolder,
publicPath: "dist",
filename: "js/[name].bundle.js",
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2017']
}
},
{
test: /\.css$/,
//exclude: /node_modules/,
use: [
{loader: 'style-loader'},
{loader: 'css-loader', query: {importLoaders: 1}},
{loader: 'postcss-loader', query: {config: {path: __dirname+'/postcss.config.js'}}}
]
},
{
test: /\.(png|jpg|jpeg|gif)$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name:'[name].[ext]',
outputPath:"/assets/images/"
}
}
]
},
{
test: /\.(ttf|eot|woff|woff2|svg)($|\?.*$)/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name:'[name].[ext]',
outputPath:"/assets/fonts/"
}
}
]
},
]
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
moment: "moment"/*,
_: "lodash",
"window._": "lodash",
angular: "angular"*/
}),
//Creates an html file with all bundles injected as <script> tags
new HtmlWebpackPlugin({
chunks: ['app'],
filename: '../angular-app/menu/' + bbProperties.app.outputFile,
hash: "true", //cache busting
template: appRoot + bbProperties.app.location + "/" + bbProperties.app.inputFile
}),
//Second html bundle for login page
new HtmlWebpackPlugin({
chunks: ['login'],
filename: '../'+bbProperties.login.outputFile,
hash: "true", //cache busting
template: appRoot + bbProperties.login.location + "/" + bbProperties.login.inputFile
})
],
stats: {colors: true},
devtool: 'source-map',
node: {
fs: "empty"
}
};
As far as I understand, webpack will start creating a bundle from each entry file, adding any file it encounters in import statements recursively.
I also understand that I can require a package from my node_modules directory, and that in such a case webpack will search for a package.json file and import the file specified in the 'main' property of that file.
Lets say we are dependant on several packages, components and frameworks (for example: angular-ui-grid, angular-material etc...), I know I can manually import their CSS files using css loaders by importing them into my JS code.
Is there any way to automatically scan or automatically import all stylesheets used by those packages, from our node_modules folder?
I'm using the tutorial here as a base, but I haven't any examples of how to import css files into an app. I've found bits and pieces but not enough to know how to do it. I've added "import styles from './styles/main.css';" into App.jsx and my webpack config is below.
var css = require("css!./main.css");
var config = {
entry: './main.js',
output: {
path:'./',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.css$/,
loader: "style!css!"
}
]
}
}
module.exports = config;`
I get the error:
./styles/main.css
Module parse failed: C:\Projects\reactApp\styles\main.css Unexpected token (1:5)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:5)
That file only has div {} right now.
In your webpack config you only need to add style-loader and css-loader (you need to npm install it first):
{ test: /\.css$/, loader: 'style-loader!css-loader' },
Then you can require the CSS in your .js files (components):
require('./styles/main.css');
I'm trying to compile all my .scss files but webpack doesn't seem to be doing it, even though I've declared style-loader & sass-loader in my webpack config file.
I don't see why the .scss files wouldn't compile? I've followed a guide from the following blog: http://www.jonathan-petitcolas.com/2015/05/15/howto-setup-webpack-on-es6-react-application-with-sass.html
Loaders:
loaders: [
// Working loaders...
{test: /\.json$/, loaders: ["json"]},
{test: /\.(ico|gif|png|jpg|jpeg|svg|webp)$/, loaders: ["file?context=static&name=/[path][name].[ext]"], exclude: /node_modules/},
{test: /\.js$/, loaders: ["babel?presets[]=es2015&presets[]=stage-0&presets[]=react"], exclude: /node_modules/},
// Non working loaders...
// ALSO TRIED THIS -> { test: /\.scss$/,loader: ExtractTextPlugin.extract( "style", 'css!sass')}
{ test: /\.css$/, loader: ExtractTextPlugin.extract(
"style-loader",
"sass-loader?sourceMap",
{
publicPath: "../dist"
}
)},
],
style.css is never generated anywhere.
Plugins:
plugins: [
new ExtractTextPlugin("style.css", {
disable: false,
allChunks: true
}),
]
Also tried:
The following was copied from the blog I linked above.
Loaders:
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css!sass')
}
Plugins:
new ExtractTextPlugin('public/style.css', {
allChunks: true
})
Other webpack configs:
Here are my other configurations. Pretty standard and do the job for everything except the .scss!
target: "node",
cache: false,
context: __dirname,
debug: false,
devtool: "source-map",
entry: ["../src/server"],
output: {
path: path.join(__dirname, "../dist"),
filename: "server.js"
},
externals: [nodeExternals({
whitelist: ["webpack/hot/poll?1000"]
})],
resolve: {
modulesDirectories: [
"src",
"src/components",
"src/containers",
"node_modules",
"static"
],
extensions: ["", ".json", ".js"]
},
node: {
__dirname: true,
fs: "empty"
}
Here is what my webpack terminal is printing out after I run it:
Waiting for dist/*.js (max 30 seconds)
[2] http://localhost:8080/webpack-dev-server/
[2] webpack result is served from http://localhost:8080/dist
[2] content is served from /Users/james/apps/react-server-side/server
[0] Ready. dist/*.js changed
[1] Hash: 12a5c90bd2564cd8880d
[1] Version: webpack 1.12.14
[1] Time: 15448ms
[1] Asset Size Chunks Chunk Names
[1] server.js 1.37 MB 0 [emitted] main
[1] server.js.map 1.23 MB 0 [emitted] main
[1] [0] multi main 40 bytes {0} [built]
[1] + 659 hidden modules
Does anyone have any ideas to what is going on?!
Edit
Forgot to add that I'm doing this server side!
Are you requiring the scss files in your js files? You have to do that.
Also, you don't need the ExtractTextPlugin, a config as the following should work:
loaders: [
// ...
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
}
]