1. 配置 webpack.config.js
添加截图配置代码:
const path = require('path'); const webpack = require('webpack'); module.exports = { entry: './src/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', }, module: { rules: [ { test: /\.css$/, use: [ { loader: "style-loader" }, { loader: "css-loader" } ] } ] } };
2. 安装 loader : npm i -D css-loader style-loader
3. 创建 css 文件 main.css,并添加内容:
html, body{ margin: 0; padding: 0; } body{ background: grey; }
4. 把 css 文件导入 index.js 文件里:import './css/main.css'
5. 创建 index.html, 引用:
6. 执行 webpack,打包成功。