我第一次尝试在一个旧项目上自己设置 webpack,我不明白为什么只加载了 html,它在启动时出错:
拒绝应用来自 'http://localhost:8080/pages/index.css' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查。
还有 404 用于脚本和图片
webpack.config.js:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require('webpack')
module.exports = {
mode: 'development',
entry: {
main: path.resolve(__dirname, './scripts/index.js'),
},
output: {
path: path.resolve(__dirname, './dist'),
filename: 'index.bundle.js',
},
plugins: [
new HtmlWebpackPlugin({
title: 'webpack Boilerplate',
template: path.resolve(__dirname, './index.html'),
filename: 'index.html',
inject: false
}),
new CleanWebpackPlugin(),
new webpack.HotModuleReplacementPlugin(),
],
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 8080,
hot: true
},
module: {
rules: [
{
test: /\.(css)$/,
use: ['style-loader', 'css-loader']
},
]
}
}
CSS连接:
<link rel="stylesheet" href="../pages/index.css">
JS连接:
<script type="module" src="../scripts/index.js"></script>
css 文件只导入 css 模块,没有别的
PS我做了所有这一切,从几个不同的教程中收集信息,所以我已经完全迷失了可能出现错误的地方