How to base64 images using webpack? - image

I am using https://github.com/wbkd/webpack-starter for my project
I do have svg and png images that referenced in my html file like <img src="/public/image.png"/>
Instead of loading image using src path I want to inject image as base64 content to my html file to improve the page performance and to minify the number of server requests.
So I wonder how would you do that?
UPDATE:
here is what I did, but that does not work
npm install url-loader --save-dev
and add this configuration to production config:
module: {
rules: [
{
test: /\.(png|jpg|gif|svg)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: false,
},
},
],
},
]
}
I tried to play with limit values but won't help.
Any ideas ?
UPDATE:
that is my current rules config, but nothing works, I also installed html-loader but no effect:
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.s?css/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: false,
},
},
],
},
{
test: /\.html$/i,
loader: 'html-loader',
},
],

Here's a few steps you should follow to sort your problem out:
Add html-loader to your webpack.config.prod.js:
webpack.config.prod.js
{
test: /\.html$/i,
loader: 'html-loader',
},
Fix your src of your <img /> to use the relative path instead of absolute path which doesn't work:
<img src="../public/image.png"/> // this is incorrect `/public/image.png`
Finally, add a publicPath to output in webpack.common.js to make it work properly with html-webpack-plugin:
webpack.common.js
{
output: {
// ...
publicPath: '/',
},
}

Make sure to set url-loader litmit to true. According to their docs, setting it to true is the default option and there's no limit to a file's size to be transformed into base64.
Setting it to false will not transform images to base64.
module: {
rules: [
{
test: /\.(png|jpg|gif|svg)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: true,
},
},
],
},
]
}
Also, in order for images to be handled by webpack, you need to import them.
import MyImage from '../public/image.png'

Related

How to copy sitemap.xml to build folder with React & Webpack?

I have a sitemap.xml file in public folder. When I build the React application, the sitemap.xml file is not present in the dist/build folder.
What Webpack configuration is needed to achieve that? How does robots.txt need to be set up?
Here is the webpack-config for serving a file to build folder witha a custom name
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
},
],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(?:ico|gif|png|jpg|jpeg|xml)$/i,
type: 'asset/resource',
},
{
test: /\.xml/,
type: 'asset/resource',
generator: {
filename: 'sitemap.xml',
},
},
{
test: /\.txt/,
type: 'asset/resource',
generator: {
filename: 'robots.txt',
},
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
type: 'asset/inline',
},
],
},
Finally make sure you import that file inside your react application or else it will not be served to build folder

How to enable and force Autoprefixer with Next.js?

I'm trying to enable vendor prefixes with Next.js, but they're not working. I'm using SCSS modules, and tried also with normal CSS but this is not working.
EDIT: I'm using a custom PostCSS file:
module.exports = {
plugins: [
"postcss-flexbugs-fixes",
[
"postcss-preset-env",
{
autoprefixer: {
flexbox: true,
grid: "autoplace",
},
stage: 3,
features: {
"custom-properties": true,
},
},
],
],
};
You will just need to adjust the browserslist in your package.json.
{
"browserslist":[
......
]
}
Try Visiting here for more info in browserslist:
https://github.com/browserslist/browserslist

Gatsby doesn't build images to public

