I installed npm: webpack webpack-cli css-loader node-sass sass sass-loader style-loader mini-css-extract-plugin
And received: 404 error on http://localhost:8080 Cannot GET /
package.json:
{
"name": "landing-dlmr",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^6.8.1",
"mini-css-extract-plugin": "^2.7.6",
"node-sass": "^9.0.0",
"sass": "^1.66.1",
"sass-loader": "^13.3.2",
"style-loader": "^3.3.3",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
webpack.config.json:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: "development",
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/main.js',
},
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
MiniCssExtractPlugin.loader,
// Translates CSS into CommonJS
"css-loader",
// Compiles Sass to CSS
"sass-loader",
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/main.css',
})
],
watch: true
};
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dellmayer</title>
<!-- <link rel="stylesheet" href="./styles/main.scss"> -->
<link rel="stylesheet" href="../dist/css/main.css">
</head>
<body class="page__body">
<header class="header">
<nav class="menu">
<ul class="menu__list">
<li class="menu__item">HOME</li>
<li class="menu__item">HISTORY</li>
<li class="menu__item">PRODUCT</li>
<li class="menu__item">SPECIFICATION</li>
<li class="menu__item">IN THE BOX</li>
<li class="menu__item">CONTACT</li>
</ul>
</nav>
</header>
<!-- <script type="text/javascript" src="./index.js"></script> -->
<script type="text/javascript" src="../dist/js/main.js"></script>
</body>
</html>
index/js:
import './styles/main.scss';
I can just open index.html directly in browser with implemented styles, but can't see the localhost
Follow the Using webpack-dev-server documentation
Use
html-webpack-pluginto inject the JS script and CSS style tag to the HTML template.E.g.
project structure:
src/index.js:src/style.scss:src/index.html:package.json:Build logs: