飞道的博客

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

498人阅读  评论(0)


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

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

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

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

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

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

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

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


   
  1. html,body {
  2. margin: 0;
  3. padding: 0;
  4. background: olive;
  5. }
  6. ul, li ,ol {
  7. list-style: none;
  8. margin: 0;
  9. padding: 0;
  10. }

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


   
  1. import './styles/css/common.css'
  2. import layer from './components/layer/layer.js'
  3. const App = () => {
  4. console.log( "layer==>", layer)
  5. }
  6. new App()

然后我们需要 配置 css-loader


   
  1. let path = require( 'path')
  2. let htmlWebpackPlugin = require( 'html-webpack-plugin')
  3. function resolve(o) {
  4. return path.resolve(__dirname, o)
  5. }
  6. module.exports = {
  7. entry: resolve( 'src/app.js'), // 指定入口文件
  8. output: {
  9. path: resolve( 'dist'),
  10. // filename: '[name]-[hash].js'
  11. filename: "js/[name].bundle.js",
  12. // publicPath: ''
  13. },
  14. module: {
  15. rules: [
  16. {
  17. test: /\.js$/,
  18. loader: 'babel-loader',
  19. exclude: resolve( 'node_modules'), //指定排除的范围,
  20. include: resolve( 'src')
  21. },
  22. {
  23. test: /\.html$/,
  24. loader: 'html-loader'
  25. },
  26. {
  27. test: /\.css$/,
  28. loader: 'style-loader!css-loader'
  29. }
  30. ]
  31. },
  32. plugins: [
  33. new htmlWebpackPlugin({
  34. // filename: "index-[hash].html",
  35. // filename: 'index-[chunkhash].html',
  36. filename: "index.html", // 生成 dist/a.html
  37. template: 'index.html', // 指定根目录下的 index.html
  38. inject: 'body',
  39. })
  40. ]
  41. }

然后执行 npm run webpack.app

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

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

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

需要安装 autoprefixer 插件

配置 webpack.app.config.js


   
  1. let path = require( 'path')
  2. let htmlWebpackPlugin = require( 'html-webpack-plugin')
  3. function resolve(o) {
  4. return path.resolve(__dirname, o)
  5. }
  6. module.exports = {
  7. entry: resolve( 'src/app.js'), // 指定入口文件
  8. output: {
  9. path: resolve( 'dist'),
  10. // filename: '[name]-[hash].js'
  11. filename: "js/[name].bundle.js",
  12. // publicPath: ''
  13. },
  14. module: {
  15. rules: [
  16. {
  17. test: /\.js$/,
  18. loader: 'babel-loader',
  19. exclude: resolve( 'node_modules'), //指定排除的范围,
  20. include: resolve( 'src')
  21. },
  22. {
  23. test: /\.html$/,
  24. loader: 'html-loader'
  25. },
  26. {
  27. test: /\.css$/,
  28. loader: 'style-loader!css-loader!postcss-loader'
  29. },
  30. {
  31. test: /\.s[c|a]ss$/,
  32. loader: 'sass-loader'
  33. }
  34. ]
  35. },
  36. plugins: [
  37. new htmlWebpackPlugin({
  38. // filename: "index-[hash].html",
  39. // filename: 'index-[chunkhash].html',
  40. filename: "index.html", // 生成 dist/a.html
  41. template: 'index.html', // 指定根目录下的 index.html
  42. inject: 'body',
  43. })
  44. ]
  45. }

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


   
  1. module.exports = {
  2. plugins: [
  3. require( 'autoprefixer')
  4. ]
  5. }

package.json


   
  1. {
  2. "name": "webpackdemo",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "build": "webpack --mode development",
  8. "webpack.dev": "webpack --mode development --config webpack.dev.config.js",
  9. "webpack.entry": "webpack --mode development --config webpack.entry.config.js",
  10. "webpack.entry.object": "webpack --mode development --config webpack.entry.object.config.js",
  11. "webpack.html.plugin": "webpack --mode development --config webpack.html.plugin.config.js",
  12. "webpack.html.plugin.multi": "webpack --mode development --config webpack.html.plugin.multi.config.js",
  13. "webpack.app": "webpack --mode development --config webpack.app.config.js",
  14. "test": "echo \"Error: no test specified\" && exit 1"
  15. },
  16. "author": "",
  17. "license": "ISC",
  18. "devDependencies": {
  19. "@babel/cli": "^7.7.4",
  20. "@babel/core": "^7.7.4",
  21. "@babel/preset-env": "^7.7.4",
  22. "autoprefixer": "^9.7.2",
  23. "babel-core": "^6.26.3",
  24. "babel-loader": "^8.0.6",
  25. "css-loader": "^3.2.0",
  26. "html-loader": "^0.5.5",
  27. "html-webpack-plugin": "^3.2.0",
  28. "node-sass": "^4.13.0",
  29. "postcss-loader": "^3.0.0",
  30. "sass-loader": "^8.0.0",
  31. "style-loader": "^1.0.0",
  32. "webpack": "^4.41.2",
  33. "webpack-cli": "^3.3.10"
  34. }
  35. }

跑一遍 npm run webpack.app

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

我们再来看看浏览器

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


转载:https://blog.csdn.net/qq_36772866/article/details/106581069
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场