I'm making a basic site with gatsby, but can't get images to work.
In markdown content/projects/askj/index.md file I have:
![livechat](./livechat.png)
and I placed an image also into that directory.
In gatsby-config I have:
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/content`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 590,
},
},
'gatsby-remark-prismjs',
'gatsby-remark-copy-linked-files',
],
},
},
but the images do not end up getting copied into the /public rendered version of the site.
I've tried various other combinations of paths and options, but I think I'm missing something.
I did put some png files into /images and they get copied across and can be ref'd, but I want to put the images into each directory with the respective content markdown.
Thanks for ideas!
refs:
- https://codebushi.com/gatsby-featured-images/
- https://www.orangejellyfish.com/blog/a-comprehensive-guide-to-images-in-gatsby/
- https://www.gatsbyjs.org/packages/gatsby-remark-images/?=remark

Webpack sass to css specify a specific folder?

I've been looking over the docs and checking other people's questions but I can't find the simple answer to how to compile all my sass down to a simple css file and specify the directory I want the resulting css file to output to.
For quick context:
I have a public directory with a stylesheets directory and a build directory in it. webpack compiles the app into build, and I'd like to have the sass compile style.css into the stylesheets directory.
Here's a screenshot of my public directory:
public dir img
I'd like to be able to do something like this in my webpack.config.js (only showing pertinent code for brevity):
const ExtractTextPlugin = require('extract-text-webpack-plugin');
...
// To be called in plugins:
const cssOutput = new ExtractTextPlugin('./public/stylesheets/style.css');
inside module loaders:
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader'),
},
In plugins:
plugins: [
.
.
.
cssOutput,
],
I'd like to be able to access the output file with this line in my index.html file located in the public directory:
<link rel="stylesheet" href="/stylesheets/style.css" />
I'm currently doing this using gulp and it works fine, I'm just trying to transition everything into webpack. Any help would be greatly appreciated!
Turns out you can just set the output file like this:
const cssOutput = new ExtractTextPlugin('../stylesheets/style.css', { allChunks: true });
I made the noob mistake of forgetting to add:
require('_scss/style.scss');
in my index.jsx file.
For anyone who runs into this issue, I still had trouble with fonts and images, so inside module loaders in the webpack.config.js I had to add:
{
test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif)(\?\S*)?$/,
loader: 'file',
},
and since this output everything into my build directory, I just changed the css to output everything in the build directory as well to prevent path errors. I changed it to this:
const cssOutput = new ExtractTextPlugin('style.css', { allChunks: true });
Hopefully this helps someone else who runs into this type of issue!
Work for Me::
webpack.config.js
module.exports = {
entry: ['./src/app.ts', './src/sass/style.scss'],
module:{
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
include: [path.resolve(__dirname, 'src', 'src/class')]
},
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: '/css/[name].css'
}
},
{
loader: 'extract-loader'
},
{
loader: 'css-loader?-url'
},
{
loader: 'postcss-loader'
},
{
loader: 'sass-loader'
}
],
include: [path.resolve(__dirname, 'src/sass')]
}
]
},
output: {
filename: 'js/app.js',
path: path.resolve(__dirname, 'public')
}
}
postcss.config.js
module.exports = {
plugins: {
'autoprefixer': {}
}
}
package.json
{
"name": "tsscript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"nov": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^9.8.5",
"css": "^3.0.0",
"css-loader": "^3.6.0",
"extract-loader": "^5.1.0",
"postcss-loader": "^3.0.0",
"sass": "^1.26.10",
"sass-loader": "^9.0.2",
"style-loader": "^1.2.1",
"ts-loader": "^8.0.0",
"typescript": "^3.9.6",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12"
}
}
Directory Snap:
Have you take a look at https://www.npmjs.com/package/extract-text-webpack-plugin?
you'll probably need it.
Recent solution, you can use mini-css-extract-plugin, css-loader, and sass-loader. mini-css-extract-plugin can create a CSS file per JS file which contains CSS.
Install dependencies:
npm i -D mini-css-extract-plugin css-loader sass-loader
For example, your main JS file that includes a style is ./src/index.js:
...
import './src/style.scss';
...
Configure webpack.config.js:
...
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
...
entry: {
main: './src/index.js',
},
plugins: [
new MiniCssExtractPlugin(),
],
...
module: {
rules: [
...
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader",
],
},
...
]
},
resolve: {
modules: ["node_modules", path.resolve(__dirname, "src")],
extensions: ['.js', '.scss', ...]
}
}
Then, you can check your output directory, there will be main.css. The plugin set default name to [name].css (entry name).

How grunt-scss-lint can exclude some files or directory?

I am using grunt-scss-lint with sucess.
but I wanna exclude a folder or files, but after runt he task still catching the folder, any idea how to exclude the file or directory?
in the docs says:
exclude
Type: String or Array
Default: null
Exclude one or more files from being linted.
grunt task
scsslint: {
allFiles: [
'scss/_main.scss',
],
options: {
config: 'scss/.scss-lint.yml',
reporterOutput: '.tmp/scss-lint-report.xml',
colorizeOutput: true,
compact:false,
},
exclude: [
'scss/vendor/font-awesome/font-awesome.scss'
]
},
any ideas?
Update:
fixed moving exclude to the options.
scsslint: {
allFiles: [
'scss/_main.scss',
],
options: {
config: 'scss/.scss-lint.yml',
reporterOutput: '.tmp/scss-lint-report.xml',
colorizeOutput: true,
compact:false,
exclude: [
'scss/vendor/font-awesome/font-awesome.scss'
]
}
},
The exclude configuration must be moved to within the options object:
options: {
config: 'scss/.scss-lint.yml',
reporterOutput: '.tmp/scss-lint-report.xml',
colorizeOutput: true,
compact:false,
exclude: [
'scss/vendor/font-awesome/font-awesome.scss'
]
},

Resources