webpack css-loader原理_webpack打包原理入门探究(五)cssloader初探

921080f5de0781fd5de637fd85b81039.png

webpack打包原理入门探究(五)loader初探(二)

webpack打包原理入门探究(五)loader初探(一)

webpack打包原理入门探究(四)插件探究(下)

webpack打包原理入门探究(四)插件探究(上)

webpack打包原理入门探究(三)入口探究

webpack打包原理入门探究(二)基本配置

webpack打包原理入门探究(一)

我们需要准备一些文件夹 src/styles/css/common.css

html,body {
margin: 0;
padding: 0;
background: olive;
}

ul, li ,ol {
list-style: none;
margin: 0;
padding: 0;
}

d4858f73d3170e3560ebbb084a216a2b.png

在 src/app.js 引入 src/styles/css/common.css

import './styles/css/common.css'
import layer from './components/layer/layer.js'

const App = () => {
console.log("layer==>", layer)
}

new App()

aa414411dffdc80c0f9b7346dc766166.png

然后我们需要 配置 css-loader

let path = require('path')
let htmlWebpackPlugin = require('html-webpack-plugin')
function resolve(o) {
return path.resolve(__dirname, o)
}
module.exports = {
entry: resolve('src/app.js'), // 指定入口文件 output: {
path: resolve('dist'),
// filename: '[name]-[hash].js' filename: "js/[name].bundle.js",
// publicPath: '' },
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: resolve('node_modules'), //指定排除的范围, include: resolve('src')
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
]
},
plugins: [
new htmlWebpackPlugin({
// filename: "index-[hash].html", // filename: 'index-[chunkhash].html', filename: "index.html", // 生成 dist/a.html template: 'index.html', // 指定根目录下的 index.html inject: 'body',
})
]
}

然后执行 npm run webpack.app

c31b906018e4b245f25f762e4a4258cd.png

样式生效,控制台打印出来了

0db569ceb97af66d702527cc5ad00bc9.png

需要安装 sass-loader node-sass 解析 scss/sass

b180b5176e8ec43e4e920d415acec2ee.png

需要安装 postcss-loader 处理浏览器样式前缀

98c0c40689e9db133f8cb50850d1fccf.png

需要安装 autoprefixer 插件

3017588e7d4cadd4ef0b08b2292cbe4f.png

配置 webpack.app.config.js

let path = require('path')
let htmlWebpackPlugin = require('html-webpack-plugin')
function resolve(o) {
return path.resolve(__dirname, o)
}
module.exports = {
entry: resolve('src/app.js'), // 指定入口文件 output: {
path: resolve('dist'),
// filename: '[name]-[hash].js' filename: "js/[name].bundle.js",
// publicPath: '' },
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: resolve('node_modules'), //指定排除的范围, include: resolve('src')
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.css$/,
loader: 'style-loader!css-loader!postcss-loader'
},
{
test: /\.s[c|a]ss$/,
loader: 'sass-loader'
}
]
},
plugins: [
new htmlWebpackPlugin({
// filename: "index-[hash].html", // filename: 'index-[chunkhash].html', filename: "index.html", // 生成 dist/a.html template: 'index.html', // 指定根目录下的 index.html inject: 'body',
})
]
}

10c587aaaa02aa96ebb41da3de5db88f.png

在根目下新增一个 postcss.config.js

module.exports = {
plugins: [
require('autoprefixer')
]
}

package.json

{
"name": "webpackdemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --mode development",
"webpack.dev": "webpack --mode development --config webpack.dev.config.js",
"webpack.entry": "webpack --mode development --config webpack.entry.config.js",
"webpack.entry.object": "webpack --mode development --config webpack.entry.object.config.js",
"webpack.html.plugin": "webpack --mode development --config webpack.html.plugin.config.js",
"webpack.html.plugin.multi": "webpack --mode development --config webpack.html.plugin.multi.config.js",
"webpack.app": "webpack --mode development --config webpack.app.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"autoprefixer": "^9.7.2",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.13.0",
"postcss-loader": "^3.0.0",
"sass-loader": "^8.0.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
}
}

跑一遍 npm run webpack.app

87358f08c6c72de81f5728ab2bc99b78.png

我们会发现我们希望处理的浏览器前缀竟然没有生效,其实 css-loader 有一个参数是 importLoaders=1,表示处理后续的 loader 当我们的样式存在 @import 其他样式的时候,需要 postcss-loader 去处理的时候,就要传递这个参数

81fffe9ade310e58c29546512545d1ca.png

我们再来看看浏览器

88d5f473c62792b36e3db9177b3c1a7c.png

嗯,今天就到此为止吧,接下来的 就等明天吧,该洗洗睡了

1ef022eb1370e14dbf02c4eb7827c9c4.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值