Manage the asset of a project with webpack.
You can import the css file from a Javascript module.
style-loader and css-loader:
npm install –save-dev style-loader css-loader
webpack.config.js:
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
style.css in /srcimport “./style.css”;
webpack.config.js:
module: {
rules: [
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
],
},
/src, for example icon.png import Icon from './icon.png';
const myIcon = new Image();
myIcon.src = Icon;
document.appendChild(myIcon);
Or
import iconImage from './icon.png';
const icon = document.createElement("img");
img.src = iconImage;
website.appendChild(icon);
webpack.config.js:
module: {
rules: [
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},
/src@font-face in css:
@font-face {
font-family: 'MyFont';
src: url('./my-font.woff2') format('woff2'),
url('./my-font.woff') format('woff');
font-weight: 600;
font-style: normal;
}
div {
font-family: 'MyFont';
}